/
MLT_CNN_no_validation.py
497 lines (409 loc) · 16.7 KB
/
MLT_CNN_no_validation.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Convolution Neural Network for Event Encoding
"""
__author__ = "Wei Wang"
__email__ = "tskatom@vt.edu"
import sys
import os
import theano
from theano import function, shared
import theano.tensor as T
import numpy as np
import cPickle
import json
import argparse
import nn_layers as nn
import logging
import timeit
from collections import OrderedDict
#theano.config.profile = True
#theano.config.profile_memory = True
#theano.config.optimizer = 'fast_run'
def ReLU(x):
return T.maximum(0.0, x)
def as_floatX(variable):
if isinstance(variable, float):
return np.cast[theano.config.floatX](variable)
if isinstance(variable, np.ndarray):
return np.cast[theano.config.floatX](variable)
return theano.tensor.cast(variable, theano.config.floatX)
def parse_args():
ap = argparse.ArgumentParser()
ap.add_argument('--prefix', type=str,
help="the prefix for input data such as spanish_protest")
ap.add_argument('--word2vec', type=str,
help="word vector pickle file")
ap.add_argument('--sufix_pop', type=str,
help="the sufix for the target file")
ap.add_argument('--sufix_type', type=str,
help="the sufix for the target file")
ap.add_argument('--dict_pop_fn', type=str,
help='population class dictionary')
ap.add_argument('--dict_type_fn', type=str,
help='event type class dictionary')
ap.add_argument('--max_len', type=int,
help='the max length for doc used for mini-batch')
ap.add_argument("--padding", type=int,
help="the number of padding used to add begin and end doc")
ap.add_argument("--exp_name", type=str,
help="experiment name")
ap.add_argument("--static", action="store_true",
help="whether update word2vec")
ap.add_argument("--max_iter", type=int,
help="max iterations")
ap.add_argument("--batch_size", type=int)
ap.add_argument("--log_fn", type=str,
help="log filename")
ap.add_argument("--perf_fn", type=str,
help="folder to store predictions")
ap.add_argument("--param_fn", type=str,
help="sepcific local params")
return ap.parse_args()
def load_dataset(prefix, sufix_1, sufix_2):
"""Load the train/valid/test set
prefix eg: ../data/spanish_protest
sufix eg: pop_cat
"""
dataset = []
for group in ["train", "valid", "test"]:
x_fn = "%s_%s.txt.tok" % (prefix, group)
y1_fn = "%s_%s.%s" % (prefix, group, sufix_1)
y2_fn = "%s_%s.%s" % (prefix, group, sufix_2)
xs = [l.strip() for l in open(x_fn)]
y1s = [l.strip() for l in open(y1_fn)]
y2s = [l.strip() for l in open(y2_fn)]
dataset.append((xs, y1s, y2s))
print "Load %d %s records" % (len(y1s), group)
return dataset
def doc_to_id(doc, word2id, max_len=2000, padding=5):
pad = padding - 1
tokens = doc.split(" ")
doc_ids = [0] * pad
for w in tokens[:max_len]:
doc_ids.append(word2id.get(w, 1))
num_suff = max([0, max_len - len(tokens)]) + pad
doc_ids += [0] * num_suff
return doc_ids
def transform_dataset(dataset, word2id, class2id, max_len=2000, padding=5):
"""Transform the dataset into digits"""
train_set, valid_set, test_set = dataset
train_doc, train_pop_class, train_type_class = train_set
valid_doc, valid_pop_class, valid_type_class = valid_set
test_doc, test_pop_class, test_type_class = test_set
train_doc_ids = [doc_to_id(doc, word2id, max_len, padding) for doc in train_doc]
valid_doc_ids = [doc_to_id(doc, word2id, max_len, padding) for doc in valid_doc]
test_doc_ids = [doc_to_id(doc, word2id, max_len, padding) for doc in test_doc]
train_pop_y = [class2id["pop"][c] for c in train_pop_class]
valid_pop_y = [class2id["pop"][c] for c in valid_pop_class]
test_pop_y = [class2id["pop"][c] for c in test_pop_class]
train_type_y = [class2id["type"][c] for c in train_type_class]
valid_type_y = [class2id["type"][c] for c in valid_type_class]
test_type_y = [class2id["type"][c] for c in test_type_class]
return [(train_doc_ids, train_pop_y, train_type_y), (valid_doc_ids, valid_pop_y, valid_type_y), (test_doc_ids, test_pop_y, test_type_y)]
def sgd_updates_adadelta(params, cost, rho=0.95, epsilon=1e-6,
norm_lim=9, word_vec_name='embedding'):
updates = OrderedDict({})
exp_sqr_grads = OrderedDict({})
exp_sqr_ups = OrderedDict({})
gparams = []
for param in params:
empty = np.zeros_like(param.get_value())
exp_sqr_grads[param] = theano.shared(value=as_floatX(empty),name="exp_grad_%s" % param.name)
gp = T.grad(cost, param)
exp_sqr_ups[param] = theano.shared(value=as_floatX(empty), name="exp_grad_%s" % param.name)
gparams.append(gp)
for param, gp in zip(params, gparams):
exp_sg = exp_sqr_grads[param]
exp_su = exp_sqr_ups[param]
up_exp_sg = rho * exp_sg + (1 - rho) * T.sqr(gp)
updates[exp_sg] = up_exp_sg
step = -(T.sqrt(exp_su + epsilon) / T.sqrt(up_exp_sg + epsilon)) * gp
updates[exp_su] = rho * exp_su + (1 - rho) * T.sqr(step)
stepped_param = param + step
if (param.get_value(borrow=True).ndim == 2) and (param.name!='embedding'):
col_norms = T.sqrt(T.sum(T.sqr(stepped_param), axis=0))
desired_norms = T.clip(col_norms, 0, T.sqrt(norm_lim))
scale = desired_norms / (1e-7 + col_norms)
updates[param] = stepped_param * scale
else:
updates[param] = stepped_param
return updates
def run_cnn(exp_name,
dataset, embedding,
log_fn, perf_fn,
emb_dm=100,
batch_size=100,
filter_hs=[1, 2, 3],
hidden_units=[200, 100, 11],
dropout_rate=0.5,
shuffle_batch=True,
n_epochs=300,
lr_decay=0.95,
activation=ReLU,
sqr_norm_lim=9,
non_static=True):
"""
Train and Evaluate CNN event encoder model
:dataset: list containing three elements[(train_x, train_y),
(valid_x, valid_y), (test_x, test_y)]
:embedding: word embedding with shape (|V| * emb_dm)
:filter_hs: filter height for each paralle cnn layer
:dropout_rate: dropout rate for full connected layers
:n_epochs: the max number of iterations
"""
start_time = timeit.default_timer()
rng = np.random.RandomState(1234)
input_height = len(dataset[0][0][0])
print "--input height ", input_height
input_width = emb_dm
num_maps = hidden_units[0]
###################
# start snippet 1 #
###################
print "start to construct the model ...."
x = T.matrix("x")
y = T.ivector("y")
words = shared(value=np.asarray(embedding,
dtype=theano.config.floatX),
name="embedding", borrow=True)
# define function to keep padding vector as zero
zero_vector_tensor = T.vector()
zero_vec = np.zeros(input_width, dtype=theano.config.floatX)
set_zero = function([zero_vector_tensor],
updates=[(words, T.set_subtensor(words[0,:], zero_vector_tensor))])
layer0_input = words[T.cast(x.flatten(), dtype="int32")].reshape((
x.shape[0], 1, x.shape[1], emb_dm
))
conv_layers = []
layer1_inputs = []
for i in xrange(len(filter_hs)):
filter_shape = (num_maps, 1, filter_hs[i], emb_dm)
pool_size = (input_height - filter_hs[i] + 1, 1)
conv_layer = nn.ConvPoolLayer(rng, input=layer0_input,
input_shape=None,
filter_shape=filter_shape,
pool_size=pool_size, activation=activation)
layer1_input = conv_layer.output.flatten(2)
conv_layers.append(conv_layer)
layer1_inputs.append(layer1_input)
layer1_input = T.concatenate(layer1_inputs, 1)
##############
# classifier pop#
##############
print "Construct classifier ...."
hidden_units[0] = num_maps * len(filter_hs)
model = nn.MLPDropout(rng,
input=layer1_input,
layer_sizes=hidden_units,
dropout_rates=[dropout_rate],
activations=[activation])
params = model.params
for conv_layer in conv_layers:
params += conv_layer.params
if non_static:
params.append(words)
cost = model.negative_log_likelihood(y)
dropout_cost = model.dropout_negative_log_likelihood(y)
grad_updates = sgd_updates_adadelta(params,
dropout_cost,
lr_decay,
1e-6,
sqr_norm_lim)
#######################
# classifier Type #####
#######################
type_hidden_units = [num for num in hidden_units]
type_hidden_units[-1] = 6
type_model = nn.MLPDropout(rng,
input=layer1_input,
layer_sizes=type_hidden_units,
dropout_rates=[dropout_rate],
activations=[activation])
type_params = type_model.params
for conv_layer in conv_layers:
type_params += conv_layer.params
if non_static:
type_params.append(words)
type_cost = type_model.negative_log_likelihood(y)
type_dropout_cost = type_model.dropout_negative_log_likelihood(y)
type_grad_updates = sgd_updates_adadelta(type_params,
type_dropout_cost,
lr_decay,
1e-6,
sqr_norm_lim)
#####################
# Construct Dataset #
#####################
print "Copy data to GPU and constrct train/valid/test func"
np.random.seed(1234)
train_x, train_pop_y, train_type_y = shared_dataset(dataset[0])
valid_x, valid_pop_y, valid_type_y = shared_dataset(dataset[1])
test_x, test_pop_y, test_type_y = shared_dataset(dataset[2])
n_train_batches = int(np.ceil(1.0 * len(dataset[0][0]) / batch_size))
n_valid_batches = int(np.ceil(1.0 * len(dataset[1][0]) / batch_size))
n_test_batches = int(np.ceil(1.0 * len(dataset[2][0]) / batch_size))
#####################
# Train model func #
#####################
index = T.iscalar()
train_func = function([index], cost, updates=grad_updates,
givens={
x: train_x[index*batch_size:(index+1)*batch_size],
y: train_pop_y[index*batch_size:(index+1)*batch_size]
})
valid_train_func = function([index], cost, updates=grad_updates,
givens={
x: valid_x[index*batch_size:(index+1)*batch_size],
y: valid_pop_y[index*batch_size:(index+1)*batch_size]
})
train_pred = function([index], model.preds,
givens={
x: train_x[index*batch_size:(index+1)*batch_size]
})
valid_pred = function([index], model.preds,
givens={
x: valid_x[index*batch_size:(index+1)*batch_size],
})
test_pred = function([index], model.preds,
givens={
x:test_x[index*batch_size:(index+1)*batch_size],
})
#########################
# train model type func #
#########################
train_type_func = function([index], type_cost, updates=type_grad_updates,
givens={
x: train_x[index*batch_size:(index+1)*batch_size],
y: train_type_y[index*batch_size:(index+1)*batch_size]
})
valid_train_type_func = function([index], type_cost, updates=type_grad_updates,
givens={
x: valid_x[index*batch_size:(index+1)*batch_size],
y: valid_type_y[index*batch_size:(index+1)*batch_size]
})
train_type_pred = function([index], type_model.preds,
givens={
x: train_x[index*batch_size:(index+1)*batch_size]
})
valid_type_pred = function([index], type_model.preds,
givens={
x: valid_x[index*batch_size:(index+1)*batch_size],
})
test_type_pred = function([index], type_model.preds,
givens={
x:test_x[index*batch_size:(index+1)*batch_size],
})
# apply early stop strategy
patience = 100
patience_increase = 2
improvement_threshold = 1.005
n_valid = len(dataset[1][0])
n_test = len(dataset[2][0])
epoch = 0
best_params = None
best_validation_score = 0.
test_perf = 0
done_loop = False
log_file = open(log_fn, 'a')
print "Start to train the model....."
cpu_tst_pop_y = np.asarray(dataset[2][1])
cpu_tst_type_y = np.asarray(dataset[2][2])
def compute_score(true_list, pred_list):
mat = np.equal(true_list, pred_list)
score = np.mean(mat)
return score
while (epoch < n_epochs) and not done_loop:
start_time = timeit.default_timer()
epoch += 1
costs = []
for minibatch_index in np.random.permutation(range(n_train_batches)):
cost_epoch = train_func(minibatch_index)
costs.append(cost_epoch)
set_zero(zero_vec)
type_costs = []
for minibatch_index in np.random.permutation(range(n_train_batches)):
cost_epoch = train_type_func(minibatch_index)
type_costs.append(cost_epoch)
set_zero(zero_vec)
# do validatiovalidn
valid_cost = [valid_train_func(i) for i in np.random.permutation(xrange(n_valid_batches))]
valid_type_cost = [valid_train_type_func(i) for i in np.random.permutation(xrange(n_valid_batches))]
if epoch % 5 == 0:
# do test
test_preds = np.concatenate([test_pred(i) for i in xrange(n_test_batches)])
test_score = compute_score(cpu_tst_pop_y, test_preds)
test_type_preds = np.concatenate([test_type_pred(i) for i in xrange(n_test_batches)])
test_type_score = compute_score(cpu_tst_type_y, test_type_preds)
with open(os.path.join(perf_fn, "%s_%d.pop_pred" % (exp_name, epoch)), 'w') as epf:
for p in test_preds:
epf.write("%d\n" % int(p))
with open(os.path.join(perf_fn, "%s_%d.type_pred" % (exp_name, epoch)), 'w') as epf:
for p in test_type_preds:
epf.write("%d\n" % int(p))
message = "Epoch %d test pop perf %f, type perf %f" % (epoch, test_score, test_type_score)
print message
log_file.write(message + "\n")
log_file.flush()
end_time = timeit.default_timer()
print "Finish one iteration using %f m" % ((end_time - start_time)/60.)
log_file.flush()
log_file.close()
def shared_dataset(data_xyz, borrow=True):
data_x, data_y, data_z = data_xyz
shared_x = theano.shared(np.asarray(data_x,
dtype=theano.config.floatX), borrow=borrow)
shared_y = theano.shared(np.asarray(data_y,
dtype=theano.config.floatX), borrow=borrow)
shared_z = theano.shared(np.asarray(data_z,
dtype=theano.config.floatX), borrow=borrow)
return shared_x, T.cast(shared_y, 'int32'), T.cast(shared_z, 'int32')
def main():
args = parse_args()
prefix = args.prefix
word2vec_file = args.word2vec
sufix_pop = args.sufix_pop
sufix_type = args.sufix_type
expe_name = args.exp_name
batch_size = args.batch_size
log_fn = args.log_fn
perf_fn = args.perf_fn
# load the dataset
print 'Start loading the dataset ...'
dataset = load_dataset(prefix, sufix_pop, sufix_type)
wf = open(word2vec_file)
embedding = cPickle.load(wf)
word2id = cPickle.load(wf)
class2id = {}
dict_pop_file = args.dict_pop_fn
class2id["pop"] = {k.strip(): i for i, k in enumerate(open(dict_pop_file))}
dict_type_file = args.dict_type_fn
class2id["type"] = {k.strip(): i for i, k in enumerate(open(dict_type_file))}
# transform doc to dig list and padding docs
print 'Start to transform doc to digits'
max_len = args.max_len
padding = args.padding
digit_dataset = transform_dataset(dataset, word2id, class2id, max_len, padding)
non_static = not args.static
exp_name = args.exp_name
n_epochs = args.max_iter
# load local parameters
loc_params = json.load(open(args.param_fn))
filter_hs = loc_params["filter_hs"]
hidden_units = loc_params["hidden_units"]
run_cnn(exp_name, digit_dataset, embedding,
log_fn, perf_fn,
emb_dm=embedding.shape[1],
batch_size=batch_size,
filter_hs=filter_hs,
hidden_units=hidden_units,
dropout_rate=0.5,
shuffle_batch=True,
n_epochs=n_epochs,
lr_decay=0.85,
activation=ReLU,
sqr_norm_lim=9,
non_static=non_static)
if __name__ == "__main__":
main()