-
Notifications
You must be signed in to change notification settings - Fork 0
/
testWebKB.m
166 lines (112 loc) · 4.33 KB
/
testWebKB.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
%% Run experiment on webKB using joint minimization
clear;
initMosek;
initMinFunc;
loadWebKBFull;
%%
Cvec = 10.^linspace(-3, 0, 10);
total = 4 * length(Cvec) * 2 * 2;
count = 1;
totalTimer = tic;
for one_example = 0:1
clear trainError testError savedW savedKappa;
for split = 1:4
%% set up training and test splits
if one_example
train = split;
test = setdiff(1:4, split);
else
train = setdiff(1:4, split);
test = split;
end
Xtr = [];
Ytr = [];
graphTr = [];
for i = 1:length(train)
Xtr = [Xtr words{train(i)}'];
Ytr = [Ytr; Y{train(i)}(:)];
tmp = cites{train(i)};
graphTr = [graphTr, sparse(size(graphTr,1), size(tmp,2));...
sparse(size(tmp,1), size(graphTr,2)), tmp];
end
graphTr = 0*graphTr;
Xte = [];
Yte = [];
graphTe = [];
for i = 1:length(test)
Xte = [Xte words{test(i)}'];
Yte = [Yte; Y{test(i)}(:)];
tmp = cites{test(i)};
graphTe = [graphTe, sparse(size(graphTe,1), size(tmp,2));...
sparse(size(tmp,1), size(graphTe,2)), tmp];
end
k = length(allLabels);
nTr = size(Xtr, 2);
nTe = size(Xte, 2);
d = size(Xtr, 1);
%% set up local marginal constraints
fprintf('Setting up local marginal polytope\n');
[Aeq, beq, featureMapTe] = edge_marginals(Xte, graphTe, length(allLabels));
Ste.A = [];
Ste.b = [];
Ste.Aeq = Aeq;
Ste.beq = beq;
Ste.lb = zeros(nTe * k + nnz(graphTe) * k^2, 1);
Ste.ub = [];
Ste.x0 = [];
[Aeq, beq, featureMap] = edge_marginals(Xtr, graphTr, length(allLabels));
S.A = [];
S.b = [];
S.Aeq = Aeq;
S.beq = beq;
S.lb = zeros(nTr * k + nnz(graphTr) * k^2, 1);
S.ub = [];
S.x0 = [];
fprintf('Set up local marginal polytope\n');
clear Aeq beq;
%% expand pairwise potentials
groundTruth = zeros(nTr * k + nnz(graphTr) * k^2, 1);
for i = 1:length(Ytr)
ind = localIndex(i, Ytr(i), nTr);
groundTruth(ind) = 1;
end
% counter = zeros(k);
[I,J] = find(graphTr);
for i = 1:nnz(graphTr)
ind = pairwiseIndex(i, Ytr(I(i)), Ytr(J(i)), nTr, k);
groundTruth(ind) = 1;
%
% counter(Ytr(I(i)), Ytr(J(i))) = ...
% counter(Ytr(I(i)), Ytr(J(i))) + 1;
end
groundTruthTe = zeros(nTe * k + nnz(graphTe) * k^2, 1);
for i = 1:length(Yte)
ind = localIndex(i, Yte(i), nTe);
groundTruthTe(ind) = 1;
end
[I,J] = find(graphTe);
for i = 1:nnz(graphTe)
ind = pairwiseIndex(i, Yte(I(i)), Yte(J(i)), nTe, k);
groundTruthTe(ind) = 1;
end
x0 = [];
%%
attract = 10 * (eye(k) - 0.5);
w = [randn(d*k, 1); attract(:)];
kappa = 1;
%%
y = dualInference(w, featureMap, kappa, S);
trainError(split) = sum(predictMax(y(1:k*nTr), nTr, k) ~= predictMax(groundTruth(1:k*nTr), nTr, k)) / nTr;
% run on test split
y = dualInference(w, featureMapTe, kappa, Ste);
testError(split) = sum(predictMax(y(1:k*nTe), nTe, k) ~= predictMax(groundTruthTe(1:k*nTe), nTe, k)) / nTe;
%%
fprintf('Training error %f\n', trainError(cIndex, split, vanilla));
fprintf('Testing error %f\n', testError(cIndex, split, vanilla));
fprintf('kappa = %d\n', kappa);
fprintf('0.5 * ||w||^2 = %f\n', 0.5 * w' * w);
fprintf('%2.1f percent done (%d of %d). ETA %f minutes for %d runs at %f seconds per run\n', 100 * count / total, ...
count, total, (total - count) * (toc(totalTimer) / count) / 60, total - count, toc(totalTimer) / count);
count = count + 1;
end
end