-
Notifications
You must be signed in to change notification settings - Fork 5
/
traintestone.m
352 lines (309 loc) · 11.6 KB
/
traintestone.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
function res=traintestone(what,varargin)
addpath('../matlab/');
prefix='./';
randn('seed',8675309);
rand('seed',90210);
start=tic;
load(what);
trainright=xt;
trainleft=yt;
testright=xs;
testleft=ys;
% train-test split
[n,d]=size(trainright);
[m,~]=size(testright);
% v=sort(eig(trainleft'*trainleft),'descend');
% vs=cumsum(v);
% vs=vs/vs(end);
% embedd=min(find(vs>0.9));
embedd=50;
% add constant feature
trainright=horzcat(trainright,ones(n,1));
testright=horzcat(testright,ones(m,1));
ultraw=ones(1,size(trainleft,1));
tic
rs=rembed(trainright',ultraw,trainleft',embedd, ...
struct('pre','diag','innerloop',100,'lambda',1,...
'tmax',2,'trainpredictor',true,'verbose',false));
toc
trainh=horzcat(rs.projectx(trainright),ones(n,1));
testh=horzcat(rs.projectx(testright),ones(m,1));
perm=randperm(n);
nsubtrain=ceil(0.9*n);
subtrainh=trainh(perm(1:nsubtrain),:);
subtrainleft=trainleft(perm(1:nsubtrain),:);
valtrainh=trainh(perm(nsubtrain+1:end),:);
valtrainleft=trainleft(perm(nsubtrain+1:end),:);
subppos=full(sum(subtrainleft,1))/size(subtrainleft,1);
maxiter=100;
allf=500+randi(6000,1,maxiter);
allrfac=0.5+3.5*rand(1,maxiter);
alllambda=exp(log(1e-5)+(log(1e-2)-log(1e-5))*rand(1,maxiter));
alllogisticiter=1+randi(24,1,maxiter);
alleta=0.25+4.0*rand(1,maxiter);
allalpha=0.25+0.7*rand(1,maxiter);
alldecay=0.9+0.15*rand(1,maxiter);
allkern=randi(6,1,maxiter);
kerntags={ 'g', 'm3', 'm5', 'qg', 'qm3', 'qm5' };
maxshrink=1/sqrt(nsubtrain);
minshrink=1/nsubtrain;
allshrink=(1-maxshrink)+(maxshrink-minshrink)*rand(1,maxiter);
for iter=1:maxiter
f=allf(iter);
rfac=allrfac(iter);
lambda=alllambda(iter);
logisticiter=alllogisticiter(iter);
eta=alleta(iter);
alpha=allalpha(iter);
decay=alldecay(iter);
kern=kerntags{allkern(iter)};
shrink=allshrink(iter);
clear wr b ww;
try
if (nargin > 1 && strcmp(varargin{1},'linear'))
wr=0;
b=0;
ww=megamls(subtrainh,subtrainleft,...
struct('lambda',lambda,'f',f,'fbs',1000,...
'rfac',rfac,'kernel',kern,...
'logisticiter',4*logisticiter,'eta',40*eta,...
'alpha',0.5+0.5*alpha,'decay',decay));
else
[wr,b,ww]=calmultimls(subtrainh,subtrainleft,...
struct('lambda',64*lambda,'f',f,'fbs',500,...
'rfac',rfac,'kernel',kern,...
'logisticiter',logisticiter,'eta',eta,...
'alpha',alpha,'decay',decay,...
'shrink',shrink,'multiclass',true));
end
catch
fprintf('*');
continue
end
thres=(logisticiter==0)*0.5;
fprintf('.');
[~,~,~,macroF1,macroF1lb,macroF1ub]=multiF1Boot(wr,b,ww,subppos,valtrainh,valtrainleft,false);
[teste,testelb,testeub,microF1,microF1lb,microF1ub,merror,merrorlb,merrorub]=multiHammingBoot(thres,wr,b,ww,valtrainh,valtrainleft,false);
if (~exist('bestham','var'))
bestham=struct('iter',iter,'loss',[teste,testelb,testeub]);
fprintf('\niter = %u, bestham.loss = %g',iter,bestham.loss(1));
bestmicro=struct('iter',iter,'loss',[microF1,microF1lb,microF1ub]);
fprintf('\niter = %u, bestmicro.loss = %g',iter,bestmicro.loss(1));
bestmacro=struct('iter',iter,'loss',[macroF1,macroF1lb,macroF1ub]);
fprintf('\niter = %u, bestmacro.loss = %g',iter,bestmacro.loss(1));
besterror=struct('iter',iter,'loss',[merror,merrorlb,merrorub]);
fprintf('\niter = %u, besterror.loss = %g',iter,besterror.loss(1));
else
if (teste < bestham.loss(1))
bestham=struct('iter',iter,'loss',[teste,testelb,testeub]);
fprintf('\niter = %u, bestham.loss = %g',iter,bestham.loss(1));
end
if (merror < besterror.loss(1))
besterror=struct('iter',iter,'loss',[merror,merrorlb,merrorub]);
fprintf('\niter = %u, besterror.loss = %g',iter,besterror.loss(1));
end
if (microF1 > bestmicro.loss(1))
bestmicro=struct('iter',iter,'loss',[microF1,microF1lb,microF1ub]);
fprintf('\niter = %u, bestmicro.loss = %g',iter,bestmicro.loss(1));
end
if (macroF1 > bestmacro.loss(1))
bestmacro=struct('iter',iter,'loss',[macroF1,macroF1lb,macroF1ub]);
fprintf('\niter = %u, bestmacro.loss = %g',iter,bestmacro.loss(1));
end
end
end
fprintf('\n');
which=1;
ppos=full(sum(trainleft,1))/size(trainleft,1);
for iter=[bestham.iter bestmicro.iter bestmacro.iter besterror.iter]
f=allf(iter);
rfac=allrfac(iter);
lambda=alllambda(iter);
logisticiter=alllogisticiter(iter);
eta=alleta(iter);
alpha=allalpha(iter);
decay=alldecay(iter);
kern=kerntags{allkern(iter)};
shrink=allshrink(iter);
tic
clear wr b ww;
if (nargin > 1 && strcmp(varargin{1},'linear'))
wr=0;
b=0;
ww=megamls(trainh,trainleft,...
struct('lambda',lambda,'f',f,'fbs',1000,...
'rfac',rfac,'kernel',kern,...
'logisticiter',4*logisticiter,'eta',40*eta,...
'alpha',0.5+0.5*alpha,'decay',decay));
else
[wr,b,ww]=calmultimls(trainh,trainleft,...
struct('lambda',64*lambda,'f',f,'fbs',500,...
'rfac',rfac,'kernel',kern,...
'logisticiter',logisticiter,'eta',eta,...
'alpha',alpha,'decay',decay,...
'shrink',shrink,'multiclass',true));
end
thres=(logisticiter==0)*0.5;
fprintf('iter=%u f=%g rfac=%g lambda=%g logisticiter=%u eta=%g alpha=%g decay=%g kern=%s shrink=%g\n',...
iter,f,rfac,lambda,logisticiter,eta,alpha,decay,kern,shrink);
if (which == 1 || which == 2 || which == 4)
[teste,testelb,testeub,microF1,microF1lb,microF1ub,merror,merrorlb,merrorub]=multiHammingBoot(thres,wr,b,ww,testh,testleft,true);
if (which == 1)
res.calmls_embed_test_hamming=[testelb,teste,testeub];
elseif (which == 4)
res.calmls_embed_test_error=[merrorlb,merror,merrorub];
else
res.calmls_embed_test_microF1=[microF1lb,microF1,microF1ub];
end
else
[~,~,~,macroF1,macroF1lb,macroF1ub]=multiF1Boot(wr,b,ww,ppos,testh,testleft,true);
res.calmls_embed_test_macroF1=[macroF1lb,macroF1,macroF1ub];
end
which=which+1;
end
toc(start)
end
function [yhati,yhatj,ymax]=blockmultiinfer(X,th,wr,b,ww)
if (size(wr,1)==1 && size(wr,2)==1 && wr==0)
Z=X*ww;
else
[fplus1,~]=size(ww);
f=fplus1-1;
Z=cos(bsxfun(@plus,X*wr,b))*ww(1:f,:);
Z=bsxfun(@plus,Z,ww(fplus1,:));
end
[yhati,yhatj]=find(Z>th);
[~,ymax]=max(Z,[],2);
end
function [loss,truepos,falsepos,falseneg,multierror]=multiHammingImpl(th,wr,b,ww,X,Y,imp)
[~,f]=size(wr);
[n,k]=size(Y);
[~,s]=size(imp);
truepos=zeros(1,s);
falsepos=zeros(1,s);
falseneg=zeros(1,s);
multierror=zeros(1,s);
bs=min(n,ceil(1e+9/max(k,f)));
for off=1:bs:n
offend=min(n,off+bs-1);
mybs=offend-off+1;
[yhati,yhatj,ymax]=blockmultiinfer(X(off:offend,:),th,wr,b,ww);
[i,j,~]=find(Y(off:offend,:));
for ss=1:s
weights=imp(i+(off-1),ss);
megay=sparse(i,j,weights,mybs,k); clear weights;
weightshat=imp(yhati+(off-1),ss);
megayhat=sparse(yhati,yhatj,weightshat,mybs,k); clear weightshat;
bstruepos=sum(megay(sub2ind(size(megay),yhati,yhatj)));
bsfalsepos=sum(sum(megayhat))-bstruepos;
bsfalseneg=sum(sum(megay))-bstruepos;
truepos(ss)=truepos(ss)+bstruepos;
falsepos(ss)=falsepos(ss)+bsfalsepos;
falseneg(ss)=falseneg(ss)+bsfalseneg;
[~,y]=max(megay,[],2);
multierror(ss)=multierror(ss)+sum(imp(off:offend,ss).*(y~=ymax));
clear megay megayhat;
end
end
total=k*sum(imp,1);
loss=(falsepos+falseneg)./total;
multierror=multierror./sum(imp,1);
end
function [microF1,macroF1]=F1metric(truepos,falsepos,falseneg)
classprec=truepos./(truepos+falsepos+1e-12);
classrec=truepos./(truepos+falseneg+1e-12);
% http://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html
macroF1=mean(2*(classprec.*classrec)./(classprec+classrec+1e-12),1);
% http://www.kaggle.com/c/lshtc/details/evaluation
% MaP=mean(classprec,1);
% MaR=mean(classrec,1);
% macroF1=2*MaP*MaR./(MaP+MaR+1e-12);
prec=sum(truepos,1)./(sum(truepos,1)+sum(falsepos,1)+1e-12);
rec=sum(truepos,1)./(sum(truepos,1)+sum(falseneg,1)+1e-12);
microF1=2*(prec.*rec)./(prec+rec+1e-12);
end
function [loss,cilb,ciub,microloss,microlb,microub,merror,merrorlb,merrorub]=multiHammingBoot(th,wr,b,ww,testh,testy,doprint)
[m,~]=size(testh);
imp=poissrnd(1,m,16);
[testloss,testtruepos,testfalsepos,testfalseneg,multierror]=...
multiHammingImpl(th,wr,b,ww,testh,testy,imp);
[~,ind]=sort(testloss);
loss=mean(testloss(ind(8:9)));
cilb=testloss(ind(2));
ciub=testloss(ind(15));
[~,ind]=sort(multierror);
merror=mean(multierror(ind(8:9)));
merrorlb=multierror(ind(2));
merrorub=multierror(ind(15));
[microF1,~]=F1metric(testtruepos,testfalsepos,testfalseneg);
[~,ind]=sort(microF1);
microloss=mean(microF1(ind(8:9)));
microlb=microF1(ind(2));
microub=microF1(ind(15));
if (doprint)
fprintf('per-ex inference: (hamming) [%g,%g,%g] (micro) [%g,%g,%g] (multiclass) [%g,%g,%g]\n',...
cilb,loss,ciub,microlb,microloss,microub,merrorlb,merror,merrorub);
end
end
function [microF1,macroF1]=F1Impl(wr,b,ww,trainh,trainy,ppos,imp)
persistent havemex;
if (isempty(havemex))
havemex = (exist('fastsoftmax', 'file') == 3);
end
[~,f]=size(wr);
[n,k]=size(trainy);
kbs=min(k,ceil(1e+9/n));
[~,s]=size(imp);
truepos=zeros(k,s);
falsepos=zeros(k,s);
falseneg=zeros(k,s);
for off=1:kbs:k
offend=min(k,off+kbs-1);
if (size(wr,1)==1 && size(wr,2)==1 && wr==0)
Z=trainh*ww(:,off:offend);
else
Z=cos(bsxfun(@plus,trainh*wr,b))*ww(1:f,off:offend);
Z=bsxfun(@plus,Z,ww(f+1,off:offend));
end
if (havemex)
fastsoftmax(Z,max(Z));
else
Z=bsxfun(@minus,Z,max(Z));
Z=exp(Z);
Z=bsxfun(@rdivide,Z,sum(Z));
end
[allsp,allind]=sort(Z,'descend');
r=bsxfun(@plus,linspace(1,n,n)'*ones(1,offend-off+1),n*ppos(off:offend));
fscores=bsxfun(@rdivide,cumsum(allsp),r);
[~,am]=max(fscores);
for jj=off:offend
yhati=allind(1:am(jj-off+1),jj-off+1);
i=find(trainy(:,jj));
bstruepos=sum(imp(intersect(i,yhati),:),1);
bsfalsepos=sum(imp(setdiff(yhati,i),:),1);
bsfalseneg=sum(imp(setdiff(i,yhati),:),1);
truepos(jj,:)=truepos(jj,:)+bstruepos;
falsepos(jj,:)=falsepos(jj,:)+bsfalsepos;
falseneg(jj,:)=falseneg(jj,:)+bsfalseneg;
end
end
[microF1,macroF1]=F1metric(truepos,falsepos,falseneg);
end
function [microF1,microF1lb,microF1ub,macroF1,macroF1lb,macroF1ub]=multiF1Boot(wr,b,ww,ppos,testh,testy,doprint)
[m,~]=size(testh);
imp=poissrnd(1,m,16);
[testmicroF1,testmacroF1]=F1Impl(wr,b,ww,testh,testy,ppos,imp);
[~,ind]=sort(testmicroF1);
microF1=mean(testmicroF1(ind(8:9)));
microF1lb=testmicroF1(ind(2));
microF1ub=testmicroF1(ind(15));
[~,ind]=sort(testmacroF1);
macroF1=mean(testmacroF1(ind(8:9)));
macroF1lb=testmacroF1(ind(2));
macroF1ub=testmacroF1(ind(15));
if (doprint)
fprintf('per-class inference: (micro) [%g,%g,%g] (macro) [%g,%g,%g]\n',...
microF1lb,microF1,microF1ub,...
macroF1lb,macroF1,macroF1ub);
end
end