Showing the training progress

Let's see how we can show the progress and various metrics during the training process interactively in the console or the notebook. Let's first import some libraries

from tensorflow import keras
import numpy as np
from tqdm import tqdm

In this example, we will be using the fashion MNIST dataset to do some basic computer vision, where we will train a Keras neural network to classify items of clothing.

In order to import the data we will be using the built in function in Keras :

keras.datasets.fashion_mnist.load_data()

The model is a very simple neural network consisting in 2 fully connected layers. The model loss function is chosen in order to have a multiclass classifier : "sparse_categorical_crossentropy"

Let's define a simple feedforward network.

##get and preprocess the data
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images = train_images / 255.0
test_images = test_images / 255.0

## define the model 

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28,28)),
    keras.layers.Dense(128, activation="relu"),
    keras.layers.Dense(10, activation="softmax")
])

model.compile(optimizer="adam",
             loss = "sparse_categorical_crossentropy",
             metrics = ["accuracy",'mae'])

We now need to define the callback by specifiying to tqdm how to show the training progress.

The callback has to be added to the callbacks list in the fit method.

with tqdm(total=10, unit="epoch") as t:
    def cbk(epoch, logs):
        t.set_postfix(logs, refresh=False)
        t.update()
    cbkWrapped = keras.callbacks.LambdaCallback(on_epoch_end=cbk)
    model.fit(train_images, train_labels,  epochs=t.total, verbose=0, callbacks=[cbkWrapped])
 80%|████████  | 8/10 [00:38<00:09,  4.76s/epoch, loss=0.257, acc=0.904, mean_absolute_error=4.42]