Training a Neural Network to play a basic snake game.

         



        We are gonna train the neural network that will allow us to keep the snake to stay alive.Essentially the output will be what direction to go in or to follow the certain direction or not. Essentially we just wanna keep the snake alive.

The first step is to decide what the input is gonna be and to decide what the output is gonna be.

For this case, the input can be, do we have anything in front of the snake and do we have anything to the left of the snake and do we have anything to the right of the snake?. We are those by 0 and 1.

And the last input will be recommended direction for the snake. The recommended direction can be anything. So for this case, let’s take recommended direction to be left. The output will be whether we have to follow the recommended direction or not.




The output in this case should be 0 or 1 representing to follow the recommended direction or not. So, the output will be 1 as we can follow the recommended direction.




Now, when we change the recommended direction to be right then we are gonna crash into our tail which means that we should not follow that direction. So, our output should be 0.( as 1 for true and 0 for false)

How do we train this?

        We start by designing the architecture of the neural network. So, we have already done this, we have the input and the output.

Now, each of the inputs that are connected to our outputs and each of these connections have weight. Now, another thing we have is each of this neuron has a value.

In this case we either had a 0 or 1. They can be a decimal value. It’s not like they need to be between 0 and 1. The point here is they have a value.

What we are gonna do in the output layer is to decide what direction to go by taking the weighted sum of the value (value of neuron , in this case 0 or 1) multiplied by the weights.

⅀ (vi * wi) (i = 1 to i)

We are gonna use variable i for looping. This will return to us what actually would be

v1w2 + v2w2 + v3w3 + v4w4 + v5w5

This is what output layer is gonna do. 

Now, there is one thing that we have to add to this is bias.

What we gonna do is we are gonna take the weighted sum and also gonna have some kind of bias on each of the weights. It is denoted by b typically. We automatically add or subtract. It’s a constant value for each of the weights. So, we are gonna say all these connections have weights and also have biases.




⅀ (vi*wi) + bi (i = 1 to i)

So now:-
v1w2 + v2w2 + v3w3 + v4w4 + v5w5 + b1 + b2 + b3 +b4 + b5

So, now that we need to do is train the network. So, we understood that, what the output layer is doing i.e we are taking the values (values of neuron) multiplying them with weights and adding them along with the bias i.e the weighted sum.

Post a Comment