-
Notifications
You must be signed in to change notification settings - Fork 1
/
seq2seq_model.py
650 lines (522 loc) · 31.9 KB
/
seq2seq_model.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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# coding=utf-8
"""Copy from https://github.com/JayParks/tf-seq2seq, with minimum modification"""
import math
import numpy as np
import tensorflow as tf
import tensorflow.contrib.seq2seq as seq2seq
from tensorflow.python.ops.rnn_cell import GRUCell
from tensorflow.python.ops.rnn_cell import LSTMCell
from tensorflow.python.ops.rnn_cell import MultiRNNCell
from tensorflow.python.ops.rnn_cell import DropoutWrapper, ResidualWrapper
# for bidictional rnn
from tensorflow.contrib import rnn
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.layers.core import Dense
from tensorflow.python.util import nest
from tensorflow.contrib.seq2seq.python.ops import attention_wrapper
from tensorflow.contrib.seq2seq.python.ops import beam_search_decoder
import tool.data_utils as data_utils
class Seq2SeqModel(object):
def __init__(self, config, mode):
assert mode.lower() in ['train', 'decode']
self.config = config
self.mode = mode.lower()
# cell
self.cell_type = config.cell_type
self.hidden_units = config.hidden_units
# encoder & decoder
self.depth = config.depth
self.attention_type = config.attention_type
self.embedding_size = config.embedding_size
self.bidirectional = config.bidirectional
# hidden_units for encoder and decoder
self.encoder_hidden_units = self.hidden_units
self.decoder_hidden_units = 2 * self.hidden_units if self.bidirectional else self.hidden_units
# vocabulary size
self.num_encoder_symbols = config.num_encoder_symbols
self.num_decoder_symbols = config.num_decoder_symbols
# hyper-parameters
self.use_residual = config.use_residual
self.attn_input_feeding = config.attn_input_feeding
self.use_dropout = config.use_dropout
self.keep_prob = 1.0 - config.dropout_rate
# data type
self.dtype = tf.float16 if config.use_fp16 else tf.float32
# others
self.global_step = tf.Variable(0, trainable=False, name='global_step')
self.global_epoch_step = tf.Variable(0, trainable=False, name='global_epoch_step')
self.global_epoch_step_op = \
tf.assign(self.global_epoch_step, self.global_epoch_step + 1)
self.keep_prob_placeholder = tf.placeholder(self.dtype, shape=[], name='keep_prob')
# optimizer
self.optimizer = config.optimizer
self.max_gradient_norm = config.max_gradient_norm
# learning rate
if self.optimizer.lower() == 'sgd':
self.learning_rate = tf.cond(
self.global_step < config.start_decay_step,
lambda: tf.constant(config.learning_rate),
lambda: tf.train.exponential_decay(
config.learning_rate,
(self.global_step - config.start_decay_step),
config.decay_steps,
config.decay_factor,
staircase=True),
name="learning_rate")
# tf.summary.scalar("lr", self.learning_rate)
elif self.optimizer.lower() == 'adam':
self.learning_rate = tf.cond(
self.global_step < config.start_decay_step,
lambda: tf.constant(config.learning_rate),
lambda: tf.train.inverse_time_decay(
config.learning_rate,
self.global_step,
config.decay_steps,
config.decay_factor,
staircase=True),
name='learning_rate')
else:
self.learning_rate = config.learning_rate
tf.summary.scalar("lr", self.learning_rate)
# beam search config
self.use_beamsearch_decode=False
if self.mode == 'decode':
self.beam_width = config.beam_width
self.use_beamsearch_decode = True if self.beam_width > 1 else False
self.max_decode_step = config.max_decode_step
# build model
self.build_model()
def build_model(self):
print("building model..")
# Building encoder and decoder networks
self.init_placeholders()
self.build_encoder()
self.build_decoder()
# Summaries and Merge all the training summaries
self.summary_op = tf.summary.merge_all()
def init_placeholders(self):
# encoder_inputs: [batch_size, max_time_steps]
self.encoder_inputs = tf.placeholder(dtype=tf.int32,
shape=(None, None), name='encoder_inputs')
# encoder_inputs_length: [batch_size]
self.encoder_inputs_length = tf.placeholder(
dtype=tf.int32, shape=(None,), name='encoder_inputs_length')
# get dynamic batch_size
self.batch_size = tf.shape(self.encoder_inputs)[0]
if self.mode == 'train':
# decoder_inputs: [batch_size, max_time_steps]
self.decoder_inputs = tf.placeholder(
dtype=tf.int32, shape=(None, None), name='decoder_inputs')
# decoder_inputs_length: [max_time_steps]
self.decoder_inputs_length = tf.placeholder(
dtype=tf.int32, shape=(None,), name='decoder_inputs_length')
decoder_start_token = tf.ones(
shape=[self.batch_size, 1], dtype=tf.int32) * data_utils.START_TOKEN
decoder_end_token = tf.ones(
shape=[self.batch_size, 1], dtype=tf.int32) * data_utils.END_TOKEN
# decoder_inputs_train: [batch_size , max_time_steps + 1]
# insert _GO symbol in front of each decoder input
self.decoder_inputs_train = tf.concat([decoder_start_token,
self.decoder_inputs], axis=1)
# decoder_inputs_length_train: [max_time_steps+1]
self.decoder_inputs_length_train = self.decoder_inputs_length + 1
# decoder_targets_train: [batch_size, max_time_steps + 1]
# insert EOS symbol at the end of each decoder input
self.decoder_targets_train = tf.concat([self.decoder_inputs,
decoder_end_token], axis=1)
def build_encoder(self):
print("building encoder..")
with tf.variable_scope('encoder'):
# Building encoder_cell
self.encoder_cell = self.build_encoder_cell()
# Initialize encoder_embeddings to have variance=1.
sqrt3 = math.sqrt(3) # Uniform(-sqrt(3), sqrt(3)) has variance=1.
initializer = tf.random_uniform_initializer(-sqrt3, sqrt3, dtype=self.dtype)
self.encoder_embeddings = tf.get_variable(name='embedding',
shape=[self.num_encoder_symbols, self.embedding_size],
initializer=initializer, dtype=self.dtype)
# Embedded_inputs: [batch_size, time_step, embedding_size]
self.encoder_inputs_embedded = tf.nn.embedding_lookup(
params=self.encoder_embeddings, ids=self.encoder_inputs)
# Input projection layer to feed embedded inputs to the cell
# ** Essential when use_residual=True to match input/output dims
input_layer = Dense(self.encoder_hidden_units, dtype=self.dtype, name='input_projection')
# Embedded inputs having gone through input projection layer
self.encoder_inputs_embedded = input_layer(self.encoder_inputs_embedded)
# Encode input sequences into context vectors:
# encoder_outputs: [batch_size, max_time_step, cell_output_size]
# encoder_state: [batch_size, cell_output_size]
if self.bidirectional:
with tf.variable_scope("BidirectionalEncoder"):
((encoder_fw_outputs,
encoder_bw_outputs),
(encoder_fw_state,
encoder_bw_state)) = (
tf.nn.bidirectional_dynamic_rnn(
# cell_fw=self.encoder_cell[0] if isinstance(self.encoder_cell, tuple) else self.encoder_cell,
# cell_bw=self.encoder_cell[1] if isinstance(self.encoder_cell, tuple) else self.encoder_cell,
cell_fw=self.encoder_cell,
cell_bw=self.encoder_cell,
inputs=self.encoder_inputs_embedded,
sequence_length=self.encoder_inputs_length,
time_major=False,
dtype=self.dtype))
self.outputs = tf.concat(
(encoder_fw_outputs, encoder_bw_outputs), 2,
name="bidirectional_output_concat")
# output = tf.concat([output_fw, output_bw], axis=-1)
if isinstance(encoder_fw_state, rnn.LSTMStateTuple): # LstmCell
state_c = tf.concat(
(encoder_fw_state.c, encoder_bw_state.c), 1, name="bidirectional_concat_c")
state_h = tf.concat(
(encoder_fw_state.h, encoder_bw_state.h), 1, name="bidirectional_concat_h")
self.state = rnn.LSTMStateTuple(c=state_c, h=state_h)
elif isinstance(encoder_fw_state, tuple) \
and isinstance(encoder_fw_state[0], rnn.LSTMStateTuple): # MultiLstmCell
self.state = tuple(map(
lambda fw_state, bw_state: rnn.LSTMStateTuple(
c=tf.concat((fw_state.c, bw_state.c), 1,
name="bidirectional_concat_c"),
h=tf.concat((fw_state.h, bw_state.h), 1,
name="bidirectional_concat_h")),
encoder_fw_state, encoder_bw_state))
else:
self.state = tf.concat(
(encoder_fw_state, encoder_bw_state), 1,
name="bidirectional_state_concat")
self.encoder_outputs, self.encoder_last_state = self.outputs, self.state
else:
self.encoder_outputs, self.encoder_last_state = tf.nn.dynamic_rnn(
cell=self.encoder_cell, inputs=self.encoder_inputs_embedded,
sequence_length=self.encoder_inputs_length, dtype=self.dtype,
time_major=False)
def build_decoder(self):
print("building decoder and attention..")
with tf.variable_scope('decoder'):
# Building decoder_cell and decoder_initial_state
self.decoder_cell, self.decoder_initial_state = self.build_decoder_cell()
# Initialize decoder embeddings to have variance=1.
sqrt3 = math.sqrt(3) # Uniform(-sqrt(3), sqrt(3)) has variance=1.
initializer = tf.random_uniform_initializer(-sqrt3, sqrt3, dtype=self.dtype)
self.decoder_embeddings = tf.get_variable(name='embedding',
shape=[self.num_decoder_symbols, self.embedding_size],
initializer=initializer, dtype=self.dtype)
# Input projection layer to feed embedded inputs to the cell
# ** Essential when use_residual=True to match input/output dims
input_layer = Dense(self.decoder_hidden_units, dtype=self.dtype, name='input_projection')
# Output projection layer to convert cell_outputs to logits
output_layer = Dense(self.num_decoder_symbols, name='output_projection')
if self.mode == 'train':
# decoder_inputs_embedded: [batch_size, max_time_step + 1, embedding_size]
self.decoder_inputs_embedded = tf.nn.embedding_lookup(
params=self.decoder_embeddings, ids=self.decoder_inputs_train)
# Embedded inputs having gone through input projection layer
self.decoder_inputs_embedded = input_layer(self.decoder_inputs_embedded)
# Helper to feed inputs for training: read inputs from dense ground truth vectors
training_helper = seq2seq.TrainingHelper(inputs=self.decoder_inputs_embedded,
sequence_length=self.decoder_inputs_length_train,
time_major=False,
name='training_helper')
training_decoder = seq2seq.BasicDecoder(cell=self.decoder_cell,
helper=training_helper,
initial_state=self.decoder_initial_state,
output_layer=output_layer)
#output_layer=None)
# Maximum decoder time_steps in current batch
max_decoder_length = tf.reduce_max(self.decoder_inputs_length_train)
# decoder_outputs_train: BasicDecoderOutput
# namedtuple(rnn_outputs, sample_id)
# decoder_outputs_train.rnn_output: [batch_size, max_time_step + 1, num_decoder_symbols] if output_time_major=False
# [max_time_step + 1, batch_size, num_decoder_symbols] if output_time_major=True
# decoder_outputs_train.sample_id: [batch_size], tf.int32
(self.decoder_outputs_train, self.decoder_last_state_train,
self.decoder_outputs_length_train) = (seq2seq.dynamic_decode(
decoder=training_decoder,
output_time_major=False,
impute_finished=True,
maximum_iterations=max_decoder_length))
# More efficient to do the projection on the batch-time-concatenated tensor
# logits_train: [batch_size, max_time_step + 1, num_decoder_symbols]
# self.decoder_logits_train = output_layer(self.decoder_outputs_train.rnn_output)
self.decoder_logits_train = tf.identity(self.decoder_outputs_train.rnn_output)
# Use argmax to extract decoder symbols to emit
self.decoder_pred_train = tf.argmax(self.decoder_logits_train, axis=-1,
name='decoder_pred_train')
# masks: masking for valid and padded time steps, [batch_size, max_time_step + 1]
masks = tf.sequence_mask(lengths=self.decoder_inputs_length_train,
maxlen=max_decoder_length, dtype=self.dtype, name='masks')
# Computes per word average cross-entropy over a batch
# Internally calls 'nn_ops.sparse_softmax_cross_entropy_with_logits' by default
self.loss = seq2seq.sequence_loss(logits=self.decoder_logits_train,
targets=self.decoder_targets_train,
weights=masks,
average_across_timesteps=True,
average_across_batch=True,)
tf.summary.scalar('loss', self.loss)
# Contruct graphs for minimizing loss
self.init_optimizer()
elif self.mode == 'decode':
# Start_tokens: [batch_size,] `int32` vector
start_tokens = tf.ones([self.batch_size,], tf.int32) * data_utils.START_TOKEN
end_token = data_utils.END_TOKEN
def embed_and_input_proj(inputs):
return input_layer(tf.nn.embedding_lookup(self.decoder_embeddings, inputs))
if not self.use_beamsearch_decode:
# Helper to feed inputs for greedy decoding: uses the argmax of the output
decoding_helper = seq2seq.GreedyEmbeddingHelper(start_tokens=start_tokens,
end_token=end_token,
embedding=embed_and_input_proj)
# Basic decoder performs greedy decoding at each time step
print("building greedy decoder..")
inference_decoder = seq2seq.BasicDecoder(cell=self.decoder_cell,
helper=decoding_helper,
initial_state=self.decoder_initial_state,
output_layer=output_layer)
else:
# Beamsearch is used to approximately find the most likely translation
print("building beamsearch decoder..")
inference_decoder = beam_search_decoder.BeamSearchDecoder(cell=self.decoder_cell,
embedding=embed_and_input_proj,
start_tokens=start_tokens,
end_token=end_token,
initial_state=self.decoder_initial_state,
beam_width=self.beam_width,
output_layer=output_layer,)
# For GreedyDecoder, return
# decoder_outputs_decode: BasicDecoderOutput instance
# namedtuple(rnn_outputs, sample_id)
# decoder_outputs_decode.rnn_output: [batch_size, max_time_step, num_decoder_symbols] if output_time_major=False
# [max_time_step, batch_size, num_decoder_symbols] if output_time_major=True
# decoder_outputs_decode.sample_id: [batch_size, max_time_step], tf.int32 if output_time_major=False
# [max_time_step, batch_size], tf.int32 if output_time_major=True
# For BeamSearchDecoder, return
# decoder_outputs_decode: FinalBeamSearchDecoderOutput instance
# namedtuple(predicted_ids, beam_search_decoder_output)
# decoder_outputs_decode.predicted_ids: [batch_size, max_time_step, beam_width] if output_time_major=False
# [max_time_step, batch_size, beam_width] if output_time_major=True
# decoder_outputs_decode.beam_search_decoder_output: BeamSearchDecoderOutput instance
# namedtuple(scores, predicted_ids, parent_ids)
(self.decoder_outputs_decode, self.decoder_last_state_decode,
self.decoder_outputs_length_decode) = (seq2seq.dynamic_decode(
decoder=inference_decoder,
output_time_major=False,
#impute_finished=True, # error occurs
maximum_iterations=self.max_decode_step))
if not self.use_beamsearch_decode:
# decoder_outputs_decode.sample_id: [batch_size, max_time_step]
# Or use argmax to find decoder symbols to emit:
# self.decoder_pred_decode = tf.argmax(self.decoder_outputs_decode.rnn_output,
# axis=-1, name='decoder_pred_decode')
# Here, we use expand_dims to be compatible with the result of the beamsearch decoder
# decoder_pred_decode: [batch_size, max_time_step, 1] (output_major=False)
self.decoder_pred_decode = tf.expand_dims(self.decoder_outputs_decode.sample_id, -1)
else:
# Use beam search to approximately find the most likely translation
# decoder_pred_decode: [batch_size, max_time_step, beam_width] (output_major=False)
self.decoder_pred_decode = self.decoder_outputs_decode.predicted_ids
def build_single_cell(self, hidden_units):
cell_type = LSTMCell
if (self.cell_type.lower() == 'gru'):
cell_type = GRUCell
cell = cell_type(hidden_units)
if self.use_dropout:
cell = DropoutWrapper(cell, dtype=self.dtype,
output_keep_prob=self.keep_prob_placeholder,)
if self.use_residual:
cell = ResidualWrapper(cell)
return cell
# Building encoder cell
def build_encoder_cell (self):
return MultiRNNCell([self.build_single_cell(self.encoder_hidden_units) for i in range(self.depth)])
# Building decoder cell and attention. Also returns decoder_initial_state
def build_decoder_cell(self):
encoder_outputs = self.encoder_outputs
encoder_last_state = self.encoder_last_state
encoder_inputs_length = self.encoder_inputs_length
# To use BeamSearchDecoder, encoder_outputs, encoder_last_state, encoder_inputs_length
# needs to be tiled so that: [batch_size, .., ..] -> [batch_size x beam_width, .., ..]
if self.use_beamsearch_decode:
print ("use beamsearch decoding..")
encoder_outputs = seq2seq.tile_batch(
self.encoder_outputs, multiplier=self.beam_width)
encoder_last_state = nest.map_structure(
lambda s: seq2seq.tile_batch(s, self.beam_width), self.encoder_last_state)
encoder_inputs_length = seq2seq.tile_batch(
self.encoder_inputs_length, multiplier=self.beam_width)
# Building attention mechanism: Default Bahdanau
# 'Bahdanau' style attention: https://arxiv.org/abs/1409.0473
self.attention_mechanism = attention_wrapper.BahdanauAttention(
num_units=self.decoder_hidden_units, memory=encoder_outputs,
memory_sequence_length=encoder_inputs_length,)
# 'Luong' style attention: https://arxiv.org/abs/1508.04025
if self.attention_type.lower() == 'luong':
self.attention_mechanism = attention_wrapper.LuongAttention(
num_units=self.decoder_hidden_units, memory=encoder_outputs,
memory_sequence_length=encoder_inputs_length,)
# Building decoder_cell
self.decoder_cell_list = [
self.build_single_cell(self.decoder_hidden_units) for i in range(self.depth)]
decoder_initial_state = encoder_last_state
def attn_decoder_input_fn(inputs, attention):
if not self.attn_input_feeding:
return inputs
# Essential when use_residual=True
_input_layer = Dense(self.decoder_hidden_units, dtype=self.dtype,
name='attn_input_feeding')
return _input_layer(array_ops.concat([inputs, attention], -1))
# AttentionWrapper wraps RNNCell with the attention_mechanism
# Note: We implement Attention mechanism only on the top decoder layer
self.decoder_cell_list[-1] = attention_wrapper.AttentionWrapper(
cell=self.decoder_cell_list[-1],
attention_mechanism=self.attention_mechanism,
attention_layer_size=self.decoder_hidden_units,
cell_input_fn=attn_decoder_input_fn,
initial_cell_state=encoder_last_state[-1],
alignment_history=False,
name='Attention_Wrapper')
# To be compatible with AttentionWrapper, the encoder last state
# of the top layer should be converted into the AttentionWrapperState form
# We can easily do this by calling AttentionWrapper.zero_state
# Also if beamsearch decoding is used, the batch_size argument in .zero_state
# should be ${decoder_beam_width} times to the origianl batch_size
batch_size = self.batch_size if not self.use_beamsearch_decode \
else self.batch_size * self.beam_width
initial_state = [state for state in encoder_last_state]
initial_state[-1] = self.decoder_cell_list[-1].zero_state(
batch_size=batch_size, dtype=self.dtype)
decoder_initial_state = tuple(initial_state)
return MultiRNNCell(self.decoder_cell_list), decoder_initial_state
def init_optimizer(self):
print("setting optimizer..")
# Gradients and SGD update operation for training the model
trainable_params = tf.trainable_variables()
if self.optimizer.lower() == 'adadelta':
self.opt = tf.train.AdadeltaOptimizer(learning_rate=self.learning_rate)
elif self.optimizer.lower() == 'adam':
self.opt = tf.train.AdamOptimizer(learning_rate=self.learning_rate)
elif self.optimizer.lower() == 'rmsprop':
self.opt = tf.train.RMSPropOptimizer(learning_rate=self.learning_rate)
else:
self.opt = tf.train.GradientDescentOptimizer(learning_rate=self.learning_rate)
# Compute gradients of loss w.r.t. all trainable variables
gradients = tf.gradients(self.loss, trainable_params)
# Clip gradients by a given maximum_gradient_norm
clip_gradients, _ = tf.clip_by_global_norm(gradients, self.max_gradient_norm)
# Update the model
self.updates = self.opt.apply_gradients(
zip(clip_gradients, trainable_params), global_step=self.global_step)
def save(self, sess, path, var_list=None, global_step=None):
# var_list = None returns the list of all saveable variables
saver = tf.train.Saver(var_list)
# temporary code
#del tf.get_collection_ref('LAYER_NAME_UIDS')[0]
save_path = saver.save(sess, save_path=path, global_step=global_step)
print('model saved at %s' % save_path)
def restore(self, sess, path, var_list=None):
# var_list = None returns the list of all saveable variables
saver = tf.train.Saver(var_list)
saver.restore(sess, save_path=path)
print('model restored from %s' % path)
def train(self, sess, encoder_inputs, encoder_inputs_length,
decoder_inputs, decoder_inputs_length):
"""Run a train step of the model feeding the given inputs.
Args:
session: tensorflow session to use.
encoder_inputs: a numpy int matrix of [batch_size, max_source_time_steps]
to feed as encoder inputs
encoder_inputs_length: a numpy int vector of [batch_size]
to feed as sequence lengths for each element in the given batch
decoder_inputs: a numpy int matrix of [batch_size, max_target_time_steps]
to feed as decoder inputs
decoder_inputs_length: a numpy int vector of [batch_size]
to feed as sequence lengths for each element in the given batch
Returns:
A triple consisting of gradient norm (or None if we did not do backward),
average perplexity, and the outputs.
"""
# Check if the model is 'training' mode
if self.mode.lower() != 'train':
raise ValueError("train step can only be operated in train mode")
input_feed = self.check_feeds(encoder_inputs, encoder_inputs_length,
decoder_inputs, decoder_inputs_length, False)
# Input feeds for dropout
input_feed[self.keep_prob_placeholder.name] = self.keep_prob
output_feed = [self.updates, # Update Op that does optimization
self.loss, # Loss for current batch
self.summary_op] # Training summary
outputs = sess.run(output_feed, input_feed)
return outputs[1], outputs[2] # loss, summary
def eval(self, sess, encoder_inputs, encoder_inputs_length,
decoder_inputs, decoder_inputs_length):
"""Run a evaluation step of the model feeding the given inputs.
Args:
session: tensorflow session to use.
encoder_inputs: a numpy int matrix of [batch_size, max_source_time_steps]
to feed as encoder inputs
encoder_inputs_length: a numpy int vector of [batch_size]
to feed as sequence lengths for each element in the given batch
decoder_inputs: a numpy int matrix of [batch_size, max_target_time_steps]
to feed as decoder inputs
decoder_inputs_length: a numpy int vector of [batch_size]
to feed as sequence lengths for each element in the given batch
Returns:
A triple consisting of gradient norm (or None if we did not do backward),
average perplexity, and the outputs.
"""
input_feed = self.check_feeds(encoder_inputs, encoder_inputs_length,
decoder_inputs, decoder_inputs_length, False)
# Input feeds for dropout
input_feed[self.keep_prob_placeholder.name] = 1.0
output_feed = [self.loss, # Loss for current batch
self.summary_op] # Training summary
outputs = sess.run(output_feed, input_feed)
return outputs[0], outputs[1] # loss
def predict(self, sess, encoder_inputs, encoder_inputs_length):
input_feed = self.check_feeds(encoder_inputs, encoder_inputs_length,
decoder_inputs=None, decoder_inputs_length=None,
decode=True)
# Input feeds for dropout
input_feed[self.keep_prob_placeholder.name] = 1.0
output_feed = [self.decoder_pred_decode]
outputs = sess.run(output_feed, input_feed)
# GreedyDecoder: [batch_size, max_time_step]
return outputs[0] # BeamSearchDecoder: [batch_size, max_time_step, beam_width]
def check_feeds(self, encoder_inputs, encoder_inputs_length,
decoder_inputs, decoder_inputs_length, decode):
"""
Args:
encoder_inputs: a numpy int matrix of [batch_size, max_source_time_steps]
to feed as encoder inputs
encoder_inputs_length: a numpy int vector of [batch_size]
to feed as sequence lengths for each element in the given batch
decoder_inputs: a numpy int matrix of [batch_size, max_target_time_steps]
to feed as decoder inputs
decoder_inputs_length: a numpy int vector of [batch_size]
to feed as sequence lengths for each element in the given batch
decode: a scalar boolean that indicates decode mode
Returns:
A feed for the model that consists of encoder_inputs, encoder_inputs_length,
decoder_inputs, decoder_inputs_length
"""
input_batch_size = encoder_inputs.shape[0]
if input_batch_size != encoder_inputs_length.shape[0]:
raise ValueError("Encoder inputs and their lengths must be equal in their "
"batch_size, %d != %d" % (input_batch_size, encoder_inputs_length.shape[0]))
if not decode:
target_batch_size = decoder_inputs.shape[0]
if target_batch_size != input_batch_size:
raise ValueError("Encoder inputs and Decoder inputs must be equal in their "
"batch_size, %d != %d" % (input_batch_size, target_batch_size))
if target_batch_size != decoder_inputs_length.shape[0]:
raise ValueError("Decoder targets and their lengths must be equal in their "
"batch_size, %d != %d" % (target_batch_size, decoder_inputs_length.shape[0]))
input_feed = {}
input_feed[self.encoder_inputs.name] = encoder_inputs
input_feed[self.encoder_inputs_length.name] = encoder_inputs_length
if not decode:
input_feed[self.decoder_inputs.name] = decoder_inputs
input_feed[self.decoder_inputs_length.name] = decoder_inputs_length
return input_feed