I have loaded a Keras model (or just created it and compiled). How do I access the list of metrics objects with which the model was compiled?
I can access the loss and the optimizer using: model.loss
and model.optimizer
.
Therefore, I assumed that I will find the list of metrics in model.metrics
, but that only returns an empty list.
You can get them before model.fit()
from the model.compiled_metrics
attribute, which is a MetricGenerator object created in model.compile()
. The memory address is the same before and after fitting so I assume it is the same object. This is working with tf 2.6.0.
>>> model.compile(metrics=[tf.keras.losses.sparse_categorical_crossentropy])
>>> model.metrics
[]
>>> model.compiled_metrics
<keras.engine.compile_utils.MetricsContainer at 0x7f701c7ed4a8>
>>> model.compiled_metrics._metrics
[<keras.metrics.SparseCategoricalCrossentropy object at 0x7facf8109b00>]
>>> model.fit(x)
...
>>> model.metrics
[<keras.metrics.Mean object at 0x7facf81099e8>,
<keras.metrics.SparseCategoricalCrossentropy object at 0x7facf8109b00>]