Making a neural network learn to play a game — Part 4 — Finishing up!
In the last post we almost finished up our program. The neural network worked really well and all that is left to do is to visualize the neural network.
We will do this by using matplotlib. Matplotlib is a very famous python library that helps in drawing graphs.
Install matplotlib by
pip3 install matplotlib
We will plot three things -
- The score per game
- Our data
- The average score per 10 games
Let’s begin by adding the graph for the score per game.
I will create a static method in the class Wrapper and use global arrays. Now, this may not be the best practice in writing code but for our relatively small example, it will do.
I define the variables as follows -
And the visualize method looks like -
Finally, with some minor changes in the gameover function and a few other places, the final code looks like -
You can run the code and you can see your own neural network learn to play the game and get better at it. Eventually the neural network gets so good that it seems like that it won’t ever make a mistake. It’s really awesome to see it in action.
I know this was a small post but there wasn’t a lot left to cover. Anyway, I have a few more topic in my mind and I will be posting some more content soon.
As usual you can find all the code on my github here.