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

Lilo picture Lilo · Feb 1, 2018 · Viewed 15.1k times · Source

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 Python bool is not allowed. Use if t is not None:

Answer

Maxim picture Maxim · Feb 1, 2018

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.