-
Notifications
You must be signed in to change notification settings - Fork 7
/
bp.pyx
297 lines (228 loc) · 10 KB
/
bp.pyx
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
from __future__ import division, print_function
import numpy as np
cimport numpy as np
import cyutils
import utils
from cymessage import Messages
from cygraph import FactorGraph, Factor, Var
from libcpp cimport bool
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
DTYPE = np.float64
ctypedef np.float64_t DTYPE_t
def belief_propogation_log(model, char *lang, int sentLen, list batch_lstm_feats, test=False):
cdef float threshold = 0.05
cdef int maxIters = 50
cdef int batch_size = len(batch_lstm_feats)
if model.model_type=="specific":
langIdx = model.langs.index(lang)
# Initialize factor graph, add vars and factors
print("Creating Factor Graph...")
graph = FactorGraph(sentLen, batch_size, model.gpu)
# Add variables to graph
for tag in model.uniqueTags:
for t in range(sentLen):
label=None
graph.addVariable(tag, label, t)
if not model.no_pairwise:
# Add pairwise factors to graph
kind = "pair"
for tag1 in model.uniqueTags:
for tag2 in model.uniqueTags:
if tag1!=tag2 and tag1.idx<tag2.idx:
for t in range(sentLen):
var1 = graph.getVarByTimestepnTag(t, tag1.idx)
var2 = graph.getVarByTimestepnTag(t, tag2.idx)
graph.addFactor(kind, var1, var2)
# Retrieve pairwise weights
pairwise_weights_np = []
for i in range(len(model.pairs)):
pairwise_weights_np.append(model.pairwise_weights[i].cpu().data.numpy())
if model.model_type=="specific":
for i in range(len(model.pairs)):
pairwise_weights_np[i] = cyutils.logSumExp(pairwise_weights_np[i], model.lang_pairwise_weights[i][langIdx].cpu().data.numpy())
if not model.no_transitions:
# Add transition factors to graph
kind = "trans"
for tag in model.uniqueTags:
for t in range(sentLen-1):
var1 = graph.getVarByTimestepnTag(t, tag.idx)
var2 = graph.getVarByTimestepnTag(t+1, tag.idx)
graph.addFactor(kind, var1, var2)
transition_weights_np = {}
for tag in model.uniqueTags:
transition_weights_np[tag.idx] = model.transition_weights[tag.idx].cpu().data.numpy()
if model.model_type=="specific":
for tag in model.uniqueTags:
transition_weights_np[tag.idx] = cyutils.logSumExp(transition_weights_np[tag.idx], model.lang_transition_weights[tag.idx][langIdx].cpu().data.numpy())
kind = "lstm"
for tag in model.uniqueTags:
for t in range(sentLen):
var = graph.getVarByTimestepnTag(t, tag.idx)
graph.addFactor(kind, var, "LSTMVar")
# Initialize messages
messages = Messages(graph, batch_size)
# Add LSTM unary factor message to each variable
for tag in model.uniqueTags:
for t in range(sentLen):
lstm_vecs = []
var = graph.getVarByTimestepnTag(t, tag.idx)
lstm_factor = graph.getFactorByVars(var, "LSTMVar")
cur_tag_lstm_weights = model.lstm_weights[tag.idx]
for batchIdx in range(batch_size):
lstm_feats = batch_lstm_feats[batchIdx]
cur_lstm_feats = lstm_feats[t]
cur_tag_lstm_feats = cur_lstm_feats[model.tag_offsets[tag.name]: model.tag_offsets[tag.name]+tag.size()]
lstm_vec = torch.unsqueeze(cur_tag_lstm_weights + cur_tag_lstm_feats, 0)
lstm_vec = utils.logNormalizeTensor(lstm_vec).squeeze(dim=0)
lstm_vecs.append(lstm_vec.cpu().data.numpy())
messages.updateMessage(lstm_factor, var, np.array(lstm_vecs))
cdef int iter = 0
while iter<maxIters:
print("[BP iteration %d]" %iter, end=" ")
maxVal = [-float("inf")]*batch_size
for tag in model.uniqueTags:
var_list = graph.getVarsByTag(tag.idx)
# FORWARD
for t in range(sentLen):
var = var_list[t]
# Get pairwise potentials
factor_list = graph.getFactorByVars(var)
factor_sum = np.zeros([batch_size, var.tag.size()], dtype=DTYPE)
# Maintaining factor sum improves efficiency
for factor_mult in factor_list:
factor_sum += messages.getMessage(factor_mult, var).value
for factor in factor_list:
if factor.kind=="pair":
var2 = factor.getOtherVar(var)
# variable2factor
message = np.zeros([batch_size, var.tag.size()], dtype=DTYPE)
message = factor_sum - messages.getMessage(factor, var).value
message = cyutils.logNormalize(message)
curVal = messages.getMessage(var, factor).value
# From (Sutton, 2012)
maxVal = np.maximum(maxVal, np.amax(np.abs(curVal - message), 1))
messages.updateMessage(var, factor, message)
# factor2variable
if var2.tag.idx < var.tag.idx:
pairwise_idx = model.pairs.index((var2.tag.idx, var.tag.idx))
transpose = False
else:
pairwise_idx = model.pairs.index((var.tag.idx, var2.tag.idx))
transpose = True
cur_pairwise_weights = pairwise_weights_np[pairwise_idx]
if transpose:
if test:
pairwise_pot = cyutils.logMax(cur_pairwise_weights, messages.getMessage(var2, factor).value, redAxis=1)
else:
pairwise_pot = cyutils.logDot(cur_pairwise_weights, messages.getMessage(var2, factor).value, redAxis=1)
else:
if test:
pairwise_pot = cyutils.logMax(messages.getMessage(var2, factor).value, cur_pairwise_weights, redAxis=0)
else:
pairwise_pot = cyutils.logDot(messages.getMessage(var2, factor).value, cur_pairwise_weights, redAxis=0)
pairwise_pot = cyutils.logNormalize(pairwise_pot)
curVal = messages.getMessage(factor, var).value
maxVal = np.maximum(maxVal, np.amax(np.abs(curVal - pairwise_pot), 1))
messages.updateMessage(factor, var, pairwise_pot)
factor_sum += pairwise_pot - curVal
if not model.no_transitions:
cur_tag_weights = transition_weights_np[tag.idx]
# Get transition potential
if t!=sentLen-1:
var2 = graph.getVarByTimestepnTag(t+1, tag.idx)
trans_factor = graph.getFactorByVars(var, var2)
# Variable2Factor Message
message = np.zeros([batch_size, var.tag.size()], dtype=DTYPE)
message = factor_sum - messages.getMessage(trans_factor, var).value
# for factor_mult in factor_list:
# if factor_mult!=trans_factor:
# message += messages.getMessage(factor_mult, var).value
message = cyutils.logNormalize(message)
curVal = messages.getMessage(var, trans_factor).value
maxVal = np.maximum(maxVal, np.amax(np.abs(curVal - message), 1))
messages.updateMessage(var, trans_factor, message)
# Factor2Variable Message
if test:
transition_pot = cyutils.logMax(messages.getMessage(var, trans_factor).value, cur_tag_weights, redAxis=0)
else:
transition_pot = cyutils.logDot(messages.getMessage(var, trans_factor).value, cur_tag_weights, redAxis=0)
transition_pot = cyutils.logNormalize(transition_pot)
curVal = messages.getMessage(trans_factor, var2).value
maxVal = np.maximum(maxVal, np.amax(np.abs(curVal - transition_pot), 1))
messages.updateMessage(trans_factor, var2, transition_pot)
# BACKWARD
if not model.no_transitions:
for t in range(sentLen-1, 0, -1):
var = var_list[t]
factor_list = graph.getFactorByVars(var)
# Variable2Factor Message
var2 = graph.getVarByTimestepnTag(t-1, tag.idx)
trans_factor = graph.getFactorByVars(var, var2)
message = np.zeros([batch_size, var.tag.size()], dtype=DTYPE)
for i, factor_mult in enumerate(factor_list):
if factor_mult!=trans_factor:
message += messages.getMessage(factor_mult, var).value
message = cyutils.logNormalize(message)
curVal = messages.getMessage(var, trans_factor).value
maxVal = np.maximum(maxVal, np.amax(np.abs(curVal - message), 1))
messages.updateMessage(var, trans_factor, message)
if test:
transition_pot = cyutils.logMax(cur_tag_weights, messages.getMessage(var, trans_factor).value, redAxis=1)
else:
transition_pot = cyutils.logDot(cur_tag_weights, messages.getMessage(var, trans_factor).value, redAxis=1)
transition_pot = cyutils.logNormalize(transition_pot)
curVal = messages.getMessage(trans_factor, var2).value
maxVal = np.maximum(maxVal, np.amax(np.abs(curVal - transition_pot), 1))
messages.updateMessage(trans_factor, var2, transition_pot)
iter += 1
print("Max Res Value: %f" % max(maxVal))
if max(maxVal) <= threshold:
print("Converged in %d iterations" %(iter))
break
if iter==1000:
print("Diverging :( Finished 1000 iterations.")
return None
# Calculate belief values and marginals
# Variable beliefs
for tag in model.uniqueTags:
for t in range(sentLen):
var = graph.getVarByTimestepnTag(t, tag.idx)
factor_list = graph.getFactorByVars(var)
for factor in factor_list:
factorMsg = Variable(torch.FloatTensor(messages.getMessage(factor, var).value))
if model.gpu:
factorMsg = factorMsg.cuda()
var.belief = var.belief + factorMsg
# Normalize
var.belief = utils.logNormalizeTensor(var.belief)
# Factor beliefs
for factor in graph.iterFactors():
var1, var2 = graph.getVarsByFactor(factor)
if factor.kind=="trans":
factor.belief = model.transition_weights[var1.tag.idx]
if model.model_type=="specific":
factor.belief = utils.logSumExpTensors(factor.belief, model.lang_transition_weights[var1.tag.idx][langIdx])
elif factor.kind=="pair":
pairwise_idx = model.pairs.index((var1.tag.idx, var2.tag.idx))
factor.belief = model.pairwise_weights[pairwise_idx]
if model.model_type=="specific":
factor.belief = utils.logSumExpTensors(factor.belief, model.lang_pairwise_weights[pairwise_idx][langIdx])
else:
continue
factor.belief = factor.belief.view(1, factor.belief.size(0), -1).expand(batch_size, -1, -1)
msg1 = torch.FloatTensor(messages.getMessage(var1, factor).value)
msg2 = torch.FloatTensor(messages.getMessage(var2, factor).value)
if model.gpu:
msg1 = msg1.cuda()
msg2 = msg2.cuda()
factor.belief = Variable(msg1.view(batch_size, -1, 1).expand(-1, -1, var2.tag.size())) + factor.belief
factor.belief = Variable(msg2.view(batch_size, 1, -1).expand(-1, var1.tag.size(), -1)) + factor.belief
factor.belief = utils.logNormalizeTensor(factor.belief)
# Calculate likelihood
# likelihood = model.calc_likelihood(graph, gold_tags)
# print("--- %s seconds ---" % (time.time() - start_time))
return graph, max(maxVal)