-
Notifications
You must be signed in to change notification settings - Fork 2
/
lbp_util.py
275 lines (247 loc) · 10.2 KB
/
lbp_util.py
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
import torch
import itertools
from collections import defaultdict
#### below functions let you provide edges
def make_log_potentials(T, K, V, bsz, edges):
"""
edges - nedges x 2
T, K, V = seqlength, nstates, vocab
returns
nedges x bsz x K x K, T x bsz x K x V
"""
# make all pairwise log potentials
pwlpots = torch.randn(edges.size(0), bsz, K, K)
# make all emission log dists
emlps = torch.log_softmax(torch.randn(T*bsz*K, V), dim=1).view(T, bsz, K, -1)
return pwlpots, emlps
# this gives real unary factors and doesn't normalize anything
def make_rbmlike_log_potentials(T, K, bsz, edges):
"""
edges - nedges x 2
T, K, V = seqlength, nstates, vocab
returns
nedges x bsz x K x K, T x bsz x K
"""
# make all pairwise log potentials
pwlpots = torch.randn(edges.size(0), bsz, K, K)
# make all emission log dists
emlps = torch.randn(T, bsz, K)
return pwlpots, emlps
def joint_energy(x, z, pwlpots, emlps, edges):
"""
not normalized.
x, z - T x bsz, T x bsz
"""
assert x.size() == z.size()
T, bsz = x.size()
_, _, K, V = emlps.size()
jen = torch.zeros(bsz)
for c, row in enumerate(edges):
left, right = row
left, right = left.item(), right.item()
for b in range(bsz):
llab, rlab = z[left, b].item(), z[right, b].item()
jen[b] += pwlpots[c][b, llab, rlab]
statedists = emlps.view(-1, K, V)[torch.arange(T*bsz), z.view(-1)] # T*bsz x V
# # can also do:
# statedists = emlps.view(-1, K, V).gather(1, z.view(-1, 1, 1).expand(T*bsz, 1, V)).squeeze()
lps = statedists.gather(1, x.view(-1, 1)) # T*bsz x 1
jen.add_(lps.view(T, bsz).sum(0))
return jen
def brute_joint_partition(pwlpots, emlps, edges):
T, bsz, K, V = emlps.size()
jens = torch.zeros(bsz, K**T * V**T)
idx = 0
for z in itertools.product(range(K), repeat=T):
for x in itertools.product(range(V), repeat=T):
xtens = torch.LongTensor(x).view(T, 1).expand(T, bsz).contiguous()
ztens = torch.LongTensor(z).view(T, 1).expand(T, bsz).contiguous()
jen = joint_energy(xtens, ztens, pwlpots, emlps, edges)
jens[:, idx] = jen
idx += 1
Zs = torch.logsumexp(jens, dim=1)
return Zs
def brute_z_partition(pwlpots, T, edges):
"""
edges are nedges x 2
"""
# Because x distribution is locally normalized, this should give the same thing as above!
_, bsz, K, _ = pwlpots.size()
jens = torch.zeros(bsz, K**T)
idx = 0
for z in itertools.product(range(K), repeat=T):
for c, row in enumerate(edges):
left, right = row
left, right = left.item(), right.item()
for b in range(bsz):
llab, rlab = z[left], z[right]
jens[b, idx] += pwlpots[c][b, llab, rlab]
idx += 1
Zs = torch.logsumexp(jens, dim=1)
return Zs
def brute_z_fac_lmarg(pwlpots, T, edges, tsrc, ttgt, x=None, emlps=None):
"""
edges are nedges x 2
returns bsz x K x K unnormalized log marginals for edge tsrc, ttgt
"""
# Because x distribution is locally normalized, this should give the same thing as above!
_, bsz, K, _ = pwlpots.size()
jens = torch.zeros(bsz, K, K, K**(T-2))
idxs = torch.LongTensor(K, K).zero_()
for z in itertools.product(range(K), repeat=T):
tsval, ttval = z[tsrc], z[ttgt]
idx = idxs[tsval, ttval]
if emlps is not None:
ztens = torch.LongTensor(z).view(T, 1).expand(T, bsz).contiguous()
jens[:, tsval, ttval, idx] += joint_energy(x, ztens, pwlpots, emlps, edges)
else:
for c, row in enumerate(edges):
left, right = row
left, right = left.item(), right.item()
for b in range(bsz):
llab, rlab = z[left], z[right]
jens[b, tsval, ttval, idx] += pwlpots[c][b, llab, rlab]
idxs[tsval, ttval] += 1
faclmarg = torch.logsumexp(jens, dim=3)
return faclmarg
def brute_z_marg(x, pwlpots, emlps, edges):
T, bsz, K, V = emlps.size()
jens = torch.zeros(bsz, K**T)
idx = 0
for z in itertools.product(range(K), repeat=T):
ztens = torch.LongTensor(z).view(T, 1).expand(T, bsz).contiguous()
jen = joint_energy(x, ztens, pwlpots, emlps, edges)
jens[:, idx] = jen
idx += 1
Zs = torch.logsumexp(jens, dim=1)
return Zs
def init_msgs(bsz, K, edges, device, x=None, emlps=None):
"""
inits messages and other stuff
x - T x bsz
emlps - T x bsz x K x V or T x bsz x K
"""
assert emlps is None or emlps.size(1) == bsz
# if x is None them emlps must already have stuff selected
assert emlps is None or x is not None or emlps.dim() == 3
fmsgs, nmsgs, nodene = {}, {}, defaultdict(list)
for e, edge in enumerate(edges):
s, t = edge[0].item(), edge[1].item()
fmsgs[e, s] = torch.zeros(bsz, K).to(device)
fmsgs[e, t] = torch.zeros(bsz, K).to(device)
nmsgs[s, e] = torch.zeros(bsz, K).to(device)
nmsgs[t, e] = torch.zeros(bsz, K).to(device)
nodene[s].append(e)
nodene[t].append(e)
if emlps is not None: # will add a bunch of factor msgs
for i in range(emlps.size(0)):
omsg = torch.Tensor(bsz, K).to(device)
if emlps.dim() == 3:
omsg.copy_(emlps[i])
else:
for b in range(bsz):
omsg[b].copy_(emlps[i][b][:, x[i][b]])
fmsgs[edges.size(0)+i, i] = omsg
nodene[i].append(edges.size(0)+i)
return nmsgs, fmsgs, nodene
def get_beliefs(nmsgs, fmsgs, pwlpots, edges):
_, bsz, K, _ = pwlpots.size()
nbeliefs, fbeliefs = {}, {}
for e, edge in enumerate(edges):
s, t = edge[0].item(), edge[1].item()
fbeliefs[e] = pwlpots[e] + nmsgs[s, e].unsqueeze(2) + nmsgs[t, e].unsqueeze(1)
if s not in nbeliefs:
nbeliefs[s] = pwlpots.new(bsz, K).zero_()
if t not in nbeliefs:
nbeliefs[t] = pwlpots.new(bsz, K).zero_()
nbeliefs[s] += fmsgs[e, s]
nbeliefs[t] += fmsgs[e, t]
# check if we have unary factor messages
unary_fmsgs = [(ue, s) for (ue, s) in fmsgs.keys() if ue >= edges.size(0)]
for (ue, s) in unary_fmsgs: # only nonempty if we have unary factors
nbeliefs[s] += fmsgs[ue, s]
return nbeliefs, fbeliefs
# we'll associate a factor w/ each edge
def bp_update(src, dst, nmsgs, fmsgs, pwlpots, edges, nodene,
node_msg=True, renorm=False):
if node_msg: # src is a node index and dst is a factor index
# get all messages from factor neighbors other than dst
neighbfmsgs = [fmsgs[facne, src] for facne in nodene[src] if facne != dst]
if len(neighbfmsgs) > 0:
#nmsgs[src, dst] = sum(neighbfmsgs)
numsg = sum(neighbfmsgs)
if renorm:
numsg = torch.log_softmax(numsg, dim=1)
diffnorm = torch.norm(nmsgs[src, dst] - numsg).item()
nmsgs[src, dst] = numsg
else:
diffnorm = 0
else: # src is a factor index and dst is a node index
lpots = pwlpots[src] # bsz x K x K
# get all messages from node neighbors other than dst; since we're only doing pw factors,
# there's only one
fleft, fright = edges[src][0].item(), edges[src][1].item()
if fright == dst: # moving "left to right" along the edge
#fmsgs[src, dst] = torch.logsumexp(lpots + nmsgs[fleft, src].unsqueeze(2), dim=1)
numsg = torch.logsumexp(lpots + nmsgs[fleft, src].unsqueeze(2), dim=1)
else: # moving "right to left" along the edge
numsg = torch.logsumexp(lpots + nmsgs[fright, src].unsqueeze(1), dim=2)
if renorm:
numsg = torch.log_softmax(numsg, dim=1)
diffnorm = torch.norm(fmsgs[src, dst] - numsg).item()
fmsgs[src, dst] = numsg
return diffnorm
# this just walks the edges back and forth
def dolbp(pwlpots, edges, x=None, emlps=None, niter=1, renorm=False, tol=1e-3, randomize=False):
"""
pwlpots - nedges x bsz x K x K
edges - nedges x 2
x - T x bsz
emlps - T x bsz x K x V or T x bsz x K
"""
nedges, bsz, K, _ = pwlpots.size()
nmsgs, fmsgs, nodene = init_msgs(bsz, K, edges, pwlpots.device, x=x, emlps=emlps)
if renorm:
for thing in nmsgs.keys():
nmsgs[thing] = torch.log_softmax(nmsgs[thing], dim=1)
for thing in fmsgs.keys():
fmsgs[thing] = torch.log_softmax(fmsgs[thing], dim=1)
# make a list of edge indexes and whether left to right
order = [(e, True) for e in range(nedges)] + [(e, False) for e in range(nedges-1, -1, -1)]
iters_taken = niter
for i in range(niter):
avgdiffnorm = 0.0
if randomize:
perm = torch.randperm(len(order))
order = [order[idx.item()] for idx in perm]
for e, left_to_right in order:
s, t = edges[e][0].item(), edges[e][1].item()
if left_to_right:
# send a node message
dnorm = bp_update(s, e, nmsgs, fmsgs, pwlpots, edges, nodene,
node_msg=True, renorm=renorm)
avgdiffnorm += dnorm
# send a factor message
dnorm = bp_update(e, t, nmsgs, fmsgs, pwlpots, edges, nodene,
node_msg=False, renorm=renorm)
avgdiffnorm += dnorm
else:
# send a node message
dnorm = bp_update(t, e, nmsgs, fmsgs, pwlpots, edges, nodene,
node_msg=True, renorm=renorm)
avgdiffnorm += dnorm
# send a factor message
dnorm = bp_update(e, s, nmsgs, fmsgs, pwlpots, edges, nodene,
node_msg=False, renorm=renorm)
avgdiffnorm += dnorm
if avgdiffnorm/(len(order) * 2.0) <= tol:
iters_taken = i+1
break
# make beliefs
nbeliefs, fbeliefs = get_beliefs(nmsgs, fmsgs, pwlpots, edges)
# normalize them
for k in nbeliefs.keys():
nbeliefs[k] = nbeliefs[k].log_softmax(1)
for k in fbeliefs.keys():
fbeliefs[k] = fbeliefs[k].view(bsz, -1).log_softmax(1).view(bsz, K, K)
return nbeliefs, fbeliefs, iters_taken, nmsgs, fmsgs