Reputation: 1891
def newactivation(x):
if x>0:
return K.relu(x, alpha=0, max_value=None)
else :
return x * K.sigmoid(0.7* x)
get_custom_objects().update({'newactivation': Activation(newactivation)})
I am trying to use this activation function for my model in keras, but I am having hard time by finding what to replace
if x>0:
ERROR i got:
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py", line 614, in bool raise TypeError("Using a
tf.Tensor
as a Pythonbool
is not allowed. "TypeError: Using a
tf.Tensor
as a Pythonbool
is not allowed. Useif >t is not None:
instead ofif t:
to test if a tensor is defined, and >use TensorFlow ops such as tf.cond to execute subgraphs conditioned on >the value of a tensor.
Can someone make it clear for me?
Upvotes: 2
Views: 2481
Reputation: 3
inspired by the previous answer from ed Mar 21 '18 at 17:28 tomhosking. This worked for me. tf.cond
def custom_activation(x):
return tf.cond(tf.greater(x, 0), lambda: ..., lambda: ....)
Upvotes: 0
Reputation: 1876
You can evaluate the tensor and then check for the condition
from keras.backend.tensorflow_backend import get_session
sess=get_session()
if sess.run(x)>0:
return t1
else:
return t2
get_session is not available for TensorFlow 2.0. Solution for that you can find here
Upvotes: 0
Reputation: 61
Try something like:
def newactivation(x):
return tf.cond(x>0, x, x * tf.sigmoid(0.7* x))
x isn't a python variable, it's a Tensor that will hold a value when the model is run. The value of x is only known when that op is evaluated, so the condition needs to be evaluated by TensorFlow (or Keras).
Upvotes: 3
Reputation: 2378
if x > 0
doesn't make sense because x > 0
is a tensor, and not a boolean value.
To do a conditional statement in Keras use keras.backend.switch
.
For example your
if x > 0:
return t1
else:
return t2
Would become
keras.backend.switch(x > 0, t1, t2)
Upvotes: 6