Neural network ValueError:对于dtype为int32的Tensor,Tensor转换请求了dtype float32

Neural network ValueError:对于dtype为int32的Tensor,Tensor转换请求了dtype float32,neural-network,tensorflow,conv-neural-network,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(scor

我得到以下错误

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


你知道为什么吗

听起来您已经定义了
input\y
——我假设它是
tf.placeholder()
——类型为
tf.int32
。将其更改为
tf.float32
或添加一个:
tf.cast(input\y,tf.float32)
tf.to\u float(input\y)
我将代码中的
%tensorflow\u version 1.x
替换为
!pip安装tensorflow==1.15.5
,错误消失。不仅限于1.15.5版,它还适用于其他一些TensorFlow版本1。

我在tflearn中找不到cast或to_float,我们必须导入哪个模块?
tf.cast()
tf.to_float()
都是TensorFlow函数,因此,您可以使用
import tensorflow as tf
获得它们。您还可以在赋值
dtype=tf.float32