Neural Network Learns to Play Snake

  Переглядів 3,024,199

Greer Viau

2 роки тому

In this project I built a neural network and trained it to play Snake using a genetic algorithm.

Thanks for watching! Subscribe if you enjoyed and Share if you know anyone who would be interested!

GitHub Repo: github.com/greerviau/SnakeAI
Twitter: greerviau
Support me on Patreon: www.patreon.com/greerviau

Thanks to Josh Cominelli for the music!
Soundcloud: soundcloud.com/josh-cominelli

КОМЕНТАРІ
datdimaster
datdimaster День тому
Gen 9999: starts to draw animated memes
master code
master code 3 дні тому
the best thing is the zigzag move to reduce its length so it dosen't crash the wall
Eng. Khalid
Eng. Khalid 6 днів тому
That is amazing I want to ask about the input neorons, what did they represent? The hidden layers, why there are two layers with 18 neoron? I hope anyone who have a knowledge to respond. This will make me better understand neural network Thank you in advance
Xarnexes
Xarnexes 9 днів тому
So do they learn just by learning from their repeated mistakes ?
ً
ً 16 днів тому
This ai plays better in 30 generations than I will in my whole life
Watchmojo Wahaha
Watchmojo Wahaha 21 день тому
the snake seems to always go through a clockwise circle to get to the food, instead of using the shortest path. Is there a penalty for going through longer path to the food?
ニム
ニム Місяць тому
Right now the fitness (or score) of a snake is only the number of food it ate before dying right? What do you think about incorporating the average time between two foods in the fitness somehow ? Wouldn't that incite the snake to go for shortest routes between foods, instead of looping around the field ? Also the main difference between a human player and the AI is that human players have a view of the entire playing field, while the AI only sees in restricted directions. So what about making one input neuron per board square, with like different input values for empty, food, head and body ? And maybe four more neurons indicating which direction the snake is going. It would make for a more expensive training, but maybe the AI would develop better strategies, having the knowledge of the entire board?
Suzan Chhetala
Suzan Chhetala Місяць тому
Holy shit! This is amazing
Balloon410
Balloon410 Місяць тому
4:30 nice
Sundeep Kumar
Sundeep Kumar Місяць тому
How does it knows where to find the food? How do you know where to stop?
SunnyMonsterMC
SunnyMonsterMC Місяць тому
Those people who disliked don't know the hard work he put into it. And apparently, 1845 people can relate.
Iceblock
Iceblock Місяць тому
how does it know that hitting the wall is a bad thing and eating the food is a good thing? can somebody pls explain. ive gotten interested in bots playing games and im thinking about creating one myself. any tips is appreciated. thanks 👍😁
Harsha Kurudi Matha
Harsha Kurudi Matha Місяць тому
It stuck with eating only in downward motion and kept itself clockwise
Kevin Lu
Kevin Lu Місяць тому
This is cool! Just a thought but i noticed the snake goes to the top left corner a lot despite not needing to. Maybe rewarding faster high scores or punishing slower times to retrieve the apple could help?
Tiến Thịnh trương
Tiến Thịnh trương Місяць тому
how to run it ?
Sebastian Midolo
Sebastian Midolo 2 місяці тому
Can someone explain why is he using a 2x18 hidden layer?
Taha A.p.a
Taha A.p.a 2 місяці тому
There's another thing The random generated points are not totally random so the nn will learn(actually changing weights :)) based on that
forwarduntodawn
forwarduntodawn 2 місяці тому
*I have a question* . I want to learn neural networks , machine learning and AI and other related stuff, on my own . I am in high school , and I know how to code in C++ . Where and how do I start ? Like , which books can a beginner like me study for this ?
Mitul Gupta
Mitul Gupta 2 місяці тому
1:20 me after 2 drinks down
Zanarih
Zanarih 2 місяці тому
Year 2070: "Neural Network learns to conquer the world"
老實小鄭
老實小鄭 2 місяці тому
A human can get more and more information by every time focusing on different places, and quickly know the whole environment of this game. Can AI do the same?
hook sanzzzyo
hook sanzzzyo 2 місяці тому
My code: If (!tail) Then head can go through Else time to suicide 😂 . . .... Jokes aside, these results are awesome...😘
Atticus Denzil
Atticus Denzil 2 місяці тому
lol you could have made a rule based bot and still get much further than this ...
Lesio Stasio
Lesio Stasio 2 місяці тому
Me, feeding a neural network it's own weights and outputs into it's inputs to make it self aware: *Detroit: Become Human*
life42theuniverse
life42theuniverse 2 місяці тому
If that was generation 30 how many did you go to test for better results?
WeeWDiedrich
WeeWDiedrich 2 місяці тому
1:23 what the dog doin?
Paulo Lellis
Paulo Lellis 2 місяці тому
btw, awesome job mate!!
Paulo Lellis
Paulo Lellis 2 місяці тому
make the environment to change randomly, not only a fixed square and they will evolve
Your Health TV
Your Health TV 2 місяці тому
Just imagine training an AI to ride a car ;) Just when you think it gets it BOOM CRASH!
memebandit
memebandit 2 місяці тому
HAHA SO FUNNY IMAGINE A CAR LOL WOW GOOD JOKE
Aivo Paas
Aivo Paas 2 місяці тому
You wouldn't train AI for a car by only telling it how far the destination, gas station and it's own butt are.
Aarav Bhatt
Aarav Bhatt 2 місяці тому
Well, there are different ways to train neural networks. This is neural evolution, which changes the weights based on the best fitness of a generation. This does not require data, but instead an equation for fitness (how good it does). As you can see, it involves the AI getting it wrong several times before getting it right. However, most neural networks are trained using loss and backpropagation. This is much better, but it requires a large dataset and sometimes other neural networks to learn. However, it is faster at getting good results... I think
Eta Ending
Eta Ending 2 місяці тому
Only dense networks without embedding self-context, thats why it fails at the end
spawn142001
spawn142001 2 місяці тому
Take your original inputs and clone them 10 times. This gives your snake a 10 step memory. The first input will be the present. Then next step copy that to the second input before updating the first. And continue on. So input 1 goes to 2. 2 goes to 3. 3 goes to 4. No doubt have to make the hidden layers bigger to make use of it. But effectively with memory of previous input states, the snake can evolve to remember which way it came into a closed in area by its body. For example when it's really long. And by remembering things like this it can evolve to get out of those situations as opposed to it evolving a general pattern that avoids entrapment but doesn't actually plan around it intelligently. There might be other better ways to incorporate memory in the network where hidden layers loop information back to itself from previous steps of the program running but I know less about how thats done.
spawn142001
spawn142001 2 місяці тому
I might grab your code and try this. I really think you'd see more intelligent behavior evolve.
blzr
blzr 2 місяці тому
It's so fascinating to look at a neural network learn and it be visualized, it's like a mini brain in a computer learning and reacting to their surroundings, telling a machine that only follows orders to figure it out themself
Teflon Pan
Teflon Pan 2 місяці тому
This seems kinda dumb. I would have programmed the AI to go straight for the food.
Jerrick Marques
Jerrick Marques 2 місяці тому
my dog is not chasing its tail! it's trying to eat the red dot before it gets deleted by god!
rocky top
rocky top 2 місяці тому
Homie in gen 5s neurons were FIRING
More Girl
More Girl 3 місяці тому
I don't think it'll go better as I guessed before watching. it didn't learn about trap in the beginning
Abhishek Chhikara
Abhishek Chhikara 3 місяці тому
I did more than this
Yuval Rosen
Yuval Rosen 3 місяці тому
A couple of months ago I made a simpler version of it (can collide with itself) but my inputs were only the snake's head position and the food position, I will try to recreate it with these inputs
Zhu Tom
Zhu Tom 3 місяці тому
Have you ever tried using Q-learning? It may cost more time on training, but I could be better than NN on this game.
StevenGaming924
StevenGaming924 3 місяці тому
UKposts Algorithm: Dis looks guud, lemme recommend it to everyone
Mohit Kumar Verma
Mohit Kumar Verma 3 місяці тому
Hi Can you recommend tutorials from where i can learn this?
Tina Ieren
Tina Ieren 3 місяці тому
at 4:31 the score is 69, but at 4:32, the high score is not updated
Creedoo
Creedoo 3 місяці тому
This dude talking about reproducing and generations; that's not how I remembered how to play snake
ABHIRAM CD
ABHIRAM CD 3 місяці тому
I can't imagine how happy would be the first guy who developed these algorithms.... ❤️❤️
192.168.0.13
192.168.0.13 Місяць тому
We
Devendra Singh
Devendra Singh 2 місяці тому
Yes
Mohammad Wasif Hossain
Mohammad Wasif Hossain 2 місяці тому
yes
Maso!
Maso! 2 місяці тому
Yes
StevenGaming924
StevenGaming924 3 місяці тому
Yes
Alfie's Funtime
Alfie's Funtime 3 місяці тому
That is satisfying to watch
C
C 3 місяці тому
Should it not be reinforcement learning?
cn7abc
cn7abc 3 місяці тому
what's the first song called that's used in this video?
Cody Friso
Cody Friso 3 місяці тому
What does the input layer correspond to? It doesn't look like it takes in 8 directions of what the snake sees and it's not the flattened out matrix of the play area so I'm confused
Olli Finn
Olli Finn 3 місяці тому
the snake will only become as smart as the programmer - i would blame the input variables
Almost Worthy
Almost Worthy 3 місяці тому
Hey one question, how do you make these models. I mean you can see the sanke moving while the model is training. Which software do you use can you please share?
Onemario1234
Onemario1234 3 місяці тому
Nobody: Neutral Network at 2:26: I will now speen
silver takana
silver takana 3 місяці тому
This is like going to a test without learning but you get multiple tries
Abhishek S
Abhishek S 3 місяці тому
Fabulous 😘
Brace Pobo
Brace Pobo 3 місяці тому
thanks
Zachary Gossett
Zachary Gossett 3 місяці тому
Humans playing Snake: Im going to take the most direct path from one food to the next. Computer playing Snake: aight ima head to the top left corner everytime I grab a piece of food.
CN Yin
CN Yin 3 місяці тому
May I ask, do the food (red point) is generated randomly? If yes, then each generation of the snacks are obtain their food at different location, in my sense is that the AI can learn prevent touch the walls, bur how does it know where are the food?
N M
N M 3 місяці тому
AI taught me to never give up trying
Fedor Laputin
Fedor Laputin 3 місяці тому
how to do this visualize?
s0akf
s0akf 3 місяці тому
I like the part when theres a white line on the screen
Sami Kobrosli
Sami Kobrosli 3 місяці тому
#moonstop #mnstp to the moooon 🚀🚀🚀 HURRY UP ⬆️ ..
izdihar amiro
izdihar amiro 3 місяці тому
this video helps me to understand the nn better.thank you
Shangkorong Khaling
Shangkorong Khaling 3 місяці тому
Bro what do you take as inputs to the network?
Neiamenase
Neiamenase 3 місяці тому
reinforcement learning?
Arghya Shubhshiv
Arghya Shubhshiv 3 місяці тому
Lmao even the snakes understand that left is death
Haden Cain
Haden Cain 3 місяці тому
awesome music! :)
Brace Pobo
Brace Pobo 3 місяці тому
song names pls
Wojciech
Wojciech 4 місяці тому
5:53 music fits
Angelo Bruno
Angelo Bruno 4 місяці тому
Program it so that the AI builds its own neurons
Nick
Nick 4 місяці тому
50 yrs from now,AI is gonna be on a level better than human brain
peter barter
peter barter 4 місяці тому
Me : int num = string;
michael birks
michael birks 4 місяці тому
Cool, love it
Yamuna Suryawanshi
Yamuna Suryawanshi 4 місяці тому
Code Bullet !
Owen DEBEST
Owen DEBEST 4 місяці тому
What was your population size ? just curious :)
Owen DEBEST
Owen DEBEST 3 місяці тому
@whattheegg 89 my bad!
whattheegg 89
whattheegg 89 3 місяці тому
it says at the beginning that it has 2000 snakes for each generation
Leonardo Torres
Leonardo Torres 4 місяці тому
What do you mean by “the snake looks in 8 directions”? Where does the number 8 come from?. I understand that because it’s tracking distance to wall, tail and food (3x8=24), hence the input layer of the NN has 24 neurons, right?
Steeloso
Steeloso 4 місяці тому
Diagonal snake is cursed
Adweya Dalvi
Adweya Dalvi 4 місяці тому
3:19 it feels like the food is doing anime dodges.
Rémi Jacquemin
Rémi Jacquemin 4 місяці тому
Hey ! How do you generate the neural network on the left simultaneously ?? With blender or something ?
Jimmy Gervais
Jimmy Gervais 4 місяці тому
Do you have another video where you explain what are the inputs and what do the nodes actually do?
rmt3589
rmt3589 4 місяці тому
Make a chatbot that controls an animal in a 3d world?
Brains Loop
Brains Loop 4 місяці тому
Great video
r n
r n 4 місяці тому
I thought that you would use CNN + reinforcement learning to solve the problem. If you already know what is food and how to calculate the distance from it then wats the use of this neural network. I think this may not be the best application
Viktor Käll
Viktor Käll 4 місяці тому
Is the neural network a representation of a policy? This must be reinforcment learning right, you do this by Q-learning or something?
Iuri Guilherme
Iuri Guilherme 4 місяці тому
To get better results you have to teach them that it's tail is growing every time they eat food, so whenever they eat they have to record all the moves they did before and calculate an escape route which won't collide with any part of themselves. That should require more memory, of course
Iuri Guilherme
Iuri Guilherme 4 місяці тому
I'd say me, as an experienced player for many years - decades actually - would classify for a generation 14.1 But I'm still learning
snugglenuts
snugglenuts 4 місяці тому
That _was_ cool.
Mami_Sasazaki
Mami_Sasazaki 4 місяці тому
i thought why doesn't the snake just go straight for the food cell but it seems its learning, and its quite hard for me to digest that, cuz i be thinking in c++ and path finding algorithms i ll learn neural nets and get back here, need a plenty time tho :]
Nipun Kothare
Nipun Kothare 4 місяці тому
You can try awarding for time taken which would force it to move away from the stick to the wall strategy into some crazy methods .. might need more training though
Bobbies1
Bobbies1 4 місяці тому
Not even code bullet could do this
Senkottuvelan
Senkottuvelan 4 місяці тому
At gen 99, "It learned to get out of my system and took control of the nuclear launch systems of every country and at the gen 100, I saw a bright light outside my window!"
Sergey
Sergey 4 місяці тому
Let it see the whole field, training is gonna get longer, but it can then evolve the best algorithm ever
Shubham Patil
Shubham Patil 4 місяці тому
I have a doubt, the food the snake collects adds another unit to his body and ai watches for its tail then how it will calculate the path which his whole body is going on and to avoid it? The wall avoid is ok but then he gets stuck in loop like he doesn't know that if he strike to his body then he is dead Can you please explain this to me?
Onur Cirag1
Onur Cirag1 5 місяців тому
Tutorial pls
Jessie Janson
Jessie Janson 5 місяців тому
well, either the inputs are flawed, the neural net is too large, there simply were not enough generations for it to learn, or the mutations are not high enough.
Atharv Naphade
Atharv Naphade 5 місяців тому
SMH not even reinforcement learning : (
Yuvan Marimuthu
Yuvan Marimuthu 5 місяців тому
I would be curious to how the evolution would change if you changed the fitness function to account for the time interval between subsequent apples, rewarding the snake more for getting the Apple quicker
Mahant Kumar Tiwari
Mahant Kumar Tiwari 5 місяців тому
How do neutral networks reproduce? How have you implemented that?
Nguyễn Anh Nguyên
Nguyễn Anh Nguyên 5 місяців тому
Wow fun :)
Ulrik
Ulrik 5 місяців тому
Crazy how it already has a "hug the wall" strategy by gen 14
Amirhossein Rahmani
Amirhossein Rahmani 5 місяців тому
Does it mean that we need more than those inputs to make it better?
ibear
ibear 5 місяців тому
my last 2 brain cells during the test: 1:22
François Ritter
François Ritter 5 місяців тому
There must be some mathematical relationships between the snake length L and the position of the food at time t+q, t+(q-1), ... , t+1,t,t-1, t-2,..., t-p with p and q functions of L. I am confident there must be a dynamic threshold between these parameters beyond which it's game over, so the question is how close the algorithm gets to this threshold at each generation. The game cannot be beaten.
Dexter
Dexter 5 місяців тому
Wouldn't be better if you develop a reinforcement learning based on Q learning? Since the actions and states are discreet
An introduction to Reinforcement Learning
16:27
Arxiv Insights
Переглядів 404 тис.
10.2: Neural Networks: Perceptron Part 1 - The Nature of Code
44:39
The Coding Train
Переглядів 405 тис.
Женщина-драйв. Мужское / Женское. Выпуск от 13.09.2021
39:54
БЕРЕМЕННА В 16 ► НАСТЯ из МЫТИЩИ
1:23:58
CheAnD TV - Андрей Чехменок
Переглядів 267 тис.
Мой кот точно пытается меня убить
19:27
TheBrianMaps
Переглядів 2 млн
ЗАКАТОВАНА ДИТИНА. Стосується кожного. Ефір від 17.09.2021
1:29:10
Coding Adventure: Ant and Slime Simulations
17:54
Sebastian Lague
Переглядів 703 тис.
FORMATION DEEP LEARNING COMPLETE (2021)
30:57
Machine Learnia
Переглядів 196 тис.
Why do prime numbers make these spirals? | Dirichlet’s theorem
22:30
How to send a self-correcting message
20:05
3Blue1Brown
Переглядів 1 млн
Coding Adventure: Chess AI
29:22
Sebastian Lague
Переглядів 1,1 млн
15 Sorting Algorithms in 6 Minutes
5:50
Timo Bingmann
Переглядів 15 млн
Math Has a Fatal Flaw
34:00
Veritasium
Переглядів 11 млн
OpenAI Plays Hide and Seek…and Breaks The Game! 🤖
6:08
Two Minute Papers
Переглядів 6 млн
AI Learns to play Snake!
9:28
Chrispresso
Переглядів 68 тис.
Женщина-драйв. Мужское / Женское. Выпуск от 13.09.2021
39:54
БЕРЕМЕННА В 16 ► НАСТЯ из МЫТИЩИ
1:23:58
CheAnD TV - Андрей Чехменок
Переглядів 267 тис.
Мой кот точно пытается меня убить
19:27
TheBrianMaps
Переглядів 2 млн
ЗАКАТОВАНА ДИТИНА. Стосується кожного. Ефір від 17.09.2021
1:29:10