forked from aelgabli/GADMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dual_averaging.m
75 lines (50 loc) · 1.59 KB
/
dual_averaging.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
function [obj_dualAvg, loss_dualAvg, Iter, dualAvg_time]=dual_averaging(XX,YY, no_workers, num_feature, noSamples, num_iter, obj0, acc, alpha)
Iter= num_iter;
s1=num_feature;
s2=noSamples;
%lambda = zeros(s1,no_workers);
out=zeros(s1,no_workers);
%out_prev=zeros(s1,no_workers);
Z=zeros(s1,no_workers);
Z_prev=zeros(s1,no_workers);
%alpha=1E-6;
max_iter = num_iter;
dualAvg_time(1)=0;
for i = 1:max_iter
for ii =1:no_workers
first = (ii-1)*s2+1;
last = first+s2-1;
H=XX(first:last,1:s1);
Y=YY(first:last);
if(ii==1)
if(i > 1)
tic
end
end
grads(:,ii)=H'*H*out(:,ii)-H'*Y;
if(ii==1)
Z(:,ii)=Z_prev(:,ii+1)+grads(:,ii);
elseif(ii==no_workers)
Z(:,ii)=Z_prev(:,ii-1)+grads(:,ii);
else
Z(:,ii)=0.5*Z_prev(:,ii+1)+0.5*Z_prev(:,ii-1)+grads(:,ii);
end
out(:,ii)=-alpha*Z(:,ii);
Z_prev(:,ii)=Z(:,ii);
if(ii==1 && i > 1)
dualAvg_time(i)=dualAvg_time(i-1)+2*toc;
end
end
final_obj = 0;
for ii =1:no_workers
first = (ii-1)*s2+1;
last = first+s2-1;
final_obj = final_obj + 0.5*norm(XX(first:last,1:s1)*out(:,ii) - YY(first:last))^2;
end
obj_dualAvg(i)=final_obj;
loss_dualAvg(i)=abs(final_obj-obj0);
if(loss_dualAvg(i) < acc)
Iter = i;
break;
end
end