Last active
January 19, 2017 23:12
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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