-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhessian.lua
87 lines (74 loc) · 3.9 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
----------------------------------------------------------------------
-- 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.enable() -- enable Hessian usage
local accDiagHessianParameters = nn.hessian.accDiagHessianParameters
local updateDiagHessianInput = nn.hessian.updateDiagHessianInput
local updateDiagHessianInputPointWise = nn.hessian.updateDiagHessianInputPointWise
local initDiagHessianParameters = nn.hessian.initDiagHessianParameters
----------------------------------------------------------------------
-- 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'}, {'diagHessianWeight'})
end
function nn.SpatialFullConvolution.initDiagHessianParameters(self)
initDiagHessianParameters(self,{'gradWeight'},{'diagHessianWeight'})
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'}, {'diagHessianWeight'})
end
function nn.SpatialFullConvolutionMap.initDiagHessianParameters(self)
initDiagHessianParameters(self,{'gradWeight'},{'diagHessianWeight'})
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
----------------------------------------------------------------------
-- 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
----------------------------------------------------------------------
-- Diag
----------------------------------------------------------------------
function nn.Diag.updateDiagHessianInput(self, input, diagHessianOutput)
updateDiagHessianInput(self, input, diagHessianOutput, {'weight'}, {'weightSq'})
return self.diagHessianInput
end
function nn.Diag.accDiagHessianParameters(self, input, diagHessianOutput)
accDiagHessianParameters(self,input, diagHessianOutput, {'gradWeight'}, {'diagHessianWeight'})
end
function nn.Diag.initDiagHessianParameters(self)
initDiagHessianParameters(self,{'gradWeight'},{'diagHessianWeight'})
end