-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUGM_Infer_Junction.m
executable file
·479 lines (426 loc) · 12.1 KB
/
UGM_Infer_Junction.m
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
function [nodeBel, edgeBel, logZ] = UGM_Infer_Junction(nodePot,edgePot,edgeStruct,ordering)
debug = 0;
[nNodes,maxState] = size(nodePot);
nEdges = size(edgePot,3);
edgeEnds = edgeStruct.edgeEnds;
V = edgeStruct.V;
E = edgeStruct.E;
nStates = edgeStruct.nStates;
if nargin < 4
ordering = 1:nNodes;
end
%% Triangulate graph and find cliques
adj = UGM_VE2adj(V,E,edgeEnds);
adj_new = adj;
cliques = cell(0,1);
for n = ordering(:)'
% Add extra
neighbors = find(adj(:,n));
for n1 = neighbors(:)'
for n2 = neighbors(:)'
if n1 ~= n2
adj(n1,n2) = 1;
adj(n2,n1) = 1;
adj_new(n1,n2) = 1;
adj_new(n1,n2) = 1;
end
end
end
% Add to set of cliques
clique(n,n) = 1;
clique(n,neighbors) = 1;
% Remove node
adj(n,:) = 0;
adj(:,n) = 0;
% If not a subset of an existing clique, add to set of cliques
cand = [n;neighbors];
add = 1;
for c = 1:length(cliques)
if isempty(setdiff(cand,cliques{c}))
add = 0;
break;
end
end
if add
cliques{end+1,1} = cand;
end
if debug
clf;
drawGraph(adj_new)
pause
end
end
nCliques = length(cliques);
if debug;
fprintf('Cliques:\n')
cliques{:}
end
%% Make initial potentials
nodePotMissing = ones(nNodes,1);
edgePotMissing = ones(nEdges,1);
for c = 1:nCliques
fprintf('Clique %d: %s\n',c,sprintf(' %d',cliques{c}));
nodes = cliques{c};
% Initialize clique potentials
cliquePot{c} = ones([nStates(nodes)' 1]);
ind = cell(length(nodes),1);
for nodeInd = 1:length(nodes)
ind{nodeInd} = 1:nStates(nodes(nodeInd));
end
for nodeInd = 1:length(nodes)
n = nodes(nodeInd);
if nodePotMissing(n)
fprintf('Including node potential for node %d in clique %d\n',n,c);
nodePotMissing(n) = 0;
ind_sub = ind;
for s = 1:nStates(n)
ind_sub{nodeInd} = s;
cliquePot{c}(ind_sub{:}) = cliquePot{c}(ind_sub{:})*nodePot(n,s);
end
end
edges = E(V(n):V(n+1)-1);
for e = edges(:)'
n1 = edgeEnds(e,1);
n2 = edgeEnds(e,2);
if ismember(n1,nodes) && ismember(n2,nodes) && edgePotMissing(e)
fprintf('Including edge potential for edge %d-%d in clique %d\n',edgeEnds(e,1),edgeEnds(e,2),c);
edgePotMissing(e) = 0;
ind_sub = ind;
nodeInd1 = find(nodes==n1);
nodeInd2 = find(nodes==n2);
for s1 = 1:nStates(n1)
for s2 = 1:nStates(n2)
ind_sub{nodeInd1} = s1;
ind_sub{nodeInd2} = s2;
cliquePot{c}(ind_sub{:}) = cliquePot{c}(ind_sub{:})*edgePot(s1,s2,e);
end
end
end
end
end
fprintf('\n');
end
if debug
fprintf('Clique Potentials:\n');
cliquePot{:}
end
%% Makes edges between cliques that satisfy RIP
weights = zeros(0,3);
for c1 = 1:nCliques
for c2 = c1+1:nCliques
sep = length(intersect(cliques{c1},cliques{c2}));
if sep > 0
weights(end+1,:) = [c1 c2 -sep];
end
end
end
edges = find(minSpan(nCliques,weights));
cliqueEdges = weights(edges,1:2);
nCliqueEdges = size(cliqueEdges,1);
[V,E] = UGM_makeEdgeVE(cliqueEdges,nCliques);
for e = 1:nCliqueEdges
separators{e,1} = intersect(cliques{cliqueEdges(e,1)},cliques{cliqueEdges(e,2)});
end
if debug
cliqueEdges
fprintf('Separators:\n');
separators{:}
end
%% Message Passing
% Count number of neighbors
nNeighbors = zeros(nCliques,1);
for e = 1:nCliqueEdges
nNeighbors(cliqueEdges(e,1)) = nNeighbors(cliqueEdges(e,1))+1;
nNeighbors(cliqueEdges(e,2)) = nNeighbors(cliqueEdges(e,2))+1;
end
% Add all leafs to initial queue
Q = find(nNeighbors == 1);
sent = zeros(nCliqueEdges*2,1);
waiting = ones(nCliqueEdges*2,1);
messages = cell(nCliqueEdges*2,1);
while ~isempty(Q)
c = Q(1);
Q = Q(2:end);
wait = waiting(V(c):V(c+1)-1);
sending = sent(V(c):V(c+1)-1);
nWaiting = sum(wait==1);
if nWaiting == 0
% Send final messages
for sendEdge = [double(V(c))+find(sending==0)-1]'
sent(sendEdge) = 1;
[messages,waiting,nei] = send(c,sendEdge,cliques,cliquePot,messages,waiting,nStates,cliqueEdges,V,E,separators);
if nNeighbors(nei) == 1 || nNeighbors(nei) == 0
Q = [Q;nei];
end
end
else
remainingEdge = V(c)+find(wait==1)-1;
sent(remainingEdge) = 1;
[messages,waiting,nei] = send(c,remainingEdge,cliques,cliquePot,messages,waiting,nStates,cliqueEdges,V,E,separators);
nNeighbors(nei) = nNeighbors(nei)-1;
if nNeighbors(nei) == 1 || nNeighbors(nei) == 0
Q = [Q;nei];
end
end
end
%messages{:}
%% Compute cliqueBel
cliqueBel = cell(nCliques,1);
for c = 1:nCliques
nodes = cliques{c};
ind = cell(length(nodes),1);
for nodeInd = 1:length(nodes)
ind{nodeInd} = 1:nStates(nodes(nodeInd));
end
% Multiply cliquePot by all incoming messages
cb = cliquePot{c};
edges = E(V(c):V(c+1)-1);
for e = edges(:)'
if c == cliqueEdges(e,2)
msg = messages{e};
else
msg = messages{e+nCliqueEdges};
end
ind_sub = ind;
sepLength = length(separators{e});
sep = zeros(sepLength,1);
s = cell(length(sep),1);
for n = 1:sepLength
s{n,1} = 1;
sep(n) = find(nodes==separators{e}(n));
end
while 1
for nodeInd = 1:length(sep)
ind_sub{sep(nodeInd)} = s{nodeInd};
end
cb(ind_sub{:}) = cb(ind_sub{:})*msg(s{:});
for nodeInd = 1:sepLength
s{nodeInd} = s{nodeInd} + 1;
if s{nodeInd} <= nStates(sep(nodeInd))
break;
else
s{nodeInd} = 1;
end
end
if nodeInd == length(sep) && s{end} == 1
break;
end
end
end
cb = cb./sum(cb(:));
cliqueBel{c} = cb;
end
%cliqueBel{:}
%% Compute nodeBel
nodeBel = zeros(size(nodePot));
nodeBelMissing = ones(nNodes,1);
for c = 1:nCliques
cb = cliqueBel{c};
nodes = cliques{c};
ind = cell(length(nodes),1);
for nodeInd = 1:length(nodes)
ind{nodeInd} = 1:nStates(nodes(nodeInd));
end
for nodeInd = 1:length(nodes)
n = nodes(nodeInd);
if nodeBelMissing(n)
nodeBelMissing(n) = 0;
ind_sub = ind;
for s = 1:nStates(n)
ind_sub{nodeInd} = s;
slice = cb(ind_sub{:});
nodeBel(n,s) = sum(slice(:));
end
end
end
end
%% Compute edgeBel
if nargout > 1
edgeBel = zeros(size(edgePot));
edgeBelMissing = ones(nEdges,1);
for c = 1:nCliques
cb = cliqueBel{c};
nodes = cliques{c};
ind = cell(length(nodes),1);
for nodeInd = 1:length(nodes)
ind{nodeInd} = 1:nStates(nodes(nodeInd));
end
for e = 1:nEdges
n1 = edgeEnds(e,1);
n2 = edgeEnds(e,2);
n1Ind = find(n1==nodes);
n2Ind = find(n2==nodes);
if edgeBelMissing(e) && ~isempty(n1Ind) && ~isempty(n2Ind)
edgeBelMissing(e) = 0;
ind_sub = ind;
for s1 = 1:nStates(n1)
for s2 = 1:nStates(n2)
ind_sub{n1Ind} = s1;
ind_sub{n2Ind} = s2;
slice = cb(ind_sub{:});
edgeBel(s1,s2,e) = sum(slice(:));
end
end
end
end
end
end
if nargout > 2
%% Compute sepBel
sepBel = cell(nCliqueEdges,1);
for e = 1:nCliqueEdges
c = cliqueEdges(e,1);
cb = cliqueBel{c};
nodes = cliques{c};
ind = cell(length(nodes),1);
for nodeInd = 1:length(nodes)
ind{nodeInd} = 1:nStates(nodes(nodeInd));
end
sepLength = length(separators{e});
sep = zeros(sepLength,1);
s = cell(length(sep),1);
for n = 1:sepLength
s{n,1} = 1;
sep(n) = find(nodes==separators{e}(n));
end
sb = ones([nStates(separators{e})' 1]);
while 1
for nodeInd = 1:length(sep)
ind{sep(nodeInd)} = s{nodeInd};
end
sb(s{:}) = sum(cb(ind{:}));
for nodeInd = 1:sepLength
s{nodeInd} = s{nodeInd} + 1;
if s{nodeInd} <= nStates(sep(nodeInd))
break;
else
s{nodeInd} = 1;
end
end
if nodeInd == length(sep) && s{end} == 1
break;
end
end
sepBel{e} = sb./sum(sb(:));
end
% Node Energy
Energy1 = 0;
for n = 1:nNodes
Energy1 = Energy1 - sum(nodeBel(n,1:nStates(n)).*log(nodePot(n,1:nStates(n))));
end
% Edge Energy
Energy2 = 0;
for e = 1:nEdges
n1 = edgeEnds(e,1);
n2 = edgeEnds(e,2);
eb = edgeBel(1:nStates(n1),1:nStates(n2),e);
ep = edgePot(1:nStates(n1),1:nStates(n2),e);
Energy2 = Energy2 - sum(eb(:).*log(ep(:)));
end
% Separator Entropy
Entropy1 = 0;
for e = 1:nCliqueEdges
Entropy1 = Entropy1 + sum(sepBel{e}(:).*log(sepBel{e}(:)));
end
% Clique Entropy
Entropy2 = 0;
for c = 1:nCliques
Entropy2 = Entropy2 - sum(cliqueBel{c}(:).*log(cliqueBel{c}(:)));
end
F = (Energy1+Energy2) - (Entropy1+Entropy2);
logZ = -F;
end
end
%% Message passing function
function [messages,waiting,nei] = send(c,e,cliques,cliquePot,messages,waiting,nStates,edgeEnds,V,E,separators)
nEdges = size(edgeEnds,1);
edge = E(e);
if c == edgeEnds(edge,1)
nei = edgeEnds(edge,2);
else
nei = edgeEnds(edge,1);
end
fprintf('Sending from %d to %d\n',c,nei);
% Opposite edge is no longer waiting
for tmp = V(nei):V(nei+1)-1
if tmp ~= e && E(tmp) == E(e)
waiting(tmp) = 0;
end
end
e = edge;
nodes = cliques{c};
for nodeInd = 1:length(nodes)
ind{nodeInd} = 1:nStates(nodes(nodeInd));
end
% Compute Product of clique potential with all incoming messages except
% along e
temp = cliquePot{c};
neighbors = E(V(c):V(c+1)-1);
for e2 = neighbors(:)'
if e ~= e2
ind_sub = ind;
sepLength = length(separators{e2});
sep = zeros(sepLength,1);
s = cell(length(sep),1);
for n = 1:sepLength
s{n,1} = 1;
sep(n) = find(nodes==separators{e2}(n));
end
while 1
for nodeInd = 1:length(sep)
ind_sub{sep(nodeInd)} = s{nodeInd};
end
if c == edgeEnds(e2,2)
temp(ind_sub{:}) = temp(ind_sub{:})*messages{e2}(s{:});
else
temp(ind_sub{:}) = temp(ind_sub{:})*messages{e2+nEdges}(s{:});
end
for nodeInd = 1:length(sep)
s{nodeInd} = s{nodeInd} + 1;
if s{nodeInd} <= nStates(sep(nodeInd))
break;
else
s{nodeInd} = 1;
end
end
if nodeInd == length(sep) && s{end} == 1
break;
end
end
end
end
% Sum out over all variables except separator set
sepLength = length(separators{e});
sep = zeros(sepLength,1);
s = cell(length(sep),1);
for n = 1:sepLength
s{n,1} = 1;
sep(n) = find(nodes==separators{e}(n));
end
newm = ones([nStates(sep)' 1]);
ind_sub = ind;
while 1
for nodeInd = 1:length(sep)
ind_sub{sep(nodeInd)} = s{nodeInd};
end
slice = temp(ind_sub{:});
newm(s{:}) = sum(slice(:));
for nodeInd = 1:length(sep)
s{nodeInd} = s{nodeInd} + 1;
if s{nodeInd} <= nStates(sep(nodeInd))
break;
else
s{nodeInd} = 1;
end
end
if nodeInd == length(sep) && s{end} == 1
break;
end
end
newm = newm./sum(newm(:));
if c == edgeEnds(e,2)
messages{e+nEdges} = newm;
else
messages{e} = newm;
end
end