-
Notifications
You must be signed in to change notification settings - Fork 0
/
RASMM_pd.m
173 lines (147 loc) · 4.73 KB
/
RASMM_pd.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
% Author: Qian Chengde
% E-Mail: qianchd(gmail)
% Date : 2021-10-19
% Copyright 2021 Qian Chengde.
% File: RASMM_pd.m
% The primal-dual solver for RASMM
function [M,S_head,A,W,M_SMM,M_2,accu_SMM,accu_2,accu_final] = RASMM_pd(X,y,X_test,y_test,p,q,K,C,M_0,gamma,s,s2,tau,rho,disp_f,issmm_only,issmm)
% X: n rows, every row is a obs
% p and q: dimension of obs
% C: the SVM parameter
% K: number of classes
% M_0: create the initial M
% s and s2: the parameter in the truncated loss
% tau: the parameter in the trace norm 0<tau<0.5
% rho: the parameter in Admm lagrangian multipler
% disp_f: control the disp procedure
% issmm_only: if true, the DCA loop will stop at iterate 1; be 0 commonly.
% issmm: if true, the first iteration in the DCA loop is the SMM solution.
A = 0;
S_head = 0;
M_2 = 0;
accu_2=0;
M_SMM = 0;
accu_SMM = 0;
% d.c. algorithm
% M K-1 * pq; X n * pq; W K-1 * K;
% initialize for d.c.
n = length(y);
lambda = 1/C;
W = [ones(K-1,1)/sqrt(K-1), -((1+sqrt(K))/(K-1)^1.5)*ones(K-1,K-1) + sqrt(K/(K-1))*eye(K-1)];
M = M_0;
M_head = M;
V = (1-gamma)*ones(K,n)/(2*lambda*n);
Weight = (1-gamma)*ones(K,n)/(2*lambda*n);
mu = (1-gamma)*ones(K,n)/(2*lambda*n);
for i = 1:n
V(y(i),i) = -gamma/(2*lambda*n);
Weight(y(i),i) = gamma/(2*lambda*n);
mu(y(i),i) = -gamma*(K-1)/(2*lambda*n);
end
mu = reshape(mu,n*K,1);
d0 = reshape(W*V*X,(K-1)*p*q,1);
v = reshape(Weight,n*K,1);
%A_head = kron(X,W');
%for i = 1:n*K
%A_head(i,:) = A_head(i,:)*v(i);
%end
obj_v = inf;
ep_dc= 5e-4;
ep=ep_dc;
Y = zeros(K*n+1,1);
for t_dc = 1:10
%if norm(M_dc_old-M,'fro')< 0.1
% conv_t = conv_t+1;
%else
% conv_t = 0;
%end
%disp({'t_dc',t_dc,'diff F-norm of M',norm(M_dc_old-M,'fro'),'conv_time',conv_t})
M_dc_old = M;
B = X*M'*W+s2;
B_2 = ones(n,K)*(gamma-1);
for i = 1:n
B(i,y(i)) = s+s2-B(i,y(i));
B_2(i,y(i)) = gamma;
end
B = (B>0).*B_2;
B = B/(n*lambda);
if t_dc == 1 && issmm == 1
B = zeros(n,K);
end
d = d0 + reshape(W*B'*X,(K-1)*p*q,1);
%disp({'L',L});
%disp('starting ADMM')
%if t_dc==1
%L = norm([A_head;d'],2);
%disp(L)
sigma = 100/lambda;
omega = 100/lambda;
%end
% solving DC sub-problem
sub_conv = 0;
for a = 1:50
z = Y(1:K*n) + sigma*reshape(Weight.*(W'*M_head*X'),K*n,1);
z = z - sigma*(max(abs(mu+z/sigma)-1/sigma,0).*sign(mu+z/sigma)-mu);
Y = [z;1];
M_new = rho*omega*M_dc_old + M - omega*(W*reshape(v.*z,K,n)*X + reshape(d,K-1,p*q));
for j = 1:K-1
M_new(j,:) = reshape(svt(reshape(M_new(j,:),p,q),omega*tau)/((1+rho)*omega+1),1,p*q);
end
%M_new = svt(M_new/((1+rho)*omega+1),tau);
theta = 1/sqrt(1+2*(1+rho)*omega);
omega = theta*omega;
sigma = sigma/theta;
M_head = M_new + theta*(M_new-M);
if mod(a,100)==1
%disp(norm(M_new-M,'fro'));
%disp({omega,sigma,theta,norm(M_new-M,'fro')/norm(M_new+M,'fro')})
end
if norm(M_new-M,'fro')/norm(M_new+M,'fro')< ep
M = M_new;
%disp({'dp break',a})
break
end
M = M_new;
end
%disp({'t_dc',t_dc,'diff F-norm of M',norm(M_dc_old-M,'fro')})
y_pred = RASMM_pred(X_test,M,K);
obj_tem = RASMM_obj(M,X,y,W,p,q,n,s,s2,tau,gamma,C,K);
accu_rate = sum(y_pred==y_test)/length(y_test);
if mod(t_dc,1)==0
%disp({'t_dc',t_dc,'diffnorm',norm(M_dc_old-M,'fro')/norm(M_dc_old+M,'fro'),'a',a,'accur',accu_rate,'loss_test',RASMM_loss(M,X_test,y_test,W,s,s2,gamma,K),C})
end
%disp({'loss on test set',RASMM_loss(M,X_test,y_test,W,s,s2,gamma,K),'C',C})
if t_dc == 1
M_SMM = M;
accu_SMM = accu_rate;
accu_2 = -1;
accu_final = -1;
if issmm_only == 1
break;
end
end
if t_dc == 2
M_2 = M;
accu_2 = accu_rate;
end
if accu_rate < accu_2 - 0.02
break
end
if norm(M_dc_old-M,'fro')/norm(M_dc_old+M,'fro')< 4e-4
break;
end
if obj_tem >= obj_v-3e-8
%disp({'dca break because obj increases',obj_tem - obj_v,obj_tem})
M = M_dc_old;
ep = ep/1000;
%break
else
obj_v = obj_tem;
accu_final = accu_rate;
ep=ep_dc;
end
if ep <ep_dc*10e-9
break;
end
end
end