1

我已经使用 tensorflow 训练了一个序列到序列模型。但是,我无法使用 The Greedy Embedding Helper 对单个序列进行预测。

这是图表的一部分供参考:

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")

使用任何批量大小的训练都可以正常工作。在推理过程中出现问题,我保存了我的模型并尝试对出现以下错误的序列进行预测。

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

如果我使用任何其他批量大小,它工作得很好,但显然在进行推理时,我希望能够对单个序列进行预测,而不是一次喂一个批次。有人可以告诉我这是 tensorflow 实现中的错误还是我做错了什么?如果有人可以建议一种方法使这项工作适用于单个序列,那也会很有帮助。

4

0 回答 0