Issue
If I make a model and then use add_loss
:
model.add_loss(myLoss1(...))
And later would like to use a different loss on the same model, how can I, for example, remove myLoss1
and replace it with myLoss2
?
I tried model.losses.clear()
, but that didn't seem to be effective. I know I could save the weights to disk, remake the model with the desired loss and reload the weights, but that seems like a hack.
Solution
You can create a new model using existing model's configuration and then call add_loss()
again to set new loss. Same goes for restoring model weights.
weights = model.get_weights()
# Instantiate model again to remove old loss
model = model.from_config(model.get_config())
model.set_weights(weights)
# Set new loss
model.add_loss(myLoss2(...))
Optionally, you might want to reset the global state before you start training your model with new loss, but it depends on your use case.
# For Tensorflow's Keras
tf.keras.backend.clear_session()
# For Standalone Keras
keras.backend.clear_session()
Answered By - Divyesh Peshavaria
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.