-
Notifications
You must be signed in to change notification settings - Fork 4
/
demoGSL.m
129 lines (115 loc) · 3.64 KB
/
demoGSL.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
function demoGSL()
clear;close all;clc;
addpath('liblinear-2.1/matlab');
% Set algorithm parameters
kernel_type = 'primal'; % kernel type, options: primal,linear,rbf
options.ReducedDim = 10; % equal to c
options.T = 5; % iterations of inloop
gamma=1; % the parameter for rbf kernel
options.alpha=0.1;
options.beta =17;
options.lamda=10; % the parameter for NGSL
% Choose S and T, 4DA C-W tasks for example
src = 'Caltech10';
tgt = 'webcam';
fprintf('%s vs %s ', src, tgt);
load(['data\' src '_SURF_L10.mat']);
Xs = fts ./ repmat(sum(fts,2),1,size(fts,2));
Xs = zscore(Xs);
Xs = normr(Xs)';
Xs_label = labels;
clear fts;
clear labels;
load(['data\' tgt '_SURF_L10.mat']);
Xt = fts ./ repmat(sum(fts,2),1,size(fts,2));
Xt = zscore(Xt);
Xt = normr(Xt)';
Xt_label = labels;
clear fts;
clear labels;
% ------------------------------------------
% Transfer Learning
% ------------------------------------------
Pt = eye(size(Xt, 1));
P=[];
Ptp = ones(size(Xs, 1), 10);
Atp = ones(size([Xs,Xt], 2), 10);
if strcmp(kernel_type,'primal')
for outloop=1:10
if outloop > 1, Ptp = Pt;end
%labeling target data
P = labeling_target(Xs, Xt, Xs_label, Xt_label, Pt, P);
Xt_fake_label=P.Yt;
X_label=[Xs_label;Xt_fake_label];
X = [Xs,Xt];
X = X*diag(sparse(1./sqrt(sum(X.^2))));
%do GSL
Pt = GSL(Xs,Xt,X,X_label,options);
deltaPt=norm(Pt - Ptp, 'fro') / norm(Pt, 'fro')
end
else
for outloop=1:10
if outloop > 1, Atp = At;end
%labeling target data
if outloop==1
P = labeling_target( Xs, Xt, Xs_label, Xt_label, Pt, P);
else
P = labeling_target( Ks, Kt, Xs_label, Xt_label, At, P);
end
Xt_fake_label=P.Yt;
X_label=[Xs_label;Xt_fake_label];
X = [Xs,Xt];
X = X*diag(sparse(1./sqrt(sum(X.^2))));
% Calculated initial Ps
[Ps0,~] = PCA1(Xs', options);
Ps1=X\Ps0;
%Structured kernel matrix
K = My_kernel(kernel_type,X,[],gamma);
Ks= My_kernel(kernel_type,X,Xs,gamma);
Kt= My_kernel(kernel_type,X,Xt,gamma);
%do GSL_kernel
At = NGSL(K,Ks,Kt,Ps1,X,X_label,options);
deltaAt=norm(At - Atp, 'fro') / norm(At, 'fro')
end
end
end
function P = labeling_target(Xs, Xt, Xs_label, Xt_label, Pt, P)
C = [0.001 0.01 0.1 1.0 10 100 1000 10000];
for chsvm = 1 :length(C)
tmd = ['-s 3 -c ' num2str(C(chsvm)) ' -B 1 -q'];
model(chsvm) = train(Xs_label, sparse(double(Xs'*Pt)),tmd);
[~,acc, ~] = predict(Xt_label, sparse(double(Xt'*Pt)), model(chsvm), '-q');
acc1(chsvm)=acc(1);
end
[acc,bestsvm_id]=max(acc1);
fprintf(' svm acc=%2.2f %%\n',acc);
model=model(bestsvm_id);
c=C(bestsvm_id);
score = model.w * [Pt'*Xt; ones(1, size(Xt, 2))];
th = mean(score, 2)';
[confidence, C] = max(score, [], 1);
P.Yt = C';
end
function K = My_kernel(ker,X,X2,gamma)
switch ker
case 'linear'
if isempty(X2)
K = X'*X;
else
K = X'*X2;
end
case 'rbf'
n1sq = sum(X.^2,1);
n1 = size(X,2);
if isempty(X2)
D = (ones(n1,1)*n1sq)' + ones(n1,1)*n1sq -2*X'*X;
else
n2sq = sum(X2.^2,1);
n2 = size(X2,2);
D = (ones(n2,1)*n1sq)' + ones(n1,1)*n2sq -2*X'*X2;
end
K = exp(-gamma*D);
otherwise
error(['Unsupported kernel ' ker])
end
end