-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptim-adadelta.lua
52 lines (47 loc) · 1.39 KB
/
optim-adadelta.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
-- Adadelta
-- Author: Minwei Feng ([email protected])
require 'optim'
function optim.adadelta(opfunc, w, config, state)
local config = config or {}
local state = state or config
local rho = config.rho or 0.9
local epsilon = config.epsilon or 1e-6
local mode = config.mode or 'global' -- global or local
local pc = config.pclient or nil
local su = config.su or 0 -- sync updates (grad and param)
state.pversion = state.pversion or 0
state.dusync = state.dusync or 0
local fx,dfdx = opfunc(w)
if pc and su>1 then
-- accumulate grad
if not config.accumulated then -- need one copy to accumulate
config.accumulated = torch.Tensor():typeAs(dfdx):resizeAs(dfdx):fill(0)
pc:reset(w,config.accumulated)
end
if mode == 'global' then
config.accumulated:add(dfdx)
if state.pversion%su==0 then
pc:async_send_grad()
pc:async_recv_param()
local synctime = sys.clock()
pc:wait()
state.dusync = state.dusync + sys.clock()-synctime
config.accumulated:fill(0)
else
--do nothing here
end
end
elseif pc and su==1 then
if mode == 'global' then
pc:async_send_grad()
pc:async_recv_param()
local synctime = sys.clock()
pc:wait()
state.dusync = state.dusync + sys.clock()-synctime
end
else
assert(false)
end
state.pversion = state.pversion + 1
return w,{fx}
end