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.
In TF2, you could just decorate the function myfunc()
with @tf.function
:
@tf.function
def myfunc(x):
if (x > 0):
return 1
return 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With