gpt4 book ai didi

tensorflow 错误: Using a `tf.Tensor` as a Python `bool` is not allowed

转载 作者:行者123 更新时间:2023-11-30 08:27:23 25 4
gpt4 key购买 nike

我正在努力实现 中的激活函数在Python中。

代码如下:

def myfunc(x):
if (x > 0):
return 1
return 0

但我总是收到错误:

Using a tf.Tensor as a Python bool is not allowed. Use if t is not None:

最佳答案

使用tf.cond :

tf.cond(tf.greater(x, 0), lambda: 1, lambda: 0)

另一种解决方案,另外还支持多维张量:

tf.sign(tf.maximum(x, 0))

但请注意,此激活的梯度在各处均为零,因此神经网络不会从中学习任何内容。

关于 tensorflow 错误: Using a `tf.Tensor` as a Python `bool` is not allowed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48571521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com