-
Notifications
You must be signed in to change notification settings - Fork 0
/
P3.py
275 lines (235 loc) · 10.8 KB
/
P3.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
from P1 import TrainProbabilities, START_TOK, STOP_TOK
from P2 import CRF
import numpy as np
from collections import defaultdict
import copy
class CRF(CRF):
def _forward(self, w, sentence):
possible_y = self.train_probabilities.y_count.keys()
# base case
alpha_list = [{START_TOK: 0}]
# START to all y
last_y = START_TOK
alpha_score = {}
for next_y in possible_y:
transition_key = "transition:%s+%s"%(last_y,next_y)
# force constraint to never transition to start token
# if next_y == START_TOK:
# w[transition_key] = -10000.
# try:
alpha_score[next_y] = w[transition_key]
# except KeyError:
# alpha_score[next_y] = 0
alpha_list.append(alpha_score)
# all y to all y
for index, x in enumerate(sentence[:-1]):
alpha_score = {}
for next_y in possible_y:
alpha_score[next_y] = 0
scores = []
for last_y in possible_y:
transition_key = "transition:%s+%s"%(last_y,next_y)
emission_key = "emission:%s+%s"%(last_y, x)
# if next_y == START_TOK or last_y == STOP_TOK:
# w[transition_key] = -10000.
# try:
score = w[transition_key] + alpha_list[index+1][last_y] + w[emission_key]
scores.append(score)
# except KeyError:
# scores.append(-10000.)
alpha_score[next_y] = log_sum_exp(np.array(scores))
alpha_list.append(alpha_score)
# all y to STOP
next_y = STOP_TOK
x = sentence[-1]
alpha_score = {}
alpha_score[next_y] = 0
scores = []
for last_y in possible_y:
transition_key = "transition:%s+%s"%(last_y,next_y)
emission_key = "emission:%s+%s"%(last_y, x)
# if last_y == STOP_TOK:
# W[transition_key] = -10000.
# try:
score = w[transition_key] + alpha_list[-1][last_y] + w[emission_key]
scores.append(score)
# except KeyError:
# scores.append(-10000.)
alpha_score[next_y] = log_sum_exp(np.array(scores))
alpha_list.append(alpha_score)
return alpha_list
def calculate_loss(self, w, path):
loss = 0
running_x = []
running_y = []
with open(path,mode='r',encoding="utf-8") as file:
for line in file:
# End of a sequence
if line=='\n':
forward_score = self._forward(w, running_x)
sentence = " ".join(running_x)
tag = " ".join(running_y)
# log the score stored at the last element of forward score
loss += forward_score[-1][STOP_TOK] - self._score(sentence, tag, w=w)
running_x = []
running_y = []
else:
x,y = line.split()
running_x.append(x)
running_y.append(y)
return loss
def _backward(self, w, sentence):
possible_y = self.train_probabilities.y_count.keys()
# base case
beta_list = [{STOP_TOK: 0}]
# all y to STOP
next_y = STOP_TOK
beta_score = {}
x = sentence[-1]
for last_y in possible_y:
emission_key = "emission:%s+%s"%(last_y, x)
transition_key = "transition:%s+%s"%(last_y,next_y)
# if last_y == STOP_TOK:
# w[transition_key] = -10000.
# try:
beta_score[last_y] = w[transition_key] + w[emission_key]
# except KeyError:
# beta_score[last_y] = 0
beta_list.append(beta_score)
# all y to all y
for index, x in enumerate(sentence[:-1][::-1]):
beta_score = {}
for last_y in possible_y:
beta_score[last_y] = 0
scores = []
for next_y in possible_y:
transition_key = "transition:%s+%s"%(last_y,next_y)
emission_key = "emission:%s+%s"%(last_y, x)
# if last_y == STOP_TOK or next_y == START_TOK:
# w[transition_key] = -10000.
# try:
score = w[transition_key] + beta_list[index+1][next_y] + w[emission_key]
scores.append(score)
# except KeyError:
# scores.append(-10000)
beta_score[last_y] = log_sum_exp(np.array(scores))
beta_list.append(beta_score)
# START to all y
last_y = START_TOK
beta_score = {}
sum_prob = []
for next_y in possible_y:
transition_key = "transition:%s+%s"%(last_y,next_y)
# if next_y == START_TOK:
# w[transition_key] = -10000.
# try:
sum_prob.append(w[transition_key] + beta_list[-1][next_y])
# except KeyError:
# sum_prob.append(-10000)
beta_score[last_y] = log_sum_exp(np.array(sum_prob))
beta_list.append(beta_score)
return beta_list[::-1]
def calculate_gradient(self, w, path):
w_score = defaultdict(float)
y_x_count = defaultdict(int)
y0_y1_count = defaultdict(int)
last_y = START_TOK
running_x = []
running_y = []
with open(path,mode='r',encoding="utf-8") as file:
for line in file:
# End of a sequence
if line=='\n':
# calculate forward backward scores and denom
y0_y1_count[(last_y,STOP_TOK)] += 1
forward_score = self._forward(w, running_x)
backward_score = self._backward(w, running_x)
# print(forward_score)
# print(backward_score)
denom = np.exp(forward_score[-1][STOP_TOK])
# iterate through the y,x sequences in the sentence
for (y,x), counts in y_x_count.items():
emission_key = "emission:%s+%s"%(y, x)
'''
expected_counts = 0
# omit y as START and STOP
for index in range(1,len(forward_score)-1):
# include all possible transitions
for next_y in forward_score[index+1].keys():
try:
transition_key = "transition:%s+%s"%(y,next_y)
expected_counts += forward_score[index][y] * backward_score[index+1][next_y] * np.exp(w[emission_key]) * np.exp(w[transition_key])
except:
pass
w_score[emission_key] += expected_counts/denom - counts
'''
expected_counts = 0
# omit y as START and STOP
for index in range(len(running_x)):
# include all possible transitions
if x == running_x[index]:
try:
expected_counts += np.exp(forward_score[index+1][y] + backward_score[index+1][y] - denom)
except KeyError as e:
pass
w_score[emission_key] += expected_counts/denom - counts
# iterate through the y0,y1 sequences in the sentence
for (y0,y1), counts in y0_y1_count.items():
transition_key = "transition:%s+%s"%(y0,y1)
expected_counts = 0
# omit y_n as STOP
for index in range(0,len(forward_score)-1):
# START doesnt have emission
if index == 0:
try:
expected_counts += np.exp(forward_score[index][y0] + backward_score[index+1][y1] + w[transition_key] - denom)
except KeyError:
pass
# include y0 emission
else:
x = running_x[index-1]
emission_key = "emission:%s+%s"%(y0, x)
try:
expected_counts += np.exp(forward_score[index][y0] + backward_score[index+1][y1] + w[emission_key] + w[transition_key] - denom)
except KeyError as e:
pass
w_score[transition_key] += expected_counts/denom - counts
# reset
y_x_count = defaultdict(int)
y0_y1_count = defaultdict(int)
last_y = START_TOK
running_x = []
running_y = []
else:
x,y = line.split()
y_x_count[(y,x)] += 1
y0_y1_count[(last_y,y)] += 1
last_y = y
running_x.append(x)
running_y.append(y)
return w_score
def test_gradient(self, w, path, key, value =0.01):
gradient = crf.calculate_gradient(w, path)[key]
w_test = copy.deepcopy(w)
w_test[key] += value
v2 = crf.calculate_loss(w_test, path)
v1 = crf.calculate_loss(w, path)
print(v2, v1)
diff = v2 - v1
diff /= value
return gradient, diff
def log_sum_exp(vec):
max_score = np.max(vec)
return max_score + np.log(np.sum(np.exp(vec - max_score)))
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print ("Please provide train filename")
else:
crf = CRF()
print ("Loss: ")
print (crf.calculate_loss( crf.train_probabilities.f, sys.argv[1]))
print ("Gradient: ")
print (crf.calculate_gradient( crf.train_probabilities.f, sys.argv[1]))
print ("Test whether gradient agrees")
print (crf.test_gradient(crf.train_probabilities.f, "data/EN/train", 'emission:O+and',value=-.1))