Keras: Loading model with 'self-made' activations

Created on 16 Aug 2016  路  4Comments  路  Source: keras-team/keras

Hello! I faced with problem that if I use a self-made activation function, for example:

def my_tanh(x):
      return 1.7159*K.tanh(2*x/3)

When I try load a saved model with such function from the .json file I, obviously, got a error, that Keras doesn't know this function. How can I avoid this? Thank!

Most helpful comment

loaded_model = model_from_json(loaded_model_json, custom_objects={'custom_activation': custom_activation})

All 4 comments

Try passing a custom object inside the load_model function
Define the function you were using in your code and then pass it as an argument inside an object like this
model = keras.models.load_model(model_filepath, custom_objects={'my_tanh':my_tanh})

@cicobalico
I've tried and got this:

 File ".../python2.7/site-packages/keras/utils/generic_utils.py", line 14, in get_from_module
    str(identifier))
Exception: Invalid activation function: my_tanh

@ipoletaev How did you fix this error?

loaded_model = model_from_json(loaded_model_json, custom_objects={'custom_activation': custom_activation})

Was this page helpful?
0 / 5 - 0 ratings