简体   繁体   中英

Loading model with custom loss + 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

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.

You can try this:

import keras.losses
keras.losses.penalized_loss = penalized_loss

(after defining 'penalized_loss' function in your current 'py' file).

i haad the same problem and after many researchs i can assume that this work

  1. At first load u model and assign compile=False 2)compile u model with u custom loss function 3)retrain u model example: def custom_loss(y_true,y_pred): nn=np.square(y_true-pred) return (nn) model=load_model("aaaa.h5",compile=False) model.compile(loss=custom_loss, optimizer='adam', metrics=custom_loss) model.fit(...)

@rickyalbert

def custom_loss(y_true, y_pred):
   nn = np.square(y_true - y_pred)
   return nn

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

You should pass the loss function as the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM