计算交叉熵损失时的Tensorflow误差

时间:2016-02-13 16:35:57

标签: neural-network tensorflow conv-neural-network

我收到以下错误

ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int32: 'Tensor("Placeholder_1:0", shape=TensorShape([Dimension(128), Dimension(2)]), dtype=int32)'

当我尝试计算交叉熵损失时

losses = tf.nn.softmax_cross_entropy_with_logits(scores, input_y)

我使用Python 3.4.3。

任何想法为什么?

2 个答案:

答案 0 :(得分:21)

听起来你定义了input_y - 我假设它是tf.placeholder() - 因为它有tf.int32类型。将此更改为tf.float32或添加casttf.cast(input_y, tf.float32)tf.to_float(input_y)

答案 1 :(得分:1)

我将代码中的 %tensorflow_version 1.x 替换为 !pip install tensorflow==1.15.5,错误消失了。不仅限于 1.15.5 版本,它也适用于其他一些 TensorFlow 版本 1。

相关问题