Tensorflow错误:不允许使用`tf.Tensor`作为Python`bool`

Lil*_*ilo 5 machine-learning deep-learning keras tensorflow activation-function

我正在努力在Python中的TensorFlow中实现激活功能.

代码如下:

def myfunc(x):
    if (x > 0):
        return 1
    return 0
Run Code Online (Sandbox Code Playgroud)

但我总是得到错误:

不允许使用tf.TensorPython作为Python bool.使用if t is not None:

Max*_*xim 11

用途tf.cond:

tf.cond(tf.greater(x, 0), lambda: 1, lambda: 0)
Run Code Online (Sandbox Code Playgroud)

另一个解决方案,它还支持多维张量:

tf.sign(tf.maximum(x, 0))
Run Code Online (Sandbox Code Playgroud)

但请注意,此激活的梯度到处都是零,因此神经网络不会学习任何东西.