-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsgd_decayed_weight_decay.lua
More file actions
87 lines (78 loc) · 2.8 KB
/
sgd_decayed_weight_decay.lua
File metadata and controls
87 lines (78 loc) · 2.8 KB
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
----------------------------------------------------------------------
-- A plain implementation of SGD
--
-- ARGS:
-- opfunc : a function that takes a single input (X), the point of
-- evaluation, and returns f(X) and df/dX
-- x : the initial point
-- state : a table describing the state of the optimizer; after each
-- call the state is modified
-- state.learningRate : learning rate
-- state.learningRateDecay : learning rate decay
-- state.weightDecay : weight decay
-- state.L1weightDecay : L1 weight decay
-- state.momentum : momentum
-- state.learningRates : vector of individual learning rates
--
-- RETURN:
-- x : the new x vector
-- f(x) : the function, evaluated before the update
--
function optim.sgd_decayed_weight_decay(opfunc, x, state)
-- (0) get/update state
local state = state or {}
local lr = state.learningRate or 1e-3
local lrd = state.learningRateDecay or 0
local wd = state.weightDecay or 0
local mom = state.momentum or 0
local lrs = state.learningRates
state.evalCounter = state.evalCounter or 0
local nevals = state.evalCounter
state.sign_tensor = state.sign_tensor or torch.Tensor()
state.abs_x = state.abs_x or torch.Tensor()
-- (1) evaluate f(x) and df/dx
local fx,dfdx = opfunc(x)
-- (2) apply momentum
if mom ~= 0 then
if not state.dfdx then
state.dfdx = torch.Tensor():typeAs(dfdx):resizeAs(dfdx):copy(dfdx)
else
state.dfdx:mul(mom):add(1-mom, dfdx)
end
dfdx = state.dfdx
end
-- (2) learning rate decay (annealing)
local clr = lr / (1 + nevals*lrd)
-- (3) weight decay -- weight decay is applied *AFTER* learning rate decay, as opposed to before as in the standard optim.sgd implementation
if wd ~= 0 then
x:add(-wd*clr, x)
end
-- (3.25) L3 weight decay
if state.L3weightDecay ~= 0 then
state.abs_x:resizeAs(x)
state.abs_x:abs(x)
x:addcmul(-wd*clr, state.abs_x, x)
end
-- (3.5) L1 weight decay
if state.L1weightDecay ~= 0 then
state.sign_tensor:resizeAs(x)
state.sign_tensor:sign(x)
x:add(-clr * state.L1weightDecay, state.sign_tensor)
end
-- (4) parameter update with single or individual learning rates
if lrs then
if not state.deltaParameters then
state.deltaParameters = torch.Tensor():typeAs(x):resizeAs(dfdx)
end
state.deltaParameters:copy(lrs):cmul(dfdx)
x:add(-clr, state.deltaParameters)
else
x:add(-clr, dfdx)
end
-- (5) update evaluation counter
if state.learningRateDecay > 0 then -- added by Jason 10/26/12; only counter iterations towards learning rate decay when learning rate decay is active
state.evalCounter = state.evalCounter + 1
end
-- return x*, f(x) before optimization
return x,{fx}
end