-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.lua
173 lines (151 loc) · 5.26 KB
/
decode.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
----------------------------------------------------------------------
--
-- 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.decode - a list of functions to decode optimal states
--
-- history:
-- February 2012 - initial draft - Clement Farabet
----------------------------------------------------------------------
-- that table contains all the decode
gm.decode = {}
-- shortcuts
local zeros = torch.zeros
local ones = torch.ones
local eye = torch.eye
local sort = torch.sort
-- messages
local warning = function(msg)
print(sys.COLORS.red .. msg .. sys.COLORS.none)
end
----------------------------------------------------------------------
-- exact decoding: only adapted to super small graphs
--
function gm.decode.exact(graph)
-- check args
if not graph.nodePot or not graph.edgePot then
xlua.error('missing nodePot/edgePot, please call graph:setFactors(...)','decode')
end
-- verbose
if graph.verbose then
print('<gm.decode.bp> decoding using exhaustive search')
end
-- local vars
local Tensor = torch.Tensor
local nNodes = graph.nNodes
local maxStates = graph.nodePot:size(2)
local nEdges = graph.nEdges
local nStates = graph.nStates
-- init
local config = ones(nNodes)
local optimalconfig = ones(nNodes)
-- decode, exactly
local maxpot = -1
while true do
-- get potential for configuration
local pot = graph:getPotentialForConfig(config)
-- compare configurations
if pot > maxpot then
maxpot = pot
optimalconfig:copy(config)
end
-- next config
local idx
for i = 1,nNodes do
idx = i
config[i] = config[i] + 1
if config[i] <= nStates[i] then
break
else
config[i] = 1
end
end
-- stop when tried all configurations
if idx == nNodes and config[config:size(1)] == 1 then
break
end
end
-- store and return optimal config
graph.optimal = optimalconfig
return optimalconfig
end
----------------------------------------------------------------------
-- belief propagation: if the given graph is loopy, and maxIter is
-- set > 1, then loopy belief propagation is done
--
function gm.decode.bp(graph,maxIter)
-- check args
if not graph.nodePot or not graph.edgePot then
xlua.error('missing nodePot/edgePot, please call graph:setFactors(...)','decode')
end
maxIter = maxIter or graph.maxIter or 1
-- verbose
if graph.verbose then
print('<gm.decode.bp> decoding using belief-propagation')
end
-- local vars
local Tensor = torch.Tensor
local nNodes = graph.nNodes
local maxStates = graph.nodePot:size(2)
local nEdges = graph.nEdges
local nStates = graph.nStates
local edgeEnds = graph.edgeEnds
local V = graph.V
local E = graph.E
local nodePot = graph.nodePot
local edgePot = graph.edgePot
-- init
local product = ones(nNodes,maxStates)
local nodeBel = ones(nNodes,maxStates)
local nodeBel_old = nodeBel:clone()
local msg = zeros(nEdges*2,maxStates)
local msg_old = zeros(nEdges*2,maxStates)
-- propagate state normalizations
msg.gm.bpInitMessages(edgeEnds,nStates,msg)
-- do loopy belief propagation (if maxIter = 1, it's regular bp)
local idx
for i = 1,maxIter do
idx = i
-- pass messages, for all nodes (true = max of products)
msg.gm.bpComputeMessages(nodePot,edgePot,edgeEnds,nStates,E,V,msg,true)
-- check convergence
if (msg-msg_old):abs():sum() < 1e-4 then break end
msg_old:copy(msg)
end
if graph.verbose then
if idx == maxIter then
warning('<gm.decode.bp> reached max iterations ('..maxIter..') before convergence')
else
print('<gm.decode.bp> decoded graph in '..idx..' iterations')
end
end
-- compute marginal node beliefs
msg.gm.bpComputeNodeBeliefs(nodePot,nodeBel,edgeEnds,nStates,E,V,product,msg)
-- get argmax of nodeBel: that's the optimal config
local pot, optimalconfig = nodeBel:max(2)
optimalconfig = optimalconfig:squeeze(2)
-- store and return optimal config
graph.optimal = optimalconfig
return optimalconfig,nodeBel
end