Keras: How do you create custom activation? I got an error.

Created on 15 Oct 2016  路  3Comments  路  Source: keras-team/keras

This is what I tried to do.

def custom_activation(x):
    return (K.sigmoid(x) * 5) - 1

layer_3 = Dense(1, activation='sigmoid', init=lambda shape, name: normal(shape, scale=1e-4, name=name))(layer_2)

I got the following error:
Exception: Invalid activation function: custom_activation

Most helpful comment

Looks like you're setting activation='sigmoid' in layer_3. If you're using your custom_activation function in layer_2 can you please share how the function is being passed?

A common construct in Keras for using custom_functions is to just use them in the argument.
For example:

Dense(activation=custom_activation)

or

model.add(Activation(custom_activation))

All 3 comments

Looks like you're setting activation='sigmoid' in layer_3. If you're using your custom_activation function in layer_2 can you please share how the function is being passed?

A common construct in Keras for using custom_functions is to just use them in the argument.
For example:

Dense(activation=custom_activation)

or

model.add(Activation(custom_activation))

@abishekk92 Thanks. I actually put colons that prevented it from working well.

@ritchieng Where did you put colons? Even I am trying to use piecewise linear functions instead of sigmoid function as activation. I am stuck around the same issue. Thanks in advance.

Was this page helpful?
0 / 5 - 0 ratings