Tensorflow error: Using a `tf.Tensor` as a Python `bool` is not allowed
Asked Answered
R

2

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:

Reflexion answered 1/2, 2018 at 20:51 Comment(0)
G
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.

Gyronny answered 1/2, 2018 at 20:53 Comment(1)
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 shapesReflexion
Y
3

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

@tf.function
def myfunc(x):
    if (x > 0):
        return 1
    return 0
Yellowlegs answered 22/9, 2020 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.