1

I have trained a sequence to sequence model using tensorflow. However, I am unable to make predictions on a single sequence using The Greedy Embedding Helper.

Here is a part of the graph for reference :

training_helper = tf.contrib.seq2seq.TrainingHelper(decoder_embeddings,decoder_lengths,time_major=True)
start_tokens = tf.fill([1], word_to_index['<go>'])
inference_helper = tf.contrib.seq2seq.GreedyEmbeddingHelper(embeddings,start_tokens,word_to_index['<eos>'])


def decode(helper,scope,reuse=None):

     decoder = tf.contrib.seq2seq.BasicDecoder(decoder_cell,helper,encoder_final_state,output_layer=projection_layer)



     final_outputs,final_state,final_sequence_lengths = tf.contrib.seq2seq.dynamic_decode(decoder,impute_finished=True,output_time_major=False)




     return final_outputs



training_outputs = decode(training_helper,'decode')
infer_outputs = decode(inference_helper,
                   'decode',reuse=True)

logits = training_outputs.rnn_output

predictions = infer_outputs.sample_id

predictions_ = tf.identity(predictions,name="predicitions")

Training works fine using any batch size. Problem comes during inference, I saved my model and tried to make a prediction on a sequence where I get the following error.

Traceback (most recent call last):
File "/home/justdial/Codes/himanshu/tensorflow_practice/Chatbot/chat2.py", line 131, in <module>
'decode',reuse=True)
File "/home/justdial/Codes/himanshu/tensorflow_practice/Chatbot/chat2.py", line 120, in decode
final_outputs,final_state,final_sequence_lengths = tf.contrib.seq2seq.dynamic_decode(decoder,impute_finished=True,output_time_major=False)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/seq2seq/python/ops/decoder.py", line 304, in dynamic_decode
swap_memory=swap_memory)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 3224, in while_loop
result = loop_context.BuildLoop(cond, body, loop_vars, shape_invariants)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 2956, in BuildLoop
pred, body, original_loop_vars, loop_vars, shape_invariants)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 2930, in _BuildLoop
next_vars.append(_AddNextAndBackEdge(m, v))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 688, in _AddNextAndBackEdge
_EnforceShapeInvariant(m, v)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 632, in _EnforceShapeInvariant
(merge_var.name, m_shape, n_shape))
ValueError: The shape for decoder_1/while/Merge_5:0 is not an invariant for the loop. It enters the loop with shape (1, 15), but has shape (?, 15) after one iteration. Provide shape invariants using either the `shape_invariants` argument of tf.while_loop or set_shape() on the loop variables

It works fine if I use any other batch size but obviously while making inference I would want to be able to make predictions on a single sequence rather than feeding a batch at once. Can someone tell me if this is a bug in the tensorflow implementation or am I doing something wrong? It would also be helpful if someone can suggest a way of making this work for a single sequence.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.