forked from jrvmalik/gradient-descent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAMSgrad.m
51 lines (41 loc) · 1.14 KB
/
AMSgrad.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
function [updates, state] = AMSgrad(gradients, state)
%AMSGRAD Summary of this function goes here
% Detailed explanation goes here
if nargin == 1
state = struct;
end
if ~isfield(state, 'beta1')
state.beta1 = 0.9;
end
if ~isfield(state, 'beta2')
state.beta2 = 0.999;
end
if ~isfield(state, 'epsilon')
state.epsilon = 1e-8;
end
if ~isfield(state, 'iteration')
state.iteration = 1;
end
if ~isfield(state, 'm')
state.m = zeros(size(gradients));
end
if ~isfield(state, 'v')
state.v = zeros(size(gradients));
end
if ~isfield(state, 'vhat')
state.vhat = zeros(size(gradients));
end
if ~isfield(state, 'alpha')
state.alpha = 1e-2;
end
% update biased first moment estimate
state.m = state.beta1 * state.m + (1 - state.beta1) * gradients;
% update biased second raw moment estimate
state.v = state.beta2 * state.v + (1 - state.beta2) * gradients.^2;
% non-decreasing
state.vhat = max(state.vhat, state.v);
% update parameters
updates = state.alpha * state.m ./ (sqrt(state.vhat) + state.epsilon);
% update iteration number
state.iteration = state.iteration + 1;
end