2

I am trying to implement a custom activation function in Keras The function is following

def picewise(x):
   if x > 0.5 :
      return 1
   elif if x < 0.5:
      return 0
   else:
      return x + 0.5

I call the activation function as

model.add(Dense(128,activation = picewise))

But I am getting a whole bunch of errors. How can i successfully implement the above as a working activation function in Keras

1 Answer 1

3

Here x is not a primitive type. x is a tensor. So the way you are trying to implement the activation function will not work. You probably need something like the following:

import tensorflow as tf
def picewise(x):
    z = tf.where(x >= 0.5, x - x + 1.0 , x)
    y = tf.where(z <= -0.5,z -  z + 0, z + 0.5)
    return y
Sign up to request clarification or add additional context in comments.

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.