Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading model with custom loss + keras

Tags:

python

keras

In Keras, if you need to have a custom loss with additional parameters, we can use it like mentioned on https://datascience.stackexchange.com/questions/25029/custom-loss-function-with-additional-parameter-in-keras

def penalized_loss(noise):     def loss(y_true, y_pred):         return K.mean(K.square(y_pred - y_true) - K.square(y_true - noise), axis=-1)     return loss 

The above method works when I am training the model. However, once the model is trained I am having difficulty in loading the model. When I try to use the custom_objects parameter in load_model like below

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

it complains ValueError: Unknown loss function:loss

Is there any way to pass in the loss function as one of the custom losses in custom_objects ? From what I can gather, the inner function is not in the namespace during load_model call. Is there any easier way to load the model or use a custom loss with additional parameters

like image 747
Jason Avatar asked Jan 22 '18 02:01

Jason


People also ask

How do I save a model in keras?

Passing a filename that ends in .h5 or .keras to save (). SavedModel is the more comprehensive save format that saves the model architecture, weights, and the traced Tensorflow subgraphs of the call functions. This enables Keras to restore both built-in layers as well as custom objects. # Create a simple model. # Train the model.

How to get the value of the noise in keras model?

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. Share Improve this answer Follow answered Jan 22, 2018 at 2:36 rickyalbertrickyalbert

How to specify keras losses for a deep learning model?

Using via compile Method: Keras losses can be specified for a deep learning model using the compile method from keras.Model.. model = keras.Sequential([ keras.layers.Dense(10, input_shape=(1,), activation='relu'), keras.layers.Dense(1) ]) And now the compile method can be used to specify the loss and metrics.

Is it possible to load a TensorFlow graph in keras?

The function name is sufficient for loading as long as it is registered as a custom object. It's possible to load the TensorFlow graph generated by the Keras. If you do so, you won't need to provide any custom_objects. You can do so like this:


1 Answers

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.

like image 91
rickyalbert Avatar answered Sep 22 '22 16:09

rickyalbert