Tensorflow error: Using a `tf.Tensor` as a Python `bool` is not allowed
In TF2, you could just decorate the function myfunc()
with @tf.function
:
@tf.function
def myfunc(x):
if (x > 0):
return 1
return 0
Use tf.cond
:
tf.cond(tf.greater(x, 0), lambda: 1, lambda: 0)
Another solution, which in addition supports multi-dimensional tensors:
tf.sign(tf.maximum(x, 0))
Note, however, that the gradient of this activation is zero everywhere, so the neural network won't learn anything with it.