Using a `tf.Tensor` as a Python `bool` is not allowed

本文探讨了在使用TensorFlow时,如何正确地在一个tf.Session()中同时运行多个变量,避免将Tensor对象作为Python布尔值使用的错误。通过实例演示了将多个变量放入数组中一起运行的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在处理数据时,有个小问题,就是

import tensorflow as tf

a = tf.constant([1,2,3])
b = tf.equal(a, 0)

with tf.Session() as ss:
    print(ss.run(a,b))
 #报错 Using a `tf.Tensor` as a Python `bool` is not allowed   

但是如果这样写就不会报错

import tensorflow as tf

a = tf.constant([1,2,3])
b = tf.equal(a, 0)

with tf.Session() as ss:
    print(ss.run(a))
    print(ss.run(b))

难道一定要将不同层的变量单独拿出来吗? 最后找到了github上的解答,发现不是这样的,原来要放到一个数组里,要不第二个参数会认为是 ==feed_dict ==

import tensorflow as tf

a = tf.constant([1,2,3])
b = tf.equal(a, 0)

with tf.Session() as ss:
    print(ss.run([a,b]))  #放到一个数组里。。。

最后我真的想说:

import torch as tf
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值