Live Stream #176: Simple 2D Black Hole Simulation and NeuroEvolution with TensorFlow.js

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Applause] [Music] [Music] good morning hello and welcome to the coding train with hello that was like I had my finger over this little air hole here and I got a higher pitch sound amazing science my name is Daniel Schiffman and I'll be your host this morning on a Wednesday which is every Wednesday I will say thank you for being here at this regularly scheduled time I have been so consistent about this I even I'm amazing myself however I will report to you that there will be no live stream next Wednesday I'll be back two weeks from today so let me just say that from the outset I have a trip planned got some stuff going on but don't worry underneath here in my coding train oven is cooking a whole set of videos about working with data and api's that you have not seen yet because I have been recording a bunch of videos not during a live stream as an experiment but that's my little pitch if you want to join the coding train as a patron or YouTube member and get some sneak previews of that stuff also a big shout out and thank you to brilliant org who is the sponsor of today's livestream on brilliant orgs homepage you will see a challenge about relativity classical relativity we also have the term special relativity general relativity oh my goodness my mind since last Wednesday I think was last Wednesday right when the first image of the black hole came out has just exploded with Wonder and awe at the universe we live in and I've been spending a lot of time reading some papers watching some YouTube videos I am NOT a physicist I do not have a background in that but I have been sent down I was gonna say rabbit hole but I think the appropriate term here would be many black holes I'm not a merge by the way once you're in it there's no way to get out I am past the event horizon but so that's what I want to do in the first segment of today so the first segment today I'm going to talk a little bit about the recent black hole image and do a very crude kind of nature of codes style physics simulation of light approaching a black hole but I will be using Newtonian physics and so it won't actually be accurate but I will talk about what's missing there and a little bit about relativity and then kind of like pose that as a challenge to you to think about and maybe next week after I've had more time to read these papers and crunch some of these numbers maybe I can come back and make it more accurate or or or such the sound come once again I take take over the the chat I see the sound sync is off I don't know someday I'll fix that I apologize oh oh oh so so then segment two of today's live stream look at this I have a plan and everything and I'm sure it will go haywire think technical problems will I'll be stuck in the black hole for four hours and never get to the next part but this is my plan segment two I am going to program the data structure a stack and look at a quiz that's actually if you go to brilliant org slash coding train right now it'll take you to a page which is a quiz on stacks and there's also a quiz on queues and this is part of some new computer science course materials that brilliant has which has been really terrific for me to review a lot of stuff that maybe I did 15 years ago so it's wonderful for both kind of continuing education for me like I can like look at this stuff again or a very what I was saying on the slack chat this morning in the coding train patreon group that the last time I think I had to implement a stack like actually implemented without just using a you know kind of using an array as a stand-in for a stack or or some other you know stack from a library was probably 15 years ago oh I'm gonna have to do that today and look at programming my own stack data structure in the context of that quiz and kind of leave that as a challenge to you to take the quiz and then pending time I will have to say this a it is in the title of this video which I probably shouldn't have put it there but at a minimum I will show you some new examples that I'm working on that do neuro evolution with tensorflow j/s I would love to do a coding challenge around that that might come the next time but a I do have a few like finished examples that are basically ports of some of my previous examples there's a steering of steering one and a flappy bird one that I will show you please say this dot once I'm just pleased that you didn't ask me to like say this dot and dab or something Kufa that's really just not a thing not a thing not a thing I am your entertainer give me your instructions and I will entertain you I think we could just get started right what else do I don't have anything else to say in my introduction I'm like efficient and organized like a professional YouTube livestream or person this won't last and there's nothing it wasn't even that good okay so oh so here we go so I want to talk about it come on no I remember what I wanted to say now it's it links are in this video's description so I want to mention a couple things I think let me just find it I have it over here let me pull up today's live stream this will be fun so here we are well there's six hundred sixty-eight people watching okay so I want to talk about two things one I want to mention a huge thank you to Chris Orban and stem coding Chris Urban from Ohio State University runs a wonderful youtube channel called stem coding with a lot of p5.js and other coding activities themed around physics with real science like me I do my like but you've got an actual you've got science people in there's PhD students and graduate students a wonderful diverse array of presenters on that channel so I highly recommend you check that out the reason why I'm thanking Chris in particular is Chris wrote up this wonderful guide which I just it's actually a called simulating a black hole so one of the coding challenges on the stem coding YouTube channel is a coding challenge called slingshot with gravity and it's very similar to some examples I have in the nature of codebook about looking at gravitational attraction and simulating that in a 2d world using you know wheeler integration and kind of simply I don't why I'm using like was like hope use a fancy term using some basically x equals x plus speed kind of code to to simulate how bodies interact with gravity so Chris modified the code to kind of talk about black holes a little bit and there's some nice information in here and a lot of explanation a really nice formula here which I'll talk about for the radius of the event horizon yeah the swartz yeah so otherwise no I'm sorry it's a Schwartz teals rate radius I was like trying to think how do you pronounce that it's like spelled Schwarzschild but I think is Schwartz yield it's very hard to say it's like a tongue twister so I want to come I'm gonna come back to this when I do the video but so I also wanted to just plug if you are a teacher a wonderful thing that is happening this summer is called the Pathfinder summer Institute which is professional development in computer science and making at Indiana University Bloomington from July 14th to 19th so if you are a teacher and you're interested in this please apply and I'm happy to help you can reach out to me but I would suggest probably reaching out to a stem coding or Chris or bond on Twitter to ask any questions about how this works alright so what I wanted to mention that Here I am mentioning that is there some kind of like theory of relativity going on here with space-time I'm curving my space-time into myself in the past I'm not even this isn't playing I'm frozen be live there yeah I was trying to get to the part where I curved I went there we go but a bend I'm gonna bend space-time you could do it oh they're really like the there's got to be like less than 500 people watching now right come on there you go this would get this time so behind all right I'm gonna give up on this oh there it's happening okay okay we got both hat alright so let's see let me get out p5.js let me login to coating train let me go to high contrast and set the text size 36 maybe let's see if I can get away with a bigger window here black oh okay alright I think I'm ready to go let me check chats yes uh-huh by the way Simon I see I see in my little message box here Simon Tiger is typing something will be said soon but you should check out Simon Tigers YouTube channel as well who has also recently made some videos about black holes ah yes that's a good point yes Simon Says come to the whiteboard then back to the screen and then bend a space-time I'm learning about how my feet are younger than my head boy it's amazing this this like my mind is blown let's go over here alright so let's see what do i how am I gonna start this this from 1978 okay all right I think I have everything I need so image of black hole what's gonna be a good is it all is there a let's try this okay start with this and then what's the name of the group of it's the event horizon like the the name of the group the of the 200 scientists I want it like reference that remember how I was all prepared to had a plan event horizon scientists like it has a name like that the event horizon telescope maybe that's what it's called event horizon horizon telescope about organization yeah eh t Vann horizon telescope every Sun great okay thank you very much Oh someone asks someone asks are you going to simulate a 2d section of a 3d black hole or rather a genuine 2d black hole because the physics are very different in two plus one dimensions all right so I would say the answer to that question is neither I'm not really gonna do anything remotely close to accurate I'm gonna talk about what I've learned in terms of looking at some of these resources and I'm basically going to just do a Newtonian gravitational attraction example but kind of consider one body to be the black hole and one the other body to be a single photon or a beam of light and look at how that light curves as it approaches in two dimensions but I'll be basic this will basically be not accurate because it's sort of like assuming only classical relativity and I would need to think I need to like look at a part two would be to revise this to be more to actually be accurate but I think the idea can be the story can be told and it'll be an interesting kind of prompt for the audience who has a more maybe a rigorous scientific background to contribute here so that's kind of my plan so what if people are joining because they're gonna see some kind of black hole simulation prepare to be disappointed welcome to the coding train where our motto is prepare to be disappointed I need like a little like I need a little effect you press the button a little glistening winking but I cannot wink I cannot wake with my right eye my family finds this hysterical this is me in front of a black hole image attempting to wink with my right eye if I were inside a black hole you would not be able to see me doing that nor would I be able to communicate with you I would also be very very very very very dead or maybe not maybe I would just be millions of years in the future and I would be Matthew McConaughey yeah okay let me cycle the key hello and what hello welcome to a coding challenge simple very crude not so accurate to tee blaq ol simulation so I'm fascinated by this image this image which appeared on the internet a week ago from the event horizon telescope group of scientists who produce this image with many different telescopes all together timed with some kind of like nano Bassano bazillion au time precision to create this image there's over a thousand people watching that's not a record I have never broke in 2000 but uh it's not classical relative by the way Simon is giving oh sound effects or a hold on so much it stopped coming at me at once so I'm told the sound effects are a little bit loud so I'm going to turn that down Simon corrects me saying it is not classical relativity it's classical mechanics it's based on Galilean relativity okay thank you very much Tech riders asking how will that include neuro evolution it will not nor evolution is the third segment of today's livestream I mean I would love to neuro evolve a black hole but I don't think that's gonna happen everybody's telling my audio is out of sync I can't do anything about that I apologize the good news I would suggest if it bothers your tournament saw something else there will be an edited version of sort highlights if there are hearty highlights of today's livestream that will have the audio sync fixed by the way does everybody have the audio sync problem when when this has happened in the past I've been told that some people have in sync hit me baby no that's Britney Spears whatever um some people have it in sync and some people have it out of sink in before pronunciation complaints oh yeah I'm sure I pronounced whatever however you say that Galilean Galilean Galilean Callahan what did I say gun Lillian I think we should be able to say it the way what first of all gif gif gif that's how I just say to you you gif an alien mechanics people I got to start this everybody's telling me it's how to sync all right all right it's out of thing for everybody all right now so all right here we go [Music] hello and welcome to a coding challenge simple very simple very crude not so accurate 2d black hole simulation now I became interested in this topic a week ago when this image the first ever image of a black hole produced by the scientist from the event horizon telescope made this thing and it's amazing and I will include many links to explanations and papers that you can read up more about how this image was made and so I became fascinated this idea and sort of like in the sense that I have this book called nature of code which has a bunch of examples about how to look at the formula for gravitational attraction and then make a little 2-d simulation of that could we demonstrate some of the ideas in how this image was produced in how a black hole behaves in how a black hole bends space-time or how space-time is bent around the black hole and how light can't even escape and the speed of light and then there's relativity and there's special relativity and all this stuff there's so much stuff now as you know if you've watched this channel before I am NOT a physicist nor do I play one on YouTube and so this is not something that I would ever even remotely close to claiming to be an expert in so I am relying on the the good-heartedness of the Internet in particular Chris or bond from the YouTube channel stem coding which has a lot of wonderful coding challenges and video tutorials related to real science and p5.js and teaching about science through coding with a diverse set of speakers graduate students and various other people so I highly recommend you check out stem coding and Chris Urban on one of his examples one of their examples has a sling shot with gravity a demonstration much like my gravitational attraction nature of code example what I'm saying that's what I'm saying that's what I say edit edit point don't move don't move all those we heard to edit don't move it moving what was I gonna say next so Chris recently when that image came out made a slightly modified version of that example too talk about some of the basic mechanics of how a black hole behaves and there's more explanation on this page and you can link here to see the modified at p5.js code so I highly recommend you check that out there's also a wonderful video by veritasium wait did I say that right I couldn't say galilean wait hold on there is also a wonderful video by veritasium which actually came out before the image was released which is kind of amazing then there's also a follow-up video of when the image was released if you don't know the veritasium youtube channel what's you know this channel you know that channel probably anyway that has this image in it showing how the photons pass rays of light bend around a black hole it's like so insane that they bend around meaning if I'm standing I mean I couldn't if I was standing on the black hole I couldn't see the black back of my head because the light would Bend around it so I want to see if I can create something along the lines of this to demonstrate that idea this is like a tiny little inkling piece of the puzzle of all of the science and work that went into producing that image so I'm going to do this I'm really just going to use Newtonian physics basic mechanics I'm not gonna get into relativity and kind of modifying how time slows down the faster you're moving what did I just say yes I'm not going to get I'm not going to get into special relativity and how time slows down as you're moving faster I would like to come back to that what I would like to do is just make something that gets at this central idea and then as a challenge to you I hope you will modify it I'm going to give you some resources to how to look into more of the science of how it actually works and then maybe I'll come back next time and do a follow-up video and in theory I could just keep going with this and make it 3d and then actually try to model how that image will produce this could go on forever I don't know how far I'll get but you the viewing on hopefully will either will take it from me and make something wonderful which then I can share our cool I will oops I'll include links to these other resources in the video's description and some of these came from veritasium Barrett Oh I'll include links to what was I trying to say veritasium veritasium veritasium veritasium it's fair to say I will include links to other resources for you some of which I got from the veritasium video as well that you can read up on and I'll be reading these a lot over the next week or so as well so one is you should check out this how to draw a black hole which goes through a lot of the math and then some nice really computer graphics shader style 3d tricks that you can think about looking through and you've got something here that actually looks quite a bit like the artistic rendering of a black hole in the film interstellar so how they recommend you check out this article by I'm sorry I don't know the person's name by rattin ELLs on github you can also read this 1979 paper image of a spherical black hole within accretion disk so I'll talk about what accretion disk is in a moment if you're not familiar with it but you can see kind of some of the math about the photon trajectories and so some of these formulas would help me make they're over here so any form of those would help me make what I'm about to do a bit more accurate possibly and then oh this was something that was sent to me by K week Mon in in the chat which has some of the formulas also the equations for the radial motion of photons so I'm gonna do this without implementing the sort of with this with any kind of scientific precision just cuz I get out get out of frame work am i apologizing and giving you enough Gavi aughts I think that I am ready to move on I took some notes I watched very testy in this video and I took some notes all right okay let's diagram the sort of elements that I want to consider in my simulation slash visual visualization and of course the real world Oh 1978 hold on oh it says 9000 the book is nighttime all right sorry correction the paper is 19 from 1978 here machen will dub the thin when the Edit version comes out 1978 from 1978 1978 there there's a few audio samples you can dub the overview saying 1979 does the coding train read chat ask slush puppy every once in a while you just have to get lucky I guess let me try to dry it done let me try to diagram out some of the elements of what I want to include in my part simulation heart visualization so in two dimensions let's say we have a black hole here's my buckle and I'm gonna do this vertically because I think that'll work actually it doesn't matter here's my here's my black hole now what do I mean by this being the black hole first of all a a black hole is not something we can see that's why it's called a black hole the reason why it's black and we can't see it is because light the force of gravity the black hole is so strong that light can't even escape from it so you could not from inside the black hole there's no way you could communicate outwards so that's why it appears black it's often referred to as a shadow and maybe I'll sort of like get to that in a sense but we have so but one thing it has the reason why it's the force of gravity is so strong is it's massive supermassive and so this black hole has a mass now I wrote this Ted took notes no like I don't know this might be the second time ever I've done this but I don't know where I wrote this down it's not these notes oh yeah see there oh but I could have memorized this now this being if this is uh if I'm talking about Sagittarius a star which is the name of the black hole of the photo the image was made of the mass of this is 2.6 million Suns give or take I think you know that's a sort of estimation approximately give or take you know I think it's point 2 on the some other page on the internet that I read earlier today 2.6 million Suns so massive that's important there is this idea of the event horizon the event horizon being the path oops sorry I hope people will correct me as I get things wrong by the way the event horizon being the path around the black hole sort of edge at which anything inside can never escape including light nothing great so another sort of key element here is C otherwise known as the speed of light this is the fastest anything in the known universe can travel nothing can travel faster than the speed of light and there is also an actual number there which is 299,792,458 meters per second okay that's gonna be great when I put that into a variable right here's my variable pixels for a second that will work I'm sure that'll work it was gonna be great so the event horizon now there is actually amazingly a formula for what this so you've analyzed in sorry is at a certain distance from the center we can call this a radius this is known as the Schwartz shield radius which I have a real problem saying but hopefully I'm pronouncing that close to correct the Schwarzschild radius now there is actually a formula to calculate this that's also in my notes it is the short shield radius is did I not write it down I have it in my code Oh Jane Christopher bond page hold on they didn't write that down hold on Oh people are telling me that neutrinos travel faster than the speed of light all right oh no this is not plugged in hold on my soundboard is not plugged in cancel uh here we go that formula is right here in Chris orbán's article so we have that as the radius the Schwarzschild radius equals two times the universal gravitational constant times the mass the mass of a black hole and divided by the speed of light squared okay so we could actually do this calculation right we could take this number square it ah we need one other thing we need G we need G the universal gravity gravitational constant let me okay good right not all black holes are supermassive that's correct sorry I'm referring to this black hole which is a supermassive black hole wait I want to find out the universal which is G written out on this page here G right okay all right so this is right all right so now we I'm looking at G the gravitational constant okay so G the gravitational constant is thank you also to Chris or Bunz article six point six seven times 10 to the negative 11 is the value for the gravitational constant so now we have all these values and I could sit here and I could calculate that radius oh let's should we do it could somebody do that for me you have some kind of big calculator I kind of probably really wish I had a prop this would be an awesome time we have a prop right now I could go over here and pull out a giant oh is this the enemy and like talking about our people trolling me or am i did I get it wrong and I'm not I thought I'm talking about Sagittarius a star hold on now I have to be sure about this right isn't that the one this is Sagittarius a star right sad okay that's that's the correct black hole right somebody's there is also they're also doing an image of another one I forgot what it's called I might have written it down somewhere but I don't remember okay I'm get this m87 no but m87 okay I'm so confused I thought m87 is the galaxy I'm so confused [Music] it's m87 yeah I just got confused and Sagittarius a star is the blurrier one oh shoot oh I didn't write that down anywhere it does hey it doesn't matter but it matters for accuracy okay shoot yeah I don't know how to fix that but yeah m87 is the galaxy the black hole at the center that's why we say m87 star right star Messier yeah okay all right I messed up let's see if I can do some kind of magic editing tricks yeah okay so a couple options would be one this is one option oops I said Sagittarius a star wait no stop I just said Sagittarius a star it's m87 there's two black holes that the scientists are working again image of the first one was m87 star ma said in being the galaxy and the black hole being at the center of it and then they're also they're working on an image of Sagittarius 87 let me try another thing this black hole this particular black hole a supermassive black hole not all black holes are supermassive but this one known as m87 star named for the m87 galaxy at which this black hole is at the center and its mass being approximately 2.6 million Suns give or take let you figure that out correct it somehow later all right where was I in this mmm-hmm no way I was gonna calculate this number so as a challenge to you calculate this number and leave it for me in the comments so okay so while you might think of the black hole as sort of like nothing it's nothing nothing can escape from there it's the end of everything this is not actually the case it's so much mass and the reason why it's so much mass is this in this one is particularly active I mean it's like just sucking up more stuff from the universe around it and the stuff that it's sucking up is orbiting it in what's known as the accretion disk so I'm gonna make make a little diagram a little bit outside here so this known as the accretion disk it's a lot of matter a lot of space stuff orbiting around and it's it's at a far enough distance that it's not being sucked in but here on the edge right this is the border of we're past this it will be sucked in eventually past the event horizon from which it cannot escape and outside here it's a little bit maybe it's a little bit more stable but there is actually also a measurement of the what that distance from the center is right remember this is the Schwarzschild radius from this formula and then the accretion disks sort of inner edge is at breaking news oh I really could have memorized this one three three short sealed radii so three RS so that's what's over here so these are things that I would first want to try to visualize right I could draw an ellipse in my code with a radius based on this formula and then I could also draw the accretion disk sort of outwards at three times that radius what's the next thing I want to look at because I also want to look at all right now right right right all right okay good Thank You Simon for the oh did I say sagitarius the 87 I'm the worst it's very hard to do this well actually maybe it's not yeah I just have an impossible time I gotta get this section done by 11:30 that's my time 11:30 is my break usually it ends up being more around noon all right well look at this I gotta allow this comment to come together all right so the next thing interestingly enough there's another ring that we could consider so we've got the event horizon we've got the accretion disk but what is the distance at which a photon of light would orbit the black hole after all a photon of light doesn't have any mass so all this matter and the accretion disk actually has mass the photon of light traveling at the speed of light doesn't have any mass or the mass of zero oh boy oh boy we're getting into relativity aren't we but it so happens that at 1.5 a Schwarzschild radius radii Schwartzel why do you say that radius radii at about halfway a 1.5 is where a ray of light a photon a photon particle would orbit so the question is here how would we look at this if light right the idea of seeing something is seeing light bounce off of something in travel path back into our eyes if light travels so fast that we it's barely like perceptible that time how would we look at something so massive so far away and so that's why there's this idea of a shadow so what if there's we wouldn't be able to see this if there wasn't the sort of matter and plasma and all the other stuff going around it what we're what we're able to see is that stuff with the shadow of the black hole and I think a piece of what's interesting about this it really lot really depends at the angle that you're looking at and you and there's so much to this but that's a bit beyond the scope of where I am capable and want to go right here in this video so what I want to do is diagram all of this and then imagine maybe I'll start I don't know where I'll start it from I'll just start from over here imagine we have a bunch of our telescopes on earth so in order to image this you would need as the scientists discovered a telescope the size of the earth and what the scientists were able to do was use many telescopes all perfectly sink all traveling or not traveling but the earth is also rotating so you have a bunch of different views to be able to compile and build this image from all the data from those telescopes so we could maybe simulate this idea in the most basic way by thinking of the signals from the telescope or beams of light traveling at the speed of light at this black hole how would we what would we see reflected back how do we see the light that is passing around here so let's see what happens in theory what we would see right so let's say okay in theory what we would see let's say we have a nice ray of light perfectly sort of aligned with the center of the black hole it's going to move in that direction past the event horizon never to be seen or heard from again something a little bit off would come like this and the gravitational pull would pull that ray of light in it would also go past the event horizon never to be seen again but at a certain point if we get a certain point outside it's going to be it's going to bend not go past the event horizon but kind of end up in that orbit or spin out into infinity or something like that and it actually so happens this is thank you to the veritasium video which explains this so succinctly that the the distance there for that ray of light is 2.6 Schwarzschild radii if the two point six times the Schwarzschild radius I don't know so in theory if I plug all these numbers and use my average force of gravity formula will see exactly this happen haha definitely not but that's the idea we're missing an important point fact at time and space bends and time slows down but I won't that's that's for you to think about and for me made me to come back to you all right okay thank you oh boy I've gotten so many things wrong in this explanation I really need to prepare more okay first of all simon is giving me a really excellent correction so I'm you know I'm gonna need to talk about that for a second thank you for that this correction about the orbits the difference between the the light orbits and the accretion the the matter in the accretion disk also the mass so mass of Sagittarius a star okay all right ah m87 star 2.4 billion suns yeah oh boy oh boy Mattia when you're looking at this later I would be happy to do recumbent or like some explainer tea stuff over top of this maybe narrated a bit because I'm making some serious mistakes here so let me come back to this I really got my black holes mixed up there are two black holes that I'm talking about I mean I'm only talking about what I'm very sorry I have really gotten my black holes mixed up this is a thing that happens to me almost every day can't keep track of all these black holes in our universe the image that you that I showed at the beginning of this video is from m87 or the Oh cameras going off look it's a black hole you just saw the second image the third image ever of a black hole cuz I also made one of the Rubik's Cube the the black hole that I am talking about in this scenario the one that the image was published the one about which the image was published the one that is in the image that was published is m87 star in other words the black hole the supermassive black hole not all black holes are supermassive but this one is supermassive at the center of the m87 galaxy that one is mass is approximately 2.4 billion suns the other black hole that I mistakenly referred to which is also currently being imaged and I'm sure I'm spelling this wrong is Sagittarius a star which is from the Sagittarius galaxy and that one is a bit smaller is approximately 2.6 million Suns which by the way when I say a bit smaller that's a lot smaller yeah it's often trusted we often think like oh it's just this one's a million this one's a billion that's just like up a little bit more than a million but no no a billion is a lot there's like a thousand millions and a billion or something like that it's amazing man so now you can revise your calculations here for m87 star but the math would all hold true for either one of those black holes now right right some other Corrections here some other corrections or perhaps further explanation number what the photon orbit which is here at one point five short shield radius is unstable so the photons circling around will eventually get sucked into the black hole or travel out towards infinity and that's kind of what I'm showing here with one of these that ends up in that order orbit it's going to do one of the or the other the the accretion disk is a more stable orbit I should also say that I'm saying this curves but it doesn't really curve see the fabric of space and time is curved there is things aren't attracted it's just correct you could think of it as like if I had a flat surface here and I put a giant bowling ball in it it would push that flat surface down and a little like ping-pong ball on the edge would roll towards it because it curved the fabric of space and time so this is actually a straight line through the curved space-time continuum let's get back to 2d canvas in javascript in the browser shall we oh it's not the Sagittarius galaxy I don't why did they call these things with inconsistent names 2,400 billions all right all right sorry 20 I got em 87th masse wrong party me actually this is not correct approximately 2400 billion suns which is quite a bit more than 2.6 million Suns a lot a lot more super massively more yeah all right Milky Way yeah okay here we go I think did I get it wrong trillion does that not okay I see her other correction Simon the black hole is unnamed the black hole is Sagittarius a star got it 6.5 billion solar masses okay 6.5 billion two point four trillion Sun masses not the black hole that's the galaxy oh so confusing it the mass of the galaxy ah okay I did kind of look this stuff up I just kind of let me correct this one more time I think I would get it right now do you like watching videos where the information is incorrect for a while then it just keeps getting corrected that's not good I don't think all right I have an idea doo-doo-doo-doo stop I have traveled to you from the future to come back into this video to correct what I just said but I'm actually recording this later so it'll look a little out of place but that's totally appropriate for this space-time continuum stuff to be clear I am named the incorrect black hole so I named a black hole called Sagittarius a star which is a black hole in the Milky Way galaxy and the scientists are currently trying to get it working on getting an image of that that's a smaller black hole about 2.4 million times the mass of the Sun something like that 2.4 million Suns this black hole the one that the image was published about and that I want to talk about in this video is called m87 star now m87 is the name of the galaxy the black hole itself is unnamed so m87 star and its mass is 2.6 no 6.50 now oh boy I gotta go wash my hand it's mass sorry I should not use my fingers it's mass is 6.5 billion suns okay solar masses so it's big really big 6.5 billion being a lot more than Sagittarius a star which is 2.4 million there we go returning to the scene of the crime where the wrong stuff will be written for a while and then it will suddenly appear correct again yeah my Google is for the entire galaxy it's not m87 star it's just m87 unnamed right yeah there's no m87 stars did I say star that was just that's a mistake we can all live with right I did talk about the unstable orbit Simon that will make it in that will make it in I mean just in case I can say oh they already have the image published already well whatever I'm behind I'm behind the times oh oh right I put asteroid baby I didn't say started first are there sorry no no m87 no ma no I'm sorry sorry no star no start just m87 that's the name of the galaxy Sagittarius a star's name a black hole Milky Way galaxy this is unnamed maybe the name by now I'm sure I hailed out at the comments how about a hobbits okay is the programming that is important you think oh it is a me maybe seven star who knows who knows no the star means it's something interesting and anomaly the black hole is unnamed okay alright okay we're gonna move on to the code now all right oh here 300 light years later Here I am about to maybe write some code so let's see we need some variables number one is we need the speed of light 299 792 458 there we go I don't need the commas right because you know this is code so there we go there we go now we got the speed of light excellent things are going very well so far in my coding now we need the universal gravitational constant M G which is six point six seven times power 10 to the negative 11 yeah what could go wrong there this seems exactly right there we go now I need to have my RS my Schwartz field radius which is two times G times o the mass okay now I need mm a constant M is the mass that was six point five billion that's new to zeros some more zeros some more zeros I don't know how much is the mass of the Sun mass of the Sun all right you see the folly here one folly here is while these numbers are incredibly meaningful and important they're not gonna do me so much good here in my p5.js editor javascript a program so what I'm gonna do is make up some numbers we're gonna create our own two-dimensional universe that has a speed of light that has a universal gravitational constant and in fact rather than put the mass of the black hole right here I'm gonna make a black coal class so let's just arbitrarily say like do something approximating this like the speed of light is 30 and the universal gravitational constant is like 6 why not right so and now let me also go and make I should have done this beforehand let me also go and make a you also go and make a black hole je s file oops black hole is and I'm going to add that to my index of HTML and then I'm going to say class black hole and oh I need a constructor just an X Y we need opes be even oh yeah let me use a P vector for this so I'm going to say this stop position equals create vector X comma Y and then I'm gonna do a function oh it now aha and this now I need a mass this dot mass equals six point five in our measurements are in billions six point five and then it's radius the Schwarzschild radius is what I say two times G times this dot mass divided by C times C now I need a little more space here to be able to see more code but fortunately actually I can do this and let's put these in parentheses and so now what I can do is I can write show and what am I gonna do let's make fill 0 let's fill in the black hole with the color black let's draw a circle at this cause X this dot pause dot Y with the radiate rates for shield radius times two and so now if and I guess I should might as well pass in an argument here for mass if I were to in my main program say let black hole let's call it at m87 we're gonna call our black hole m87 and 87 equals a new black hole I'm gonna place it at sort of like the top I'll let actually the left of my canvas so let's place it at like 100 comma 320 and we're gonna give it a mass of what I say 6.5 and then I'm gonna say an 87 dot show we have a black hole Oh R is not defined this stop-stop there we go that fixes that and where where where is my black hole I guess my black hole has sucked all the light out of it so I can't see it let's take a look at what this dot RS is ah way too tiny right so my units are really kind of all over the place so let's make some units that make more sense for our pixel space let's make the mass of the black hole probably much bigger like 6500 there we go look thank you good night I have now made my simulation of eyeball this is the first ever known image of a black hole made in p5.js I need to blow my nose all right just because I don't have a lot of pixel space to work with right now let's make let's make the mass 1/10 as large and we can see there's my much smaller black hole okay fine let's make it a thousand let me put on auto-refresh which will be nicer and five thousand and three thousand all right this is looking good to me that's the size of the black hole that I want okay let's now draw the accretion disk so let's make some sort of accretion disk so I'm gonna say I am gonna say ellipse I'm actually going to sorry I'm gonna say no fill stroke and let's make it kind of greyish and I'm going to draw the accretion disk as a circle this pause X this pause dot y this dot RS x this just in from this just in from the chat I forgot about this I can use I did not know I forgot about this I can say ellipse mode radius which should now allow me to and comment this out just use the radius as the ellipse perfect as the just used the radius value as the size of the circle Thank You Simon Tiger for that great correction all right so now I'm gonna say this circle is that the radius times three and maybe I will give it a stroke sorry stroke weight of eight oh and I want no stroke here and let's make it a stroke weight of like 24 or 64 and let's give it some alpha and there we go alright so that's kind of my visualization of the accretion disk now the truth of the matter is this is not exactly correct because I have a feeling V that distance is right here in the center whereas the stroke weight kind of fans out the thickness there so it's a little bit off and I probably should you know be a little bit more accurate about that I guess what I could do is just add to the radius itself the half of the stroke weight so if I add 32 there there we go I think that's I think that's accurate now I mean know nothing about this is really accurate all right now let's draw the unstable photon orbit so that would be at 1.5 of the radius so I can do the same exact thing but this time at 1.5 maybe I want to make that one a little less thick myself a little more space here so that make the 16 and make this oh and make this 32 and maybe this one should be like some kind of color cuz life is so good at design you know some kind of oranges truth it was better before I guess some kind of oranges color okay look at this we now have the black hole the accretion disk and a sort of photon or betray now the other thing that I want to do is I want to start a bunch of photons coming from I'm gonna try to like zoom into this here I want to have them coming from over here towards the black hole I need to know how far I can't jump up because my green screen only goes this I don't know how how far up does it need to be for it to not really curve but travel in a straight line around the path of curved path of state space of time to end up in an orbit to either spiral out to infinity or end up into the call so that we said was 2 point 6 2 point 6 Schwartz yield radiate radius okay so now what I'm going to do is I'm going to create another class I'm gonna call this photon got J s photon j s will be a photon make a constructor this will also have a position I'll give it an X and a Y and let's give it a little let's make sure we include it in our HTML file and let's also write a show function which says let's just make this stroke weight for a point this dot paws dot X this dot paws dot Y and Stroke let's let's look to make this distinctly red just for right now actually make it yeah all right so now what I could do potentially is let me make an array it could be I'm gonna call it light may I just call it photons particles what I'll call particles whatever and I want to say for let y equal 320 Y is less than it really this is height divided by 2 y is less than or Y is greater than height divided by 2 minus so I'm actually gonna put these in two variables so I'll make more sense let's say start is height divided by two and is height divided by two minus m87 s Schwarzschild radius times two point six height divided by what I divided by 2 so y equals actually I can start it at the end and it's less than start and why plus equal like every 10 pixels so let's do that every 10 pixels and what I want to do is say particles index I equals a new photon at where X will be just with minus some amount with - like 20 comma Y what's wrong there I is not defined Oh particles oh okay so I also need it I'll just say particles dot push so I can add things to the array with the push function because I'm not using I to iterate that makes more sense and now here in the draw function I can say for every photon of particles pho so now there we go I have all of my points of light that I want to send out at the speed of light towards the black hole and watch them follow their straight-line path through the curved space-time state so now let me give them a velocity so if I go back into my photon whoops I'm also going to say this dot velocity equals create vector traveling at the speed of light in the negative x-direction negative C comma zero then I am going to do but what's not constructor an update function in which case this stop position dot add this dot velocity and what I can do now is in sketch j/s is I can say P dot update ready here we go everybody all right so what is the speed of light here 30 30 pixels per frame is gonna be going quite slowly actually like the frame rate seems slow 30 pixels per 4 that's not really a nice way to watch an animation so I'm gonna include another variable them to call it DT this is the delta time step so even though my frame animation frame goes one at a time thirty frames per second maybe I only want to travel ahead a little bit in time so let's try 0.1 a tenth and so all I need to do is the what I need to adjust by that delta T is the photons velocity so mmm I guess I'm gonna make a copy of this so I will just say this dot Delta V equals this dot velocity dot copy and then this dot Delta this is a little bit awkward about using P vector or multiply by DT and then I actually oh not this dot I just want a Delta V and then multiply that by DT and then add that instead so now I can see there we go there are the photons traveling at the speed of light and I see and then they go ah but they're not curving they're not changing so now break for a second okay now we just need to have them bent we need to have them Bend according to the curvature of space-time this is now the moment where it makes sense for me to go read one of those papers and try to look at those formula but I'm going to actually see what happens if I just apply Newtonian gravitational attraction so I need this photon to basically have a force pointed in the direction of the center of the black hole that it accelerates its velocity towards it so and but but an interesting side note here and this was my guess is like an approximation of relativity is this we can't go faster than the speed of light I out it rule really only adjusting the direction so that the speed is going to remain constant at the speed of light again means your caveats to how inaccurate this is but it's a starting point so are you so what I want I want to add a function basically I want to be able to say I have some sort of force which is m87 I like you know curve pull I don't know it's a good say attract I'm just gonna say pull as a kind of arbitrary word pull that particle and then I want to say particle apply force that force so this is very similar to how I approach a very basic physics simulation in my nature of code example so you could refer back to the chapters 1 & 2 of that book which basically which is essentially what I've got here now so what I need is a function now in the black hole object called pull that expects expects a photon and so what I need is first thing I need to calculate this force vector is a vector that points from the particle itself towards the center of the black hole so that's easy because I can take this position minus this position and that gives me a vector and that gives me a vector that gives me a vector in the right direction so I can say force equals P v vector dot subtract this dot position minus photon dot position great so now I want to normalize that force to give it unit of length one then oh I'm in the wrong I'm in the wrong camera thank you uh okay thank goodness so to make that vector I just want to I'm going to cut make effect a variable called force and I want to say p5 vector dot subtract what is it this dot position the position of the black hole - photon dot position the position of that particular photon so that's the vector now what I need to calculate the force of gravity according to Newtonian of the Newtonian laws of motion is that force of gravity equals the universal gravitational constant G times the mass of one object guess what that's this times the mass of another object ok what's the mass of this oh we've got a problem now so the mass of this is well zero but the force isn't zero hmm so this makes kind of no sense the good news for us is we're going to use this in acceleration and it's because we also have force equals mass times acceleration and I want to know the acceleration of this acceleration equals force divided by mass so this mass will just get divided out and of course I couldn't divide by zero and this is this is why this is the essence of why I'm kind of going down the wrong direction here to do this accurately but it's a starting point I can just consider this right now the G times the mass of the black hole itself divided by R squared or the distance of between the photon and the black hole R squared or distance squared so I have that value actually in my code because I have R is the magnitude of the force then I could just say force dot set oh I'm sorry then I could get the act I have R which is the magnitude of the force then I could actually calculate that force magnitude which is the force of gravity equals what I say G times this mass divided by a R / R times R and now I could say force dot set magnitude F of G right Oh photo photon and then I could just say this dot velocity ad force because what I want to do huh Oh photon dot velocity then I just want to add I want to add that force to the the photons velocity okay P dot apply force is not a function so actually I've simplified this I what I could just do is get rid of this whole extra step of returning the vector and I could just do this there you see those photons so let's be able to see this a bit better and we'll see why this is not correct but first let me do let me do a couple things one let me say let me make these variables and let me let me draw a lines so let me say stroke zero line from zero start zero end and sorry sorry with at sorry zero start with start and let me say stroke wait one and then let me also do the same for the end you just want to be able to see where these are so this is showing that spot now you can see oh and guess what the speed of light the photon should always be traveling at the speed of light so I forgot about that so this is a huge hack here I'm going to say right here when I do that pull right in the black hole I do that pull I'm gonna say photon velocity dot set magnitude see o set magazine here she's just limit it let's just limit it it could slow down I suppose it could slow down let's just limit it and see what happens Oh everything got sucked in look at that the black hole sucked it all in okay so a couple things one is so this is good in other words this is kind of a nice little simulation in the sense that the force is so strong even in my very crude simulation that it's never going to escape now next next up let's add a little trail here so the photon I'm going to give it a variable called this dot history and I am going to every time it updates its position or every time before it updates its position I'm going to say history dot push this dot pause copy and so then also this dot history then also let's just say if this dot history dot length is greater than I don't know 100 this stud history got splice let's just remove the oldest element and now I can say stroke 255 a stroke zero stroke weight one begin shape and shape no fill for every vector in history vertex V dot X the type B dot why this dot history so now we're able to see those paths as well as they've and so again I don't have it right we can give ourselves a much longer history here but I can at least at a very minimum now I can look and try to see what I can try to where how this is working a bit more I don't know I just said that part out all right let me give the history a little bit more oomph to it and let's what I'm gonna do here now also is I am going to start some particles from higher than end so I'm going to say and divided I'm gonna just going to start all the way from zero like the top and then let's space them out by a little bit more and here we go and let's actually let's give myself a little bit more space to work with here and see what we've got Wow nothing escaped look at that nothing escaped my black hole let's give it a smaller let's give it a smaller again it's not accurate I've got this wrong because I'm not taking two accounts but I'm visualizing the idea here let's try some let's try what happens if I make the gravitational pull like constant smaller let's see if we can get anything to escape come on rays of light come back to the telescope there we go you can see it's there in the photon orbit oh but back into the center so we're getting the idea do we have something sort of similar to this I think we do change the color to red try shrinking the mass okay let's go back and change you want the color to be red of the photons paths so I can just take this out let's also make this stroke weight a little bit wider so I can see it and then let me let me give myself here let me let me actually give myself much more space here so I'm gonna say window with whatever space I have window height and let's also make this even less and see what happens and then I'm gonna do share share let's do fullscreen and here we go oops oh I have it hard-coded at 320 hold on one thing I need to fix so what do I have hard-coded at 320 this height divided by two there we go and let me go to fullscreen here we go all right here is my black hole simulation rays of light go I think I need to move it over this is bothering me sorry everybody let's move it over so we can have a little bit more space to work with 300 there we go all right now of course you can see this should be where they're able to escape but my simulation is completely off so I should take out those start and end lines and just watch this go there it is the curvature of space and time center of the black hole please okay that's actually not a bad idea Center the black hole on the page okay last thing let's Center the black hole that makes sense I'm also just going to unfortunately take out these lines which are no just because it really shows how incorrect it is so for me to have some plausible deniability here as my new favorite phrase oops now what did I get wrong invalid or unexpected token what did I do wrong once I just change huh 9:13 oh whoops some sort of typo thing happened there all right let's look at this full screen now and here we go let's see what we've got now it's running quite slow because of all drawing all these trails I think actually something that I could really do to make this some things that I could do to make this better a little faster is number one is in the photon itself I could say I don't have to add every single spot I could just say like if frame count modulus 10 equals zero that's gonna improve the speed quite a bit I also feel like you know what's the why am I only doing half and maybe it's nice to do half let's just keep doing with half whoops and now let me change this twofold oops let me change this to full well that's kind of funny a little funny effect that I didn't mean and there we go look at this look at that perfect orbit huh that's kind of amazing that that happened and there we are I think this wraps this up so there's a lot of flaws on what I've done there's probably a lot more thoughtful ways that you could think about first designing this first of all it probably would look nicer one thing I kind of do want to change is just to draw the black hole a last because then at a minimum then at a ver at a minimum at least it will look as if because I'm drawing the Rays behind that black circle now I'm not I must not have saved and give draw the black hole I want to draw the black hole after hit save and then also let's as I draw it let's give a little alpha to this inner ring here and then when we hit refresh and we should see now we should this should be a little bit more nicer to look at in the sense that it will look as if the photons are disappearing into the black hole because it's pi so there we go and we've got a nice simulation of the idea of a black hole and rays of light curving according to space and time wildly inaccurate very problematic I will please I can't wait to hear your comments about what I've got right what I got wrong what kinds of ways I could improve this the code for this is linked right here in the video's description you can take a look at it and make your own version share it with me at the coding train comm there's a little way you can submit your own version of this challenge and I will hopefully come back and do a follow-up video either showing some of the ones that you have made or make one that maybe tries to look at how the actual orbits equations work to simulate what they a more accurate version of them I'm very tired after doing this for quite a long time thank you so much for watching I appreciate it and I'll see you soon more photons why does everybody want more photons all right you asked for it I will give you more photons I mean one thing I could do is just make a lot of random ones but I don't think that a look is nice so what I mean I could make them every like five pixels I was just worried about performance so let me fix a couple things here oh that's kind of nice looking let me fix a couple of things here one thing that I want to do is in the photon class I want to oh I have an idea of how to make this much better oh I got an idea what time is it twelve o'clock okay getting to the next segment very soon but let me just do this because this will be good the things that I wanted to do are number one is let's let's do this right here so that's not going to help yeah I don't love how that is but that's fine I'll just do it a little bit more often and what I'm gonna do is in pull let's just say in pull if if R is less than this dot the event horizon then photon dot stop right this is gonna make things run much faster yeah I don't think the photons are slowing down I think the I mean I could I could make sure that they aren't but I'm pretty sure that this will produce the same result like photon dot velocity dot set mag C hold on some people are talking about the photon slowing down I think this will be the same result oh that is kind of a different result oh they are okay okay this video is not over yet thanks to the chat I think this is actually kind of problematic the way that I'm doing this is the photons are actually slowing down and they should stay I shouldn't limit them to the speed of light I should actually just keep their magnitude at the speed of light again it's sort of an act inaccurate anyway so what's the difference I've also added more photons here so you can look at it this way and so this is a slight they're gonna be a slightly different result because now I'm locking them at the speed of light I think there's also a big performance improvement that I can make which is that once they're sucked into the black hole I don't need to keep tracking their position as well as I don't need to be tracking their position and keeping that history going so I should really like shut them off as soon as they've been sucked in so another thing that I can do here is since I have that distance I can say if R is less than the event horizon I mean I could remove the photon just delete it but I want to still see its path so I think I'll say photons stop I mean that doesn't mean anything but I can then in the photon class I can have a variable called like stopped equals false and then I can write a function called stop where I say this stop stopped equals true and then what I can do is like I don't want to continue updating it as long as it's not stopped I can just freeze its history so I still want to draw it where at last was but I can freeze its history so with that we now have here we go there's my photons flying at the black hole space-time starts to bend the photons spin around and someone gets sucked in some curve and there we go wonderful all right so what I've done here is a nice visual demonstration of this particular idea as explained in the veritasium veritasium ferret has explained in the veritasium video but as you know using Newtonian physics and sort of classical mechanics here there's a lot that's not accurate so I would really love to hear from you number one what did I get right what did I get wrong give me your feedback about my kind of journey to try to learn about this stuff and create a diagram that looks somewhat like what was a picture in some of the explanations about how that image was made also make your own version of this think about design think about color think about animation I mean you could certainly do this in 3d and share that with me you can go to the coding train comm link in the video's description to the actual page where you can submit your own version of this the code will be right there as well that you can play with then if you have ideas for how I might apply the formulas that I mentioned in some of these papers here and here which I will also link to make this actually behave more accurately I would love to hear from you about that thank you so much for watching I appreciate your willingness to indulge me with trying to do this for making a lot of mistakes through it sort of figuring out as I go and I really appreciate it thanks so much I'll see you in the soon right they're slowing down when they're going away from the black hole of course of course they are yes and they shouldn't all right so black background yeah a second black hole right that would be cool all right I think I'm done with this right now yes thank you go thank you J store for keeping with me in the chat to get that correct alright are you ready for segment two little bit late here but I think I have a lot of you watching stick with me I'm coming soon to talk to you about neuro evolution tensorflow Jess but before I do that let me just close this out I want to tell you a little bit about brilliant org the sponsor of today's coding Train livestream amazingly so if you like the kind of stuff that I do here and you're looking for some ways to practice the ideas outside even of coding really although they're what I'm going to show you is there are a lot of computer science lessons in their courses and puzzles and daily challenges and discussions you can visit brilliant org and in fact I have been trying it recently and lots of members in our patron slack group have been as a couple have been telling me they've been signing up and really enjoying it as a kind of companion to the stuff that I'm doing here one thing that I want to just point out to you if I go to the website and look at the daily challenges this one right here is a challenge about relativity now my brain hurts a little bit from doing that whole black hole thing so you might need to help me with answering this but let's look at it so by the way relativity we're not talking here about special relativity in terms of things moving at the speed of light but the idea of relativity is as follows you know if you're sitting in a car or on a train that's moving fast and you have a you know a bottle of water and a cup you can pour that water into the cup because you're you're at you're at you're sitting still relative to the speed of the train you and the Train are moving at the same speed the whole idea of relativity is how we observe motion from different perspectives relative to each other so in a way saying something is moving at a certain velocity is it accurate the only way to accurately say that is moving at a certain velocity relative to something else after all we're standing on the earth that's spinning around its axis and the earth is spinning also around the Sun itself so you know there but like I said I'm not a physicist nor do I play one on YouTube if you're interested learning more about physics and not just learning by reading textbooks or watching videos but by practicing through quizzes and puzzles and social engagement you can check out brilliant org you should go by the way if you want to sign up right now go to brilliant org slash coding trained to let them know that you came from here and that that helps me out a bit so here you are so look at this a bouncy rubber ball is sitting on a tee when it get hits by a train moving to the right at speed V after the collision how fast will the ball moved to the right according to an observer standing on the ground well if it's a perfect ly elastic or collision right wouldn't it be just BV I'm gonna get this wrong by the way part of learning is getting it wrong in fact when I get it wrong incorrect it gives me a nice message so let's see - V is the correct answer so why is that the correct answer moving at the same speed as a train to us the train is still and the ball is coming against the train with speed V what happens when it crashes it rebounds to the same speed but the opposite direction going away from the Train by the way yoha pedro Alfonso please get in touch your answers are always at the top your answers are always at the top people in the chat are talking about a typo so I'm not sure what that means but it rebounds at the same speed the opposite reaction going away from the Train in the frame where the train is moving with speed V that means a ball moving away with speed V Plus V the ball speed after is not exactly - V just like the Train would not continue exactly speed V because that would violate the momentum conservation law but with the difference of the masses between the Train and ball so big the practical effect is the one described wonderful thank you for that excellent explanation you can see how how how how I missed carrot i'm thought about that incorrectly but any if you want to learn more about things like relativity and science there's more there for you now what I want to do is highlight something for you and I didn't mention this but if you go to brilliant org slash coding train right now it's going to take you let me just do that from here brilliant org slash coding train it is going to take you to the stacks and queues set of quizzes from the computer science fundamentals course so you know I can just click here and look at all the courses and you can see there's a computer science fundamentals and computer science algorithms which you could fear my recent ones cuz those other ones I've been going through at some point I'm definitely gonna go through this artificial neural networks one and maybe I'll refer to that as I do a lot of videos about that topic as well and so if I click here on stacks we can find this visualization and explanation of a stack so give me a second to erase the whiteboard and I am now going to do a little coding challenge whoops about stacks and then you um those you're going to be able to do those quizzes during when I take a break and I would love to hear from you how it went and if it made sense to you and if you got the answers right that sort of thing okay so there are two there are many data structures and ways to store data and way to sort data and look through data in computer programming and I cover those I sort of weave through them during a lot of the different coding challenges that I do to data structures that are quite common are known as a stack this is also known as first in last out is that how you say it the last thing in is the first thing out know last in first out right I think that's how you say it that's a stack the idea of a stack is I'm piling things so I might pile the letter B then I pile the letter C then I Apolo the letter Z then I pile the letter A that's adding things to the stack they're stacking up so that would be a push operation push is adding something to the stack so if I were say push K right now we would push the letter K on top of the stack the other operation pop is taking something off this Zach if I said pop right now that would take ke remove it and pop it off and I would then have it act in a variable presumably you know some value equals pop so push is pushing a value pop is popping a value this is incredibly useful and by the way because and it's used in a lot of scenarios where you need it you've seen this before you've seen it in p5 you've seen it in processing push matrix pop matrix push style pop style or plane just push and pop that's the way that matrix transformations are stored in a stack because you want to save up you want to build all these different transformations and you want to peel them back backwards and if you want to do that push and pop is incredibly useful a queue is a data structure that would rip is just like a queue waiting in line so you could think of it as like okay there's a little like ticket booth tickets whoever gets in line first is the first one to buy the tickets first-in first-out does that you say I don't I figure what those terms arms one's gonna tell me nobody knows no one has answered me whether its first in first out let me let me let me look that up actually probably explains it here oh yeah there it is look brilliants gonna tell me last in first out LIFO thank you brilliant push last in Oh sir stack last in first out okay Q being the opposite or the inverse whereas the first one in is the first one out because the first person of stand aligned gets the tickets right so of course you know if I were doing this in a coding challenge right now and I needed a stack I would probably just make an array because I can make an array I can add things to the beginning I can add things to the end I can take things off the end there's a lot of arrays can serve as a way of storing data that could be a stack or a queue or or and manipulate it in all sorts of ways but if I want sort of like computer science bare bones way of doing this the most efficient way where I just need exactly a stack and I never need to look at anything but the last thing that went in I would basically use something that looks more like a linked list which is I would have nodes and I would have maybe a stack is the idea of a set of nodes that are connected to each other and if I always add to the beginning and pull off of the beginning for push and pop then there we go that's my stack let's go implement that in code okay I heard like some noise and the cameras going off all right so okay so I am going to now hold let me make sure I saved my black hole thing and close that let me close all this other stuff and here we are okay all right here we are in the p5 web editor i'm using the p5.js library and i'm going to implement a stack in JavaScript so I'm gonna make a class called stack and I'm also gonna make a class called node and the idea of the stack because it just has I don't know it has a first first element name I don't say this stock first equals null so when you first create a stack data structure you just have basically this idea of a pointer pointing to nothing so when I push to the stack and sorry I do you can't see that it's a stack when I push to the stack I'm going to add the first element there so the idea is that I might have a function in the stack called push with some value and what I'm going to do is I'm going to create a new node with that value and then if this dot first equals null then this dot first equals that node so this is the code so far for putting in for putting in a first node a first sorry putting in this is the code so far for putting the first for pushing an element into the stack when it's empty so in order for that to work I need to now have a node class so I'm gonna have a node class it's just a thing that has what does it have it has a value right that's the thing that's being stored in the stack itself this could be an object could be a letter it could be a number maybe I'll just use characters like I did in my diagram over there and then most importantly a node right if I push that first node in here the node has this empty little pointer that's pointing now also to null because I need to just keep having these things point to each other so what I would then do is I would say so then I would say this dot next I can call it next like where it's pointing to is no so this would work to push something in so I should be able to say let's get rid of the draw I don't need the draw loop here right now at least let's stack equal a new stack stack dot push the letter A so let's push the letter A now what if I were to say stack dot push the letter B and the fact that these are in alphabetical order is irrelevant I should probably pick letters that aren't which I will do right now just to make it let's go there's no lot it doesn't matter what the letters are they're not it's not going to be in sorted order alphabetically or numerically the whole point of the order is it's the order that I'm putting things in so if I now push the next one in what do I need to do well you would kind of think like Oh put it here right because that's the empty spot but actually I don't want to do that because I want to keep a pointer if I'm gonna pop I always want to get the latest one off so actually what I'm going to do where's my eraser and I'm gonna fix first I'm gonna fix the fact so that you can see the word stack here cuz stack is actually this is actually first that's where this this first is pointing to this which has the value M in it right so first has the value M in it and it's pointing to no so now I want to push B so what I actually want to do is I want to create a node with B in it I want to have B point to M as its next node and then I want to have first point to it so I basically want to insert it in between first and the latest thing and another thing would come in point to B and then first would point to that so I always have this first pointer to the latest thing so how would I do that let's see so if it's not null then what I want to do is say what did I say n dot next equals equals this dot first right I want a point I want to point the thing that I'm putting in right into what was first and then what do I want to do want to say this dot first equals n there we go that's it so now I have the functions push and I have the func I know I don't but let's make the function pop so now what I'm going to do in pop well I should probably test this to make sure it's working but let's do that just yet well I went on these pop as a way of testing it because if I push things in the right order and pop things in the right order then it should work and I'm gonna use the brilliant quiz to sort of figure that out all right pop what I want to do so oh this is easy I just want to return oh well actually I need to do a couple things what do I want to do I mean I when I pop something I actually get rid of it so popping is the process of oh you can't see this one I've really got off let me maybe make a new diagram for you over here alright I'm making a new diagram to illustrate the push and pop process better so what I have here is I have this when I've create sorry when I first create this stack I have a pointer our variable name called first which is pointing to nothing then if I want to say push some value like M then I want to make a new node called em and I want first to point to it then if I'm gonna say push B and push you know X then what I would do when I push B is I make a new node called B point that to em and then first I change first to pointing to B then when I push X I make a new node called X I point it to be because I have that and then I have first point over here so what do I want to do if I want to pop if I want to pop what I want to do is just take the value X right take the value X return it return basically first value and then what I want to do but I'm gonna have to do this in a different order because I can't once I return I'm out of the function but this is the idea I want to grab this value and then I want first to point to first dot next right because once this is popped up then this pointer is what first points to and this is gone now if I were in some programming language where I had to do memory management I'd have to release the memory for this but I'm in JavaScript it's fine it'll be garbage collected okay so let's try doing pop so I want the value to be first dot and what is it in the node value then I want first dot first to equal sorry this dot first and then I want this dot first first is null to equal this dot first dot next I think that's right so first get the value then point it to the next one and then return that value okay so there we go so now if I were to do this M be X and then I'm going to say console.log stacked up pop we are now going to find out if I did this correctly if my code is correct mbx pop off X pop off be pop off M so I'm just looking at the chat for a second it's kind of interesting what people are saying so it looks like things are working correctly now let's make our test a little bit more sophisticated to see if it really is working correctly so I'm going to go back to this this quiz on stacks and I'm gonna I'm gonna there's some wonderful explanation here that's going through with some nice animations it's basically going through exactly what I just talked about and now we're gonna get to our first question so this is and it's using numbers so this sequence I should be able to implement now with my stack so I'm actually just gonna copy paste this and I'm going to put it into my code like this and what I'm going to do is I'm going to say stack push so I'm gonna add my object name here for all of these and then what I want to do is console I guess what the quiz is asking me to say if I go sorry if I go back here with the quiz is asking me to say is what will the stack look like after the operations are applied so what does the stack look like so I should probably at a function here to call like traverse or examine and what that will do is I will start with cut a while so I'm gonna say let node equal first while note is not equal to null console.log no dot value and then a node equals no dot next so this is like a loop to loop go from node to node start with the first node spit out its value go to the next one okay so in theory if I say now if I say stack Traverse and I'm not gonna do it you I'm gonna comment it out I'm gonna comment this out and I'm gonna run it so I don't get any errors Oh pop was called without a matching push oh I must have an error fascinating all right this is interesting I mean this can happen oh no no no no I forgot oh I have a big big problem here so pop this is one of the one of the flaws sometimes I'm working with p5 is that p5 is a beginner friendly environment where there's lots of stuff in the global namespace so this has to be stack pop of course so I'm glad I test that and stack top because there is actually a function called pop it's popping off the transformation state the styles that kind of thing but that's not what I'm doing here so you know I don't there's nothing about this that I need p5 for although I could visualize diagram the stock ok stack alright so no more errors so that's good let's look at the quiz so let's try to answer this you think about this for yourself for a moment I'm going to go over to the whiteboard and I'm gonna figure out the answer myself so you hopefully you have the quiz up maybe on a different screen so you can see so that it's pushed 7 so push I'll just write it here push 7 push 8 pop push I guess I don't need to write it out but why not pop times three and push 40 and then push pop / - okay let's do this quiz so here's my stack first thing I'm doing is push seven then I'm gonna push eight so you could probably do this in your head but I'm gonna diagram it out I always find that to be helpful then I'm gonna pop so I pop off eight then I'm gonna push pot way I'm gonna pop 7 x 3 what's gone and push that back on 7 x 3 21 then I'm going to push 40 so I push 40 then on a push and then pop that off divide it but / - egg at 20 and push it back then I have this so this should be the state of the stack 21 20 in my other way of looking at it it would be this with this being first so let's look at the quiz let's see if I can get it right this time 21 20 hit submit hey yeah I got this correct okay I got it correct now the question is will my code get it correct so now if I add this stack dot reverse ah first is not to find its line 38 that must be down here I always have this trouble this dot first okay there we go let's try this again 20 21 now is that right as the order right so I think it is right but just to because of the way I'm traversing it but let's say right oh yeah it is because 20 is what would pop off first so if I console.log stack top hop and console.log stack pop 20 21 20 20 okay so that is the correct order so now you have seen me implement a stack I will leave it to you as a challenge to maybe visualize and animate this so what if you're adding things to the stack and you're actually seeing them pile up you can use p5 canvas to draw them when you're popping things you see them pop off there could be a nice way to make an animated version of this but you can also now if you're I would be curious here I'm gonna take a five-minute break please go to brilliant org slash coding train that will take you directly to this quiz page while I'm taking a short break if you wouldn't you can go through and try to answer some of these quizzes questions I mean go through the stacks then you could go through the queues and when I come back after I'm gonna get some water wash my hands which are full of whiteboard marker stuff I'm gonna come back and oh yeah sorry this is very absurd before I got that this is what's wrong everything was right this is right the rat was right this 21st points here sorry this is very important that's wrong come back and I will be here I'm gonna talk I'm gonna spend a little bit of time talking about neuro evolution in tensorflow yes so thank you so much for believing torque to the sponsorship if you have a minute today to sign up for free at brilliant org slash coding train also the first 200 people to us to sign up for the Premium Subscription which unlocks all of the courses on brilliant will get 20% off from that link so thanks very much I will be back in just a short bit and enjoy your stacks excuse quiz let me know how it goes and I look forward to hearing about it [Music] [Applause] [Music] [Applause] [Music] [Applause] [Music] [Applause] [Music] [Applause] [Music] [Music] [Music] [Music] [Music] [Music] [Music] I'm gonna do this this star this star this star this star song never forget the best are unicorns and rainbows and kicks what else is there yes kids thank you very much kittens and rainbows and cupcakes notice that look what I get really losing my mind okay let's [Music] [Music] [Music] I feel just sort of like a nice feeling of relaxation everything's gonna be okay today dream is not broken it does not frozen this isn't this is a wonderful thing okay we're gonna do it I'm really getting something I need my sound [Music] [Applause] [Music] okay what else is there unique chords and keys that was a valid syntax I forgotten there's one other thing here that I will use meet English Lee over and over again all sorts of text generation analysis things that I will use continuously over and over again first you need to do [Music] we're gonna do [Music] I'm getting skinny [Music] hello I'm back I don't know if you've heard that particular remix of unicorn what's called kittens I know what it's called there's a there's a whole playlist on Soundcloud encoding train remixes there's one it's it's one actually that I don't know that I have over here that I really love I need to kind of redo my sound board if you're like making those kind of remixes they pretty much happiness enjoy I love to play them listen to them I even want to like learn them on the ukulele then there are these weird like MIDI instrumental versions of them so that said so uh Simon was just telling me in the chat that if you keep going I'm gonna screw up to this message that there's a fun that there's op if you keep going with this quiz there's other operations on stacks that it asks about and if they're really fun to do so I hope that you have a chance to check out brilliant and practice some of these concepts that I'm doing in my videos on their website as well thank you so much - brilliant org for the opportunity and sponsorship of the coding train if you have any questions or run into any issues you can post them there you can send me a message leave a comment in the video and I'd love to try to help you out alright so the next thing that I want to do here is talk about neuro evolution and talk about neuro evolution with talk about neuro evolution with ten's flo-jo sorry my brain is starting to shut off and I've got about a little about a half an hour or so to do this so I have an idea of how I'm gonna work on this first let me just do this stack j/s example so I'm saving this and I will put a link to this stack code in the video's description if somebody who is a mod right now in the chat wants to sort of post a link to that this sketch URL then you can access it right now okay so let me get these cameras but you think then I took my five or ten minute break that I would remember to cycle the cameras but hard oh yeah and peak so people talk about that peak so peak is the peak function instead of pop and push is an additional function in a stack data structure so I would say try implementing that I didn't implement peak you could implement it and let me know how that goes and you're gonna need to implement it if you want to answer the rest of the questions on brilliant through your code example okay so I'm over here for a second to erase this and so let's see so I'm erasing a whiteboard in case I need it I'm closing this out and what I'm going to do is I'm gonna go to the coding train calm and go to coding challenges I'm gonna look for by the way if you missed last week's live stream rubik's cube part three will be coming out tomorrow I have two we match you and I've been sort of like editing it together it's as if the content is already online in the live stream but the part three edited down to about twenty five minutes which has the animated turns I haven't tried applying a genetic algorithm for a neural network you know some for machine learning way to solve it that I've I thought I would take a break from the Rubik's Cube today but I will try to come back to that okay so I'm looking for neuro evolution flappy bird part five so improve saving and loading brain model let's see let's go to here let's look at the download the code don't remember which example I'm getting right now so let me grab the code for this this is not the GA one so I actually need to go back just fine let's go back to part four maybe and then do download the code and let's see if it's this one yes this is it so I'm gonna I'm gonna change this to fleckner Oh flappy tfj s-sure and then I'm gonna put this on the desktop so if you want to follow along I'm starting from the code base from this particular video and so oops I'm on the desktop here it is and then what I want to do is open up I term and go to the desktop and go to this say and open this up in Visual Studio code let's just go here great okay let me just let me do that to all of these and oh I actually have the save functionality built into it interesting okay so that I might need to take up I'm gonna leave everything as it is okay then I need to run a server and get the console going here and then I need what else do I need this this this gonna go away then I need a tensor flow chess and [Music] we're just like the QuickStart see tutorials see the guys see demos get started get set up there's what I'm looking for great actually the newest version i think is 1.0 0.4 so i'll do this okay and let's just also go to because what I would like to do here let's do this and speed this up a little bit okay that's a little bit too much okay so I'm ready to begin let me just make sure the code is visible yep that's fine to turn this off and I'm ready so in a way this could be so this could be a new coding challenge it could just be like a supplemental video as part of minor Aleutian series it could be part six of the earlier coding challenge yeah maybe I'll just do that it's sort of weird but maybe it makes sense to make this like part six I'm not sure tricky I don't know what to do but we can always fix that later all right let me cycle the cameras and we're gonna get started this probably is gonna take more than a half an hour but let's see okay Oh okay Simon is pointing out that again he's reminded been reminding me about this and it is true that Cody challenged 144 in base 12 is actually 1 0-0 so since I didn't celebrate coding challenge 100 I could celebrate 144 and I could do something about base 12 but I just couldn't come up with an idea so I think that one the black hole to me is special so in a way that will be coding challenge 144 and we'll be celebrating in base 12100 ok all right let me just check one thing on the channel torch during hue all let me just check this like neural evolution like am I making well doesn't really matter it's gonna go in this playlist but c'est la vie okay all right I probably should find this probably a version of this that has the nice design in it but that's okay okay I think I won't say coding challenge because I will oh hello grave and edible thank you so much for your for your donation you're superdad it's really kind of you I certainly appreciate it okay alright hello welcome to another coding train video about neuro evolution the process of evolving an optimal neural network for some type of scenario like playing the game flappy bird or kind of crude version of the game flappy bird for hello and welcome to another coding training video about neuro evolution which it's a term that describes and that the sort of scientific term for this are neat neuro evolved them I never mind about the neat thing they've been up mul hello and welcome to another coding train video about neuro evolution so this video what I'm going to do is I'm gonna take my previous finished sort of finished version of neuro evolution the process of evolving an optimal the optimal weights of a neural network to solve some kind of perform some kind of machine learning tasks in this case I'm making a guess based on the environment of this very crude flappy bird game I'm not making this the neural network is making a guess whether it should jump or not and so this is a very simple actually scenario to solve and so it's a nice demonstration the idea I've been meeting to return to this topic for quite a while so I can try to look at some more complex scenarios so neuro volution is can be applied to a lot of problems at different reinforcement learning algorithms are also applied to but it's a pretty different technique and so if you if you there's like five or six or seven videos all about neural evolution that I would recommend you check out or you can start right here because what I'm gonna do in this video is I am going to take the existing version that I made which used a toy neural network JavaScript library that I implemented it yet another video series to kind of learn more about neural networks and spin up a very very basic one in JavaScript with out other dependencies but I'm gonna replace that now with tensorflow j/s and I think this is better because I will be able to build better more robust projects without having to be responsible for the sort of underlying machine learning framework I can rely on tensorflow J s to give me all of the sort of possible configurations of neural network that I can then use in my own system in my own evolutionary system yeah I should say what was I gonna say there's something that I just thought of a second ago there was something important oh thank you - there's a new member it's gonna come back to me what I was saying Nats team thank you so much if you sign up to be a new member and you're looking for your invitation to our members slack channel go back to the community tab this is all a message just from hats by the way going to the channels community tab and there's some posts there that are members only that will explain ok wait yeah the collision detection is not pixel-perfect wait I was saying something I don't know remember the worst thing I was gonna say hi that's fine cut okay mmm I just have to keep going so what you're seeing here is the last example from part 5 where I saved a trained model and I'm loading it into a version of the game and watching it play out what I did previous to that is what you're seeing right here which is this is launching 500 random neural networks to try to make guesses I can use this slider to sort of speed up the system and over time those neural networks are going to perform the processes of crossover and mutation it's not actually doing crossover it's been crossover which would be the act of combining two agents genetic material together into one I'm just taking one and copying it but then I am mutating it to optimally search for the configuration of weights that will work the best and just to remind you what is what am I talking about the weights just to remind you of when I'm talking about the weights this is what I'm talking about so we have the flappy bird game this is the agent that needs to make a decision in a way it's a classification problem should I go up or should I not go up right these are the only two possibilities a very simple classification problem two categories I have done I the human being have basically done the feature extraction I could use the environment the input to their neural network as this image like all the pixels of the game that would be a wonderful thing to try and I would love to do that especially once I have convolutional layers with tensorflow j/s which is not something I have in my coin neural network library you might not know what a convolutional layer is don't worry if I use it I will explain it but I have done that feature extraction so I have decided that the core the features that I want to use as inputs into my neural network are I think it was like the bird Y position the bird like Y velocity the top pipe location the bottom pipe location and then I'll call this like X the distance to the nearest pipe so these are what I've decided might be are the important values to use from this game to feed into the neural network so that means the neural network has one two three four five inputs these all get normalized into a value with a range between 0 and 1 and fed into the neural network then the outputs is a classification problem so there are just two outputs and they would each output a number if I got something like 0.8 here and point to here that means there's an 80% basically 80% probability confidence score that I should jump so I will jump now I could actually pick random numbers and that kind of thing but I'm just going to take the highest one the Arg max so to speak and go so neural networks are able to learn a sophisticated amount of information through hidden layers so the inputs don't pass directly to the outputs but the inputs pass through a hidden layer and I don't recall what what I picked is the configuration of the hidden layer but I think was something like 8 so if there were eight I think that's what it is well look 1 2 3 4 5 6 7 8 these are what are known as dense layers meaning every single node is connected to every node I will not sit here and draw all of them but I will start that process and then all of these sorry are connected to the outputs like this so a Network's core data the configuration of that data is in a matrix because every one of these connections has a weight so if they're five here and eight here that is 40 weights if there are eight here and two here that's sixteen weights now the truth of the matter is there's also a bias so there's a bias for each one of these and a bias for each one these so there's the configuration is all of the weights all of the biases for each of these nodes but this is that these are the details that I cover a lot more in this series about building a neural network from scratch in JavaScript so if you want to learn about that you can go back what all you need to know for here is that I have a way of creating this neural network I have a way of feeding these values into it and looking at the stuff that comes out so let's go look at that code so if I go look at the code we can see that here there's this idea of a bird object and the bird object makes a neural network with five inputs eight hidden nodes and two outputs this is I don't know what I've said here but this is known as a feed-forward neural network multi-layered perceptron it's two layers the inputs it looks like a layer but these are just numbers coming in there's a hidden layer and output layer that's an important terminology so that's happening here and you can see in the bird the bird sort of like takes all of those properties its Y position the closest pipe the closest pipes x position its velocity makes that into an array feeds that into a predict function and then I just sort of like if the first output is greater than the second output jump so this is that process and all of this happens in o in this neural network library so there's neural network jf and matrix such as this is like if tensorflow J s was made by one person who really didn't know what they were doing that's what's in here so what I want to do is I am going to delete this folder right I'm just gonna I'm deleting it it's gone gone move the trash boom so now when I go over here and I hit refresh of course it doesn't work because we have no neural network J s can I get this you know I don't have a watch fun no ever long I have right now in the course of this video working again without my library both tension flow Jes instead that's the thought experiment here so in order to do this actually though ironically I think what I want to do a couple things I need to do whoa what's this ridiculous looking HTML file that I have sorry everybody hold on a sec let me just clean this up a little bit this can come out there we go that's better and just do this so the header should have all these script tags whoops yeah stop should have the p5 libraries this should be neuro evolution with tfj s recent p5 is 8.0 and this this would be in the body shouldn't have like bird there also I must have lost that by accident bird and pipe too right bird type okay same thing so here's my HTML file and you can see that it has the p5 libraries it's loading my neural network library and then these are all the files for the flappy bird game itself as well as a file that includes some information about how the genetic algorithm works so what I want to do I need to take these out and then I want to import tensorflow chess so I have the attention flow days website up and this is the script tag for importing tensorflow j/s so I'm going to add that here and actually I'm pretty sure that the most current version is 1.0 0.4 so let's add that and now I'm going to go back here and hit refresh now of course ah neural network is not defined so we've got fewer error messages here now which is good I just want to make sure TF is loaded so like for example I can call TF memory and there's no memory being used but I could see that TF is DF TF chest is loaded because I can call TF functions in the console now which I'm going to need to do to figure all this out all right so now that now I think the thing that I want to do is actually I don't need to do this but I think it would be nice for me to create I'm going to create a file called neuro nnj s and I'm gonna make a little wrapper for a neural network and actually one of the reasons why I want to do this whoops sorry sometimes things go haywire one of the reasons why I want to do this is as you know I've made a lot of videos using a library called ml 5 which is a wrapper around tensorflow chest which allows you to work with some of the machine learning algorithms without having to manipulate the lower level details of tensorflow J s and so this is kind of a little bit of maybe a preview of that because this ultimately this idea of neuro evolution is something that I would like to work on putting into the ml 5 library so if I make a neural network class and I write a constructor we know here that in the bird object what it's doing is it's making a neural network with five inputs eight hidden nodes and two outputs so there's no reason why I can't actually just do keep that same structure and I'm gonna have three arguments I'm just gonna call them a B and C because at some point I might need to call the constructor in different ways and I'm gonna then say this dot input notes equals a this dot hidden nodes equals B and this dot output nodes equals C then I'm gonna put I already need by the way I did this in a class that I'm teaching so uh last week so I kind of have quite a bit of a sense of the plan here so I'm also going to write a function called this dot create model because I might need to do that in different places and then I'm going to make a separate function called create model so this now is the function where I want to create my neural network using tension flow jjs and I'm going to use the layers API the truth of the matter is this particular architecture right this is perhaps one of the simplest most basic vanilla so to speak neural network architectures it's got two layers one hidden layer very few nodes there's no convolutions no fancy stuff just the very basics so I could probably do this with just simple tensor flow operations itself but I'm going to use something called TF dot layers which is actually based on Keros which is a Python library for tensor flow and python so many things I've talked about these in videos but basically tensor T F dot layers is an API that allows me to create a model from a slightly higher level perspective so one of the key functions in tftf layers and I'm going to create a variable called model is TF dot sequential so if I say TF dots sequential that should create for me a sequential model and we can just take a look at that here in the console and see there we go it made something that works you can see there's all these parameters as a training now interestingly enough what I'm doing in this video it's not really what tension flow jess is designed for almost everything in tension flow jess has to do with optimization functions and algorithms for learning and tweaking weight you know that propagation I'm not doing any of that this neural evolution thing is quirky and weird all I actually need is a bunch of neural networks and the ability to feed data through them and then I can just delete an ad and mix them up so actually most of this stuff will not get used but what I want to create then is my first layer so the first layer I'm gonna call hidden so I'm gonna say let our Const hidden equals TF I think it's dense so I think I better look up the documentation at this point I certainly don't have this stuff memorized so I'm gonna go to the API and I'm gonna go oh boy I'm there's many many more revisions since I think point zero four but I'm gonna go here and I'm gonna look for dents so I want TF layers oh I mean tensorflow org ah wrong API I want the tension flow jsap I clicked on the wrong place that's those were there version numbers for the current tension flow depth entry flow Jas so I want to go to dense TF layers dense so this is what I'm looking to do and I can see a nice little example of it here this so I'm gonna create a TF layers dets TF oh sorry dot layers dense and then I need a little object which is going to contain all of the configuration information for that dense layer and what are some things I can configure I can give it the number of units that's my hidden nodes which is the hidden nodes I also want to tell it how many things are coming in like what is the input shape so this is the layer I need to tell it about the input shape I forget what that's called somewhere in here alpha input dim would be the input dimensions I guess which would be input Vista input nodes I'm forgetting my this dots as always as always I always forget so there we go what else do I need I think an activation function I definitely need so activation activation now this merits quite a bit of discussion an activation function is is how the data gets processed as it's fed forward so all of these inputs the numbers that are these values come in here they get multiplied by those weight values sum together and pass through an activation function the activation functions job is basically to squash the values to sort of enforce non-linearity to allow for the stuff to go to the next layer and there are all sorts of reasons why you might pick one or the other there's more recent research and it's change over the years but I've talked about this in other videos I'm gonna pick sigmoid as a kind of default activation function that squashes all values between 0 & 1 I'll include some links about sigmoid in the video's description if you're curious so I forget exactly what I do here but I think I can just write sigmoid as a string and somewhere in the documentation there is a list of all the activation functions do I need anything else I think that's good so then I just want to say this dot model dot add layer maybe so TF sequential is what I'm doing here which would be here and O in put shape is probably what I want not input dimensions we'll see I'm noticing that there's input shape so actually I think that's what I want I mean that's certainly what I used previously so input shape and it would be just that number of nodes but inside an array a shape is a way of defining the dimensionality of data so if it's just a list of stuff like a list of numbers it's an array with five things in it so so that's how I define a shape so then I would say model dot add so this dot model dot add the hidden layer now I want to make an output layer which is going to be TF layers also dense and I want to what do i what do I want the units is this dot output nodes and then I actually do not have to define the input shape for this layer because it can be inferred from the previous one so I couldn't infer the number of inputs because from the hidden cuz hidden is first but the last layer here I these outputs I know that I just added this layer if it's dense this is the shape of the input so all I need to do is that and then I also need an activation function soften and I'm gonna I'm gonna make it soft max so soft max is an activation function which takes which like sigmoid squashes all the values to arrange between 0 and 1 but it does something even more it basically enforces that all those values also add up to 1 it basically turns them into probability or confidence scores and all of them add up to 100% or 1.0 and there's more to it than that it's kind of in terms of how the mathematics of work but that's the basic gist of it and that's what I want I want the probability of whether I should jump or not jump so now I should be able to say this dot model dot add output and then if I go back to the documentation I don't need to do this fit stuff but let's call oh do I need to compile it hmm so interestingly enough usually you need to compile you to give a loss function traditional machine learning you're gonna have a loss function an optimization function you're gonna train it with data doing any of that neural evolution remember if you've watched my other videos on ever evolution I'm doing this weird thing I just need a random neural network that's all I need so I don't know if I need that compile step but I'm guessing I kind of do so I'm gonna say this dot compile but I'm just gonna give it an empty object cuz I don't care about those other things so let's try let's this dot model dot compile so now that I have that I should be able to it with my code I'm Kylee getting a little bit further like look at that cannot read property minimize of undefined because I didn't give it any op today let's get rid of the compile let's you might get this to work without this dot model dot compile maybe compile only has to do with the learning process that I'm not using great this stop brain that predict is not a function so now I have the next thing that I need to implement so where is that that's in bird Jas and what is it saying so I'm able to make the neural network and then I am calling predict and I'm giving it an array perfect so what I need to do now in my class here is I'm gonna write a function called predict which will receive an array now what I would essentially be doing is now calling this dot model dot predict with that array only here's the complexity of using tensorflow j s tensorflow doesn't work with traditional JavaScript arrays it works with something called tensors which are multi-dimensional arrays that they're multi-dimensional things collections of numbers that are living in the GPUs memory that's for optimization purposes the operations can happen faster on the GPU which is optimized for a lot of matrix math I don't really need that optimization to be clear because like I'm working with the tiniest thing here but I still need to convert this stuff to tensor so I'm gonna need to say the X is I'm going to call this the inputs are often referred to as X's equals TF tensor and what do I have what's the shape of my array it's one dimensional right it's just an array but the thing is what tensorflow j s expects so this is like my dina it comes in as an array of numbers 1 2 3 4 5 right all of this stuff 1 2 3 heads only 4 but whatever you get the idea there's another one there the thing is tensorflow des is kind of things like you would never be so crazy is's give me just one thing usually you're gonna give me like a thousand rows or two data samples so it's actually looking for a shape that is like this that would have a bunch of these so it's so I just need to take my inputs and put them in a 2d tensor because it's an array of arrays so if I do that tensor 2d X's I think that's all I need to do then I need to call predict with the X's now that's happening synchronously I need to get the data off of oh and this would be wise sorry the Y's would be the result of feeding it forward so that's you exactly this process predictors the function to feed this stuff forward and now I have the Y's I'm gonna do something which is a little bit ill-advised I want to be able to look at those values so right now the Y's are a tensor and that's living on the GPU in order for me to pull it off the GPU to use it I need to call a function called data which is getting the actual data and that's thing that happens asynchronously so I need to deal with a promise or a callback but I'm because I'm working with it to tiny bits event in data here it's gonna be much simpler for me right now at least to get this working to just say basically the outputs equal wise data sync so the data sync function gives me those outputs let's console.log them and let's say return outputs also just so we see what's going on here so I'm now gonna run this neural network tout predict at bird think is not defined hmm what did I miss predict neural network predict where am i bird line 54 this dot brained I predict hmm what did I miss Oh exes is not defined sorry the air is exes is not defined that's weird neural network jazz 10 array exes oh sorry sorry there's a major error in here that you probably all noticed but I didn't I'm taking that array and converting to a tensor so ARR is the array and maybe it would make sense for me to call these inputs I'm taking the inputs as a raw array converting them into a tensor passing them through the model pulling out the outputs and returning it okay let's see how that goes tensor to D requires shape to be revised when values are flat huh Oh guess what this has to be inside an array right because the whole point is that once the 2d thing so it wants that array inside of an array okay so that it I'm tick-tock so that okay stop so that should fix that right oh look I'm getting stuff I'm getting things why aren't I seeing it oh look how slow it is it's so slow so this is sort of like the weird IRA IRA knee of all of this so this is really the irony here I am doing a ton of operations that are copying and removing data from the GPU I and this I really should be doing all of them at once right there's no reason why I couldn't pass all oh no can't I can't do that I have different models nevermind we just so this is kind of ironic in a sense like I'm using a much more powerful JavaScript engine for deep learning that's optimum that does all the operations on the GPU with WebGL to optimize it and yet it runs super slow this is really because I'm a crazy person and kind of doing this in a weird way that attentional jess is not really designed for weight I'm like have all of these different models and I'm copying the data with data sync so there's a couple things I could do number one is let's just for now and I'm off to come back to this or think about how to make this run faster or be more thoughtful and maybe do this stuff asynchronously but what I'm gonna do right now is I am going to just reduce the number of birds to half and I'm also going to do something a little bit silly which I'm going to inset I'm gonna say TF set back-end CPU because actually the amount of data that I'm working with is so small I don't really need to use the GPU I don't need to copy stuff back and forth let's just use the CPU it's perfectly fast enough remember how well it worked when I had my own silly little JavaScript library so let's use the CPU and I think you'll find that now it's still running like kind of slow I have a feeling this console.log is definitely making things kind of unhappy so let's take out that console log and let's refresh and there we go so now it's performing pretty well is it as fast as it was before I don't know could I get back into 500 but it's better ah but now we have a problem copy so copy is not a function because I didn't write a function to copy a neural network so what do I mean by copy and neural network so normally this isn't something you would necessarily do but it is fundamental to neuro evolution and by copying in I need to say like this was a good one let's make a copy of it to keep it and I actually want to copy it not keep a pointer to it because I also want to mutate it and I want to mutate it a bunch of times so I have to figure out what is the way to copy a model in tensorflow j/s and ultimately if you want to try a more the proper way to do this in terms of neuro evolution would not be to just make a copy to go to the next generation but to take two of them and make a new one that's kind of a mixture and when I say mixture I mean mixture of all these weights but I'm just gonna copy it what I need to do is I need to go back to this class again and I need to say copy so if I say copy I could just say a return new neural network and I could just put like input notes hidden nodes output nodes but it wouldn't have the right weights so let's think about this let's do I'm going to say copy model copy equals this create model so I'm creating a model which is the same I and I need to create model with oh it has that stuff so I can call create model that's going to do it with the right input notes in notes out of the notes okay so it's not a copy yet it's another random neural network but really I the architecture is the same so in a sense I've copied the archetype made a new neural network the same architecture now I need to get those weights so luckily for me there is a in TF guess there are two functions called like get weights and set weights so let's take a look at those functions so for example if I go to the console here and I were to say something like can I say let n equal new neural network was it eight five two right now five to eight five eight to so I'm making one of those and so n dot model is that model itself and you can see it's got all that data in there so what I want to do is say n dot model get weights no oh I said mode I want to say end model can I just click here model get weights these are all the weight matrices why are there four shouldn't there just be two well there's four because there are the weights and the bias these one two the weights and the bias these three four so that that is what is here so all I need to do is take these weights and copy them so let's see so I could say constant weights equals this dot model get weights and then I could say model copy each dot set weights weights and then I could say return new neural network with model copy now there's a bit of a problem here that I think I'm gonna have to do but this is pretty decent start right make a model the same architecture get the weights from the other one then put those weights in the new model and then return it I think there's gonna be a big problem here but this is kind of the idea so now if I were to what I need to do here is now I'm making a neural network without giving it the input nodes I need to basically say if a is an instance of TF sequential is that right let's see so if I go back to the console end model instance instance of TF sequential false no TF sequential with a capital true okay so I need to check if it's TF sequential object then this dot model equals a so in other words I have two ways of creating a neural network if I pass the constructor an existing model then I just assign it otherwise I create a new one however I've really always got to keep track of what it's a little this is some redundancy here because the input nodes hidden nodes and output nodes are in the model itself but let's just add another argument here and then I can say also this is awkward but and I could refactor this later so now I'm also going to always require that I also give it the shape basically and so here when I'm making this new neural network with model copy I'm also going to say this dot what is it input nodes this dot hidden nodes this dot output nodes okay so this is me making that new neural network with the models copy and I'm also just giving it the shape also so that you sort of retains that information so now I should be able to go back and we've got a copy function let's speed this up okay great who cannot read property set weights of undefined that's not good where's the copy this got create model huh all right let's look at uh create model doesn't return the model ah okay I've got a little bit of an issue here ah okay I've sort of made a mistake in the way that I've designed this program what I want to say is this stop model equals this create model and I don't actually want to set it here I'm going to just make in the create model function which is where I'm going to make this a variable Const model and so and then I'm gonna say model dot add model dot add and then I'm gonna say return model so this function this could be living somewhere else or a static function or something but it's making the model and it is in setup I'm sorry in the constructor it is creating it and returning it and placing in an instance variable or here it's creating it and returning it and putting in this copy variable so that should work better so let's see great so now I don't have a mutate function no problem no problem this is a way to deal with that bird digest line 33 bird digest line 33 let's just not bother mutate no worries don't mutate let's speed this up let it run for a bit all right let it run for a bit we can sort of see I did it get better kind of but there's only one they're all making the exact same decisions so without me two things are wrong here number one is without mutation I can't get any variety but there's something else that I'm pretty sure is wrong not a 100% sure but I'm pretty sure is wrong so I want to protect myself against this is I haven't really made a copy so I think in the case of my copy function which is here this is the weights as tensors and I am assigning them to the copy but they're all being assigned the same tensor so even though there are multiple models they're all using the same weight table so if I were to mutate them they're all going to mutate in exactly the same way so I need to really make sure I'm copying them and there is a function in tfj s called clone which is to make a new tensor that is a copy of an existing one so I think what I should do is I should say Const wait copies is a new array and then for let I equals 0 I is less than weights index I I plus plus no sorry weights at length so I'm going to iterate over all the weights and I'm gonna say weight copies index I equals weight index I clone so this is a kind of the idea of like sort of deep copying I guess I'm not sure if I'm 100% accurate about that but the idea of actually copying them into a new tensor so it's not the previous one and then I should be able to set the weights to weight copies so this should be this should guarantee that I really got it copied correctly so let's do that I'll run it again speed it up a little bit so it's still converging kind of into it ultimately being just one of them that the one that happened to do the best but it's not doing that because I haven't properly copied it's doing that because there's no mutation and you can see actually that seems to be taking a little longer to get there so I think maybe that copying is better now okay so I do need that mutate function so what do I need where was mutate so I need a function called mutate and it's getting an argument point one which is presumably the mutation rate so the process of mutation in a genetic algorithm is to say look at the genetic information which is basically like all of these weights and biases and one by one examine them and at some probability make it some adjustment make it random change it mutate it so that's what I need to do so it's actually it's more involved than just making a copy of the weights I need to look at every single weight individually and then mutate it ten percent of the time I'm gonna do this but let's see okay so if I go into neural network and I go into I made a copy function now let's look at sorry let's look at mutate so I want to do the same thing the first thing that I want to do is I want to get the weights what I call this this stock model that get weights then I want to maybe have mutated weights is a new just an array so so I get the current weight tables and then a weight tensors sorry and then I'm gonna go through and make mutated weights so let me once again loop over this array and I'm going to say tensor equals weights index I because it's a tensor and then what I want to do I want to get the values so I'm gonna use Data Sync again I want the shape because I'm gonna need that when I convert it back I think I can just do this so I need the tensor which is the data I'm going to need to retain the shape because I need to put it back into a tensor but I want to mutate them I don't think I can mutate the tensor without looking at the values who knows so now I'm gonna go through and I'm gonna say let a equals 0 J so J equals 0 J is less than values dot length J plus plus so the actual weight which I'll call W is values index J and what I want to do is say values index J equals a new random weight so I could do something like this right just give it like I actually even need the current weight I could just make a random one right I'm mutating oh but only 10% of the time so this gets a rate so I could say if random 1 is less than that rate then make a new weight I do think this is a case where I might there might be some benefit to rather than picking a totally new weight and I don't know what initialization was used to make the initial random weights that's probably somewhere in tensorflow yes so it might be some operation I could call here but I'm just gonna tweak it so this is why I want to grab that weight and I'm gonna say weight plus and I'm gonna use P fives random Gaussian function which basically gives me a random number that's kind of near to up with a mean of 0 and a standard deviation of 1 so it's just gonna adjust that weight a tiny bit so it's like kind of what you might sort of see with gradient descent dial up dial down but I'm just making a total guess just dial it some direction so it's going to equal that so once that's done right I have I I have my weights I have my weights sorry I'm confused here right this loop is looking at an individual set of weights it might be between inputs and hidden or between hidden and outputs or it might be one of the biases of either of those weights and I have it as a tensor and then have its values now I'm you tainted those values aha I need to make a new tensor new tensor equals TF tensor and give it those values again write the mutated values with the shape so this is what's nice I don't have to I other I don't have to worry about what the original shape was data think is just gonna flatten everything and give me all the numbers pretty sure okay and then what I can do is then mutate those it's flat I can put it back into a tensor with the same shape and it should be back just like it was before I can say mutated weights oh wait a sec yeah mutated weights index I is now that new tensor so I the mutant so I grab the tensor I got its shape I got the values I mutated them I made a new tensor and it's going to go into my new array of mutated weights and then when that's done I can say this dot model dot set weights mutated weights I think this is mutation I can't believe we'll be shocked if I didn't make a mistake you I know I'm no one mistake that I made so there's like a really we there's a thing that and and this is something that thank you to the creators of tensioner float yes thank you sorry there is one mistake here which is that tensor dip sorry which is that data sink is actually not making a copy of the values so I'm still going to be stuck with a lot of things pointing to the same exact data so if I do take one and I'm it's copied for like a bunch all those are gonna get mutated in the same way and I want to have a bunch of different mutations happening in parallel so the fix to this oh let me pull up the discussion of this t fjs github.com Schiffman tensorflow Oh hold on just give me a second to find which one 45 but I'm really close to being done tensorflow Jess examples issues closed I discovered this issue around data sink in this discussion and thank you so much to one of the creators of sensor lo Jas tetany oke Laura who answered my question about this so data sink is mutating I'm mutating the underlying values in the tensor apparently there's an array sink function that I could use instead of data sink that does an actual copy but me I am Sonia Cody drain viewer actually suggested that I just called dot slice which is a JavaScript function for copying the array so this I really really need in there because I really got to make sure that I'm not mutating the same underlying information so I shouldn't add dot slice here and then I think I have this done we're about to find out you know the only way to find out I mean there's other ways to find out I'm sure is to let this run for a while and to sort of see if it gets anywhere so I'll let that do that for a little while so I would say that this worked because there we go I have an agent that learned basically again it's very very simple problem for it to solve but this technique that I've now done there's no reason why it couldn't be applied to a more complex scenario so I now have neuro evolution with tensorflow yes but I'm not finished I would I could do this in the next part but there's a huge problem here I haven't thought at all about memory management and if I do TF memory in the console I have at one twenty eight thousand eight hundred seventy tensors and I'm using all these bytes and if I go sorry if I start from the beginning I have that many tensors and look how it's going up so I'm leaking memory like crazy I'm not cleaning up any of the tensors that I'm using and so the way to do that I don't know why I'm coming over here but it's very important in addition to the get weights and set weights function there are two functions that I really need to be conscientious about when working with tensor flow DHS one is TF dot tidy and this is a function that you can basically give it a call back and any code that you put up in there it'll do the cleanup for you so it's kind of like tension flow Jazz's garbage collection feature it's like just put some code in here and don't worry we'll take care of disposing memory that's not used so this is probably what I'm going to use most of the time but any tensor I could also individually call dispose on when I'm not using it anymore so let me go and find everywhere in my neural network code that I'm using tensors and make sure I'm cleaning up memory that's not being used anymore all right so I've got I'm in my code scrolling scrolling scrolling this should be fine I don't think I need to dispose models maybe I do but here okay oh look at this mutation so I'm sure I'm creating new tensors I'm doing stuff this is definitely a place where I want to say TF tidy and basically it's the nice thing is it's literally as simple as this I just put a everything inside of TF tidy so maybe there's a slightly more optimal a better way of doing this but this should work in predict you know this one's a little easier like I could say like oh I don't need the X's anymore so dispose of those and now once I have the outputs I can dispose of the Y's so I could put manual dispose in here like that's me disposing but I think I probably will also just use TF tidy same exact thing but there's a little something else that I need to do here because this code is returning something I need to return the result of TF tidy so that should be fine I feel like the model stuff is fine this should be all the cleanup that I need let's see a problem okapi oh yeah I guess let's do TF tidy in here also TF tidy let's do this I also need to say return since I'm returning a new model I mean I could put I could put TF tidy in this let's see if I need to let's look at the memory now so there's a thousand tensors that makes sense because I have 250 Birds and there's four tensors for weight matrices that's a thousand so three thousand oh I missed something so every generation I'm not I'm keeping some of the old tensors that I don't need anymore so let's try putting I suppose TF tidy here and also return and let's see what happens oops ah wait mop something undefined what did I mess up TF tidy hmm it's weird let's take this out okay so that causes an error but once they hit the next generation I've got a thousand more oh all right okay that didn't seem to work but I have a better idea I don't know why that didn't work there's probably a very good reason and I will hear from somebody about it but I think I know what I should do where am i copying here oh but I have TF tidy here I was thinking that once I make this new neural network I would call dispose on the existing model hmm where I don't know where I have my memory leak to be honest with you like nothing needs to tidy here because oh wait a second No No [Music] hmm dispose the old generation hmm I mean ultimately what I'm thinking I need to do is say this stop model dot dispose let's just let's just see what happens there 1000 it's already disposed so that makes no sense hmm what am I missing and I couldn't put it here I'm totally lost here this should really be a function outside of the class I'm not disposing the old birds all right hold on those have once a bird hits a pipe you are hittin the those have a model with tensors I hold on that could be yet Update next generation next generation calculate Fitness Birds saved Birds where's calculate Fitness so baby like these like this let's see here Oh oh there we go area that was it okay alright that's it okay after examining this for a bit with a little bit of help from the chat it looks like the issue that I have is that and it's a little bit weird what I'm doing here I have a bunch of different arrays I have birds saved and saved birds so saved Birds is kind of like the previous generation and I make all these new birds for the next generation put them in the birds array but the models the saved bird models still exist so I need to dispose those and so the way I could do that is just find the place where I'm done making the next generation and then iterate over the saved Birds and a way I could do that is I could also like in the bird object right now I could add a function called disposed little silly to do this but why not which just says this dot brain dot dispose and then in the neural network class I now have a function called dispose which I would say this stop model got disposed so that would this is sort of a manual disposal of all of the memory that that particular model is using and then somewhere in the sketch when I call next generation that makes the next generation and then I go on that's in my genetic algorithm which is here so this is making this is making the next generation and then clearing the saved Birds so before I clear the saved Birds I think it's you know I could just use this total value what I can do now is I can say saved Birds index i dot dispose so that should call the dispose function which calls the dispose function which calls the dispose function so now it's I'm looking at a thousand tensors and then let me speed this up so it gets to the next generation faster oh wow it just got like a good one next generation and now still a thousand tensors so this is really complete I mean there's so much more than I could do but this is complete in terms of the idea of neuro evolution with tensorflow chess and this particular flappy bird game alright so what's coming next what could you do what could I do next so one thing is in my previous coding challenge I saved the model to reload later intensive largess has functions for doing that save and load layers model I forgot what the names of the functions are called but they're in there so I could come back and add that to this the other thing would be to apply this idea to different scenarios different games to different environments one of things that I would like to do is work with steering agents whether they're finding food or avoiding predators that would be an interesting thing to try so hopefully I'll come back and do this same idea but with a different environment that maybe has more complexity to it with a less obvious result because I have a feeling that I could have gotten a bird to solve this game with just some if statements like I could probably guess like you should always jump if you're too high well it shouldn't jump if you're higher than where the the pipe opening is and if you're lower you should jump so you know I could write an if statement instead I've used a neural network thank you for watching if you make your own version of this if you have ideas for how to make this better please let me know in the comments and also over at the coding training com you can submit a link to your version of this or running in the browser and I'll take a look at those and hopefully share them in a future video or livestream thanks for watching and good bye alright black hole new revolution stack I did everything I said I was going to do I recorded two discs I'm done for the week there will be no live stream next Wednesday the next time you'll see me is hopefully two weeks from today the way to find out when I'm live streaming is just subscribe don't smash the button just like click it very gently and nicely if this were my subscribe button now we'd do this thank you Lauren Bell you could click that if you want but only if you want you don't want to be notifications are so annoying reduce those in your life but that's why I tend to schedule them either the day of or the day before and so you'll see them pop up in your feet random numbers I think to round out this yeah Joshua Burke in the chat is saying try using this as a base for running your own pong game agents exactly I definitely want to do that and to round out the day to the tune of my flappy bird girl evolved ages six one three eight three one seven two two two five five two three four one eight nine six three nine zero zero six nine three five zero four eight two three seven three four nine eight one five five two zero eight zero two six one six seven five seven two six five five nine seven three two four five four one four three three five three four five two six four seven one nine and four five two six four three two five three three two two one five seven two seven six five six eight nine six one six two five [Music] don't you feel relaxed deep breathing policy gradients minimax reinforcement learning frozen lake cue learning there's too much in this world I can't do it all people I'm trying my best I have to admit I'm a little bit over scheduled this month and I'm very I feel very I'm sort of I think it's good for me not to be to rebuild some strength and catch up on some things but also I'll be out of town so thank you if it's for everyone who supports me whether you're signed just watching signed up to patreon clicking on the brilliant org sponsor link all those things super helpful and kind of motivate me helps me kind of build the resources to do this stuff more and I appreciate it very much so I will see you all two weeks from today stay tuned for edited versions of these videos coming out to watch those because you're watching right now and thank you very much as always I always forget [Music] [Music] [Music] [Music] this stop this top spot never forget to this stop I'm gonna do this this dot this dot this star this star song never forget the Vista but somebody composed that song for me
Info
Channel: The Coding Train
Views: 50,593
Rating: undefined out of 5
Keywords:
Id: tWyp2OWWcws
Channel Id: undefined
Length: 215min 27sec (12927 seconds)
Published: Wed Apr 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.