My Notebook
#ai#project#programming
2

AI from Scratch: Flappy Bird

2020-05-11 · 2 min readA cool game where an AI teaches itself to play flappy bird.... built from scratch with JS Core libraries!

What is this?

One day, I decided to learn a bit more about neural networks, and set myself to try and build and train a neural network using reinforcement learning. This is a great start for me because I don't have to start to learn back propagation, and can rely on mutation. So first I decided on a game, and realized that flappy bird was one of the best games to try reinforcement learning with. This is because the rewards are easy to figure out, and you can spawn many birds in one environment. Kudos to Coding train for the initial base flappy bird game.

Neural Networks

The cool thing that I learned about neural networks during this time is that its simple linear algebra! If you consider the neurons to be a vector, all you need to do is take the dot product of the nodes and the weights, add the bias, then you get the new neurons value! I was so surprised that it was so simple, so I started to create a class, along with a mutation method so that I can mutate each weight slightly so it can learn like in Darwinian Evolution. Bellow is the neural net used for the birds. It takes in the velocity, y value of bird, y value of top and bottom of pipe, and x value of pipe! The output is 2 numbers, and if the first node is larger, it jumps. That way I don't have to implement a soft max activation.

Training

After building the neural network, all I had to do was pop it in to the birds, so some debugging with the inputs, and it worked flawlessly! The birds were learning fast, and after a few generations, started to play the game without any mistakes! This was amazing because I didn't think I would be able to create my very own AI from scratch, and it's amazing to see something actually make decisions with math.

If you want to check out the project, take a look at https://flappyai.hacker22.repl.co or look at the GitHub at @neelr/FlappyAI.

Thanks for reading! Liked the story? Click the heart
Created with ☕ by @neelr