question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unknown metric error when loading a model trained with stateful metrics

See original GitHub issue

Hi everyone,

I’m trying to load a saved model which has been trained with a custom stateful metric (object of the class ValidAccuracy using the code

dependencies = {
     'ValidAccuracy': ValidAccuracy
}
model = keras.models.load_model(model_file, custom_objects=dependencies)

and I get the error ValueError: Unknown metric function: {'config': {'batch_size': 32, 'name': 'valid_accuracy', 'trainable': False}, 'class_name': <class 'spearec.run.metrics.ValidAccuracy'>}.

Is there any solution or workaround?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

14reactions
brge17commented, Jul 13, 2018

Stateful Metrics are custom layers:

If you really want to load a custom model with custom layers: https://github.com/keras-team/keras/issues/4871.

Or you can just do load_weights.

I’m of the opinion that load_model shouldn’t even exist. Have a function that defines your network, train your model, save the weights, create the network, load your weights, predict.

You shouldn’t need the training metrics, or the optimizer to do predictions.

8reactions
brge17commented, May 7, 2018
dependencies = {
    'valid_accuracy': ValidAccuracy
}

should solve the issue.

Stateful metric inherits from layer and forces it to lower_case_underscore. As per the error message, it is looking for valid_accuracy and not ValidAccuracy.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unknown metric error when loading keras model
I have trained my regression model with no issues. Trains and tests perfectly. I used the model.save(model_filename) to save it.
Read more >
Metrics - Keras
Metric functions are similar to loss functions, except that the results from evaluating a metric are not used when training the model. Note...
Read more >
Keras Metrics: Everything You Need to Know - neptune.ai
Keras metrics are functions that are used to evaluate the performance of your deep learning model. Choosing a good metric for your problem...
Read more >
tensorflow could not interpret metric function identifier - You.com
train model with keras.metrics.Precision and save as a hdf5 file load model from the hdf5 file, got the ValueError. ValueError: Unknown metric function: ......
Read more >
Evaluate the Performance of Deep Learning Models in Keras
Use a manual verification dataset. Use an Automatic Verification Dataset. Keras can separate a portion of your training data into a validation ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found