forked from chen0040/keras-text-summarization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseq2seq.py
591 lines (480 loc) · 25.5 KB
/
seq2seq.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
from __future__ import print_function
from keras.models import Model
from keras.layers import Embedding, Dense, Input
from keras.layers.recurrent import LSTM
from keras.preprocessing.sequence import pad_sequences
from keras.callbacks import ModelCheckpoint
from keras_text_summarization.library.utility.glove_loader import load_glove, GLOVE_EMBEDDING_SIZE
import numpy as np
import os
HIDDEN_UNITS = 100
DEFAULT_BATCH_SIZE = 64
VERBOSE = 1
DEFAULT_EPOCHS = 10
class Seq2SeqSummarizer(object):
model_name = 'seq2seq'
def __init__(self, config):
self.num_input_tokens = config['num_input_tokens']
self.max_input_seq_length = config['max_input_seq_length']
self.num_target_tokens = config['num_target_tokens']
self.max_target_seq_length = config['max_target_seq_length']
self.input_word2idx = config['input_word2idx']
self.input_idx2word = config['input_idx2word']
self.target_word2idx = config['target_word2idx']
self.target_idx2word = config['target_idx2word']
self.config = config
self.version = 0
if 'version' in config:
self.version = config['version']
encoder_inputs = Input(shape=(None,), name='encoder_inputs')
encoder_embedding = Embedding(input_dim=self.num_input_tokens, output_dim=HIDDEN_UNITS,
input_length=self.max_input_seq_length, name='encoder_embedding')
encoder_lstm = LSTM(units=HIDDEN_UNITS, return_state=True, name='encoder_lstm')
encoder_outputs, encoder_state_h, encoder_state_c = encoder_lstm(encoder_embedding(encoder_inputs))
encoder_states = [encoder_state_h, encoder_state_c]
decoder_inputs = Input(shape=(None, self.num_target_tokens), name='decoder_inputs')
decoder_lstm = LSTM(units=HIDDEN_UNITS, return_state=True, return_sequences=True, name='decoder_lstm')
decoder_outputs, decoder_state_h, decoder_state_c = decoder_lstm(decoder_inputs,
initial_state=encoder_states)
decoder_dense = Dense(units=self.num_target_tokens, activation='softmax', name='decoder_dense')
decoder_outputs = decoder_dense(decoder_outputs)
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
self.model = model
self.encoder_model = Model(encoder_inputs, encoder_states)
decoder_state_inputs = [Input(shape=(HIDDEN_UNITS,)), Input(shape=(HIDDEN_UNITS,))]
decoder_outputs, state_h, state_c = decoder_lstm(decoder_inputs, initial_state=decoder_state_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
self.decoder_model = Model([decoder_inputs] + decoder_state_inputs, [decoder_outputs] + decoder_states)
def load_weights(self, weight_file_path):
if os.path.exists(weight_file_path):
self.model.load_weights(weight_file_path)
def transform_input_text(self, texts):
temp = []
for line in texts:
x = []
for word in line.lower().split(' '):
wid = 1
if word in self.input_word2idx:
wid = self.input_word2idx[word]
x.append(wid)
if len(x) >= self.max_input_seq_length:
break
temp.append(x)
temp = pad_sequences(temp, maxlen=self.max_input_seq_length)
print(temp.shape)
return temp
def transform_target_encoding(self, texts):
temp = []
for line in texts:
x = []
line2 = 'START ' + line.lower() + ' END'
for word in line2.split(' '):
x.append(word)
if len(x) >= self.max_target_seq_length:
break
temp.append(x)
temp = np.array(temp)
print(temp.shape)
return temp
def generate_batch(self, x_samples, y_samples, batch_size):
num_batches = len(x_samples) // batch_size
while True:
for batchIdx in range(0, num_batches):
start = batchIdx * batch_size
end = (batchIdx + 1) * batch_size
encoder_input_data_batch = pad_sequences(x_samples[start:end], self.max_input_seq_length)
decoder_target_data_batch = np.zeros(shape=(batch_size, self.max_target_seq_length, self.num_target_tokens))
decoder_input_data_batch = np.zeros(shape=(batch_size, self.max_target_seq_length, self.num_target_tokens))
for lineIdx, target_words in enumerate(y_samples[start:end]):
for idx, w in enumerate(target_words):
w2idx = 0 # default [UNK]
if w in self.target_word2idx:
w2idx = self.target_word2idx[w]
if w2idx != 0:
decoder_input_data_batch[lineIdx, idx, w2idx] = 1
if idx > 0:
decoder_target_data_batch[lineIdx, idx - 1, w2idx] = 1
yield [encoder_input_data_batch, decoder_input_data_batch], decoder_target_data_batch
@staticmethod
def get_weight_file_path(model_dir_path):
return model_dir_path + '/' + Seq2SeqSummarizer.model_name + '-weights.h5'
@staticmethod
def get_config_file_path(model_dir_path):
return model_dir_path + '/' + Seq2SeqSummarizer.model_name + '-config.npy'
@staticmethod
def get_architecture_file_path(model_dir_path):
return model_dir_path + '/' + Seq2SeqSummarizer.model_name + '-architecture.json'
def fit(self, Xtrain, Ytrain, Xtest, Ytest, epochs=None, batch_size=None, model_dir_path=None):
if epochs is None:
epochs = DEFAULT_EPOCHS
if model_dir_path is None:
model_dir_path = './models'
if batch_size is None:
batch_size = DEFAULT_BATCH_SIZE
self.version += 1
self.config['version'] = self.version
config_file_path = Seq2SeqSummarizer.get_config_file_path(model_dir_path)
weight_file_path = Seq2SeqSummarizer.get_weight_file_path(model_dir_path)
checkpoint = ModelCheckpoint(weight_file_path)
np.save(config_file_path, self.config)
architecture_file_path = Seq2SeqSummarizer.get_architecture_file_path(model_dir_path)
open(architecture_file_path, 'w').write(self.model.to_json())
Ytrain = self.transform_target_encoding(Ytrain)
Ytest = self.transform_target_encoding(Ytest)
Xtrain = self.transform_input_text(Xtrain)
Xtest = self.transform_input_text(Xtest)
train_gen = self.generate_batch(Xtrain, Ytrain, batch_size)
test_gen = self.generate_batch(Xtest, Ytest, batch_size)
train_num_batches = len(Xtrain) // batch_size
test_num_batches = len(Xtest) // batch_size
history = self.model.fit_generator(generator=train_gen, steps_per_epoch=train_num_batches,
epochs=epochs,
verbose=VERBOSE, validation_data=test_gen, validation_steps=test_num_batches,
callbacks=[checkpoint])
self.model.save_weights(weight_file_path)
return history
def summarize(self, input_text):
input_seq = []
input_wids = []
for word in input_text.lower().split(' '):
idx = 1 # default [UNK]
if word in self.input_word2idx:
idx = self.input_word2idx[word]
input_wids.append(idx)
input_seq.append(input_wids)
input_seq = pad_sequences(input_seq, self.max_input_seq_length)
states_value = self.encoder_model.predict(input_seq)
target_seq = np.zeros((1, 1, self.num_target_tokens))
target_seq[0, 0, self.target_word2idx['START']] = 1
target_text = ''
target_text_len = 0
terminated = False
while not terminated:
output_tokens, h, c = self.decoder_model.predict([target_seq] + states_value)
sample_token_idx = np.argmax(output_tokens[0, -1, :])
sample_word = self.target_idx2word[sample_token_idx]
target_text_len += 1
if sample_word != 'START' and sample_word != 'END':
target_text += ' ' + sample_word
if sample_word == 'END' or target_text_len >= self.max_target_seq_length:
terminated = True
target_seq = np.zeros((1, 1, self.num_target_tokens))
target_seq[0, 0, sample_token_idx] = 1
states_value = [h, c]
return target_text.strip()
class Seq2SeqGloVeSummarizer(object):
model_name = 'seq2seq-glove'
def __init__(self, config):
self.max_input_seq_length = config['max_input_seq_length']
self.num_target_tokens = config['num_target_tokens']
self.max_target_seq_length = config['max_target_seq_length']
self.target_word2idx = config['target_word2idx']
self.target_idx2word = config['target_idx2word']
self.version = 0
if 'version' in config:
self.version = config['version']
self.word2em = dict()
if 'unknown_emb' in config:
self.unknown_emb = config['unknown_emb']
else:
self.unknown_emb = np.random.rand(1, GLOVE_EMBEDDING_SIZE)
config['unknown_emb'] = self.unknown_emb
self.config = config
encoder_inputs = Input(shape=(None, GLOVE_EMBEDDING_SIZE), name='encoder_inputs')
encoder_lstm = LSTM(units=HIDDEN_UNITS, return_state=True, name='encoder_lstm')
encoder_outputs, encoder_state_h, encoder_state_c = encoder_lstm(encoder_inputs)
encoder_states = [encoder_state_h, encoder_state_c]
decoder_inputs = Input(shape=(None, self.num_target_tokens), name='decoder_inputs')
decoder_lstm = LSTM(units=HIDDEN_UNITS, return_state=True, return_sequences=True, name='decoder_lstm')
decoder_outputs, decoder_state_h, decoder_state_c = decoder_lstm(decoder_inputs,
initial_state=encoder_states)
decoder_dense = Dense(units=self.num_target_tokens, activation='softmax', name='decoder_dense')
decoder_outputs = decoder_dense(decoder_outputs)
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
self.model = model
self.encoder_model = Model(encoder_inputs, encoder_states)
decoder_state_inputs = [Input(shape=(HIDDEN_UNITS,)), Input(shape=(HIDDEN_UNITS,))]
decoder_outputs, state_h, state_c = decoder_lstm(decoder_inputs, initial_state=decoder_state_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
self.decoder_model = Model([decoder_inputs] + decoder_state_inputs, [decoder_outputs] + decoder_states)
def load_weights(self, weight_file_path):
if os.path.exists(weight_file_path):
self.model.load_weights(weight_file_path)
def load_glove(self, data_dir_path):
self.word2em = load_glove(data_dir_path)
def transform_input_text(self, texts):
temp = []
for line in texts:
x = np.zeros(shape=(self.max_input_seq_length, GLOVE_EMBEDDING_SIZE))
for idx, word in enumerate(line.lower().split(' ')):
if idx >= self.max_input_seq_length:
break
emb = self.unknown_emb
if word in self.word2em:
emb = self.word2em[word]
x[idx, :] = emb
temp.append(x)
temp = pad_sequences(temp, maxlen=self.max_input_seq_length)
print(temp.shape)
return temp
def transform_target_encoding(self, texts):
temp = []
for line in texts:
x = []
line2 = 'START ' + line.lower() + ' END'
for word in line2.split(' '):
x.append(word)
if len(x) >= self.max_target_seq_length:
break
temp.append(x)
temp = np.array(temp)
print(temp.shape)
return temp
def generate_batch(self, x_samples, y_samples, batch_size):
num_batches = len(x_samples) // batch_size
while True:
for batchIdx in range(0, num_batches):
start = batchIdx * batch_size
end = (batchIdx + 1) * batch_size
encoder_input_data_batch = pad_sequences(x_samples[start:end], self.max_input_seq_length)
decoder_target_data_batch = np.zeros(shape=(batch_size, self.max_target_seq_length, self.num_target_tokens))
decoder_input_data_batch = np.zeros(shape=(batch_size, self.max_target_seq_length, self.num_target_tokens))
for lineIdx, target_words in enumerate(y_samples[start:end]):
for idx, w in enumerate(target_words):
w2idx = 0 # default [UNK]
if w in self.target_word2idx:
w2idx = self.target_word2idx[w]
if w2idx != 0:
decoder_input_data_batch[lineIdx, idx, w2idx] = 1
if idx > 0:
decoder_target_data_batch[lineIdx, idx - 1, w2idx] = 1
yield [encoder_input_data_batch, decoder_input_data_batch], decoder_target_data_batch
@staticmethod
def get_weight_file_path(model_dir_path):
return model_dir_path + '/' + Seq2SeqGloVeSummarizer.model_name + '-weights.h5'
@staticmethod
def get_config_file_path(model_dir_path):
return model_dir_path + '/' + Seq2SeqGloVeSummarizer.model_name + '-config.npy'
@staticmethod
def get_architecture_file_path(model_dir_path):
return model_dir_path + '/' + Seq2SeqGloVeSummarizer.model_name + '-architecture.json'
def fit(self, Xtrain, Ytrain, Xtest, Ytest, epochs=None, batch_size=None, model_dir_path=None):
if epochs is None:
epochs = DEFAULT_EPOCHS
if model_dir_path is None:
model_dir_path = './models'
if batch_size is None:
batch_size = DEFAULT_BATCH_SIZE
self.version += 1
self.config['version'] = self.version
config_file_path = Seq2SeqGloVeSummarizer.get_config_file_path(model_dir_path)
weight_file_path = Seq2SeqGloVeSummarizer.get_weight_file_path(model_dir_path)
checkpoint = ModelCheckpoint(weight_file_path)
np.save(config_file_path, self.config)
architecture_file_path = Seq2SeqGloVeSummarizer.get_architecture_file_path(model_dir_path)
open(architecture_file_path, 'w').write(self.model.to_json())
Ytrain = self.transform_target_encoding(Ytrain)
Ytest = self.transform_target_encoding(Ytest)
Xtrain = self.transform_input_text(Xtrain)
Xtest = self.transform_input_text(Xtest)
train_gen = self.generate_batch(Xtrain, Ytrain, batch_size)
test_gen = self.generate_batch(Xtest, Ytest, batch_size)
train_num_batches = len(Xtrain) // batch_size
test_num_batches = len(Xtest) // batch_size
history = self.model.fit_generator(generator=train_gen, steps_per_epoch=train_num_batches,
epochs=epochs,
verbose=VERBOSE, validation_data=test_gen, validation_steps=test_num_batches,
callbacks=[checkpoint])
self.model.save_weights(weight_file_path)
return history
def summarize(self, input_text):
input_seq = np.zeros(shape=(1, self.max_input_seq_length, GLOVE_EMBEDDING_SIZE))
for idx, word in enumerate(input_text.lower().split(' ')):
if idx >= self.max_input_seq_length:
break
emb = self.unknown_emb # default [UNK]
if word in self.word2em:
emb = self.word2em[word]
input_seq[0, idx, :] = emb
states_value = self.encoder_model.predict(input_seq)
target_seq = np.zeros((1, 1, self.num_target_tokens))
target_seq[0, 0, self.target_word2idx['START']] = 1
target_text = ''
target_text_len = 0
terminated = False
while not terminated:
output_tokens, h, c = self.decoder_model.predict([target_seq] + states_value)
sample_token_idx = np.argmax(output_tokens[0, -1, :])
sample_word = self.target_idx2word[sample_token_idx]
target_text_len += 1
if sample_word != 'START' and sample_word != 'END':
target_text += ' ' + sample_word
if sample_word == 'END' or target_text_len >= self.max_target_seq_length:
terminated = True
target_seq = np.zeros((1, 1, self.num_target_tokens))
target_seq[0, 0, sample_token_idx] = 1
states_value = [h, c]
return target_text.strip()
class Seq2SeqGloVeSummarizerV2(object):
model_name = 'seq2seq-glove-v2'
def __init__(self, config):
self.max_input_seq_length = config['max_input_seq_length']
self.num_target_tokens = config['num_target_tokens']
self.max_target_seq_length = config['max_target_seq_length']
self.target_word2idx = config['target_word2idx']
self.target_idx2word = config['target_idx2word']
self.version = 0
if 'version' in config:
self.version = config['version']
self.word2em = dict()
if 'unknown_emb' in config:
self.unknown_emb = config['unknown_emb']
else:
self.unknown_emb = np.random.rand(1, GLOVE_EMBEDDING_SIZE)
config['unknown_emb'] = self.unknown_emb
self.config = config
encoder_inputs = Input(shape=(None, GLOVE_EMBEDDING_SIZE), name='encoder_inputs')
encoder_lstm = LSTM(units=HIDDEN_UNITS, return_state=True, name='encoder_lstm')
encoder_outputs, encoder_state_h, encoder_state_c = encoder_lstm(encoder_inputs)
encoder_states = [encoder_state_h, encoder_state_c]
decoder_inputs = Input(shape=(None, GLOVE_EMBEDDING_SIZE), name='decoder_inputs')
decoder_lstm = LSTM(units=HIDDEN_UNITS, return_state=True, return_sequences=True, name='decoder_lstm')
decoder_outputs, decoder_state_h, decoder_state_c = decoder_lstm(decoder_inputs,
initial_state=encoder_states)
decoder_dense = Dense(units=self.num_target_tokens, activation='softmax', name='decoder_dense')
decoder_outputs = decoder_dense(decoder_outputs)
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
self.model = model
self.encoder_model = Model(encoder_inputs, encoder_states)
decoder_state_inputs = [Input(shape=(HIDDEN_UNITS,)), Input(shape=(HIDDEN_UNITS,))]
decoder_outputs, state_h, state_c = decoder_lstm(decoder_inputs, initial_state=decoder_state_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
self.decoder_model = Model([decoder_inputs] + decoder_state_inputs, [decoder_outputs] + decoder_states)
def load_weights(self, weight_file_path):
if os.path.exists(weight_file_path):
self.model.load_weights(weight_file_path)
def load_glove(self, data_dir_path):
self.word2em = load_glove(data_dir_path)
def transform_input_text(self, texts):
temp = []
for line in texts:
x = np.zeros(shape=(self.max_input_seq_length, GLOVE_EMBEDDING_SIZE))
for idx, word in enumerate(line.lower().split(' ')):
if idx >= self.max_input_seq_length:
break
emb = self.unknown_emb
if word in self.word2em:
emb = self.word2em[word]
x[idx, :] = emb
temp.append(x)
temp = pad_sequences(temp, maxlen=self.max_input_seq_length)
print(temp.shape)
return temp
def transform_target_encoding(self, texts):
temp = []
for line in texts:
x = []
line2 = 'start ' + line.lower() + ' end'
for word in line2.split(' '):
x.append(word)
if len(x) >= self.max_target_seq_length:
break
temp.append(x)
temp = np.array(temp)
print(temp.shape)
return temp
def generate_batch(self, x_samples, y_samples, batch_size):
num_batches = len(x_samples) // batch_size
while True:
for batchIdx in range(0, num_batches):
start = batchIdx * batch_size
end = (batchIdx + 1) * batch_size
encoder_input_data_batch = pad_sequences(x_samples[start:end], self.max_input_seq_length)
decoder_target_data_batch = np.zeros(shape=(batch_size, self.max_target_seq_length, self.num_target_tokens))
decoder_input_data_batch = np.zeros(shape=(batch_size, self.max_target_seq_length, GLOVE_EMBEDDING_SIZE))
for lineIdx, target_words in enumerate(y_samples[start:end]):
for idx, w in enumerate(target_words):
w2idx = 0 # default [UNK]
if w in self.word2em:
emb = self.unknown_emb
decoder_input_data_batch[lineIdx, idx, :] = emb
if w in self.target_word2idx:
w2idx = self.target_word2idx[w]
if w2idx != 0:
if idx > 0:
decoder_target_data_batch[lineIdx, idx - 1, w2idx] = 1
yield [encoder_input_data_batch, decoder_input_data_batch], decoder_target_data_batch
@staticmethod
def get_weight_file_path(model_dir_path):
return model_dir_path + '/' + Seq2SeqGloVeSummarizerV2.model_name + '-weights.h5'
@staticmethod
def get_config_file_path(model_dir_path):
return model_dir_path + '/' + Seq2SeqGloVeSummarizerV2.model_name + '-config.npy'
@staticmethod
def get_architecture_file_path(model_dir_path):
return model_dir_path + '/' + Seq2SeqGloVeSummarizerV2.model_name + '-architecture.json'
def fit(self, Xtrain, Ytrain, Xtest, Ytest, epochs=None, batch_size=None, model_dir_path=None):
if epochs is None:
epochs = DEFAULT_EPOCHS
if model_dir_path is None:
model_dir_path = './models'
if batch_size is None:
batch_size = DEFAULT_BATCH_SIZE
self.version += 1
self.config['version'] = self.version
config_file_path = Seq2SeqGloVeSummarizerV2.get_config_file_path(model_dir_path)
weight_file_path = Seq2SeqGloVeSummarizerV2.get_weight_file_path(model_dir_path)
checkpoint = ModelCheckpoint(weight_file_path)
np.save(config_file_path, self.config)
architecture_file_path = Seq2SeqGloVeSummarizerV2.get_architecture_file_path(model_dir_path)
open(architecture_file_path, 'w').write(self.model.to_json())
Ytrain = self.transform_target_encoding(Ytrain)
Ytest = self.transform_target_encoding(Ytest)
Xtrain = self.transform_input_text(Xtrain)
Xtest = self.transform_input_text(Xtest)
train_gen = self.generate_batch(Xtrain, Ytrain, batch_size)
test_gen = self.generate_batch(Xtest, Ytest, batch_size)
train_num_batches = len(Xtrain) // batch_size
test_num_batches = len(Xtest) // batch_size
history = self.model.fit_generator(generator=train_gen, steps_per_epoch=train_num_batches,
epochs=epochs,
verbose=VERBOSE, validation_data=test_gen, validation_steps=test_num_batches,
callbacks=[checkpoint])
self.model.save_weights(weight_file_path)
return history
def summarize(self, input_text):
input_seq = np.zeros(shape=(1, self.max_input_seq_length, GLOVE_EMBEDDING_SIZE))
for idx, word in enumerate(input_text.lower().split(' ')):
if idx >= self.max_input_seq_length:
break
emb = self.unknown_emb # default [UNK]
if word in self.word2em:
emb = self.word2em[word]
input_seq[0, idx, :] = emb
states_value = self.encoder_model.predict(input_seq)
target_seq = np.zeros((1, 1, GLOVE_EMBEDDING_SIZE))
target_seq[0, 0, :] = self.word2em['start']
target_text = ''
target_text_len = 0
terminated = False
while not terminated:
output_tokens, h, c = self.decoder_model.predict([target_seq] + states_value)
sample_token_idx = np.argmax(output_tokens[0, -1, :])
sample_word = self.target_idx2word[sample_token_idx]
target_text_len += 1
if sample_word != 'start' and sample_word != 'end':
target_text += ' ' + sample_word
if sample_word == 'end' or target_text_len >= self.max_target_seq_length:
terminated = True
if sample_word in self.word2em:
target_seq[0, 0, :] = self.word2em[sample_word]
else:
target_seq[0, 0, :] = self.unknown_emb
states_value = [h, c]
return target_text.strip()