tf怎么使用if

在TensorFlow中,不能直接使用if语句或==运算符进行条件判断,因为它们在图构建时即被评估。要实现运行时的条件控制流,应该使用tf.cond操作。该操作根据一个布尔条件 tensor 选择执行两个分支之一。若希望在条件判断后执行特定函数,一种方法是先计算函数返回值,然后通过if判断来决定是否使用。
摘要由CSDN通过智能技术生成

tf中用if是不行的,用==也是不行的

因为是先建图,建图的时候也只是用tensor来进行判断

You're correct that the if statement doesn't work here, because the condition is evaluated at graph construction time, whereas presumably you want the condition to depend on the value fed to the placeholder at runtime. (In fact, it will always take the first branch, because condition > 0evaluates to a Tensor, which is "truthy" in Python.)

To support conditional control flow, TensorFlow provides the tf.cond() operator, which evaluates one of two branches, depending on a boolean condition. To show you how to use it, I'll rewrite your program so that condition is a scalar tf.int32 value for simplicity:

x = tf.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值