One way to create animations of Keras models learning

Say you want to create an animation that shows how a neural network learns from data. Maybe something like this, where a neural network learns the shape of a sine function:

Here’s how I did that. First, get the code for the neural network, so you can follow along: ml-sine on GitHub

When training your model you’ll usually use a statement like this:, y_train, batch_size=128, epochs=100)

After every epoch we want to take a snapshot of how well our prediction fits the training data. Let’s iterate the epochs manually instead of using the epochs=100 argument:

epochs = 100
for epoch in np.arange(1, epochs):, y_train, batch_size=128)

This allows us to run additional code in between the training epochs. We want to save a plot of our prediction and the ‘ground-truth’ y_train. Using matplotlib:

import matplotlib.pyplot as plt

plt.plot(x_train, y_train, 'g.', ms=1)
plt.plot(x_train, self.model.predict(x_train), 'r.', ms=1)
   x_train.min() - 0.5, x_train.max() + 0.5,
   y_train.min() - 0.5, y_train.max() + 0.5,
plt.title(f"Epoch #{epoch}")

We’re using plt.savefig() to store an image of the plot on disk. We will end up with 100 images that we can turn into a slideshow video using ffmpeg. Assuming the images are stored in a subfolder named img:

ffmpeg -n -framerate 4  -i "img/plot_%d.png"  -vf "fps=25,format=yuv420p" movie.mp4

The option -framerate 4 makes each image appear for a quarter of a second.

Have fun