12

I am struggling to implement an activation function in 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 Python bool is not allowed. Use if t is not None:

2 Answers 2

18

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, I have this error now Shape must be rank 0 but is rank 2 for 'actfc1_36/activation_15/cond/Switch' (op: 'Switch') with input shapes
4

In TF2, you could just decorate the function myfunc() with @tf.function:

@tf.function
def myfunc(x):
    if (x > 0):
        return 1
    return 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.