-
Notifications
You must be signed in to change notification settings - Fork 2
/
pretrain_CNN+bgrnn+blstm.py
115 lines (95 loc) · 5.04 KB
/
pretrain_CNN+bgrnn+blstm.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: xiwen zhao
@created: 2016.12.15
"""
from optparse import OptionParser
from pre_trainer import BasePreTrainer
from common import data_manager
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Embedding, Merge
from keras.layers import LSTM, SimpleRNN, GRU
from keras.layers import Embedding
from keras.layers import Convolution1D, GlobalMaxPooling1D
from keras.layers.convolutional import ZeroPadding1D
from keras.layers.wrappers import Bidirectional
from keras.optimizers import RMSprop, SGD
class Trainer(BasePreTrainer):
def get_model_name(self):
return __file__.split('/')[-1].split('.')[0]
def post_prepare_X(self, x):
return [x for _ in range(3)]
def set_model_config(self, options):
self.config = dict(
nb_filter_pre = options.nb_filter_pre,
filter_length = options.filter_length,
dropout_W = options.dropout_W,
dropout_U = options.dropout_U,
dropout = options.dropout,
optimizer = options.optimizer,
rnn_output_dims_pre = options.rnn_output_dims_pre,
lr = options.lr
)
def get_optimizer(self, key_optimizer):
if key_optimizer == 'rmsprop':
return RMSprop(lr=self.config['lr'], rho=0.9, epsilon=1e-08)
else: # 'sgd'
return SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=False)
def build_pre_model(self, config, weights):
bgrnn_model = Sequential()
bgrnn_model.add(Embedding(config['max_features'],
config['embedding_dims'],
input_length = config['input_length'],
weights = [weights['Wemb']] if 'Wemb' in weights else None))
bgrnn_model.add(Bidirectional(GRU(config['rnn_output_dims_pre'],
dropout_W=config['dropout_W'], dropout_U=config['dropout_U'])))
blstm_model = Sequential()
blstm_model.add(Embedding(config['max_features'],
config['embedding_dims'],
input_length = config['input_length'],
weights = [weights['Wemb']] if 'Wemb' in weights else None))
blstm_model.add(Bidirectional(LSTM(config['rnn_output_dims_pre'],
dropout_W=config['dropout_W'], dropout_U=config['dropout_U'])))
cnn_model = Sequential()
cnn_model.add(Embedding(config['max_features'],
config['embedding_dims'],
input_length = config['input_length'],
weights = [weights['Wemb']] if 'Wemb' in weights else None))
#dropout = 0.2))
# cnn_model.add(ZeroPadding1D(int(config['filter_length_1'] / 2)))
cnn_model.add(Convolution1D(nb_filter = config['nb_filter_pre'],
filter_length = config['filter_length'],
border_mode = 'valid',
activation = 'relu',
subsample_length = 1))
cnn_model.add(GlobalMaxPooling1D())
# cnn_model.add(Dense(config['hidden_dims']))
# cnn_model.add(Activation('sigmoid'))
# merged model
merged_model = Sequential()
merged_model.add(Merge([bgrnn_model, blstm_model, cnn_model], mode='concat', concat_axis=1))
merged_model.add(Dropout(self.config['dropout']))
merged_model.add(Dense(self.config['nb_classes'], activation='softmax', name="dense_pretrain"))
print '<dense output dimension>:', self.config['nb_classes']
merged_model.compile(loss="categorical_crossentropy",
optimizer=self.get_optimizer(config['optimizer']),
metrics=['accuracy'])
return merged_model
def main():
optparser = OptionParser()
optparser.add_option("-p", "--nb_epoch", dest="nb_epoch", type="int", default=50)
optparser.add_option("-e", "--embedding", dest="fname_Wemb", default="glove.twitter.27B.25d.txt")
optparser.add_option("-f", "--nb_filter_pre", dest="nb_filter_pre", type="int", default=200)
optparser.add_option("-r", "--rnn_output_dims_pre", dest="rnn_output_dims_pre", type="int", default=100)
optparser.add_option("-l", "--filter_length", dest="filter_length", type="int", default=3)
optparser.add_option("-w", "--dropout_W", dest="dropout_W", type="float", default=0.25)
optparser.add_option("-u", "--dropout_U", dest="dropout_U", type="float", default=0.25)
optparser.add_option("-d", "--dropout", dest="dropout", type="float", default=0.25)
optparser.add_option("-o", "--optimizer", dest="optimizer", default="rmsprop")
optparser.add_option("-v", "--learning rate", dest="lr", type="float", default=0.001)
opts, args = optparser.parse_args()
trainer = Trainer(opts)
trainer.pre_train()
if __name__ == '__main__':
main()