Skip to content

Instantly share code, notes, and snippets.

@JasonKessler
Last active January 19, 2017 23:12
Show Gist options
  • Save JasonKessler/d2febea89584abe4cdb0f81de58b6aff to your computer and use it in GitHub Desktop.
Save JasonKessler/d2febea89584abe4cdb0f81de58b6aff to your computer and use it in GitHub Desktop.
Loading a model with custom activation function (or custom_objects) in Keras 1.1.0 via monkey patching
import mock, keras
def custom(x):
return x # Replace this with your activation function.
model_json = open('config.json').read()
'''
Trying to run:
model_from_json(model_json, custom_objects={'custom': custom}
Raises:
Exception: Invalid activation function: one_to_five_star
'''
old_get = keras.activations.get
def patch_get(x):
return custom if x == 'custom' else old_get(x)
@mock.patch('keras.activations.get', patch_get)
def load():
return model_from_json(model_json, custom_objects={'custom': custom}
model = load()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment