Dejvovo
Dejvovo

Reputation: 129

List compiled metrics in a keras model?

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.

Upvotes: 1

Views: 3400

Answers (2)

Aniket Bote
Aniket Bote

Reputation: 3574

You have to run the model for at least 1 epoch for the metric names to be available:

import numpy as np
import tensorflow as tf
x = np.random.uniform(0,1, (37432,512))
y = np.random.randint(0,2, (37432,1))
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(256, activation = 'relu'))
model.add(tf.keras.layers.Dense(2, activation='softmax'))

model.compile(loss="sparse_categorical_crossentropy",
              optimizer='adam',
              metrics=['accuracy'])
print(model.metrics_names)
_ = model.fit(x= x, y = y, validation_split=0.2, verbose = 0)
print(model.metrics_names)

Output:

[]
['loss', 'accuracy']

For metric objects:

model.metrics[1:]

Output:

[<tensorflow.python.keras.metrics.MeanMetricWrapper at 0x7fbe702aee50>]

Upvotes: 3

Felipe Miranda
Felipe Miranda

Reputation: 161

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>]

Upvotes: 2

Related Questions