No video

Snake learns with NEUROEVOLUTION (implementing NEAT from scratch in C++)

  Рет қаралды 23,208

Tech With Nikola

Tech With Nikola

Күн бұрын

Пікірлер: 83
@prexen
@prexen 9 ай бұрын
Ive been trying to undestand NEAT for soooo many years now...i understood the concept but could not understand how to encode/evolve the networks. Thanks for that video, great explanation on the core concepts of NEAT. Also the way you present code is nice on the eyes...good fontsize and tweening, nice editiing.
@TechWithNikola
@TechWithNikola 9 ай бұрын
Thanks for the comment. I’m glad you’ve liked it. I ran into the same problem while I was trying to understand NEAT. It took me a long time, so I decided to share what I’ve learned.
@JoelRehra
@JoelRehra 10 ай бұрын
i love your content :) And a suggestion for how to make the algorithm work: Dont start each Training with a short snake, randomize the starting snake length and position (or save snapshots of a snake every now and then and use them as new starting pos in next generations) That way you can still train without running each generation until death but you get the benefit of it training with longer and longer snakes. Another benefit is it wont overfit the algorithm for short snakes :)
@TechWithNikola
@TechWithNikola 10 ай бұрын
I’m glad :-) That makes sense. Thanks for the suggestion. I’ll give it a go. I just didn’t understand one part: “without running each snake until death”. When would you stop the training? What would the fitness function look like?
@dineshasavsani154
@dineshasavsani154 10 ай бұрын
​​​​​@@TechWithNikolafor the first que. When you stop the training? You should just take no. for score like 130 when you hit stop the trainning and then you increase your bar like hit to 150 score and stop the training (end of the game like level 1 , level 2 cleared) For the Fitness function Here you start your snake size with 3 and score is 0 then you start with snake size like 10 or 20 and score zero and you can adjust the score when you stop training by reducing it Your this approach is good for train short snake again and again(overfitting with short snake) so ai has knowledge about how to play with short snake but when it become longer AI has very less knowledge about how to play with long snake compare to short snake so first you train with short snake then you train with long snake (you should try like level 1 when snake hit the size 20 level 1 cleared then increase snake size and go for level 2 and hit that mark and so on so like I mentioned above)
@Olaxan4
@Olaxan4 10 ай бұрын
What a video! Well presented and beautifully illustrated to the point even I could follow along. This channel will blow up, I predict -- I'll be sure to recommend your videos!
@TechWithNikola
@TechWithNikola 10 ай бұрын
I’m so glad you’ve liked it! Thanks a lot for recommending my channel, it will help a lot with the growth.
@peterwangsc
@peterwangsc 7 ай бұрын
Great results from these simple heuristics. I think the biggest point you overlooked for the inputs was to include the distance to the food in each of the four directions. Instead of a simple 0 or 1 value for whether the food exists in that direction, replace that value with the normalized distance to the food in each direction. The closer the food column is to the snake's head column, the closer to 1 that value is for the x direction facing the food. And likewise, the closer the food row is to the snake's head row, the closer to 1 that value is for the y direction facing the food. The 0 value still means the same thing, that there's no food in that direction, basically infinite distance from snake head to food position, but now that the value isn't just 1, but a normalized value between 0 and 1, you won't get such spiky behavior when the snake's head crosses the x or y position of the food, and the snake will always have knowledge of how far away it is from the food, just like the player has knowledge of the distance the snake is from the food. My intuition is that this simple change could produce some pretty "NEAT" strategies :) Let me know if you end up trying this solution and whether it produces better results. I'm not smart enough to code it up on my own.
@TechWithNikola
@TechWithNikola 7 ай бұрын
Thanks Peter. I have actually tried many variations with different kinds of inputs before publishing the video, one of them being the distance to the food (encoded as you suggested). Unfortunately, I didn't get much better results when training the network - some comments suggest that this is due to overfitting, and I should be training the network by generating random states that include long snakes, and reward it for each eaten food. I'm keen to try this at some point.
@peterwangsc
@peterwangsc 7 ай бұрын
@@TechWithNikola I took it upon myself to try this using my own crude python implementation and ended up finding some genomes that were able to seek out food very quickly by adding the proximity of the head to the food into the fitness function, so that the snake not only fits for a high score, but also sums the average proximity of head to food to the score. This gave me a lot more aggressive behavior for food seeking behavior, but I weighted it too much in relation to the score, because it will move towards food regardless of whether or not it needs to move through its own body to get to the food. So I was able to find some genomes that achieved scores of 65 on a 50x50 grid but it would only be because the food did not have the snake's body in the way, and 37 on a 10x10 grid was my highest score with a max step of 200 per game. I noticed that if i gave the snake more steps to train before getting cut off, even at 1000 steps, it would run into itself before the step limit and end the game. I think moving out of its own way to get to food will require an even more complex input embedding, complete with free spaces to move towards, so that it knows to leave room for itself to escape from a dead end. I'm not sure how to get much better results than that without increasing the input space by a lot, and thereby increasing the size of the neural net, requiring much longer training and much tighter mutation rates. I don't have the compute power or time to give this a try, but it's a fun experiment to run. Thanks for introducing this idea to me, it was a nice recommendation from the youtube algorithm after diving into a few machine learning videos recently.
@user-sb1uv3pe1y
@user-sb1uv3pe1y 10 ай бұрын
Love how genetics background in living organisms influence and inspire the technological solutions, programming included! It was insightful to see reasoning behind analysis of trial and error approach to training the snake. Well done 😄
@TechWithNikola
@TechWithNikola 10 ай бұрын
I'm so glad that you've enjoyed it Sara, and thank you for the kind words :) Indeed, it's amazing how much of real-life ideas are applied to computer science.
@typicalhog
@typicalhog 7 ай бұрын
Extremely detailed and high quality NEAT video! Great job!!
@TechWithNikola
@TechWithNikola 7 ай бұрын
Thanks a lot! I'm so happy to hear you've enjoyed it.
@tobecontinued.
@tobecontinued. 8 ай бұрын
Please continue making more videos on NEAT, such a fascinating topic!
@TechWithNikola
@TechWithNikola 8 ай бұрын
Thank you! I will consider more neat videos in the future if there’s enough interest.
@vadiks20032
@vadiks20032 5 ай бұрын
2:23 my brain itches thinking how you implemented the dot following the current point in a curve in visualization. that was probably a challenge
@TechWithNikola
@TechWithNikola 4 ай бұрын
I used an animation library called manim
@vadiks20032
@vadiks20032 4 ай бұрын
@@TechWithNikola oh right libraries exist. i forgot
@IgorSantarek
@IgorSantarek 10 ай бұрын
Thank you! I was searching exactly for this kind of knowledge!❤
@TechWithNikola
@TechWithNikola 10 ай бұрын
You're welcome! I'm so glad. :)
@TarcisioXavierGruppi
@TarcisioXavierGruppi Ай бұрын
Some ideas: 1. Double the input neuron count + 1, feed it the previous "frame" data as a kind of memory and the extra neuron you can use to input a value (lets say 1) for the first frame and another value (lets say zero) for all the next frames, this way the snake may learn to ignore the memory values for the first frame. 2. feed it the entire board, that's how we play snake, we know everything that is in the board at all times 3. feed it the entire board with the memory thing from idea one
@revimfadli4666
@revimfadli4666 Ай бұрын
Suggestion: Chrispresso and Ezra Anderson both managed to code snakes that solve the game by using inputs in all 8 directions including diagonals, instead of just 4, plus the tail's current direction. Since NEAT seems to struggle with lots of neurons, I'd add hacks like unifying the wall, food, and body inputs into +distance values for food and -distance values for body and wall. I'd also map the inputs relative to the head direction instead of using absolute map directions, so the network doesn't have to mutate the same function 4 or 8 times. Plus I could add the ratio between current length to maximum size, since strategies change as the snake gets longer.
@rafa_br34
@rafa_br34 9 ай бұрын
Very interesting, we need more videos like these.
@TechWithNikola
@TechWithNikola 9 ай бұрын
Thank you! I’ll try to make more videos like this going forward.
@semperzero
@semperzero 7 ай бұрын
It can be highly improved. I had the same issue as you and i struggled for months (ofc on and off) to make it actually learn. What actually worked was another meta genetic algorithm which optimized the hyperparameters of mutation (rate and power) for the neat algorithm, by running 200 trainings in parallel up to a certain generation number. Random search or grid search were also pretty good. Just gotta try random mutation rates and powers and re-start the training many times to go on the right learning path.
@TechWithNikola
@TechWithNikola 7 ай бұрын
That’s very interesting. Thanks a lot for the suggestion. I will give this a go at some point.
@danyavernik4498
@danyavernik4498 9 ай бұрын
Love your video, I would even try it following your example as a practice with neat
@TechWithNikola
@TechWithNikola 9 ай бұрын
Thank you Danya. I’m very happy to hear that you’ve loved it.
@jmirodg7094
@jmirodg7094 10 ай бұрын
Excellent, I really liked the approach, one of the common pitfalls is premature convergence of the population, there are a few tricks, like carefully managing the selection pressure and ensuring that you have it as constant as possible, then the choice of your operators are crucial as mutation /crossover operators with finite state limits your capacity of having a diverse genetic pool, if you suffer from premature convergence operators with infinite states should be preferred.
@TechWithNikola
@TechWithNikola 10 ай бұрын
Thank you, I'm glad you've enjoyed it :) Also, thanks for taking the time to suggest improvements. I'll porobably play a bit more with the configs at some point. What are the operators with infinite states?
@jmirodg7094
@jmirodg7094 10 ай бұрын
​@@TechWithNikola you can look in this document page 154 it is a bit old but should to the job, there might be more modern implemejntation now document:dspace.lib.cranfield.ac.uk/bitstream/handle/1826/93/J-M.Roger2003.pdf;sequence=2
@effestop
@effestop 10 ай бұрын
Great video, thx. I second the suggestion about starting with random length snake. My first approach would be also drastically increasing population size and mutation rate. Also dynamic management of hyper parameters so that you always have a fixed amount of species no matter the fitness and stagnation. I’m obsessed with neat and I’m learning python just for this reason now 😅
@TechWithNikola
@TechWithNikola 9 ай бұрын
Thanks, I'm glad you've liked the video! Thanks for the suggestions too. That makes sense. I will try the proposed updates at some point. I'm curious to see how that performs :)
@beyzanurkocak2678
@beyzanurkocak2678 6 ай бұрын
lots of work in this video, thank you!
@TechWithNikola
@TechWithNikola 6 ай бұрын
Indeed. It was a lot of work to research the topic, a lot to implement it, and also a lot to make a video :-) i’m glad when people appreciate it, so thank you taking the time to comment!
@DrawWithBrian
@DrawWithBrian 4 ай бұрын
Now I appreciate Machine Learning more than ever before because of this
@LolTroll217
@LolTroll217 6 ай бұрын
Love the video! Definitely agree with others that it would be very interesting to see more from you in regards to neat. Im sure you might have thought about it already or already tried implementing it, but different kinds of input as well as different kinds of fitness functions can be ways to "push" your model into a different behaviors. Im still learning as well with trying to train my own model, but with being careful to not overfit for certain behaviors that may be a result of survivorship bias, giving rewards or penalties for desired/undesired outcomes can be a way to get past or flat out avoid unintended behavior loops, For instance, record how long the snake as been X distance from any wall && how many moves its been since its last score, and if it reaches a certain threshold then penalize it. There are already some obvious problems with that solution, but just an idea off the top of my head.
@TechWithNikola
@TechWithNikola 6 ай бұрын
Thank you :-) I’d like to get back go this project at some point. There are a few ideas that I’d like to try. Maybe even experiment with other ML techniques. I think that one of the downsides of my current model is that it overfits was short snakes, so I’d like to try different fitness function. Maybe generate longer snakes and instead of scoring snake length, try to score hoe many times it ate food.
@juliansantos1900
@juliansantos1900 2 ай бұрын
Am thinking of making the metwork to take the entire screen and express everything as a strength from 0 to 1
@-mwolf
@-mwolf 18 күн бұрын
putting src behind patreon is laaaaame
@126sivgucsivanshgupta2
@126sivgucsivanshgupta2 6 ай бұрын
Nit pick ahead, while resolving the network (feeding inputs and getting outputs) u remove the un connected neurones, i think that might be wrong as that unconnected neuron still has an output (because of the bias) and will affect the output
@TechWithNikola
@TechWithNikola 6 ай бұрын
Yep, agreed. However, I don’t know if that matters much in practice? My feeling is that it doesn’t because that’s just a way to interpret the genotype, and if it was relevant I’d hope that other nodes would appear over time. I may be wrong though.
@ttrss
@ttrss 10 ай бұрын
quality explanations, and editing is on POINT
@TechWithNikola
@TechWithNikola 10 ай бұрын
Thank you!
@SeanMB100
@SeanMB100 7 ай бұрын
This was extremely helpful! My only thought on the snake was maybe you can scale the brain conditions "vision" under the different criteria. Early game should be food and avoid wall. Later game should be avoid yourself first and then food second. Maybe you can weight the inputs based on the length of the snake?
@TechWithNikola
@TechWithNikola 6 ай бұрын
Glad you've liked it. Yeah, a few people have suggested ways to train the snake, starting with levels, or more generally, generating random-length snake and rewarding it based on how many times it ate food, rather than length. I might try this at some point.
@rodrigoqteixeira
@rodrigoqteixeira 6 ай бұрын
Ideas to improve: Add more "rays" of decting food, walls and body unsetad of the 3 that it has now, merge the wall and body rays to the same (effectively making it detect body as wall), making the snake know the (relative to his head and direction) position of the apple, and tell it how close it is to the wall in the direction that it is closer. Another Snake Game AI video series (in portuguese) Part 1 (AI): kzfaq.info/get/bejne/l92qZMqZ1NG0Y58.html Part 2 (Monte Carlo): kzfaq.info/get/bejne/iZygarF7ut7Sf6M.html Part 3 (dijkstra pathfinder): kzfaq.info/get/bejne/jM-ZbLuatp7GlZc.html
@TechWithNikola
@TechWithNikola 6 ай бұрын
Thanks. I will watch these at some point. I do want to get back to this project again, and give it another go. FWIW, I have tried all of those suggestions without much progress. I think the problem is in overfitting the solution for short snakes.
@bishallamichhane8711
@bishallamichhane8711 24 күн бұрын
The video and explaination is fascinating.I have one question.Do you code these illustrations (graphs, curves) or are they made in video editing softwares?
@TechWithNikola
@TechWithNikola 22 күн бұрын
Thank you! I code most of them yeah, then edit it later. I use manim, powerpoint and adobe premiere to do that.
@Endelin
@Endelin 8 ай бұрын
That's pretty NEAT
@hiddenkendo
@hiddenkendo 16 күн бұрын
9:16 how do you detect a cycle?
@mahdihoseinzade1276
@mahdihoseinzade1276 10 ай бұрын
nice content man good job!
@TechWithNikola
@TechWithNikola 10 ай бұрын
Thanks man!
@that_one_salad9778
@that_one_salad9778 2 ай бұрын
3:12, looking at NEAT as simply a network training algorithm can miss a bit of nuance. NEAT optimizes a *topology* and *parameters* (weights), while Backpropagation and many other optimization routines only optimize *parameters.* So you could use NEAT as a topology generator, then fine-tune the network using Backpropagation or an STDP method if you're into SNNs.
@Nerthexx
@Nerthexx 10 ай бұрын
Cool video. One question, rather offtopic: how do you animate code changes?
@TechWithNikola
@TechWithNikola 10 ай бұрын
Thanks. I'm using Keynote on MacOS for code animation.
@ANTIMONcom
@ANTIMONcom 7 ай бұрын
Quick question, why do you implement it with the restriction that it cant have cycles? The original Neat paper never mentions that it has to be a feed forward network. This is a bit random, but about a year ago i compared how different Neat implementations handle this. Some enforce an asyclic graph, and move through the network one layer at a time. Some implementations have no consept of layers, and simply updates the network N times, where n is the minimum/maximum number of neurons that has to be visited before reaching the output. The original paper is not clear on how to deal with this, and so many people have made different solutions. I Kind of have to ask, how/why did you decide to not have the mutations add cycles?
@TechWithNikola
@TechWithNikola 7 ай бұрын
That's a great question. I have spent a lot of time trying to understand how original neat paper handles cycles, and as you've said it simply updates the network N times. I think the choice of using RNN (Recurrent Neural Networks) or a multilayer percepton networks shouldn't be coupled with how the network is trained. I see NEAT as a way to train the network, similar to backpropagation. My current implementation can work with RNNs as well, but I haven't implemented it - it wouldn't be too hard to add this functionality. As for why I decided to go with layered network for this project, I just thought it was easier to implement. I also have a better understanding of such networks (i know very little about RNNs), so I didn't want to diverge from that. Given that this is the first time I'm implementing NEAT, layered NNs made debugging easier.
@ANTIMONcom
@ANTIMONcom 7 ай бұрын
@@TechWithNikola thanks for answer 😃 Also congrats on a great video 👍
@mickeyspanish9709
@mickeyspanish9709 4 ай бұрын
This might be a dumb question, but I'm just starting out. Can this be done on Python? I've never used C++, and I'm still trying to lean Python.
@TechWithNikola
@TechWithNikola 4 ай бұрын
Hello, yes this can be done in python as well. In fact, python already has python-neat library that you can use out-of-box.
@angelg.s.3560
@angelg.s.3560 7 ай бұрын
Hi! nice video, I'm really hoping to learn much more about ai learning. I've coded in python (for almost a year, but done lots of projects), and i'm actually learning C# in hope of learning to work with Unity, and I'm really interested in the ai world. Where can I find the official NEAT documentation shown in the video? Thanks in advance!!
@TechWithNikola
@TechWithNikola 7 ай бұрын
Hi, thank you! AI is fun. The official NEAT whitepaper is here: nn.cs.utexas.edu/downloads/papers/stanley.cec02.pdf Just a heads up, it's not a very detailed paper and I've had to figure out a lot of details on my own and by reading lots of source code.
@SuperEmilio1994
@SuperEmilio1994 7 ай бұрын
Great video, One question: what is the song that start at 26:26? Thank you so much.
@TechWithNikola
@TechWithNikola 7 ай бұрын
Thank you. It's called "Matrika - Lazer Beam"
@Mlambolindo6
@Mlambolindo6 8 ай бұрын
With Neural Nets, I struggle on back propagation. I can do it on paper and all, but implementing it programmatically... Have you got tips on how I could approach this?
@TechWithNikola
@TechWithNikola 7 ай бұрын
Hi, apologies for the late response. It's been a very long time since I've looked at or implemented backpropagation. If you can do it by hand but you struggle to convert this to code, maybe I can try and help. Can you provide more information on what is troubling you? Have you tried representing the network as a matrix or vectors? If so, then it shouldn't be too hard to do this if you define operators such as dot product on the vector (multiplying each field with the corresponding field in another vector).
@Mlambolindo6
@Mlambolindo6 7 ай бұрын
Ohh, that sounds like a better idea. I tend to OOP everything. What I'd do is try represent everything as an object, from the nodes all the way to the layers. Thanks for the reply@@TechWithNikola
@JavierFausLlopis
@JavierFausLlopis 7 ай бұрын
You didi it again, to good to be true. Are you an AI? :D
@TechWithNikola
@TechWithNikola 7 ай бұрын
Haha thanks. Not an AI (but, would an AI say that?) 😀
@TedsWorld101
@TedsWorld101 8 ай бұрын
Aren’t they known as perceptrons?
@TechWithNikola
@TechWithNikola 7 ай бұрын
I assume you're referring to what I called neurons. Correct, they are known as perceptrons in mathematical models.
@rpraver1
@rpraver1 10 ай бұрын
Where can one find the associated code?
@TechWithNikola
@TechWithNikola 10 ай бұрын
Hi, source code is currently available for Pateron supporters: www.patreon.com/TechWithNikola I will probably make the neat library public in a couple of months.
@aa.castro
@aa.castro 4 ай бұрын
I would like to produce videos about programming, would it be possible to share some tips, such as how to assemble the graphics, the code part, and the formula animations?
@TechWithNikola
@TechWithNikola 4 ай бұрын
Hey, sure. I use a combination of Manim, powerpoint, keynote for code animations, adobe premiere for editing. I make formulas in powerpoint or Manim depending on the complexity.
@aa.castro
@aa.castro 4 ай бұрын
@@TechWithNikola thank you very much for the tips
@MichaelBarry-gz9xl
@MichaelBarry-gz9xl 6 күн бұрын
Give it an X,Y location of the food. You are uneccesarily restricting it's vision. Think of the players vision, not the snakes vision. Also give it the locations of its body. Give it ALL the information that the player has access to. You're not evolving a snake: You're evolving the players ability to play the game.
@MichaelBarry-gz9xl
@MichaelBarry-gz9xl 6 күн бұрын
The input would just be a 4-bit bitmap: food?, empty?, head? , body?. Along with an extra 4-bit value to store the current direction. (as the bitmap will lose this)
@Stuff447
@Stuff447 6 ай бұрын
22:37 the one that got -.75
@TechWithNikola
@TechWithNikola 6 ай бұрын
Yeah, I have used function smoothness for better rendering which sometimes adds noise. The more likely result here was 0.
How to train simple AIs to balance a double pendulum
24:59
Pezzza's Work
Рет қаралды 235 М.
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
What will he say ? 😱 #smarthome #cleaning #homecleaning #gadgets
01:00
а ты любишь париться?
00:41
KATYA KLON LIFE
Рет қаралды 3,6 МЛН
Transformer Neural Networks Derived from Scratch
18:08
Algorithmic Simplicity
Рет қаралды 136 М.
The moment we stopped understanding AI [AlexNet]
17:38
Welch Labs
Рет қаралды 966 М.
NEAT Algorithm Visually Explained
18:07
David Schäfer
Рет қаралды 3,9 М.
A.I. Learns to play Flappy Bird
7:46
Code Bullet
Рет қаралды 14 МЛН
Training an unbeatable AI in Trackmania
20:41
Yosh
Рет қаралды 13 МЛН
But, what is Virtual Memory?
20:11
Tech With Nikola
Рет қаралды 261 М.
Watching Neural Networks Learn
25:28
Emergent Garden
Рет қаралды 1,2 МЛН
AI Learns to Speedrun Mario
8:07
Kush Gupta
Рет қаралды 787 М.
I Built a Neural Network from Scratch
9:15
Green Code
Рет қаралды 259 М.
The Most Important Algorithm in Machine Learning
40:08
Artem Kirsanov
Рет қаралды 391 М.