I am struggling to implement an activation function in TensorFlow in Python.
The code is the following:
def myfunc(x):
if (x > 0):
return 1
return 0
But I am always getting the error:
Using a
tf.Tensor
as a Pythonbool
is not allowed. Useif t is not None:
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.