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:

Innat
  • 16,113
  • 6
  • 53
  • 101
Lilo
  • 640
  • 1
  • 9
  • 22

2 Answers2

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.

Innat
  • 16,113
  • 6
  • 53
  • 101
Maxim
  • 52,561
  • 27
  • 155
  • 209
  • 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 – Lilo Feb 01 '18 at 20:58
2

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

@tf.function
def myfunc(x):
    if (x > 0):
        return 1
    return 0
Innat
  • 16,113
  • 6
  • 53
  • 101
Ayush Garg
  • 21
  • 3