Loading model with custom loss + keras

Yes, there is! custom_objects expects the exact function that you used as loss function (the inner one in your case):

model = load_model(modelFile, custom_objects={ 'loss': penalized_loss(noise) })

Unfortunately keras won't store in the model the value of noise, so you need to feed it to the load_model function manually.


If you are loading your model just for prediction (not training), you can set the compile flag to False in load_model as following:

model = load_model(model_path, compile=False)

This will not search for the loss function as it is only needed for compiling the model.

Tags:

Python

Keras