TypeError: Input 'y' of 'Add' Op has type float32 that does not match type int32 of argument 'x'.

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lyq_12/article/details/84616119

学习Python,碰到数据类型不一致进行运算出现的问题,问题现象、原因、解决办法如下。

1、问题代码

# 引入 tensorflow 模块
import tensorflow as tf

# 创建两个常量节点
node1 = tf.constant([2,5], dtype=tf.int32)
node2 = tf.constant([1,2], dtype=tf.float32)

#创建add节点,实现上面2个节点的加操作
adder = node1 + node2

#打印3个节点
print(node1)
print(node2)
print(adder)

# 打印 adder 运行后的结果
sess = tf.Session()
print(sess.run(adder))

问题现象:

2、问题原因

node1数据类型是int,但node2数据类型是float

3、解决办法

# 引入 tensorflow 模块
import tensorflow as tf

# 创建两个常量节点
node1 = tf.constant([2,5], dtype=tf.float32)
node2 = tf.constant([1,2], dtype=tf.float32)

#创建add节点,实现上面2个节点的加操作
adder = node1 + node2

#打印3个节点
print(node1)
print(node2)
print(adder)

# 打印 adder 运行后的结果
sess = tf.Session()
print(sess.run(adder))

正确运行结果:

猜你喜欢

转载自blog.csdn.net/lyq_12/article/details/84616119