-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenergies.lua
268 lines (236 loc) · 7.92 KB
/
energies.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
----------------------------------------------------------------------
--
-- Copyright (c) 2012 Clement Farabet
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--
----------------------------------------------------------------------
-- description:
-- gm.energies - a list of functions to compute the energy of
-- a graph, and the gradient ot that energy wrt
-- its internal parameters
--
-- history:
-- February 2012 - initial draft - Clement Farabet
----------------------------------------------------------------------
-- that table contains all the gradient functions
gm.energies = {}
gm.energies.crf = {}
gm.energies.mrf = {}
-- shortcuts
local zeros = torch.zeros
local ones = torch.ones
local eye = torch.eye
local sort = torch.sort
local log = torch.log
local exp = torch.exp
-- messages
local warning = function(msg)
print(sys.COLORS.red .. msg .. sys.COLORS.none)
end
----------------------------------------------------------------------
-- Negative log-likelihood of a CRF
--
function gm.energies.crf.nll(graph,w,nodeMap,edgeMap,inferMethod,maxIter,Y,Xnode,Xedge)
-- check sizes
if Xnode:nDimension() == 2 then -- single example
Xnode = Xnode:reshape(1,Xnode:size(1),Xnode:size(2))
Xedge = Xedge:reshape(1,Xedge:size(1),Xedge:size(2))
Y = Y:reshape(1,Y:size(1))
end
-- locals
local Tensor = torch.Tensor
local nInstances = Y:size(1)
local nNodes = graph.nNodes
local maxStates = nodeMap:size(2)
local nNodeFeatures = Xnode:size(2)
local nEdgeFeatures = Xedge:size(2)
local nEdges = graph.nEdges
local nStates = graph.nStates
local edgeEnds = graph.edgeEnds
-- init
local nll = 0
local grad = zeros(w:size())
-- verbose
if graph.verbose then
print('<gm.energies.crf.nll> computing negative log-likelihood')
end
-- compute E=nll and dE/dw
for i = 1,nInstances do
-- make potentials
gm.energies.crf.makePotentials(graph,w,nodeMap,edgeMap,Xnode[i],Xedge[i])
-- perform inference
local nodeBel,edgeBel,logZ = graph:infer(inferMethod,maxIter)
-- update nll
nll = nll - graph:getLogPotentialForConfig(Y[i]) + logZ
-- compute gradients wrt nodes
grad.gm.crfGradWrtNodes(Xnode[i],nodeMap,w,nStates,Y[i],nodeBel,grad)
-- compute gradients wrt edges
grad.gm.crfGradWrtEdges(Xedge[i],edgeMap,w,edgeEnds,nStates,Y[i],edgeBel,grad)
end
-- return nll and grad
return nll,grad
end
----------------------------------------------------------------------
-- Negative log-likelihood of an MRF
--
function gm.energies.mrf.nll(graph,w,nodeMap,edgeMap,inferMethod,maxIter,Y)
-- locals
local Tensor = torch.Tensor
local nNodes = graph.nNodes
local nEdges = graph.nEdges
local nStates = graph.nStates
local edgeEnds = graph.edgeEnds
local maxStates = nodeMap:size(2)
local nInstances = Y:size(1)
-- verbose
if graph.verbose then
print('<gm.energies.mrf.nll> computing negative log-likelihood')
end
-- compute sufficient statistics
local suffStats = g.w:clone():zero()
for i = 1,nInstances do
local y = Y[i]
for n = 1,nNodes do
local idx = nodeMap[n][y[n]]
if idx > 0 then
suffStats[idx] = suffStats[idx] + 1
end
end
for e = 1,nEdges do
local n1 = edgeEnds[e][1]
local n2 = edgeEnds[e][2]
local idx = edgeMap[e][y[n1]][y[n2]]
if idx > 0 then
suffStats[idx] = suffStats[idx] + 1
end
end
end
-- make potentials
gm.energies.mrf.makePotentials(graph,w,nodeMap,edgeMap)
-- perform inference
local nodeBel,edgeBel,logZ = graph:infer(inferMethod,maxIter)
-- update nll
local nll = -w*suffStats + logZ*nInstances
-- compute gradients wrt nodes
local grad = suffStats:clone():mul(-1)
for n = 1,nNodes do
for s = 1,nStates[n] do
local idx = nodeMap[n][s]
if idx > 0 then
grad[idx] = grad[idx] + nInstances * nodeBel[n][s]
end
end
end
-- compute gradients wrt edges
for e = 1,nEdges do
local n1 = edgeEnds[e][1]
local n2 = edgeEnds[e][2]
for s1 = 1,nStates[n1] do
for s2 = 1,nStates[n2] do
local idx = edgeMap[e][s1][s2]
if idx > 0 then
grad[idx] = grad[idx] + nInstances * edgeBel[e][s1][s2]
end
end
end
end
-- return nll and grad
return nll,grad
end
----------------------------------------------------------------------
-- Make potentials for a CRF
--
function gm.energies.crf.makePotentials(graph,w,nodeMap,edgeMap,Xnode,Xedge)
-- locals
local Tensor = torch.Tensor
local nNodes = graph.nNodes
local maxStates = nodeMap:size(2)
local nNodeFeatures = Xnode:size(1)
local nEdgeFeatures = Xedge:size(1)
local nEdges = graph.nEdges
local nStates = graph.nStates
local edgeEnds = graph.edgeEnds
-- verbose
if graph.verbose then
print('<gm.energies.crf.makePotentials> making potentials from parameters')
end
-- generate node potentials
local nodePot = graph.nodePot or Tensor()
nodePot:resize(nNodes,maxStates)
nodePot.gm.crfMakeNodePotentials(Xnode,nodeMap,w,nStates,nodePot)
-- generate edge potentials
local edgePot = graph.edgePot or Tensor()
edgePot:resize(nEdges,maxStates,maxStates)
nodePot.gm.crfMakeEdgePotentials(Xedge,edgeMap,w,edgeEnds,nStates,edgePot)
-- store potentials
graph:setPotentials(nodePot,edgePot)
end
----------------------------------------------------------------------
-- Make potentials for an MRF
--
function gm.energies.mrf.makePotentials(graph,w,nodeMap,edgeMap)
-- locals
local Tensor = torch.Tensor
local exp = math.exp
local nNodes = graph.nNodes
local maxStates = nodeMap:size(2)
local nEdges = graph.nEdges
local nStates = graph.nStates
local edgeEnds = graph.edgeEnds
-- verbose
if graph.verbose then
print('<gm.energies.mrf.makePotentials> making potentials from parameters')
end
-- generate node potentials
local nodePot = graph.nodePot or Tensor()
nodePot:resize(nNodes,maxStates)
nodePot:zero()
for n = 1,nNodes do
for s = 1,nStates[n] do
local idx = nodeMap[n][s]
if idx == 0 then
nodePot[n][s] = 1
else
nodePot[n][s] = exp(w[idx])
end
end
end
-- generate edge potentials
local edgePot = graph.edgePot or Tensor()
edgePot:resize(nEdges,maxStates,maxStates)
edgePot:zero()
for e = 1,nEdges do
local n1 = edgeEnds[e][1]
local n2 = edgeEnds[e][2]
for s1 = 1,nStates[n1] do
for s2 = 1,nStates[n2] do
local idx = edgeMap[e][s1][s2]
if idx == 0 then
edgePot[e][s1][s2] = 1
else
edgePot[e][s1][s2] = exp(w[idx])
end
end
end
end
-- store potentials
graph:setPotentials(nodePot,edgePot)
end