-
Notifications
You must be signed in to change notification settings - Fork 1
/
ADAM.m
61 lines (50 loc) · 1.65 KB
/
ADAM.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
function [x, t] = ADAM(Problem, x0, eps, alpha, beta, MaxIter, use_acc, color, style, verbose)
%function [x] = ADAM(p, x0, eps, t, MaxIter)
% Apply the Adam algorithm.
A = Problem.A;
b = Problem.b;
m = Problem.m;
n = Problem.n;
f = Problem.cost;
grad_f = Problem.grad;
x = x0; % starting point
x1 = x0;
if Problem.name == "quadratic"
Problem.plot_surface();
end
t = 0;
beta1 = 0.9; beta2 = 0.999;
m0 = 0; v0 = 0;
if verbose == 1
fprintf( '---Adam method\n');
end
while true
v = f(x); % value of the function at x
if use_acc && t > 0
y = x + beta*(x1 - x0);
g = grad_f(y);
ng = norm(g);
else
g = grad_f(x); % gradient at x
ng = norm(g); % norm of the gradient
end
if ng <= eps || t == MaxIter
break;
else
t = t + 1;
end
m1 = beta1*m0 + (1 - beta1)*g; % Update biased first moment estimate
v1 = beta2*v0 + (1 - beta2)*(g.^2); % Update biased second raw moment estimate
m1_hat = m1 / (1 - beta1*t); % Compute bias-corrected first moment estimate
v1_hat = v1 / (1 - beta2*t); % Compute bias-corrected second raw moment estimate
x = x - alpha*m1_hat ./ (v1_hat*(1/2) + 1e-8); % Update parameters
m0 = m1; v0 = v1;
x0 = x1; x1 = x;
if Problem.name == "quadratic"
Problem.plot_line(x0, x1, color, style);
end
if verbose == 1
fprintf('%4d\t v=%1.8e \t ng=%1.4e\n' , i, v, ng);
end
end
end