Create animation of overfitting a Sin Wave with neural network

Jeff Lee
2 min readJun 20, 2019

--

Overfitting is a common problem that all Machine Learning Algorithms run into. This occurs when the model is fit too well to the training set but does not perform as well on the test set.

In this tutorial, I am going to show you how to create animation of overfitting neural network.

First, we need to create the dataset.

By using random feature in numpy, we create 100000 sample as true value and 100 for testing value.

Create a simple ANN model for overfitting

With a very small learning rate which is 0.00001, the model take very small step each epoch. So we can see the difference and create the animation later. We use Adam for optimiser and mean square error for loss function in this example.

function show_compare display a graph of true value and predicted value and save it to output folder.

Here is full implementation of above tutorial.

Right now, you should see a list of image in your output folder like this.

Imagrick is used to create the animation from list of jpeg file.

Remember to change the size of your image and the number of image in the script. After executing the script, you should see a gif as below.

Enjoy Coding!

--

--

Jeff Lee

Full-stack Engineer, Cloud Architecture, Micro-service