forked from harvardnlp/urnng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeCRF.py
executable file
·204 lines (188 loc) · 7.81 KB
/
TreeCRF.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
#!/usr/bin/env python3
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import itertools
import utils
import random
class ConstituencyTreeCRF(nn.Module):
def __init__(self):
super(ConstituencyTreeCRF, self).__init__()
self.huge = 1e9
def logadd(self, x, y):
d = torch.max(x, y)
return torch.log(torch.exp(x - d) + torch.exp(y - d)) + d
def logsumexp(self, x, dim=1):
d = torch.max(x, dim)[0]
return torch.log(torch.exp(x - d.unsqueeze(dim).expand_as(x)).sum(dim)) + d
def _init_table(self, scores):
# initialize dynamic programming table
batch_size = scores.size(0)
n = scores.size(1)
self.alpha = [[scores.new(batch_size).fill_(-self.huge) for _ in range(n)] for _ in range(n)]
def _forward(self, scores):
# inside step
batch_size = scores.size(0)
n = scores.size(1)
self._init_table(scores)
for i in range(n):
self.alpha[i][i] = scores[:, i, i]
for k in np.arange(1, n + 1):
for s in range(n):
t = s + k
if t > n - 1:
break
tmp = [self.alpha[s][u] + self.alpha[u + 1][t] + scores[:, s, t] for u in np.arange(s, t)]
tmp = torch.stack(tmp, 1)
self.alpha[s][t] = self.logsumexp(tmp, 1)
def _backward(self, scores):
# outside step
batch_size = scores.size(0)
n = scores.size(1)
self.beta = [[None for _ in range(n)] for _ in range(n)]
self.beta[0][n - 1] = scores.new(batch_size).fill_(0)
for k in np.arange(n - 1, 0, -1):
for s in range(n):
t = s + k
if t > n - 1:
break
for u in np.arange(s, t):
if s < u + 1:
tmp = self.beta[s][t] + self.alpha[u + 1][t] + scores[:, s, t]
if self.beta[s][u] is None:
self.beta[s][u] = tmp
else:
self.beta[s][u] = self.logadd(self.beta[s][u], tmp)
if u + 1 < t + 1:
tmp = self.beta[s][t] + self.alpha[s][u] + scores[:, s, t]
if self.beta[u + 1][t] is None:
self.beta[u + 1][t] = tmp
else:
self.beta[u + 1][t] = self.logadd(self.beta[u + 1][t], tmp)
def _marginal(self, scores):
batch_size = scores.size(0)
n = scores.size(1)
self.log_marginal = [[None for _ in range(n)] for _ in range(n)]
log_Z = self.alpha[0][n - 1]
for s in range(n):
for t in np.arange(s, n):
self.log_marginal[s][t] = self.alpha[s][t] + self.beta[s][t] - log_Z
def _entropy(self, scores):
batch_size = scores.size(0)
n = scores.size(1)
self.entropy = [[None for _ in range(n)] for _ in range(n)]
for i in range(n):
self.entropy[i][i] = scores.new(batch_size).fill_(0)
for k in np.arange(1, n + 1):
for s in range(n):
t = s + k
if t > n - 1:
break
score = []
prev_ent = []
for u in np.arange(s, t):
score.append(self.alpha[s][u] + self.alpha[u + 1][t])
prev_ent.append(self.entropy[s][u] + self.entropy[u + 1][t])
score = torch.stack(score, 1)
prev_ent = torch.stack(prev_ent, 1)
log_prob = F.log_softmax(score, dim=1)
prob = log_prob.exp()
entropy = ((prev_ent - log_prob) * prob).sum(1)
self.entropy[s][t] = entropy
def _sample(self, scores, alpha=None, argmax=False):
# sample from p(tree | sent)
# also get the spans
if alpha is None:
self._forward(scores)
alpha = self.alpha
batch_size = scores.size(0)
n = scores.size(1)
tree = scores.new(batch_size, n, n).zero_()
all_log_probs = []
tree_brackets = []
spans = []
for b in range(batch_size):
sampled = [(0, n - 1)]
span = [(0, n - 1)]
queue = [(0, n - 1)] # start, end
log_probs = []
tree_str = get_span_str(0, n - 1)
while len(queue) > 0:
node = queue.pop(0)
start, end = node
left_parent = get_span_str(start, None)
right_parent = get_span_str(None, end)
score = []
score_idx = []
for u in np.arange(start, end):
score.append(alpha[start][u][b] + alpha[u + 1][end][b])
score_idx.append([(start, u), (u + 1, end)])
score = torch.stack(score, 0)
log_prob = F.log_softmax(score, dim=0)
if argmax:
sample = torch.max(log_prob, 0)[1]
else:
prob = log_prob.exp()
sample = torch.multinomial(log_prob.exp(), 1)
sample_idx = score_idx[sample.item()]
log_probs.append(log_prob[sample.item()])
for idx in sample_idx:
if idx[0] != idx[1]:
queue.append(idx)
span.append(idx)
sampled.append(idx)
left_child = '(' + get_span_str(sample_idx[0][0], sample_idx[0][1])
right_child = get_span_str(sample_idx[1][0], sample_idx[1][1]) + ')'
if sample_idx[0][0] != sample_idx[0][1]:
tree_str = tree_str.replace(left_parent, left_child)
if sample_idx[1][0] != sample_idx[1][1]:
tree_str = tree_str.replace(right_parent, right_child)
all_log_probs.append(torch.stack(log_probs, 0).sum(0))
tree_brackets.append(tree_str)
spans.append(span[::-1])
for idx in sampled:
tree[b][idx[0]][idx[1]] = 1
all_log_probs = torch.stack(all_log_probs, 0)
return tree, all_log_probs, tree_brackets, spans
def _viterbi(self, scores):
# cky algorithm
batch_size = scores.size(0)
n = scores.size(1)
self.max_scores = scores.new(batch_size, n, n).fill_(-self.huge)
self.bp = scores.new(batch_size, n, n).zero_()
self.argmax = scores.new(batch_size, n, n).zero_()
self.spans = [[] for _ in range(batch_size)]
tmp = scores.new(batch_size, n).zero_()
for i in range(n):
self.max_scores[:, i, i] = scores[:, i, i]
for k in np.arange(1, n):
for s in np.arange(n):
t = s + k
if t > n - 1:
break
for u in np.arange(s, t):
tmp = self.max_scores[:, s, u] + self.max_scores[:, u + 1, t] + scores[:, s, t]
self.bp[:, s, t][self.max_scores[:, s, t] < tmp] = int(u)
self.max_scores[:, s, t] = torch.max(self.max_scores[:, s, t], tmp)
for b in range(batch_size):
self._backtrack(b, 0, n - 1)
return self.max_scores[:, 0, n - 1], self.argmax, self.spans
def _backtrack(self, b, s, t):
u = int(self.bp[b][s][t])
self.argmax[b][s][t] = 1
if s == t:
return None
else:
self.spans[b].insert(0, (s, t))
self._backtrack(b, s, u)
self._backtrack(b, u + 1, t)
return None
def get_span_str(start=None, end=None):
assert (start is not None or end is not None)
if start is None:
return ' ' + str(end) + ')'
elif end is None:
return '(' + str(start) + ' '
else:
return ' (' + str(start) + ' ' + str(end) + ') '