-
Notifications
You must be signed in to change notification settings - Fork 0
/
lm.py
354 lines (300 loc) · 12.8 KB
/
lm.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
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
import nltk, math, numpy, collections, pdb, sys, pickle
import matplotlib.pyplot as plt
class Model():
def __init__(self, tmat, emat, vocab, num_classes):
self.tmat = tmat
self.emat = emat
self.vocab = vocab
self.num_classes = num_classes
def q1(freq_1gram, freq_2gram):
q1_wc = {'colorless':0, 'green':0, 'ideas':0, 'sleep':0, 'furiously':0, '$$':0}
q1_biwc = {('colorless', 'green'):0, ('green','ideas'):0, ('ideas','sleep'):0, ('sleep','furiously'):0, ('furiously','$$'):0}
uni_mle = 0
for key in q1_wc:
q1_wc[key] = freq_1gram[key]
uni_mle += math.log(freq_1gram[key]/freq_1gram.N())
print("Word count for- colorless green ideas sleep furiously", q1_wc)
for key in q1_biwc:
q1_biwc[key] = freq_2gram[key]
return round(uni_mle,3)
def get_testcases():
test_cases = []
with open(TEST_CASES_FILE) as f:
for line in f:
arr = line.split("||")
if len(arr) == 1:
test_cases.append(arr[0].strip().lower())
elif len(arr) == 2:
test_cases.append((arr[0].strip().lower(), arr[1].strip().lower()))
return test_cases
def run_tests(tests, tmat, emat, vocab, num_classes):
input("\nHit 'enter' to continue...(test sample sentences using trained model)")
c = 0
for test in tests:
if isinstance(test, tuple):
sent, sent_rev = test[0], test[1]
else:
sent, sent_rev = test, " ".join(reversed(test.split()))
_ll = getsentence_ll(sent, tmat, emat, vocab, num_classes)
_ll_rev = getsentence_ll(sent_rev, tmat, emat, vocab, num_classes)
print('test #' + repr(c) + ':log p(' + sent + '):'+ repr(round(_ll, 3)) + ' -vs- log p(' + sent_rev + '):' + repr(round(_ll_rev,3)))
print('Probability ratio:',round(math.exp(_ll - _ll_rev), 3), '\n')
c += 1
def plot_stats(toklls, win_pr):
plt.figure()
for tokll, col in toklls:
plt.plot(list(range(1,1+EM_ITERATIONS)), -1*tokll, col)
plt.ylabel('-tokLL')
plt.xlabel('EM iterations')
plt.figure()
axes = plt.gca()
axes.set_xlim([0,1])
plt.xlabel('winning probability assigments')
plt.ylabel('words/count')
plt.bar(list(win_pr.keys()), list(win_pr.values()), width=0.01)
plt.show()
#for latent class 'c', find words transitioning to 'c' with highest probability, basically record c* = argmax_c P(c|w)
#now rank words in each class by their probability p(c*|w) and filter so they are amongst the 300 most common words, now choose top10
def get_stats(freq_1gram, tmat, vocab, num_classes):
top_wrds = dict(freq_1gram.most_common(302)).keys()
inv_vocab = list(vocab.keys())
#for each wrd find most probable class
wrd_max_c = numpy.argmax(tmat, 0) #max class index
wrd_max_pr = numpy.amax(tmat, 0) #its probability
#pdb.set_trace()
win_pr = {}
for c in range(num_classes):
ind = numpy.where(wrd_max_c == c)
if ind[0].size < 1:
print('No words assigned to class: ',c)
continue
wrds_in_c = numpy.array([inv_vocab[x] for x in ind[0].tolist()]) #get all words transition to 'c'
wrds_pr = wrd_max_pr[ind[0]] #their probability
srt_ind = numpy.argsort(wrds_pr)
srt_ind = srt_ind[::-1]
srt_wrds_c = wrds_in_c[srt_ind].tolist()
srt_wrd_pr = wrds_pr[srt_ind].tolist()
topk = {}
for x in range(len(srt_wrds_c)):
w, pr = srt_wrds_c[x], srt_wrd_pr[x]
if w in top_wrds and w != '$$' and w != '@@':
topk[w] = pr
round_pr = round(pr, 2)
if round_pr not in win_pr:
win_pr[round_pr] = 0
win_pr[round_pr] += 1
#limit to top 20 words per class
if len(topk) >= 20:
break
print('class',c,':',list(topk.keys()),'\n')
return win_pr
def initialize(num_vocab, K):
#random initialization
emat = numpy.random.rand(num_vocab, K) #V x C matrix
tmat = numpy.random.rand(K, num_vocab) #C x V matrix
#emat = emat*0.9 + 0.1
#tmat = tmat*0.9 + 0.1
#normalize
er, ec = numpy.shape(emat)
tr, tc = numpy.shape(tmat)
for col in range(tc):
su = sum(tmat[:,col])
tmat[:,col] /= su
for col in range(ec):
su = sum(emat[:,col])
emat[:,col] /= su
checkemat = numpy.around(sum(emat), 10) == numpy.ones((K, ))
assert numpy.all(checkemat), 'parameters of emission matrix(V x C) no well-formed'
checktmat = numpy.around(sum(tmat), 10) == numpy.ones(( num_vocab, ))
assert numpy.all(checktmat), 'parameters of transition matrix(C x V) not well-formed'
#p(c|EOS) = na, p(BOS|c) = 0
tmat[:, 1] = numpy.nan
emat[0, :] = 0
return tmat, emat
def loglikelihood(freq_2gram, tmat, emat, vocab, num_classes):
ll = 0
for bg, count in freq_2gram.items():
w1, w2 = bg
pr = 0
for c in range(num_classes):
pr += emat[vocab[w2], c] * tmat[c, vocab[w1]]
if pr > 0:
ll += math.log(pr)*count
return ll
def getsentence_ll(sent, tmat, emat, vocab, num_classes):
toks =['@@']
toks.extend(sent.split(" "))
toks.append('$$')
freq_2gram = nltk.FreqDist(list(nltk.bigrams(toks)))
return loglikelihood(freq_2gram, tmat, emat, vocab, num_classes)
def estep(freq_2gram, tmat, emat, posterior, K, vocab):
#accross all 'valid bigrams' compute p(c|w_1,w_2)
ctr = 0
for bg, count in freq_2gram.items():
w1, w2 = bg
posterior_vals = numpy.zeros(K)
w1_i, w2_i = vocab[w1], vocab[w2]
normalizer = 0
#for each possible class
for c in range(K):
posterior_vals[c] = emat[w2_i, c]*tmat[c, w1_i]
normalizer += posterior_vals[c]
#normalize by dividing with sum across all classes
posterior[bg] = posterior_vals/normalizer
assert round(sum(posterior[bg]), 7) == 1, 'posterior not well formed'
ctr += 1
if ctr % 10000 == 0:
print('.',end='')
sys.stdout.flush()
return posterior
def mstep(freq_1gram, cfreq_2gram, cfreq_2gram_w2, tmat, emat, posterior, K, vocab):
num_vocab = len(vocab)
#compute P(c|w_1) update tmat (transition matrix)
for w1, w1_i in vocab.items():
#p(c|END_TOK) = NaN *no transition from end token to any class*
if w1 == '$$':
tmat[:,1] = numpy.nan
continue
if w1_i % 10000 == 0:
print('.',end='')
sys.stdout.flush()
normalizer = 0
for c in range(K):
expected_bg_count = 0
for w2, bg_count in cfreq_2gram[w1].items():
bg = (w1,w2) #w_1=END eliminated at top itself
expected_bg_count += bg_count * posterior[bg][c]
tmat[c, w1_i] = expected_bg_count
normalizer += expected_bg_count
tmat[:,w1_i] /= normalizer
assert math.fabs((freq_1gram[w1] - normalizer)) < 1, 'expected count way too off from word count'
sum_tmat = numpy.around(sum(tmat), 7)
sum_tmat_nonan = numpy.append(sum_tmat[:1], sum_tmat[2:])
checktmat = sum_tmat_nonan == numpy.ones(( num_vocab - 1, ))
assert numpy.all(checktmat), 'parameters of transition matrix(C x V) not well-formed'
#compute P(w_2|c) update emat (emission matrix)
w2_expc = {}
for c in range(K):
normalizer = 0
for w2, w2_i in vocab.items():
#there are no bigrams with w_2 as second word
if w2 not in cfreq_2gram_w2:
emat[w2_i, c] = 0
continue
#no class can generate the START_TOK, assume it always to be there
if w2 == '@@':
emat[0, c] = 0
continue
if w2_i % 10000 == 0:
print('.',end='')
sys.stdout.flush()
expected_bg_count = 0
for w1 in cfreq_2gram_w2[w2]:
bg = (w1, w2)
bg_count = cfreq_2gram_w2[w2][w1]
posterior_pr = posterior.get(bg, numpy.zeros(K))
expected_bg_count += bg_count * posterior_pr[c]
emat[w2_i, c] = expected_bg_count
normalizer += expected_bg_count
w2_expc[w2_i] = w2_expc.get(w2_i, 0) + expected_bg_count
emat[:, c] /= normalizer
sum_emat = numpy.around(sum(emat), 7)
checkemat = sum_emat == numpy.ones((K, ))
assert numpy.all(checkemat), 'parameters of emission matrix(V x C) no well-formed'
for w2 in cfreq_2gram_w2:
w2_sum = 0
for w in cfreq_2gram_w2[w2]:
w2_sum += cfreq_2gram_w2[w2][w]
assert math.fabs((w2_expc[vocab[w2]] - w2_sum)) < 1, 'expected count way too off from word count'
return tmat, emat
def run_em(freq_2gram, cfreq_2gram, cfreq_2gram_w2, freq_1gram, vocab, num_classes, emitr):
#EM algorithm implementation
posterior = {}
toklls = numpy.zeros((emitr, ))
tmat, emat = initialize(len(vocab), num_classes)
for it in range(emitr):
print('Iteration:', it + 1,end='')
posterior = estep(freq_2gram, tmat, emat, posterior, num_classes, vocab)
tmat, emat = mstep(freq_1gram, cfreq_2gram, cfreq_2gram_w2, tmat, emat, posterior, num_classes, vocab)
logll = loglikelihood(freq_2gram, tmat, emat, vocab, num_classes)
toklls[it] = logll/freq_1gram.N()
print(' marginal tokLL:',round(toklls[it],3))
print('')
return tmat, emat, toklls
def conditional_count_bigrams(list_bgs):
cond_count_w2 = {}
for bg in list_bgs:
w1, w2 = bg
if w2 not in cond_count_w2:
cond_count_w2[w2] = {}
if w1 not in cond_count_w2[w2]:
cond_count_w2[w2][w1] = 0
cond_count_w2[w2][w1] += 1
return cond_count_w2
def check_params(freq_1gram, freq_2gram, cfreq_2gram, cfreq_2gram_w2):
for bg, count in freq_2gram.items():
w1,w2 = bg
if w1 == '$$':
print('invalid bigram', bg)
elif w2 == '@@':
print('invalid bigram', bg)
elif count < 1:
print('invalid bigram', bg)
assert cfreq_2gram['$$'].N() == 0, 'there shld be no bigrams w1=END_TOK'
assert len(cfreq_2gram_w2.get('@@', {})) == 0, 'there shld be no bigrams w2=START_TOK'
def test():
with open(MDL_PARAMS_FILE, 'rb') as input:
mdl = pickle.load(input)
tests = get_testcases()
run_tests(tests, mdl.tmat, mdl.emat, mdl.vocab, mdl.num_classes)
print("that's all folks!")
def train(num_classes, emitr):
num_sents, pos, END_TOK, START_TOK = 0, 0, '$$', '@@'
bigrams_list, toks, vocab = [], [], collections.OrderedDict()
vocab[START_TOK] = pos
pos += 1
vocab[END_TOK] = pos
pos += 1
print('Reading corpus')
for fileid in nltk.corpus.brown.fileids():
for sent in nltk.corpus.brown.sents(fileid):
num_sents += 1
if num_sents % 1000 == 0:
print('.', end='')
sys.stdout.flush()
sent_tok = [START_TOK]
for wrd in sent:
n_wrd = wrd.strip().lower()
sent_tok.append(n_wrd)
if n_wrd not in vocab:
vocab[n_wrd] = pos
pos += 1
sent_tok.append(END_TOK)
toks.extend(sent_tok)
bigrams_list.extend(list(nltk.bigrams(sent_tok)))
freq_1gram = nltk.FreqDist(toks)
freq_2gram = nltk.FreqDist(bigrams_list)
cfreq_2gram = nltk.ConditionalFreqDist(bigrams_list)
cfreq_2gram_w2 = conditional_count_bigrams(bigrams_list)
check_params(freq_1gram, freq_2gram, cfreq_2gram, cfreq_2gram_w2)
print('\n---Corpus Info---')
print('Number of sentences:',num_sents)
print('Number of tokens(without END/START):',len(toks) - num_sents*2)
print('Vocabulary size:',len(vocab))
input("\nHit 'enter' to continue...(prints Q1)")
print('Q1.1 log likelihood for sentence assuming unigram:', q1(freq_1gram, freq_2gram),'\n')
input("Hit 'enter' to continue...(starts training EM)")
tmat_1, emat_1, tokll_1 = run_em(freq_2gram, cfreq_2gram, cfreq_2gram_w2, freq_1gram, vocab, num_classes, emitr)
input("Hit 'enter' to continue...(prints stats and plots)")
win_pr = get_stats(freq_1gram,tmat_1,vocab,num_classes)
print("close both the plots to continue")
plot_stats([(tokll_1,'g')], win_pr)
with open(MDL_PARAMS_FILE, 'wb') as output:
mdl = Model(tmat_1, emat_1, vocab, num_classes)
pickle.dump(mdl, output, pickle.HIGHEST_PROTOCOL)
LATENT_CLASSES, EM_ITERATIONS = 3, 20
TEST_CASES_FILE = 'test_sentences.txt'
MDL_PARAMS_FILE = 'model_params.pkl'
print('\n HW1 by Harshal Godhia \n')
#train(LATENT_CLASSES, EM_ITERATIONS)
test()