-
Notifications
You must be signed in to change notification settings - Fork 2
/
lstm_binary_clf.py
347 lines (309 loc) · 12.8 KB
/
lstm_binary_clf.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
from __future__ import unicode_literals
import dynet_config
#dynet_config.set(random_seed=127, autobatch=1)
import io
import re
import sys
import time
import random
import pickle
from argparse import ArgumentParser
from collections import Counter, defaultdict
from sklearn.metrics import classification_report as cr
import gensim
import dynet as dy
import numpy as np
import torch
from pytorch_transformers import BertModel, BertTokenizer
from torch.utils.data import DataLoader
class Meta:
def __init__(self):
self.c_dim = 32 # character-rnn input dimension
self.w_dim_e = 0 # pretrained word embedding size (0 if no pretrained embeddings)
self.n_hidden = 64 # pos-mlp hidden layer dimension
self.lstm_char_dim = 32 # char-LSTM output dimension
self.lstm_word_dim = 128 # LSTM (word-char concatenated input) output dimension
class Tagger():
def __init__(self, model=None, meta=None, wvm=None):
self.model = dy.Model()
self.meta = pickle.load(open('%s.meta' %model, 'rb')) if model else meta
self.trainer = self.meta.trainer(self.model)
# pretrained embeddings
if wvm:
self.wvm = wvm
self.meta.w_dim_e = wvm.syn0.shape[1]
# MLP on top of biLSTM outputs 100 -> 32 -> ntags
self.w1 = self.model.add_parameters((self.meta.n_hidden, self.meta.lstm_word_dim*2))
self.w2 = self.model.add_parameters((self.meta.n_tags, self.meta.n_hidden))
self.b1 = self.model.add_parameters(self.meta.n_hidden)
self.b2 = self.model.add_parameters(self.meta.n_tags)
self.aw = self.model.add_parameters((self.meta.lstm_word_dim, self.meta.lstm_word_dim*2))
self.ab = self.model.add_parameters(self.meta.lstm_word_dim)
self.av = self.model.add_parameters((1, self.meta.lstm_word_dim))
# word-level LSTMs
self.fwdRNN = dy.LSTMBuilder(1, self.meta.w_dim_e+self.meta.lstm_char_dim*0, self.meta.lstm_word_dim, self.model)
self.bwdRNN = dy.LSTMBuilder(1, self.meta.w_dim_e+self.meta.lstm_char_dim*0, self.meta.lstm_word_dim, self.model)
self.fwdRNN2 = dy.LSTMBuilder(1, self.meta.lstm_word_dim*2, self.meta.lstm_word_dim, self.model)
self.bwdRNN2 = dy.LSTMBuilder(1, self.meta.lstm_word_dim*2, self.meta.lstm_word_dim, self.model)
# char-level LSTMs
self.cfwdRNN = dy.LSTMBuilder(1, self.meta.c_dim, self.meta.lstm_char_dim, self.model)
self.cbwdRNN = dy.LSTMBuilder(1, self.meta.c_dim, self.meta.lstm_char_dim, self.model)
# unk for unknown word embeddings
self.unk = np.zeros(self.meta.w_dim_e)
# define char lookup table
self.CHAR_LOOKUP = self.model.add_lookup_parameters((self.meta.n_chars, self.meta.c_dim))
# load pretrained dynet model
if model:
self.model.populate('%s.dy' %model)
def word_rep(self, batch):
#char_embs = self.char_rep([w for wi in zip(*[b[0] for b in batch]) for w in wi])
batch_embs = [[] for _ in range(len(batch[0][0])+2)]
for sample, tag in batch:
for i,word in enumerate(['<'] + sample + ['>']):
if word not in self.wvm:
batch_embs[i].append(self.unk)
elif self.eval:
batch_embs[i].append(self.wvm[word])
#elif random.random() < 0.3:
# batch_embs[i].append(self.unk)
else:
batch_embs[i].append(self.wvm[word])
return [dy.inputTensor(emb) for emb in batch_embs]
def enable_dropout(self):
self.fwdRNN.set_dropout(0.3)
self.bwdRNN.set_dropout(0.3)
self.fwdRNN2.set_dropout(0.3)
self.bwdRNN2.set_dropout(0.3)
def disable_dropout(self):
self.fwdRNN.disable_dropout()
self.bwdRNN.disable_dropout()
self.fwdRNN2.disable_dropout()
self.bwdRNN2.disable_dropout()
def initialize_paramerets(self):
# apply dropout
if self.eval:
self.disable_dropout()
else:
self.enable_dropout()
# initialize the RNNs
self.f_init = self.fwdRNN.initial_state()
self.b_init = self.bwdRNN.initial_state()
self.f2_init = self.fwdRNN2.initial_state()
self.b2_init = self.bwdRNN2.initial_state()
def build_tagging_graph(self, batch):
self.initialize_paramerets()
# get the word vectors.
batch_embs = self.word_rep(batch)
# feed word vectors into biLSTM
fw_exps = self.f_init.transduce(batch_embs)
bw_exps = self.b_init.transduce(reversed(batch_embs))
# biLSTM states
bi_exps = [dy.concatenate([f,b]) for f,b in zip(fw_exps, reversed(bw_exps))]
# 2nd biLSTM
fw_exps = self.f2_init.transduce(bi_exps)
bw_exps = self.b2_init.transduce(reversed(bi_exps))
# biLSTM states
bi_exps = dy.concatenate([dy.concatenate([f,b]) for f,b in zip(fw_exps, reversed(bw_exps))], d=1)
aT = self.meta.activation(self.aw * bi_exps + self.ab)
alpha = self.av * aT
attn = dy.softmax(alpha, 1)
weighted_sum = dy.reshape(bi_exps * dy.transpose(attn), (self.meta.lstm_word_dim*2, ))
if not self.eval:
weighted_sum = dy.dropout(weighted_sum, 0.3)
xh = self.meta.activation(self.w1 * weighted_sum + self.b1)
if not self.eval:
xh = dy.dropout(xh, 0.3)
xo = self.w2 * xh + self.b2
return xo
def sent_loss(self, samples):
self.eval = False
vecs = self.build_tagging_graph(samples) # seq_len, ntags, batch_size
tids = [self.meta.t2i[tag] for sample,tag in samples]
self.loss.append(dy.pickneglogsoftmax_batch(vecs, tids))
def tag_sent(self, samples, attn={}, freq={}):
dy.renew_cg()
self.eval = True
dev_order = [s[2] for s in samples]
probs = self.build_tagging_graph([(s[0], s[1]) for s in samples])
probs = probs.npvalue()
btags = []
if probs.ndim == 1:
probs = probs[:, np.newaxis]
for i in range(len(probs[0])):
tag = probs[:, i][self.meta.t2i[1]]
btags.append((tag, dev_order[i]))
return btags
def make_batches(args, samples):
data = {}
bdata = []
for sent in samples:
bucket = len(sent[0])
data.setdefault(bucket, [])
data[bucket].append(sent)
data = data.values()
for bucket in data:
n = max(1, int(len(bucket) / args.batch_size))
bdata += [bucket[i::int(n)] for i in range(n)]
return bdata
def read_train_test_dev(args):
dev_X = []
test_X = []
train_X = []
dev_files = set(open(args.dev_files).read().split())
test_files = set(open(args.test_files).read().split())
dev_id, test_id = 0, 0
with io.open(args.data_file, encoding='utf-8') as fp:
for line in fp:
title, revision_group, src, tgt = line.strip().split('\t')
src = src.split()
tgt = tgt.split()
if len(src) > 150 or len(tgt) > 150:
continue
if title in dev_files:
dev_id += 1
dev_X.append((src, 0, dev_id))
dev_id += 1
dev_X.append((tgt, 1, dev_id))
elif title in test_files:
test_id += 1
test_X.append((src, 0, test_id))
test_id += 1
test_X.append((tgt, 1, test_id))
else:
train_X.append((src, 0))
train_X.append((tgt, 1))
return make_batches(args, train_X), make_batches(args, dev_X), make_batches(args, test_X)
def eval(tagger, dev, ofp=None):
tagged = []
good, bad = 0, 0
attn, freq = {}, {}
for batch in dev:
btags = tagger.tag_sent(batch, attn, freq)
tagged.extend(btags)
tagged = [t for t,o in sorted(tagged, key=lambda x:x[1])] # get back original dev set sentence order
for src_sc, tgt_sc in zip(tagged[::2], tagged[1::2]):
if src_sc < tgt_sc:
good += 1
else:
bad += 1
print(good/(good+bad))
sys.stdout.flush()
return good/(good+bad)
def train_tagger(args, tagger, train, dev):
pr_acc = 0.0
n_samples = len(train)
num_tagged, cum_loss = 0, 0
status_step = int(5000/args.batch_size) + 2
print(len(train))
print(len(dev))
sys.stdout.flush()
for ITER in range(args.iter):
random.shuffle(train)
tagger.eval = False
tagger.loss = []
for i,batch in enumerate(train, 1):
dy.renew_cg()
if i % status_step == 0 or i == n_samples: # print status
tagger.trainer.status()
print(cum_loss / num_tagged)
sys.stdout.flush()
cum_loss, num_tagged = 0, 0
tagger.sent_loss(batch)
num_tagged += args.batch_size #NOTE batch size
batch_loss = dy.sum_batches(dy.esum(tagger.loss))
cum_loss += batch_loss.scalar_value()
batch_loss.backward()
tagger.trainer.update()
tagger.loss = []
dy.renew_cg()
acc = eval(tagger, dev)
if acc > pr_acc:
pr_acc = acc
print('Save Point:: %d' %ITER)
if args.save_model:
tagger.model.save('%s.dy' %args.save_model)
sys.stdout.flush()
print("epoch %r finished" % ITER)
sys.stdout.flush()
def set_labels(args, data):
trainers = {
'simsgd' : dy.SimpleSGDTrainer,
'cysgd' : dy.CyclicalSGDTrainer,
'momsgd' : dy.MomentumSGDTrainer,
'adam' : dy.AdamTrainer,
'adagrad' : dy.AdagradTrainer,
'adadelta' : dy.AdadeltaTrainer,
'amsgrad' : dy.AmsgradTrainer
}
act_fn = {
'sigmoid' : dy.logistic,
'tanh' : dy.tanh,
'relu' : dy.rectify,
}
meta = Meta()
meta.trainer = trainers[args.trainer]
meta.activation = act_fn[args.act_fn]
tags = set()
meta.c2i = {'bos':0, 'eos':1, 'unk':2, 'pad':3}
cid = len(meta.c2i)
for batch in data:
for sent, tag in batch:
tags.add(tag)
for word in sent:
for c in word:
if c not in meta.c2i:
meta.c2i[c] = cid
cid += 1
meta.n_chars = len(meta.c2i)
meta.n_tags = len(tags)
meta.i2t = dict(enumerate(tags))
meta.t2i = {t:i for i,t in meta.i2t.items()}
return meta
def load_data(args):
wvm = None
train, dev, test = read_train_test_dev(args)
meta = set_labels(args, train)
if args.save_model:
pickle.dump(meta, open('%s.meta' %args.save_model, 'wb'))
return train, dev, meta
def main():
parser = ArgumentParser(description="LSTM Clf Model")
group = parser.add_mutually_exclusive_group()
parser.add_argument('--dynet-gpu')
parser.add_argument('--dynet-mem')
parser.add_argument('--dynet-devices')
parser.add_argument('--dynet-autobatch')
parser.add_argument('--dynet-seed', dest='seed', type=int, default='127')
parser.add_argument('--data_file', help='wikiHow data file')
parser.add_argument('--train_files')
parser.add_argument('--test_files')
parser.add_argument('--dev_files')
parser.add_argument('--batch_size', type=int, default=256, help='Batch size')
parser.add_argument('--pre_word_vec', help='Pretrained word2vec Embeddings')
parser.add_argument('--bin_vec', type=int, help='1 if binary embedding file else 0')
parser.add_argument('--elimit', type=int, default=None, help='load only top-n pretrained word vectors (default=all vectors)')
parser.add_argument('--trainer', default='amsgrad', help='Trainer [momsgd|adam|adadelta|adagrad|amsgrad]')
parser.add_argument('--activation', dest='act_fn', default='tanh', help='Activation function [tanh|rectify|logistic]')
parser.add_argument('--iter', type=int, default=25, help='No. of Epochs')
group.add_argument('--save-model', dest='save_model', help='Specify path to save model')
group.add_argument('--load-model', dest='load_model', help='Load Pretrained Model')
parser.add_argument('--output-file', dest='outfile', default='/tmp/out.txt', help='Output File')
args = parser.parse_args()
np.random.seed(args.seed)
random.seed(args.seed)
wvm = gensim.models.KeyedVectors.load_word2vec_format(args.pre_word_vec, binary=args.bin_vec, limit=args.elimit)
if args.load_model:
sys.stdout.write('Loading Models ...\n')
train, dev, test = read_train_test_dev(args)
tagger = Tagger(model=args.load_model, wvm=wvm)
eval(tagger, test)
sys.stdout.write('Done!\n')
else:
# load data
train, dev, meta = load_data(args)
meta.w_dim_e = wvm.syn0.shape[1]
# initialize parser
tagger = Tagger(meta=meta, wvm=wvm)
train_tagger(args, tagger, train, dev)
if __name__ == '__main__':
main()