我正在尝试从github上当前主分支安装tensorflow 0.12.1来运行翻译教程(https://github.com/tensorflow/models/tree/master/tutorials/rnn/translate)。
我在这里对Kangmo建议的seq2seq_model.py进行了更改(https://github.com/tensorflow/models/issues/853)但是我遇到了不同的错误。
Creating 3 layers of 1024 units.
Traceback (most recent call last):
File "translate.py", line 317, in <module>
tf.app.run()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 44, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "translate.py", line 314, in main
train()
File "translate.py", line 173, in train
model = create_model(sess, False)
File "translate.py", line 131, in create_model
dtype=dtype)
File "/home/TFRun/seq2seq_model.py", line 171, in __init__
softmax_loss_function=softmax_loss_function)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/legacy_seq2seq/python/ops/seq2seq.py", line 1195, in model_with_buckets
softmax_loss_function=softmax_loss_function))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/legacy_seq2seq/python/ops/seq2seq.py", line 1110, in sequence_loss
softmax_loss_function=softmax_loss_function))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/legacy_seq2seq/python/ops/seq2seq.py", line 1067, in sequence_loss_by_example
crossent = softmax_loss_function(target, logit)
File "/home/TFRun/seq2seq_model.py", line 111, in sampled_loss
num_samples, self.target_vocab_size),
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/nn_impl.py", line 1191, in sampled_softmax_loss
name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/nn_impl.py", line 974, in _compute_sampled_logits
array_ops.reshape(true_w, new_true_w_shape))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 357, in multiply
return gen_math_ops._mul(x, y, name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 1625, in _mul
result = _op_def_lib.apply_op("Mul", x=x, y=y, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 522, in apply_op
inferred_from[input_arg.type_attr]))
TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int32 of argument 'x'.
有没有人知道解决此错误的方法?
答案 0 :(得分:0)
您需要将x张量转换为float32,因此请在seq2seq_model.py中使用以下代码:
def sampled_loss(inputs,labels):
inputs= tf.cast(inputs, tf.float32)
labels = tf.reshape(labels, [-1, 1])
return tf.cast(tf.nn.sampled_softmax_loss(w_t, b, labels, inputs,num_samples,
self.target_vocab_size),tf.float32)
您还需要替换此行
crossent = softmax_loss_function(target, logit)
带
crossent = softmax_loss_function(logit, target)