Piggy Wenzhou
Piggy Wenzhou

Reputation: 305

Load customized regularizer in Keras

I want to use load_model('path/to/model.h5') to load a trained model.

The architecture of the model.h5 is like

model = Sequential()
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
# The last layer is regularized with a custom regularizer
model.add(Dense(10, activation='softmax', W_regularizer=my_reg))

model.save('path/to/model.h5')

, which contains a customized regularizer my_reg. Then, when I want to load the model.h5 by using load_model('path/to/model.h5'), it shows error message:

  File "myfile.py", line 30, 
    model = load_model(model_path)
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 140, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 189, in model_from_config
    return layer_from_config(config, custom_objects=custom_objects)
  File "/usr/local/lib/python2.7/dist-packages/keras/utils/layer_utils.py", line 34, in layer_from_config
    return layer_class.from_config(config['config'])
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 1060, in from_config
    layer = get_or_create_layer(conf)
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 1039, in get_or_create_layer
    layer = layer_from_config(layer_data)
  File "/usr/local/lib/python2.7/dist-packages/keras/utils/layer_utils.py", line 34, in layer_from_config
    return layer_class.from_config(config['config'])
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 899, in from_config
    return cls(**config)
  File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 705, in __init__
    self.W_regularizer = regularizers.get(W_regularizer)
  File "/usr/local/lib/python2.7/dist-packages/keras/regularizers.py", line 162, in get
    instantiate=True, kwargs=kwargs)
  File "/usr/local/lib/python2.7/dist-packages/keras/utils/generic_utils.py", line 30, in get_from_module
    str(identifier))
Exception: Invalid regularizer: {}

Seems that load_model doesn't support models with customized regularizer. How can I load this model? Really appreciate if someone can help me!

Upvotes: 1

Views: 1080

Answers (2)

Mitiku
Mitiku

Reputation: 5412

keras enables to define custom objects(layers, optimizers, loss function, metric function etc.) and has functionality to load custom objects when loading model using load_model method by passing custom objects dictionary to custom_objects . E.g


from keras import backend as K

def custom_regularizer(weight_matrix):
    # the rest of the code
    pass

model = load_model("path/to/model.h5", custom_objects={"custom_regularizer":custom_regularizer})

Upvotes: 0

Eypros
Eypros

Reputation: 5723

As far as I know keras has some trouble loading custom components (not only regularizers but also optimizes etc) from saved models. I am not an expert in the field of custom models in keras though and I am just providing my experience with similar situations.

The most obvious solution would be to save the weights of your model using model.save_weights() which will save your weights only (not the architecture) and then when you want to load the model again, create your model from scratch (and also define your custom regularizer) and just load the weights using model.load_weights().

It does not seem the more convenient solution but it will probably work as expected.

Upvotes: 1

Related Questions