/
keras_models.py
630 lines (493 loc) · 28.6 KB
/
keras_models.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
from __future__ import print_function
from abc import abstractmethod
import os
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Input
# AttributeError: 'tuple' object has no attribute 'layer' #478
# https://github.com/tensorflow/probability/issues/478
from tensorflow.keras.layers import Embedding, Conv1D, Lambda, LSTM, Dense, concatenate
from tensorflow.keras.layers import BatchNormalization, Activation
from tensorflow.keras import backend as K
from tensorflow.keras.models import Model, model_from_json
from tensorflow.keras.utils import plot_model
from tensorflow.keras.losses import cosine_similarity
from tensorflow.keras.optimizers import Adam
from attention_unif import AttentionLayer, AttentionLayerWithBatchNormalization
from average_unif import AverageLayer
import numpy as np
class LanguageModel:
def __init__(self, config):
#self.question = Input(shape=(config.question_len(),), dtype='int32', name='question_base')
#self.answer_good = Input(shape=(config.answer_len(),), dtype='int32', name='answer_good_base')
#self.answer_bad = Input(shape=(config.answer_len(),), dtype='int32', name='answer_bad_base')
self.question_len = config.question_len()
self.answer_len = config.answer_len()
self.config = config
self.params = config.similarity_params()
self.kernel_size = self.config.kernel_size()
# initialize a bunch of variables that will be set later
self._models = None
self._similarities = None
self._answer = None
self._qa_model = None
training_model, prediction_model = self.create_model()
self.training_model = training_model
self.prediction_model = prediction_model
@abstractmethod
def build(self):
return
def hinge_loss(self, x):
margin = self.config.margin()
return K.relu(margin - x[0] + x[1])
def custom_loss(self, y_true, y_pred):
return y_pred
def create_model(self):
question = Input(shape=(self.question_len,), dtype='int32', name='question_base')
answer_good = Input(shape=(self.answer_len,), dtype='int32', name='answer_good')
answer_bad = Input(shape=(self.answer_len,), dtype='int32', name='answer_bad')
qa_model = self.build()
good_similarity = qa_model([question, answer_good])
bad_similarity = qa_model([question, answer_bad])
loss = Lambda(self.hinge_loss,
output_shape=lambda x: x[0], name='hinge_loss')([good_similarity, bad_similarity])
training_model = Model(inputs=[question, answer_good, answer_bad], outputs=loss,
name='training_model')
prediction_model = Model(inputs=[question, answer_good], outputs=good_similarity,
name='prediction_model')
return training_model, prediction_model
def compile(self, optimizer, **kwargs):
self.prediction_model.compile(loss=self.custom_loss, optimizer=Adam(), **kwargs)
self.training_model.compile(loss=self.custom_loss, optimizer=Adam(), **kwargs)
def training_summary(self):
self.training_model.summary()
def prediction_summary(self):
self.prediction_model.summary()
def save_training_plot_model(self, filename):
# visualize model
plot_model(self.training_model, to_file=filename, show_shapes=True, expand_nested=True)
def save_prediction_plot_model(self, filename):
# visualize model
plot_model(self.prediction_model, to_file=filename, show_shapes=True, expand_nested=True)
def fit(self, x, **kwargs):
assert self.training_model is not None, 'Must compile the model before fitting data'
y = np.zeros(shape=(x[0].shape[0],)) # doesn't get used
return self.training_model.fit(x, y, **kwargs)
def predict(self, x):
assert self.prediction_model is not None and isinstance(self.prediction_model, Model)
return self.prediction_model.predict_on_batch(x)
def save_json(self, filename, **kwargs):
# Save JSON config to disk
json_config = self.prediction_model.to_json()
with open(filename, 'w') as json_file:
json_file.write(json_config)
def save_weights(self, file_name, **kwargs):
assert self.prediction_model is not None, 'Must compile the model before saving weights'
self.prediction_model.save_weights(file_name, **kwargs)
def load_json(self, filename, **kwargs):
with open(filename) as json_file:
json_config = json_file.read()
self.prediction_model = model_from_json(json_config, custom_objects={'AttentionLayer': AttentionLayer,
'AttentionLayerWithBatchNormalization': AttentionLayerWithBatchNormalization,
'AverageLayer': AverageLayer})
def load_weights(self, file_name, **kwargs):
assert self.prediction_model is not None, 'Must compile the model loading weights'
self.prediction_model.load_weights(file_name, **kwargs)
class EmbeddingModel(LanguageModel):
def build(self):
question = Input(shape=(self.question_len,), dtype='int32', name='question')
answer = Input(shape=(self.answer_len,), dtype='int32', name='answer')
# add embedding layers
question_weights = np.load(self.config.initial_question_weights())
q_embedding = Embedding(input_dim=question_weights.shape[0],
output_dim=question_weights.shape[1],
weights=[question_weights],
name='question_embedding')
question_embedding = q_embedding(question)
answer_weights = np.load(self.config.initial_answer_weights())
a_embedding = Embedding(input_dim=answer_weights.shape[0],
output_dim=answer_weights.shape[1],
weights=[answer_weights],
name='answer_embedding')
answer_embedding = a_embedding(answer)
# maxpooling
maxpool = Lambda(lambda x: K.max(x, axis=1, keepdims=False), output_shape=lambda x: (x[0], x[2]),
name='max')
maxpool.supports_masking = True
question_pool = maxpool(question_embedding)
answer_pool = maxpool(answer_embedding)
cos_similarity = Lambda(lambda x: cosine_similarity(x[0], x[1], axis=1)
, output_shape=lambda _: (None, 1), name='similarity')([question_pool,
answer_pool])
return Model(inputs=[question, answer], outputs=cos_similarity,
name='qa_model')
class SharedConvolutionModel(LanguageModel):
def build(self):
assert self.config.question_len() == self.config.answer_len()
question = Input(shape=(self.question_len,), dtype='int32', name='question')
answer = Input(shape=(self.answer_len,), dtype='int32', name='answer')
# add embedding layers
question_weights = np.load(self.config.initial_question_weights())
q_embedding = Embedding(input_dim=question_weights.shape[0],
output_dim=question_weights.shape[1],
weights=[question_weights],
name='question_embedding')
question_embedding = q_embedding(question)
answer_weights = np.load(self.config.initial_answer_weights())
a_embedding = Embedding(input_dim=answer_weights.shape[0],
output_dim=answer_weights.shape[1],
weights=[answer_weights],
name='answer_embedding')
answer_embedding = a_embedding(answer)
# cnn
filters = self.config.filters()
kernel_size = self.kernel_size
question_cnn = None
answer_cnn = None
if len(kernel_size) > 1:
cnns = [Conv1D(kernel_size=k,
filters=filters,
activation='relu',
padding='same',
name=f'shared_conv1d_{k}') for k in kernel_size]
# question_cnn = merge([cnn(question_embedding) for cnn in cnns], mode='concat')
question_cnn = concatenate([cnn(question_embedding) for cnn in cnns])
# answer_cnn = merge([cnn(answer_embedding) for cnn in cnns], mode='concat')
answer_cnn = concatenate([cnn(answer_embedding) for cnn in cnns])
else:
k = kernel_size[0]
cnn = Conv1D(kernel_size=k,
filters=filters,
activation='relu',
padding='same',
name=f'shared_conv1d_{k}')
# question_cnn = merge([cnn(question_embedding) for cnn in cnns], mode='concat')
question_cnn = cnn(question_embedding)
# answer_cnn = merge([cnn(answer_embedding) for cnn in cnns], mode='concat')
answer_cnn = cnn(answer_embedding)
# maxpooling
maxpool = Lambda(lambda x: K.max(x, axis=1, keepdims=False), output_shape=lambda x: (x[0], x[2]),
name='max')
maxpool.supports_masking = True
# enc = Dense(100, activation='tanh')
# question_pool = enc(maxpool(question_cnn))
# answer_pool = enc(maxpool(answer_cnn))
question_pool = maxpool(question_cnn)
answer_pool = maxpool(answer_cnn)
cos_similarity = Lambda(lambda x: cosine_similarity(x[0], x[1], axis=1)
, output_shape=lambda _: (None, 1), name='similarity')([question_pool,
answer_pool])
return Model(inputs=[question, answer], outputs=cos_similarity,
name='qa_model')
class SharedConvolutionModelWithBatchNormalization(LanguageModel):
def build(self):
assert self.config.question_len() == self.config.answer_len()
question = Input(shape=(self.question_len,), dtype='int32', name='question')
answer = Input(shape=(self.answer_len,), dtype='int32', name='answer')
# add embedding layers
question_weights = np.load(self.config.initial_question_weights())
q_embedding = Embedding(input_dim=question_weights.shape[0],
output_dim=question_weights.shape[1],
weights=[question_weights],
name='question_embedding')
question_embedding = q_embedding(question)
answer_weights = np.load(self.config.initial_answer_weights())
a_embedding = Embedding(input_dim=answer_weights.shape[0],
output_dim=answer_weights.shape[1],
weights=[answer_weights],
name='answer_embedding')
answer_embedding = a_embedding(answer)
# cnn
filters = self.config.filters()
kernel_size = self.kernel_size
question_cnn = None
answer_cnn = None
if len(kernel_size) > 1:
cnns = [Conv1D(kernel_size=k,
filters=filters,
padding='same',
name=f'shared_conv1d_with_bn_{k}') for k in kernel_size]
# question_cnn = merge([cnn(question_embedding) for cnn in cnns], mode='concat')
question_outputs_cnn = [cnn(question_embedding) for cnn in cnns]
bn_question_outputs_cnn = [BatchNormalization()(output) for output in question_outputs_cnn]
activation_question_outputs_cnn = [Activation('relu')(output) for output in bn_question_outputs_cnn]
question_cnn = concatenate([activation_output for activation_output in activation_question_outputs_cnn])
# answer_cnn = merge([cnn(answer_embedding) for cnn in cnns], mode='concat')
answer_outputs_cnn = [cnn(answer_embedding) for cnn in cnns]
bn_answer_outputs_cnn = [BatchNormalization()(output) for output in answer_outputs_cnn]
activation_answer_outputs_cnn = [Activation('relu')(output) for output in bn_answer_outputs_cnn]
answer_cnn = concatenate([activation_output for activation_output in activation_answer_outputs_cnn])
else:
k = kernel_size[0]
cnn = Conv1D(kernel_size=k,
filters=filters,
padding='same',
name=f'shared_conv1d_with_bn_{k}')
# question_cnn = merge([cnn(question_embedding) for cnn in cnns], mode='concat')
question_output_cnn = cnn(question_embedding)
bn_question_output_cnn = BatchNormalization()(question_output_cnn)
activation_question_output_cnn = Activation('relu')(bn_question_output_cnn)
question_cnn = activation_question_output_cnn
# answer_cnn = merge([cnn(answer_embedding) for cnn in cnns], mode='concat')
answer_output_cnn = cnn(answer_embedding)
bn_answer_output_cnn = BatchNormalization()(answer_output_cnn)
activation_answer_output_cnn = Activation('relu')(bn_answer_output_cnn)
answer_cnn = activation_answer_output_cnn
# maxpooling
maxpool = Lambda(lambda x: K.max(x, axis=1, keepdims=False), output_shape=lambda x: (x[0], x[2]),
name='max')
maxpool.supports_masking = True
# enc = Dense(100, activation='tanh')
# question_pool = enc(maxpool(question_cnn))
# answer_pool = enc(maxpool(answer_cnn))
question_pool = maxpool(question_cnn)
answer_pool = maxpool(answer_cnn)
cos_similarity = Lambda(lambda x: cosine_similarity(x[0], x[1], axis=1)
, output_shape=lambda _: (None, 1), name='similarity')([question_pool,
answer_pool])
return Model(inputs=[question, answer], outputs=cos_similarity,
name='qa_model')
class ConvolutionModel(LanguageModel):
def build(self):
assert self.config.question_len() == self.config.answer_len()
question = Input(shape=(self.question_len,), dtype='int32', name='question')
answer = Input(shape=(self.answer_len,), dtype='int32', name='answer')
# add embedding layers
question_weights = np.load(self.config.initial_question_weights())
q_embedding = Embedding(input_dim=question_weights.shape[0],
output_dim=question_weights.shape[1],
weights=[question_weights],
name='question_embedding')
question_embedding = q_embedding(question)
answer_weights = np.load(self.config.initial_answer_weights())
a_embedding = Embedding(input_dim=answer_weights.shape[0],
output_dim=answer_weights.shape[1],
weights=[answer_weights],
name='answer_embedding')
answer_embedding = a_embedding(answer)
# cnn
filters = self.config.filters()
kernel_size = self.kernel_size
question_cnn = None
answer_cnn = None
if len(kernel_size) > 1:
q_cnns = [Conv1D(kernel_size=k,
filters=filters,
activation='relu',
padding='same',
name=f'question_conv1d_{k}') for k in kernel_size]
# question_cnn = merge([cnn(question_embedding) for cnn in cnns], mode='concat')
question_cnn = concatenate([cnn(question_embedding) for cnn in q_cnns])
# answer_cnn = merge([cnn(answer_embedding) for cnn in cnns], mode='concat')
a_cnns = [Conv1D(kernel_size=k,
filters=filters,
activation='relu',
padding='same',
name=f'answer_conv1d_{k}') for k in kernel_size]
answer_cnn = concatenate([cnn(answer_embedding) for cnn in a_cnns])
else:
k = kernel_size[0]
q_cnn = Conv1D(kernel_size=k,
filters=filters,
activation='relu',
padding='same',
name=f'question_conv1d_{k}')
# question_cnn = merge([cnn(question_embedding) for cnn in cnns], mode='concat')
question_cnn = q_cnn(question_embedding)
# answer_cnn = merge([cnn(answer_embedding) for cnn in cnns], mode='concat')
a_cnn = Conv1D(kernel_size=k,
filters=filters,
activation='relu',
padding='same',
name=f'answer_conv1d_{k}')
answer_cnn = a_cnn(answer_embedding)
# maxpooling
maxpool = Lambda(lambda x: K.max(x, axis=1, keepdims=False), output_shape=lambda x: (x[0], x[2]),
name='max')
maxpool.supports_masking = True
# enc = Dense(100, activation='tanh')
# question_pool = enc(maxpool(question_cnn))
# answer_pool = enc(maxpool(answer_cnn))
question_pool = maxpool(question_cnn)
answer_pool = maxpool(answer_cnn)
cos_similarity = Lambda(lambda x: cosine_similarity(x[0], x[1], axis=1)
, output_shape=lambda _: (None, 1), name='similarity')([question_pool,
answer_pool])
return Model(inputs=[question, answer], outputs=cos_similarity,
name='qa_model')
class ConvolutionModelWithBatchNormalization(LanguageModel):
def build(self):
assert self.config.question_len() == self.config.answer_len()
question = Input(shape=(self.question_len,), dtype='int32', name='question')
answer = Input(shape=(self.answer_len,), dtype='int32', name='answer')
# add embedding layers
question_weights = np.load(self.config.initial_question_weights())
q_embedding = Embedding(input_dim=question_weights.shape[0],
output_dim=question_weights.shape[1],
weights=[question_weights],
name='question_embedding')
question_embedding = q_embedding(question)
answer_weights = np.load(self.config.initial_answer_weights())
a_embedding = Embedding(input_dim=answer_weights.shape[0],
output_dim=answer_weights.shape[1],
weights=[answer_weights],
name='answer_embedding')
answer_embedding = a_embedding(answer)
# cnn
filters = self.config.filters()
kernel_size = self.kernel_size
question_cnn = None
answer_cnn = None
if len(kernel_size) > 1:
q_cnns = [Conv1D(kernel_size=k,
filters=filters,
padding='same',
name=f'question_conv1d_with_bn_{k}') for k in kernel_size]
# question_cnn = merge([cnn(question_embedding) for cnn in cnns], mode='concat')
question_outputs_cnn = [cnn(question_embedding) for cnn in q_cnns]
bn_question_outputs_cnn = [BatchNormalization()(output) for output in question_outputs_cnn]
activation_question_outputs_cnn = [Activation('relu')(output) for output in bn_question_outputs_cnn]
question_cnn = concatenate([activation_output for activation_output in activation_question_outputs_cnn])
# answer_cnn = merge([cnn(answer_embedding) for cnn in cnns], mode='concat')
a_cnns = [Conv1D(kernel_size=k,
filters=filters,
padding='same',
name=f'answer_conv1d_with_bn_{k}') for k in kernel_size]
answer_outputs_cnn = [cnn(answer_embedding) for cnn in a_cnns]
bn_answer_outputs_cnn = [BatchNormalization()(output) for output in answer_outputs_cnn]
activation_answer_outputs_cnn = [Activation('relu')(output) for output in bn_answer_outputs_cnn]
answer_cnn = concatenate([activation_output for activation_output in activation_answer_outputs_cnn])
else:
k = kernel_size[0]
q_cnn = Conv1D(kernel_size=k,
filters=filters,
padding='same',
name=f'question_conv1d_with_bn_{k}')
# question_cnn = merge([cnn(question_embedding) for cnn in cnns], mode='concat')
question_output_cnn = q_cnn(question_embedding)
bn_question_output_cnn = BatchNormalization()(question_output_cnn)
activation_question_output_cnn = Activation('relu')(bn_question_output_cnn)
question_cnn = activation_question_output_cnn
# answer_cnn = merge([cnn(answer_embedding) for cnn in cnns], mode='concat')
a_cnn = Conv1D(kernel_size=k,
filters=filters,
padding='same',
name=f'answer_conv1d_with_bn_{k}')
answer_output_cnn = a_cnn(answer_embedding)
bn_answer_output_cnn = BatchNormalization()(answer_output_cnn)
activation_answer_output_cnn = Activation('relu')(bn_answer_output_cnn)
answer_cnn = activation_answer_output_cnn
# maxpooling
maxpool = Lambda(lambda x: K.max(x, axis=1, keepdims=False), output_shape=lambda x: (x[0], x[2]),
name='max')
maxpool.supports_masking = True
# enc = Dense(100, activation='tanh')
# question_pool = enc(maxpool(question_cnn))
# answer_pool = enc(maxpool(answer_cnn))
question_pool = maxpool(question_cnn)
answer_pool = maxpool(answer_cnn)
cos_similarity = Lambda(lambda x: cosine_similarity(x[0], x[1], axis=1)
, output_shape=lambda _: (None, 1), name='similarity')([question_pool,
answer_pool])
return Model(inputs=[question, answer], outputs=cos_similarity,
name='qa_model')
class ConvolutionalLSTM(LanguageModel):
def build(self):
question = Input(shape=(self.question_len,), dtype='int32', name='question')
answer = Input(shape=(self.answer_len,), dtype='int32', name='answer')
# add embedding layers
question_weights = np.load(self.config.initial_question_weights())
q_embedding = Embedding(input_dim=question_weights.shape[0],
output_dim=question_weights.shape[1],
weights=[question_weights])
question_embedding = q_embedding(question)
answer_weights = np.load(self.config.initial_answer_weights())
a_embedding = Embedding(input_dim=answer_weights.shape[0],
output_dim=answer_weights.shape[1],
weights=[answer_weights])
answer_embedding = a_embedding(answer)
f_rnn = LSTM(141, return_sequences=True, implementation=1)
b_rnn = LSTM(141, return_sequences=True, implementation=1, go_backwards=True)
qf_rnn = f_rnn(question_embedding)
qb_rnn = b_rnn(question_embedding)
# question_pool = merge([qf_rnn, qb_rnn], mode='concat', concat_axis=-1)
question_pool = concatenate([qf_rnn, qb_rnn], axis=-1)
af_rnn = f_rnn(answer_embedding)
ab_rnn = b_rnn(answer_embedding)
# answer_pool = merge([af_rnn, ab_rnn], mode='concat', concat_axis=-1)
answer_pool = concatenate([af_rnn, ab_rnn], axis=-1)
# cnn
filters = self.config.filters()
cnns = [Conv1D(kernel_size=kernel_size,
filters=filters,
activation='tanh',
padding='same') for kernel_size in [1, 2, 3, 5]]
# question_cnn = merge([cnn(question_pool) for cnn in cnns], mode='concat')
question_cnn = concatenate([cnn(question_pool) for cnn in cnns])
# answer_cnn = merge([cnn(answer_pool) for cnn in cnns], mode='concat')
answer_cnn = concatenate([cnn(answer_pool) for cnn in cnns])
maxpool = Lambda(lambda x: K.max(x, axis=1, keepdims=False), output_shape=lambda x: (x[0], x[2]))
maxpool.supports_masking = True
question_pool = maxpool(question_cnn)
answer_pool = maxpool(answer_cnn)
cos_similarity = Lambda(lambda x: cosine_similarity(x[0], x[1], axis=1)
, output_shape=lambda _: (None, 1), name='similarity')([question_pool,
answer_pool])
return Model(inputs=[question, answer], outputs=cos_similarity,
name='qa_model')
class UnifModel(LanguageModel):
def build(self):
print(tf.__version__)
print(tf.keras.__version__)
question = Input(shape=(self.question_len,), dtype='int32', name='question')
answer = Input(shape=(self.answer_len,), dtype='int32', name='answer')
# add embedding layers
question_weights = np.load(self.config.initial_question_weights())
q_embedding = Embedding(input_dim=question_weights.shape[0],
output_dim=question_weights.shape[1],
weights=[question_weights],
name='question_embedding')
question_embedding = q_embedding(question)
f_average_layer = AverageLayer(name='average')
e_q = f_average_layer([question_embedding])
answer_weights = np.load(self.config.initial_answer_weights())
a_embedding = Embedding(input_dim=answer_weights.shape[0],
output_dim=answer_weights.shape[1],
weights=[answer_weights],
name='answer_embedding')
answer_embedding = a_embedding(answer)
f_attention_layer = AttentionLayer(name='attention')
e_c = f_attention_layer([answer_embedding])
cos_similarity = Lambda(lambda x: cosine_similarity(x[0], x[1], axis=1)
, output_shape=lambda _: (None, 1), name='similarity')([e_q,
e_c])
return Model(inputs=[question, answer], outputs=cos_similarity,
name='qa_model')
class UnifModelWithBatchNormalization(LanguageModel):
def build(self):
print(tf.__version__)
print(tf.keras.__version__)
question = Input(shape=(self.question_len,), dtype='int32', name='question')
answer = Input(shape=(self.answer_len,), dtype='int32', name='answer')
# add embedding layers
question_weights = np.load(self.config.initial_question_weights())
q_embedding = Embedding(input_dim=question_weights.shape[0],
output_dim=question_weights.shape[1],
weights=[question_weights],
name='question_embedding')
question_embedding = q_embedding(question)
f_average_layer = AverageLayer(name='average')
e_q = f_average_layer([question_embedding])
answer_weights = np.load(self.config.initial_answer_weights())
a_embedding = Embedding(input_dim=answer_weights.shape[0],
output_dim=answer_weights.shape[1],
weights=[answer_weights],
name='answer_embedding')
answer_embedding = a_embedding(answer)
f_attention_layer = AttentionLayerWithBatchNormalization(name='attention-with-bn')
e_c = f_attention_layer([answer_embedding])
cos_similarity = Lambda(lambda x: cosine_similarity(x[0], x[1], axis=1)
, output_shape=lambda _: (None, 1), name='similarity')([e_q,
e_c])
return Model(inputs=[question, answer], outputs=cos_similarity,
name='qa_model')