forked from torch/nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hessian.lua
375 lines (334 loc) · 15.4 KB
/
hessian.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
----------------------------------------------------------------------
-- hessian.lua: this file appends extra methods to modules in nn,
-- to estimate diagonal elements of the Hessian. This is useful
-- to condition learning rates individually.
----------------------------------------------------------------------
nn.hessian = {}
----------------------------------------------------------------------
-- Hessian code is still experimental,
-- and deactivated by default
----------------------------------------------------------------------
function nn.hessian.enable()
local function accDiagHessianParameters(module, input, diagHessianOutput, gw, hw)
if #gw ~= #hw then
error('Number of gradients is nto equal to number of hessians')
end
module.inputSq = module.inputSq or input.new()
module.inputSq:resizeAs(input)
torch.cmul(module.inputSq, input, input)
-- replace gradients with hessian
for i=1,#gw do
local gwname = gw[i]
local hwname = hw[i]
local gwval = module[gwname]
local hwval = module[hwname]
if hwval == nil then
module[hwname] = gwval.new():resizeAs(gwval)
hwval = module[hwname]
end
module[gwname] = hwval
module[hwname] = gwval
end
local oldOutput = module.output
module.output = module.output.new():resizeAs(oldOutput)
module.forward(module, module.inputSq)
module.accGradParameters(module, module.inputSq, diagHessianOutput, 1)
-- put back gradients
for i=1,#gw do
local gwname = gw[i]
local hwname = hw[i]
local gwval = module[gwname]
local hwval = module[hwname]
module[gwname] = hwval
module[hwname] = gwval
end
module.output = oldOutput
end
nn.hessian.accDiagHessianParameters = accDiagHessianParameters
local function updateDiagHessianInput(module, input, diagHessianOutput, w, wsq)
if #w ~= #wsq then
error('Number of weights is not equal to number of weights squares')
end
module.diagHessianInput = module.diagHessianInput or input.new()
module.diagHessianInput:resizeAs(input)
local gi = module.gradInput
module.gradInput = module.diagHessianInput
for i=1,#w do
local wname = w[i]
local wsqname = wsq[i]
local wval = module[wname]
local wsqval = module[wsqname]
if wsqval == nil then
module[wsqname] = wval.new()
wsqval = module[wsqname]
end
wsqval:resizeAs(wval)
torch.cmul(wsqval, wval, wval)
module[wsqname] = wval
module[wname] = wsqval
end
module.updateGradInput(module,input,diagHessianOutput)
for i=1,#w do
local wname = w[i]
local wsqname = wsq[i]
local wval = module[wname]
local wsqval = module[wsqname]
module[wname] = wsqval
module[wsqname] = wval
end
module.gradInput = gi
end
nn.hessian.updateDiagHessianInput = updateDiagHessianInput
local function updateDiagHessianInputPointWise(module, input, diagHessianOutput)
local tdh = diagHessianOutput.new():resizeAs(diagHessianOutput):fill(1)
updateDiagHessianInput(module,input,tdh,{},{})
module.diagHessianInput:cmul(module.diagHessianInput)
module.diagHessianInput:cmul(diagHessianOutput)
end
nn.hessian.updateDiagHessianInputPointWise = updateDiagHessianInputPointWise
local function initDiagHessianParameters(module,gw,hw)
module.diagHessianInput = module.diagHessianInput or module.gradInput.new();
for i=1,#gw do
module[hw[i]] = module[hw[i]] or module[gw[i]].new():resizeAs(module[gw[i]])
end
end
nn.hessian.initDiagHessianParameters = initDiagHessianParameters
----------------------------------------------------------------------
-- Module
----------------------------------------------------------------------
function nn.Module.updateDiagHessianInput(self, input, diagHessianOutput)
error(torch.typename(self) .. ':updateDiagHessianInput() is undefined')
end
function nn.Module.accDiagHessianParameters(self, input, diagHessianOutput)
end
function nn.Module.initDiagHessianParameters()
end
----------------------------------------------------------------------
-- Sequential
----------------------------------------------------------------------
function nn.Sequential.initDiagHessianParameters(self)
for i=1,#self.modules do
self.modules[i]:initDiagHessianParameters()
end
end
function nn.Sequential.updateDiagHessianInput(self, input, diagHessianOutput)
local currentDiagHessianOutput = diagHessianOutput
local currentModule = self.modules[#self.modules]
for i=#self.modules-1,1,-1 do
local previousModule = self.modules[i]
currentDiagHessianOutput = currentModule:updateDiagHessianInput(previousModule.output, currentDiagHessianOutput)
currentModule = previousModule
end
currentDiagHessianOutput = currentModule:updateDiagHessianInput(input, currentDiagHessianOutput)
self.diagHessianInput = currentDiagHessianOutput
return currentDiagHessianOutput
end
function nn.Sequential.accDiagHessianParameters(self, input, diagHessianOutput)
local currentDiagHessianOutput = diagHessianOutput
local currentModule = self.modules[#self.modules]
for i=#self.modules-1,1,-1 do
local previousModule = self.modules[i]
currentModule:accDiagHessianParameters(previousModule.output, currentDiagHessianOutput)
currentDiagHessianOutput = currentModule.diagHessianInput
currentModule = previousModule
end
currentModule:accDiagHessianParameters(input, currentDiagHessianOutput)
end
----------------------------------------------------------------------
-- Criterion
----------------------------------------------------------------------
function nn.Criterion.updateDiagHessianInput(self, input, diagHessianOutput)
error(torch.typename(self) .. ':updateDiagHessianInput() is undefined')
end
----------------------------------------------------------------------
-- MSECriterion
----------------------------------------------------------------------
function nn.MSECriterion.updateDiagHessianInput(self, input, target)
self.diagHessianInput = self.diagHessianInput or input.new()
local val = 2
if self.sizeAverage then
val = val / input:nElement()
end
self.diagHessianInput:resizeAs(input):fill(val)
return self.diagHessianInput
end
----------------------------------------------------------------------
-- WeightedMSECriterion
----------------------------------------------------------------------
function nn.WeightedMSECriterion.updateDiagHessianInput(self,input,target)
return nn.MSECriterion.updateDiagHessianInput(self,input,target)
end
----------------------------------------------------------------------
-- L1Cost
----------------------------------------------------------------------
function nn.L1Cost.updateDiagHessianInput(self,input)
self.diagHessianInput = self.diagHessianInput or input.new()
self.diagHessianInput:resizeAs(input)
self.diagHessianInput:fill(1)
self.diagHessianInput[torch.eq(input,0)] = 0
return self.diagHessianInput
end
----------------------------------------------------------------------
-- Linear
----------------------------------------------------------------------
function nn.Linear.updateDiagHessianInput(self, input, diagHessianOutput)
updateDiagHessianInput(self, input, diagHessianOutput, {'weight'}, {'weightSq'})
return self.diagHessianInput
end
function nn.Linear.accDiagHessianParameters(self, input, diagHessianOutput)
accDiagHessianParameters(self,input, diagHessianOutput, {'gradWeight','gradBias'}, {'diagHessianWeight','diagHessianBias'})
end
function nn.Linear.initDiagHessianParameters(self)
initDiagHessianParameters(self,{'gradWeight','gradBias'},{'diagHessianWeight','diagHessianBias'})
end
----------------------------------------------------------------------
-- SpatialConvolution
----------------------------------------------------------------------
function nn.SpatialConvolution.updateDiagHessianInput(self, input, diagHessianOutput)
updateDiagHessianInput(self, input, diagHessianOutput, {'weight'}, {'weightSq'})
return self.diagHessianInput
end
function nn.SpatialConvolution.accDiagHessianParameters(self, input, diagHessianOutput)
accDiagHessianParameters(self,input, diagHessianOutput, {'gradWeight','gradBias'}, {'diagHessianWeight','diagHessianBias'})
end
function nn.SpatialConvolution.initDiagHessianParameters(self)
initDiagHessianParameters(self,{'gradWeight','gradBias'},{'diagHessianWeight','diagHessianBias'})
end
----------------------------------------------------------------------
-- SpatialFullConvolution
----------------------------------------------------------------------
function nn.SpatialFullConvolution.updateDiagHessianInput(self, input, diagHessianOutput)
updateDiagHessianInput(self, input, diagHessianOutput, {'weight'}, {'weightSq'})
return self.diagHessianInput
end
function nn.SpatialFullConvolution.accDiagHessianParameters(self, input, diagHessianOutput)
accDiagHessianParameters(self,input, diagHessianOutput, {'gradWeight','gradBias'}, {'diagHessianWeight','diagHessianBias'})
end
function nn.SpatialFullConvolution.initDiagHessianParameters(self)
initDiagHessianParameters(self,{'gradWeight','gradBias'},{'diagHessianWeight','diagHessianBias'})
end
----------------------------------------------------------------------
-- SpatialConvolutionMap
----------------------------------------------------------------------
function nn.SpatialConvolutionMap.updateDiagHessianInput(self, input, diagHessianOutput)
updateDiagHessianInput(self, input, diagHessianOutput, {'weight','bias'}, {'weightSq','biasSq'})
return self.diagHessianInput
end
function nn.SpatialConvolutionMap.accDiagHessianParameters(self, input, diagHessianOutput)
accDiagHessianParameters(self,input, diagHessianOutput, {'gradWeight','gradBias'}, {'diagHessianWeight','diagHessianBias'})
end
function nn.SpatialConvolutionMap.initDiagHessianParameters(self)
initDiagHessianParameters(self,{'gradWeight','gradBias'},{'diagHessianWeight','diagHessianBias'})
end
----------------------------------------------------------------------
-- SpatialFullConvolutionMap
----------------------------------------------------------------------
function nn.SpatialFullConvolutionMap.updateDiagHessianInput(self, input, diagHessianOutput)
updateDiagHessianInput(self, input, diagHessianOutput, {'weight'}, {'weightSq'})
return self.diagHessianInput
end
function nn.SpatialFullConvolutionMap.accDiagHessianParameters(self, input, diagHessianOutput)
accDiagHessianParameters(self,input, diagHessianOutput, {'gradWeight','gradBias'}, {'diagHessianWeight','diagHessianBias'})
end
function nn.SpatialFullConvolutionMap.initDiagHessianParameters(self)
initDiagHessianParameters(self,{'gradWeight','gradBias'},{'diagHessianWeight','diagHessianBias'})
end
----------------------------------------------------------------------
-- Tanh
----------------------------------------------------------------------
function nn.Tanh.updateDiagHessianInput(self, input, diagHessianOutput)
updateDiagHessianInputPointWise(self, input, diagHessianOutput)
return self.diagHessianInput
end
----------------------------------------------------------------------
-- TanhShrink
----------------------------------------------------------------------
function nn.TanhShrink.updateDiagHessianInput(self, input, diagHessianOutput)
updateDiagHessianInputPointWise(self.tanh, input, diagHessianOutput)
self.diagHessianInput = self.diagHessianInput or input.new():resizeAs(input)
torch.add(self.diagHessianInput, self.tanh.diagHessianInput, diagHessianOutput)
return self.diagHessianInput
end
----------------------------------------------------------------------
-- Square
----------------------------------------------------------------------
function nn.Square.updateDiagHessianInput(self, input, diagHessianOutput)
updateDiagHessianInputPointWise(self, input, diagHessianOutput)
return self.diagHessianInput
end
----------------------------------------------------------------------
-- Sqrt
----------------------------------------------------------------------
function nn.Sqrt.updateDiagHessianInput(self, input, diagHessianOutput)
updateDiagHessianInputPointWise(self, input, diagHessianOutput)
return self.diagHessianInput
end
----------------------------------------------------------------------
-- Reshape
----------------------------------------------------------------------
function nn.Reshape.updateDiagHessianInput(self, input, diagHessianOutput)
self.diagHessianInput = self.diagHessianInput or input.new()
diagHessianOutput = diagHessianOutput:contiguous()
self.diagHessianInput:set(diagHessianOutput):resizeAs(input)
return self.diagHessianInput
end
----------------------------------------------------------------------
-- Parameters manipulation:
-- we modify these functions such that they return hessian coefficients
----------------------------------------------------------------------
function nn.Module.parameters(self)
if self.weight and self.bias then
return {self.weight, self.bias}, {self.gradWeight, self.gradBias}, {self.diagHessianWeight, self.diagHessianBias}
elseif self.weight then
return {self.weight}, {self.gradWeight}, {self.diagHessianWeight}
elseif self.bias then
return {self.bias}, {self.gradBias}, {self.diagHessianBias}
else
return
end
end
function nn.Module.getParameters(self)
-- get parameters
local parameters,gradParameters,hessianParameters = self:parameters()
-- flatten parameters and gradients
local flatParameters = nn.Module.flatten(parameters)
collectgarbage()
local flatGradParameters = nn.Module.flatten(gradParameters)
collectgarbage()
local flatHessianParameters
if hessianParameters and hessianParameters[1] then
flatHessianParameters = nn.Module.flatten(hessianParameters)
collectgarbage()
end
-- return new flat vector that contains all discrete parameters
return flatParameters, flatGradParameters, flatHessianParameters
end
function nn.Sequential.parameters(self)
local function tinsert(to, from)
if type(from) == 'table' then
for i=1,#from do
tinsert(to,from[i])
end
else
table.insert(to,from)
end
end
local w = {}
local gw = {}
local ggw = {}
for i=1,#self.modules do
local mw,mgw,mggw = self.modules[i]:parameters()
if mw then
tinsert(w,mw)
tinsert(gw,mgw)
tinsert(ggw,mggw)
end
end
return w,gw,ggw
end
----------------------------------------------------------------------
-- Avoid multiple calls to enable()
----------------------------------------------------------------------
function nn.hessian.enable()
end
end