forked from torch/optim
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsgdhd.lua
61 lines (50 loc) · 1.76 KB
/
sgdhd.lua
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
--[[
Hypergradient descent, SGD-HD and SGD-Nesterov-HD
https://arxiv.org/abs/1703.04782
Atilim Gunes Baydin, University of Oxford, March 2017
ARGS:
- `opfunc` : a function that takes a single input (X), the point
of a evaluation, and returns f(X) and df/dX
- `x` : the initial point
- `config` : a table with configuration parameters for the optimizer
- `config.learningRate` : learning rate
- `config.learningLearningRate` : hypergradient learning rate
- `config.weightDecay` : weight decay
- `config.momentum` : Nesterov momentum
- `state` : a table describing the state of the optimizer; after each
call the state is modified
RETURN:
- `x` : the new x vector
- `f(x)` : the function, evaluated before the update
]]
function optim.sgdhd(opfunc, x, config, state)
-- (0) get/update state
local config = config or {}
local state = state or config
local lr = config.learningRate or 1e-3
local llr = config.learningLearningRate or 1e-6
local wd = config.weightDecay or 0
local mom = config.momentum or 0
-- (1) evaluate f(x) and df/dx
local fx, dfdx = opfunc(x)
-- (2) weight decay
if wd ~= 0 then
dfdx:add(wd, x)
end
state.u = state.u or x.new(dfdx:size()):zero()
state.lr = state.lr or lr
-- (3) learning rate update (hypergradient descent)
state.lr = state.lr + llr * torch.dot(dfdx, state.u)
-- (3) apply Nesterov momentum
if mom ~= 0 then
state.v = state.v or x.new(dfdx:size()):zero()
state.v:mul(mom):add(dfdx)
state.u:copy(dfdx):add(mom, state.v)
else
state.u:copy(dfdx)
end
-- (4) update x
x:add(-state.lr, state.u)
-- return x*, f(x) before optimization
return x, {fx}
end