【Python】Error:Input 'y' of 'Add' Op has type float32 that does not match type int32 of argument 'x'.

学习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))

正确运行结果:

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值