Can I change the order that metrics / loss is printed while training?

This is a very small thing I guess, but is there any way I could change the order that Keras prints things while training? So instead of [loss, accuracy, val_loss, val_accuracy] I could have [loss, val_loss, accuracy, val_accuracy]? It doesn’t matter with only two things but with multiple outputs / metrics it can get hard to read.

Hi @Mog,As per my knowledge, to get the loss, val_loss, accuracy, val_accuracy in the desired format you have to write you own custom training loop. Thank You.

@Mog

To change the order, simply rearrange the metrics in the metrics parameter of the compile method.

Here’s an example:

from keras.models import Sequential
from keras.layers import Dense

model = Sequential()
model.add(Dense(64, activation='relu', input_dim=10))
model.add(Dense(1, activation='sigmoid'))

# Define metrics in the desired order
custom_metrics_order = ['loss', 'val_loss', 'accuracy', 'val_accuracy']

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=custom_metrics_order)

In this example, the metrics will be displayed in the order specified in custom_metrics_order during training.

Adjust the list custom_metrics_order according to your preference. The order should correspond to the metrics you want to display during training.

Let me know if it helped. @Mog

1 Like

Oh cool. I thought I could only specify the “raw” metrics and then Keras would add the equivalents if a validation set was available.

To be honest I think this order should be the default. Maybe I should make a pull request… On the other hand that is a lot of work for a small change.

@Mog glad you found the info helpful! Your idea to improve the default order of metrics in Keras sounds great. Even small changes can enhance user experience. If you decide to contribute, check existing discussions on the Keras GitHub and open a new issue if needed. Best of luck with your potential pull request!

1 Like

I tested it and it doesn’t work at all for me. Can you show me what I am doing wrong? Code attached.

from keras.models import Sequential
from keras.layers import Dense, Flatten
import tensorflow_datasets as tfds

# Load the data
ds = tfds.load('mnist', split='train', shuffle_files=True, as_supervised=True)
val_ds = tfds.load('mnist', split='test', shuffle_files=True, as_supervised=True)
ds = ds.shuffle(4 * 32).batch(32)
val_ds = val_ds.batch(32)

model = Sequential()
model.add(Flatten())
model.add(Dense(64, activation='relu', input_dim=10))
model.add(Dense(10, activation='softmax'))

# Define metrics in the desired order
custom_metrics_order = ['loss', 'val_loss', 'accuracy', 'val_accuracy']
# custom_metrics_order = ['accuracy']

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=custom_metrics_order)

model.fit(ds, validation_data=val_ds, epochs=2)

@BadarJaffer

It works with custom_metrics_order = ['accuracy'] because then it finds the accuracy metric callable and adds it when we compile.

1 Like

Interesting. I am glad you found the solution. Feel free to reach out if you ever need any help. @Mog

@BadarJaffer

Oh, I did absolutely not find the solution. I just meant that the code runs with custom_metrics_order = ['accuracy']. I don’t think it’s doable though because of the way that Keras prints to the terminal when fitting with default verbosity.

@Mog Thank you for the clarification. Can you share more details about your specific requirements or any challenges you’re facing with metric display in the terminal. This way, we can work together to find a solution that fits your needs. Looking forward to your insights!