-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_zoo.py
481 lines (414 loc) · 25.8 KB
/
model_zoo.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
import logging
import os
import numpy as np
from keras.layers import *
from keras.models import model_from_json, Model
from keras.regularizers import l2
from keras_wrapper.cnn_model import CNN_Model
from keras_wrapper.extra.regularize import Regularize
class Text_Classification_Model(CNN_Model):
def __init__(self, params, type='Basic_Text_Classification_Model', verbose=1, structure_path=None,
weights_path=None,
model_name=None, vocabularies=None, store_path=None):
"""
Text_Classification_Model object constructor.
:param params: all hyperparameters of the model.
:param type: network name type (corresponds to any method defined in the section 'MODELS' of this class). Only valid if 'structure_path' == None.
:param verbose: set to 0 if you don't want the model to output informative messages
:param structure_path: path to a Keras' model json file. If we speficy this parameter then 'type' will be only an informative parameter.
:param weights_path: path to the pre-trained weights file (if None, then it will be randomly initialized)
:param model_name: optional name given to the network (if None, then it will be assigned to current time as its name)
:param vocabularies: vocabularies used for GLOVE word embedding
:param store_path: path to the folder where the temporal model packups will be stored
References:
[PReLU]
Kaiming He et al. Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification
[BatchNormalization]
Sergey Ioffe and Christian Szegedy. Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift
"""
super(self.__class__, self).__init__(type=type, model_name=model_name,
silence=verbose == 0, models_path=store_path, inheritance=True)
self.__toprint = ['_model_type', 'name', 'model_path', 'verbose']
self.verbose = verbose
self._model_type = type
self.params = params
self.vocabularies = vocabularies
# Sets the model name and prepares the folders for storing the models
self.setName(model_name, models_path=store_path)
# Prepare GLOVE embedding
if params['SRC_PRETRAINED_VECTORS'] is not None:
if self.verbose > 0:
logging.info(
"<<< Loading pretrained word vectors from file " + params['SRC_PRETRAINED_VECTORS'] + " >>>")
self.word_vectors_src = np.load(os.path.join(params['SRC_PRETRAINED_VECTORS'])).item()
else:
self.word_vectors_src = dict()
# Prepare GLOVE embedding
if params['TRG_PRETRAINED_VECTORS'] is not None:
if self.verbose > 0:
logging.info(
"<<< Loading pretrained word vectors from file " + params['TRG_PRETRAINED_VECTORS'] + " >>>")
self.word_vectors_trg = np.load(os.path.join(params['TRG_PRETRAINED_VECTORS'])).item()
else:
self.word_vectors_trg = dict()
# Prepare model
if structure_path:
# Load a .json model
if self.verbose > 0:
logging.info("<<< Loading model structure from file " + structure_path + " >>>")
self.model = model_from_json(open(structure_path).read())
else:
# Build model from scratch
if hasattr(self, type):
if self.verbose > 0:
logging.info("<<< Building " + type + " Text_Classification_Model >>>")
eval('self.' + type + '(params)')
else:
raise Exception('Text_Classification_Model type "' + type + '" is not implemented.')
# Load weights from file
if weights_path:
if self.verbose > 0:
logging.info("<<< Loading weights from file " + weights_path + " >>>")
self.model.load_weights(weights_path)
# Print information of self
if verbose > 0:
print str(self)
self.model.summary()
self.setOptimizer()
# ------------------------------------------------------- #
# VISUALIZATION
# Methods for visualization
# ------------------------------------------------------- #
def __str__(self):
"""
Plot basic model information.
"""
obj_str = '-----------------------------------------------------------------------------------\n'
class_name = self.__class__.__name__
obj_str += '\t\t' + class_name + ' instance\n'
obj_str += '-----------------------------------------------------------------------------------\n'
# Print pickled attributes
for att in self.__toprint:
obj_str += att + ': ' + str(self.__dict__[att])
obj_str += '\n'
obj_str += '\n'
obj_str += 'MODEL PARAMETERS:\n'
obj_str += str(self.params)
obj_str += '\n'
obj_str += '-----------------------------------------------------------------------------------'
return obj_str
# ------------------------------------------------------- #
# PREDEFINED MODELS
# ------------------------------------------------------- #
def CNN_Classifier(self, params):
# Store inputs and outputs names
self.ids_inputs = params['INPUTS_IDS_MODEL']
self.ids_outputs = params['OUTPUTS_IDS_MODEL']
# Prepare GLOVE vectors for text embedding initialization
embedding_weights = np.random.rand(params['INPUT_SRC_VOCABULARY_SIZE'],
params['SRC_TEXT_EMBEDDING_HIDDEN_SIZE'])
for word, index in self.vocabularies[self.ids_inputs[0]]['words2idx'].iteritems():
if self.word_vectors_src.get(word) is not None:
embedding_weights[index, :] = self.word_vectors_src[word]
self.word_vectors_src = {}
# Source text
src_text = Input(name=self.ids_inputs[0], batch_shape=tuple([None, params['MAX_INPUT_TEXT_LEN']]),
dtype='int32')
sentence_embedding_glove = Embedding(params['INPUT_SRC_VOCABULARY_SIZE'],
params['SRC_TEXT_EMBEDDING_HIDDEN_SIZE'],
name='source_word_embedding_glove',
weights=[embedding_weights],
trainable=params['SRC_PRETRAINED_VECTORS_TRAINABLE'],
embeddings_regularizer=l2(params['WEIGHT_DECAY']),
mask_zero=False)(src_text)
convolutions = []
for filter_len in params['FILTER_SIZES']:
conv = Convolution1D(params['NUM_FILTERS'],
filter_len,
activation=params['CNN_ACTIVATION'],
kernel_regularizer=l2(params['WEIGHT_DECAY']),
bias_regularizer=l2(params['WEIGHT_DECAY']))(sentence_embedding_glove)
pool = MaxPooling1D()(conv)
# pool = Regularize(pool, params, name='pool_' + str(filter_len))
convolutions.append(Flatten()(pool))
if len(convolutions) > 1:
out_layer = Concatenate()(convolutions)
else:
out_layer = convolutions[0]
# Optional deep ouput
for i, (activation, dimension) in enumerate(params['DEEP_OUTPUT_LAYERS']):
if activation.lower() == 'maxout':
out_layer = MaxoutDense(dimension,
kernel_regularizer=l2(params['WEIGHT_DECAY']),
name='maxout_%d' % i)(out_layer)
else:
out_layer = Dense(dimension,
activation=activation,
kernel_regularizer=l2(params['WEIGHT_DECAY']),
name=activation + '_%d' % i)(out_layer)
out_layer = Regularize(out_layer, params, name='out_layer_' + str(i))
# Softmax
output = Dense(params['N_CLASSES'],
activation=params['CLASSIFIER_ACTIVATION'],
name=self.ids_outputs[0],
kernel_regularizer=l2(params['WEIGHT_DECAY']))(out_layer)
self.model = Model(inputs=src_text, outputs=output)
def BLSTM_Classifier(self, params):
# Store inputs and outputs names
self.ids_inputs = params['INPUTS_IDS_MODEL']
self.ids_outputs = params['OUTPUTS_IDS_MODEL']
# Prepare GLOVE vectors for text embedding initialization
embedding_weights = np.random.rand(params['INPUT_SRC_VOCABULARY_SIZE'],
params['SRC_TEXT_EMBEDDING_HIDDEN_SIZE'])
for word, index in self.vocabularies[self.ids_inputs[0]]['words2idx'].iteritems():
if self.word_vectors_src.get(word) is not None:
embedding_weights[index, :] = self.word_vectors_src[word]
self.word_vectors_src = {}
# Source text
src_text = Input(name=self.ids_inputs[0], batch_shape=tuple([None, None]), dtype='int32')
sentence_embedding = Embedding(params['INPUT_SRC_VOCABULARY_SIZE'], params['SRC_TEXT_EMBEDDING_HIDDEN_SIZE'],
name='source_word_embedding',
weights=[embedding_weights],
trainable=params['SRC_PRETRAINED_VECTORS_TRAINABLE'],
embeddings_regularizer=l2(params['WEIGHT_DECAY']),
mask_zero=True)(src_text)
sentence_embedding = Regularize(sentence_embedding, params, name='source_word_embedding')
for activation, dimension in params['ADDITIONAL_EMBEDDING_LAYERS']:
sentence_embedding = TimeDistributed(Dense(dimension, name='%s_1' % activation, activation=activation,
kernel_regularizer=l2(params['WEIGHT_DECAY'])))(sentence_embedding)
sentence_embedding = Regularize(sentence_embedding, params, name='%s_1' % activation)
out_layer = Bidirectional(LSTM(params['LSTM_ENCODER_HIDDEN_SIZE'],
kernel_regularizer=l2(params['WEIGHT_DECAY']),
recurrent_regularizer=l2(params['WEIGHT_DECAY']),
bias_regularizer=l2(params['WEIGHT_DECAY']),
return_sequences=False),
name='bidirectional_encoder')(sentence_embedding)
out_layer = Regularize(out_layer, params, name='out_layer')
# Optional deep ouput
for i, (activation, dimension) in enumerate(params['DEEP_OUTPUT_LAYERS']):
if activation.lower() == 'maxout':
out_layer = MaxoutDense(dimension,
kernel_regularizer=l2(params['WEIGHT_DECAY']),
name='maxout_%d' % i)(out_layer)
else:
out_layer = Dense(dimension,
activation=activation,
kernel_regularizer=l2(params['WEIGHT_DECAY']),
name=activation + '_%d' % i)(out_layer)
out_layer = Regularize(out_layer, params, name=activation + '_%d' % i)
# Softmax
output = Dense(params['N_CLASSES'],
activation=params['CLASSIFIER_ACTIVATION'],
name=self.ids_outputs[0],
kernel_regularizer=l2(params['WEIGHT_DECAY']))(out_layer)
self.model = Model(inputs=src_text, outputs=output)
def Bilingual_CNN_Classifier(self, params):
# Store inputs and outputs names
self.ids_inputs = params['INPUTS_IDS_MODEL']
self.ids_outputs = params['OUTPUTS_IDS_MODEL']
# Prepare GLOVE vectors for text embedding initialization
embedding_weights = np.random.rand(params['INPUT_SRC_VOCABULARY_SIZE'],
params['SRC_TEXT_EMBEDDING_HIDDEN_SIZE'])
for word, index in self.vocabularies[self.ids_inputs[0]]['words2idx'].iteritems():
if self.word_vectors_src.get(word) is not None:
embedding_weights[index, :] = self.word_vectors_src[word]
self.word_vectors_src = {}
# Source text model
src_text = Input(name=self.ids_inputs[0], batch_shape=tuple([None, params['MAX_INPUT_TEXT_LEN']]),
dtype='int32')
src_sentence_embedding = Embedding(params['INPUT_SRC_VOCABULARY_SIZE'],
params['SRC_TEXT_EMBEDDING_HIDDEN_SIZE'],
name='source_word_embedding',
weights=[embedding_weights],
trainable=params['SRC_PRETRAINED_VECTORS_TRAINABLE'],
embeddings_regularizer=l2(params['WEIGHT_DECAY']),
mask_zero=False)(src_text)
src_sentence_embedding = Regularize(src_sentence_embedding, params, name='source_word_embedding')
for activation, dimension in params['ADDITIONAL_EMBEDDING_LAYERS']:
src_sentence_embedding = TimeDistributed(
Dense(dimension, name='%s_1_src' % activation, activation=activation,
kernel_regularizer=l2(params['WEIGHT_DECAY'])))(src_sentence_embedding)
src_sentence_embedding = Regularize(src_sentence_embedding, params, name='%s_1_src' % activation)
src_convolutions = []
for filter_len in params['FILTER_SIZES']:
src_conv = Convolution1D(params['NUM_FILTERS'],
filter_len,
activation=params['CNN_ACTIVATION'],
kernel_regularizer=l2(params['WEIGHT_DECAY']),
bias_regularizer=l2(params['WEIGHT_DECAY']))(src_sentence_embedding)
src_pool = MaxPooling1D()(src_conv)
# pool = Regularize(pool, params, name='pool_' + str(filter_len))
src_convolutions.append(Flatten()(src_pool))
if len(src_convolutions) > 1:
src_out_layer = Concatenate()(src_convolutions)
else:
src_out_layer = src_convolutions[0]
# Optional deep ouput
for i, (activation, dimension) in enumerate(params['DEEP_OUTPUT_LAYERS']):
if activation.lower() == 'maxout':
src_out_layer = MaxoutDense(dimension,
kernel_regularizer=l2(params['WEIGHT_DECAY']),
name='maxout_%d_src' % i)(src_out_layer)
else:
src_out_layer = Dense(dimension,
activation=activation,
kernel_regularizer=l2(params['WEIGHT_DECAY']),
name=activation + '_%d_src' % i)(src_out_layer)
src_out_layer = Regularize(src_out_layer, params, name='out_layer_%s_src' % str(i))
# Target text model
# Prepare GLOVE vectors for text embedding initialization
embedding_weights = np.random.rand(params['INPUT_TRG_VOCABULARY_SIZE'],
params['TRG_TEXT_EMBEDDING_HIDDEN_SIZE'])
for word, index in self.vocabularies[self.ids_inputs[1]]['words2idx'].iteritems():
if self.word_vectors_trg.get(word) is not None:
embedding_weights[index, :] = self.word_vectors_trg[word]
self.word_vectors_trg = {}
# Target text
trg_text = Input(name=self.ids_inputs[1], batch_shape=tuple([None, params['MAX_INPUT_TEXT_LEN']]),
dtype='int32')
trg_sentence_embedding = Embedding(params['INPUT_TRG_VOCABULARY_SIZE'],
params['TRG_TEXT_EMBEDDING_HIDDEN_SIZE'],
name='target_word_embedding',
weights=[embedding_weights],
trainable=params['TRG_PRETRAINED_VECTORS_TRAINABLE'],
embeddings_regularizer=l2(params['WEIGHT_DECAY']),
mask_zero=False)(trg_text)
trg_sentence_embedding = Regularize(trg_sentence_embedding, params, name='target_word_embedding')
for activation, dimension in params['ADDITIONAL_EMBEDDING_LAYERS']:
trg_sentence_embedding = TimeDistributed(
Dense(dimension, name='%s_1_trg' % activation, activation=activation,
kernel_regularizer=l2(params['WEIGHT_DECAY'])))(trg_sentence_embedding)
trg_sentence_embedding = Regularize(trg_sentence_embedding, params, name='%s_1_trg' % activation)
trg_convolutions = []
for filter_len in params['FILTER_SIZES']:
trg_conv = Convolution1D(params['NUM_FILTERS'],
filter_len,
activation=params['CNN_ACTIVATION'],
kernel_regularizer=l2(params['WEIGHT_DECAY']),
bias_regularizer=l2(params['WEIGHT_DECAY']))(trg_sentence_embedding)
trg_pool = MaxPooling1D()(trg_conv)
# pool = Regularize(pool, params, name='pool_' + str(filter_len))
trg_convolutions.append(Flatten()(trg_pool))
if len(trg_convolutions) > 1:
trg_out_layer = Concatenate()(trg_convolutions)
else:
trg_out_layer = trg_convolutions[0]
# Optional deep ouput
for i, (activation, dimension) in enumerate(params['DEEP_OUTPUT_LAYERS']):
if activation.lower() == 'maxout':
trg_out_layer = MaxoutDense(dimension,
kernel_regularizer=l2(params['WEIGHT_DECAY']),
name='maxout_%d_trg' % i)(trg_out_layer)
else:
trg_out_layer = Dense(dimension,
activation=activation,
kernel_regularizer=l2(params['WEIGHT_DECAY']),
name=activation + '_%d_trg' % i)(trg_out_layer)
trg_out_layer = Regularize(trg_out_layer, params, name='out_layer_%s_trg' % str(i))
out_layer = Concatenate()([src_out_layer, trg_out_layer])
# Softmax
output = Dense(params['N_CLASSES'],
activation=params['CLASSIFIER_ACTIVATION'],
name=self.ids_outputs[0],
kernel_regularizer=l2(params['WEIGHT_DECAY']))(out_layer)
self.model = Model(inputs=[src_text, trg_text], outputs=output)
def Bilingual_BLSTM_Classifier(self, params):
# Store inputs and outputs names
self.ids_inputs = params['INPUTS_IDS_MODEL']
self.ids_outputs = params['OUTPUTS_IDS_MODEL']
# Prepare GLOVE vectors for text embedding initialization
embedding_weights = np.random.rand(params['INPUT_SRC_VOCABULARY_SIZE'],
params['SRC_TEXT_EMBEDDING_HIDDEN_SIZE'])
for word, index in self.vocabularies[self.ids_inputs[0]]['words2idx'].iteritems():
if self.word_vectors_src.get(word) is not None:
embedding_weights[index, :] = self.word_vectors_src[word]
self.word_vectors_src = {}
# Source text model
src_text = Input(name=self.ids_inputs[0], batch_shape=tuple([None, None]), dtype='int32')
src_sentence_embedding = Embedding(params['INPUT_SRC_VOCABULARY_SIZE'],
params['SRC_TEXT_EMBEDDING_HIDDEN_SIZE'],
name='source_word_embedding',
weights=[embedding_weights],
trainable=params['SRC_PRETRAINED_VECTORS_TRAINABLE'],
embeddings_regularizer=l2(params['WEIGHT_DECAY']),
mask_zero=True)(src_text)
src_sentence_embedding = Regularize(src_sentence_embedding, params, name='source_word_embedding')
for activation, dimension in params['ADDITIONAL_EMBEDDING_LAYERS']:
src_sentence_embedding = TimeDistributed(
Dense(dimension, name='%s_1_src' % activation, activation=activation,
kernel_regularizer=l2(params['WEIGHT_DECAY'])))(src_sentence_embedding)
src_sentence_embedding = Regularize(src_sentence_embedding, params, name='%s_1_src' % activation)
src_out_layer = Bidirectional(LSTM(params['LSTM_ENCODER_HIDDEN_SIZE'],
kernel_regularizer=l2(params['WEIGHT_DECAY']),
recurrent_regularizer=l2(params['WEIGHT_DECAY']),
bias_regularizer=l2(params['WEIGHT_DECAY']),
return_sequences=False),
name='bidirectional_encoder_src')(src_sentence_embedding)
src_out_layer = Regularize(src_out_layer, params, name='out_layer_src')
# Optional deep ouput
for i, (activation, dimension) in enumerate(params['DEEP_OUTPUT_LAYERS']):
if activation.lower() == 'maxout':
src_out_layer = MaxoutDense(dimension,
kernel_regularizer=l2(params['WEIGHT_DECAY']),
name='maxout_%d_src' % i)(src_out_layer)
else:
src_out_layer = Dense(dimension,
activation=activation,
kernel_regularizer=l2(params['WEIGHT_DECAY']),
name=activation + '_%d_src' % i)(src_out_layer)
src_out_layer = Regularize(src_out_layer, params, name=activation + '_%d_src' % i)
# Prepare GLOVE vectors for text embedding initialization
embedding_weights = np.random.rand(params['INPUT_TRG_VOCABULARY_SIZE'],
params['TRG_TEXT_EMBEDDING_HIDDEN_SIZE'])
for word, index in self.vocabularies[self.ids_inputs[1]]['words2idx'].iteritems():
if self.word_vectors_trg.get(word) is not None:
embedding_weights[index, :] = self.word_vectors_trg[word]
self.word_vectors_trg = {}
# Target text
trg_text = Input(name=self.ids_inputs[1], batch_shape=tuple([None, None]), dtype='int32')
trg_sentence_embedding = Embedding(params['INPUT_TRG_VOCABULARY_SIZE'],
params['TRG_TEXT_EMBEDDING_HIDDEN_SIZE'],
name='target_word_embedding',
weights=[embedding_weights],
trainable=params['TRG_PRETRAINED_VECTORS_TRAINABLE'],
kernel_regularizer=l2(params['WEIGHT_DECAY']),
mask_zero=True)(trg_text)
trg_sentence_embedding = Regularize(trg_sentence_embedding, params, name='target_word_embedding')
for activation, dimension in params['ADDITIONAL_EMBEDDING_LAYERS']:
trg_sentence_embedding = TimeDistributed(
Dense(dimension, name='%s_1_trg' % activation, activation=activation,
kernel_regularizer=l2(params['WEIGHT_DECAY'])))(trg_sentence_embedding)
trg_sentence_embedding = Regularize(trg_sentence_embedding, params, name='%s_1_trg' % activation)
trg_out_layer = Bidirectional(LSTM(params['LSTM_ENCODER_HIDDEN_SIZE'],
kernel_regularizer=l2(params['WEIGHT_DECAY']),
recurrent_regularizer=l2(params['WEIGHT_DECAY']),
bias_regularizer=l2(params['WEIGHT_DECAY']),
return_sequences=False),
name='bidirectional_encoder_trg')(trg_sentence_embedding)
trg_out_layer = Regularize(trg_out_layer, params, name='out_layer_trg')
# Optional deep ouput
for i, (activation, dimension) in enumerate(params['DEEP_OUTPUT_LAYERS']):
if activation.lower() == 'maxout':
trg_out_layer = MaxoutDense(dimension,
kernel_regularizer=l2(params['WEIGHT_DECAY']),
name='maxout_%d_trg' % i)(trg_out_layer)
else:
trg_out_layer = Dense(dimension,
activation=activation,
kernel_regularizer=l2(params['WEIGHT_DECAY']),
name=activation + '_%d_trg' % i)(trg_out_layer)
trg_out_layer = Regularize(trg_out_layer, params, name=activation + '_%d_trg' % i)
out_layer = Concatenate()([src_out_layer, trg_out_layer])
# Softmax
output = Dense(params['N_CLASSES'],
activation=params['CLASSIFIER_ACTIVATION'],
name=self.ids_outputs[0],
kernel_regularizer=l2(params['WEIGHT_DECAY']))(out_layer)
self.model = Model(inputs=[src_text, trg_text], outputs=output)
def __getstate__(self):
"""
Behaviour applied when pickling a Text_Classification_Model instance.
"""
obj_dict = self.__dict__.copy()
del obj_dict['model']
return obj_dict