Adding metrics to existing model in Keras

Viewed 2757

I have an existing model and would like to add additional metrics to it. The Keras metrics page says the metrics are added at compile time, but I would like to add them after loading (in part because model.load_model() only seems to load the first metric, and because I have new metrics I would like to try on existing model first). Is that possible?

2 Answers

Just adding this gist for quick and easy copy/paste answer for your convenience:

from keras.models import load_model

model_path = 'path/to/your/old_model.h5'
new_metrics = [<metrics to add>...]  # for example ['binary_accuracy']

model = load_model(model_path)
model.compile(optimizer=model.optimizer,
                        loss=model.loss,
                        metrics=model.metrics+new_metrics)

Make sure to add needed parameters to the compile function if used any other than the above case.

Related