Keras: customized loss function cannot be save to a keras model

Created on 13 Feb 2018  路  3Comments  路  Source: keras-team/keras

I used a customized loss function called mbe1_4, and I saved it to a local file, but when I reload it, it has a bug

ValueError                                Traceback (most recent call last)
<ipython-input-2-c53988bad9f7> in <module>()
----> 1 x=get_model('nn-0-36-20180209')

~/stock/src/feature.py in get_model(version)
   1270     if 'keras' in f:
   1271         from keras.models import load_model
-> 1272         model = load_model(f'{model_dir}/{version}/{f}')
   1273     else:
   1274         try:

~/anaconda3/lib/python3.6/site-packages/Keras-2.1.3-py3.6.egg/keras/models.py in load_model(filepath, custom_objects, compile)
    272                       metrics=metrics,
    273                       loss_weights=loss_weights,
--> 274                       sample_weight_mode=sample_weight_mode)
    275 
    276         # Set optimizer weights.

~/anaconda3/lib/python3.6/site-packages/Keras-2.1.3-py3.6.egg/keras/models.py in compile(self, optimizer, loss, metrics, sample_weight_mode, weighted_metrics, target_tensors, **kwargs)
    822                            weighted_metrics=weighted_metrics,
    823                            target_tensors=target_tensors,
--> 824                            **kwargs)
    825         self.optimizer = self.model.optimizer
    826         self.loss = self.model.loss

~/anaconda3/lib/python3.6/site-packages/Keras-2.1.3-py3.6.egg/keras/engine/training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs)
    634             loss_functions = [losses.get(l) for l in loss]
    635         else:
--> 636             loss_function = losses.get(loss)
    637             loss_functions = [loss_function for _ in range(len(self.outputs))]
    638         self.loss_functions = loss_functions

~/anaconda3/lib/python3.6/site-packages/Keras-2.1.3-py3.6.egg/keras/losses.py in get(identifier)
    120     if isinstance(identifier, six.string_types):
    121         identifier = str(identifier)
--> 122         return deserialize(identifier)
    123     if isinstance(identifier, dict):
    124         return deserialize(identifier)

~/anaconda3/lib/python3.6/site-packages/Keras-2.1.3-py3.6.egg/keras/losses.py in deserialize(name, custom_objects)
    112                                     module_objects=globals(),
    113                                     custom_objects=custom_objects,
--> 114                                     printable_module_name='loss function')
    115 
    116 

~/anaconda3/lib/python3.6/site-packages/Keras-2.1.3-py3.6.egg/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    162             if fn is None:
    163                 raise ValueError('Unknown ' + printable_module_name +
--> 164                                  ':' + function_name)
    165         return fn
    166     else:

ValueError: Unknown loss function:mbe1_4

Most helpful comment

@fayeshine Suppose you trained your model with a custom function named X, you saved the model.
Now while loading the model, you need to define dictionary of custom object as an argument to load model, and also you need to define that model.
Let us assume your training file was nn.py, and you are loading the trained model in test_nn.py
in test_nn.py you need to define your custom loss again.
test_nn.py:

def _loss(y_true, y_pred):
    return calc_loss

model = load_model(sys.argv[1], custom_objects=dict(x=x))

All 3 comments

this issue was closed on #3977 where you first opened your issue.
From this line in your code:
model = load_model(f'{model_dir}/{version}/{f}')
try this:
model = load_model(f'{model_dir}/{version}/{f}', custom_objects={'mbe1_4': mbe1_4})

The loss function argument (mbe1_4 in quote marks) should be same with the name when you first trained and saved the model.

@fayeshine Suppose you trained your model with a custom function named X, you saved the model.
Now while loading the model, you need to define dictionary of custom object as an argument to load model, and also you need to define that model.
Let us assume your training file was nn.py, and you are loading the trained model in test_nn.py
in test_nn.py you need to define your custom loss again.
test_nn.py:

def _loss(y_true, y_pred):
    return calc_loss

model = load_model(sys.argv[1], custom_objects=dict(x=x))
Was this page helpful?
0 / 5 - 0 ratings