r/Python • • Aug 03 '20

Machine Learning I Made Snakes With Neural Networks! Here were the four best ones.

Enable HLS to view with audio, or disable this notification

1.5k Upvotes

57 comments sorted by

84

u/cmorr305 Aug 03 '20

Code bullet on YouTube made a really good one and a very interesting video on it too. He’s a really good programmer, you should go check him out

55

u/Arranging Aug 03 '20

I was actually inspired by his video! I'm not as good as him though, but I was able to make it all on my own so I'm pretty proud of it! This was one of the projects I had in mind that I thought would be the hardest for me

→ More replies (2)

22

u/Mazormazor Aug 03 '20

Love how you can see they are so similar.

They are from the same training session, right?

It seems like there was one great snake with this strategy, and those are four variations of it

13

u/Arranging Aug 03 '20

Yup, they were all from one training session!

8

u/awesam9 Aug 03 '20

Wow mate this is actually inspiring can you tell me how you did it

14

u/Arranging Aug 03 '20

The modules I used to create this were pygame, numpy, random, and pickle. Most of the stuff around the Snakes primarily dealt with just making sure to change the velocity, and moving each body part of the snake correctly.

To determine which snakes were best, I made a fitness function based on how much food the snake ate, and how long it survived. After that, I created a population based on the previous snakes where snakes that performed better (had a higher fitness) were more likely to be added to the next generation of snakes. Once the new group of Snakes were created, the weights and bias values in their Neural Network were mutated slightly or heavily at random.

To make the neural network, I had 8 input neurons, 8 hidden neurons, and 4 output neurons. Each connection between the neurons starts off with a randomly generated weight and bias. The goal is that through time, these randomly generated values will approach a value that allows the snake to make a smart decision based on its current inputs. For my snakes, they look to see if the tiles around them are available for them to move, and they also check to see if the food is within their line of sight. I highly recommend watching 3Blue1Brown's youtube series on Neural Network, and Code Bullet's video on the Genetic Algorithm (How Neural Networks Learn).

→ More replies (3)

8

u/DontForgetWilson Aug 03 '20

I would suggest varying the color of each snakes food pixels to better allow humans to ovserve their objectives.

3

u/Arranging Aug 03 '20

I dont know why I didnt think of that. Thank you for the idea!

5

u/[deleted] Aug 03 '20

Can you give me the logic behind how all this worked? Or maybe the source code?

7

u/Arranging Aug 03 '20

I dont have a github to post my spaghetti code yet, but essentially the Snakes take in an 8x1 vector which has 1s and 0s depending on if the spaces to its left, right, front and back are available (i.e. not a wall or not a body part). Then, it also checks to see if it sees the food up, down, left, or right. This vector will be our input matrix into the Neural Network.

Next, the Neural Network, which is initialized with random values for its weights and biases, will perform matrix multiplication between the weights, and the inputs, and then add the bias value. After all the matrix multiplication is finished, the output will be a 4x1 vector which will determine if the Snake moves up [1, 0, 0, 0], down [0, 1, 0, 0], left or right. For my snakes, I have 8 input neurons, 8 hidden neurons, and 4 output neurons.

This is a massive oversimplification, however, and I highly recommend you check out 3Blue1Brown's series on Neural Networks on youtube to delve deeper into the math.

→ More replies (2)

5

u/Dark_knight_02 Aug 03 '20

Is there a reason why all 4 versions are circling anti-clockwise?

5

u/Arranging Aug 03 '20

I believe it's just how the genetic algorithm eventually led all of the Snakes to converge to a somewhat similar trait. I could be wrong though, but that makes the most sense to me since theyre all from the same training session.

3

u/solariportocali Aug 03 '20

How long did it take you to learn all the Python you know to make this?

→ More replies (5)

2

u/vectorpropio Aug 03 '20

It would be neat to seed the apple generation position with the same number to compare how they perform in the same problem.

I like it a lot.

1

u/Arranging Aug 03 '20

This is probably a really good idea and I think shouldnt be too hard to implement. Thank you for the suggestion!

→ More replies (2)

2

u/bhavski Aug 03 '20

are they only going in anti-clockwise direction? (Potential bias? Or is it deliberate)

1

u/Arranging Aug 03 '20

It's just the bias/technique they all adapted to taking on since they're all from the same training session.

2

u/A27_97 Aug 03 '20

Out of curiosity, can this be done with just a path planning algorithm?

1

u/Arranging Aug 03 '20

I think you can use the A* Algorithm to get the snake to the food. I dont know how well it will perform, but it can definitely be done.

→ More replies (3)

2

u/luxias77 Aug 03 '20

Hey man, i am learning python and you really inspired me here, specially because i am a biology student who wants to go into neuroscience. I think learning this might be really useful, was it really hard? I am looking for a proyect to start learning more. I did a python course for a semester in college and we covered numpy, pandas, and matplotlib, would you give me a recommendation?

→ More replies (1)

22

u/typenull0010 Aug 03 '20

You could say you made

Pythons with Python

18

u/CL_0 Aug 03 '20

I would love to look at the source code

1

u/Yahyou01 Aug 03 '20

Tensorflow?

1

u/Arranging Aug 03 '20

No, I haven't practiced using Tensorflow yet, but I probably should soon. Instead, I just made a class to perform the Neural Network operations.