How to Make a Tic Tac Toe Neural Network Easily (LIVE)
Key Takeaways
This video demonstrates how to build a neural network using pure JavaScript to play Tic Tac Toe, utilizing a genetic algorithm to evolve the network over time. The network is trained using a game of Tic Tac Toe and can be improved by selecting the best performing neural nets.
Full Transcript
hello world it's SJ hi guys how's it going good to see you hi everybody uh wow just start off with the request a video request stock recommender system uh that's going to happen in the future I actually I have a video on that uh check out predicting stock prices anyway uh I'll probably do another one in the future hi guys hello world oh my God everybody is coming in I'm so happy to see all of you I'm gonna I'm gonna list some names really quick uh George pericas aad sun Nico Sanjay mudit luia anjana bavia Arthur hello everybody today we are thank you I got it from open AI I'm glad you like my t-shirt I'm so proud to wear this t-shirt even though I don't work for them I love this t-shirt okay so today everybody uh we are going to we are going to build uh tic tac toe and we're gonna build a neural network that gets better and better at Tic Tac Toe over time okay that's what we're going to do uh and uh the first thing I want to do before we start building this neural net to defeat Tico is I want to uh answer a few questions so we're gonna do a f minute Q&A and then we're gonna get right to the code and it's in JavaScript I know it's in JavaScript and that's okay because JavaScript is my third favorite language it's like python go and then JavaScript and javascripts actually pure and it's in pure JavaScript so we're not using any kind of jQuery or any of that like you know libraries we're using some pure JavaScript okay so that's what we're going to do so let's start off with a f- minute Q&A and then we are going to uh get started okay ready set go and I'm gonna show you guys a demo at the beginning and the end go five minute Q&A why JavaScript uh because I want to show this to you in the browser and I don't know I'm just I'm just kind of experimenting with JavaScript you know like I'm I'm changing as a as a as a programmer as an artist I I don't want to just like keep doing the same thing and and you know what it's it's really about the the the greater idea and I think it's going to it's you know however many inputs I can show you like how a neural netw works the better okay what do you think about Mark Zuckerberg's AI I think you know I have a lot of respect for Zuckerberg that he did that and he wrote and he wrote something about it Zuckerberg is a CEO of a multi-billion dollar company and he still programs that is something to admire uh Minimax algo actually no it's not going to be using the Minx algo PHP is the shittiest language you're absolutely right can we build a neural net to curb the overwhelming feeling feeling of existential dread yes um we a neural net that can read our emotional state that can we we need some kind of like uh brain computer interface but it could read our emotional state and it could tell us what to do best so kind of like a personal psychologist when are you going to do the AI neural network for Daniel shipments asteroid code uh that's going to be either next week or the week after that but I'm aiming for next week do you have a separate bed for your hair when you go to sleep no uh SJ please do a video on auto encoders for data compression I do have a video on that it's called Uh build an auto encoder in five minutes but I'll do another one soon how advanced is this JS GNA be don't have much experience in JS uh how advanced is it going to be in terms of JavaScript like raw JavaScript it's not that advanced in terms of computer science theory it's a little Advanced because we're using something called bitwise operations which I'll explain in the in the video okay so which which one is better svm or neural network uh it depends on what you're trying to do uh but generally neural Nets perform better uh across a wide variety of tasks uh neuronet from but if you have like just very very very little data and you're trying to do something simple like linear regression probably an svm neural net from scratch injs exactly what is your New Year's resolution I want uh next year I want to get let's see probably 500,000 subscribers and I want to create a movie um with starring myself and I direct it and I want to uh interview Elon and uh yeah that's about it talk about ambition right SJ please do a video on wind load power forecast uh yeah okay I mean something on weather something on weather I've actually been planning that what are you going to make today I'm gonna explain that in a second uh we have two more minutes in this Q&A why is PHP so bad because it doesn't have a lot of Maintenance it's programming language is only as good as the people who maintain the language not many people who are good maintain PHP anymore because all those resources are moving to Python and go and JavaScript why not make an AI that directs a movie someone actually did that check out AI movie script uh Google that what to do after Andrew un's course build watch my videos okay by the way I'm trying to interview Andrew Ang and I tweeted him directly so guys check out the Twitter check out my Twitter and then look at my tweets like tweets and replies and see the one that I asked Andrew Ang to interview for my channel like it retweet it okay the more people who like and retweet it the more likely he is to say yes I want to interview that guy okay what is your secret sauce my secret sauce is I genuinely truly from the bottom of my heart I love what I do I love AI I love neural networks I love being a YouTuber and it took me a lot of tries to to fail fail to get to where I am so my secret is that no matter how many times I failed I I I pick myself up I talked to my friends and I and I and I just kept going okay so one more question and then we're going to get going uh I want to do a question I've never answered before uh why serology to SJ rvel great question I changed the name of my YouTube channel and my just whole public Persona to my full name because as I grow I I'm becoming a public figure and I want to be known as my full name SJ Ral it's not sjy the game is over it shit's getting real now okay so I am SJ Ral okay so just remember that okay so here we go that's it for the 5 minute Q&A let's get started with the code okay so what we're going to do is we're going to build a neural network uh to beat the game of tic tac to I'm gonna go ahead and start screen sharing and then we are going to get started okay uh all right so here we go let's get started screen share okay so what I'm going to do is I'm going to demo this for you guys this actually in the browser okay it's actually in the browser um but this is what it looks like let me just let me just move this to the side and let's see what you guys are saying here uh okay so this is what it looks like guys so what it what it's doing is it is it is running a genetic algorithm okay and it's going to look like this these are these are the four board games it's got four board games running simultaneously this is on the web this is a web demo of the code that I'm going to do and what it's doing is it's trying out a bunch of uh it's four AI simultaneously trying out a bunch of moods and it's recording what's happening in the source code okay it's recording what's happening in the source code okay and we can take this source code the source is the weights file okay the the weights are the connection between the synapses in the neural network we can take these weights and we can put them in our neural net and it's going to get better over time okay so I'm going to explain this step by step this is what it's going to look like okay it's going to look like this it's a game of tic taac toe we're going to build Tic Tac Toe from scratch and then we're going to build our neural network from scratch okay and there's quite a bit of code here so I'm just going to get started let me X out of this and we're gonna get to the text editor okay all right uh okay so what I'm going to do is I'm going to put this uh on the side so I can see what you guys are saying just like that palms are sweaty knees weak arms are heavy there's vomit on his sweater already mom SP okay let me make sure I'm in the screen this time okay there I'm in the screen okay so let's get started okay I'm going to code two class filed why do you talk so fast makes me nervous okay ryus I will I will you're right okay everything's cool everything's cool okay so um let's get started the first thing we're going to do is we are going to build our neural network H sorry we're gonna build the game we're gonna build tic TCT toe okay it's going to be tit. JS okay here we we go um so let's get started the first thing okay so what is this let me let me uh let me explain what we're going to do here we're gonna Step One is we're going to generate um a randomly sized let me make it bigger up up size population are you guys seeing these comments can you build harambe can you build harambe oh man I love you guys man you guys are just the best okay so we're going to generate a randomly sized population it's going to be player versus computer okay it's to be player versus computer okay so we're going to generate a randomly sized population of neuron Nets of neuron nets for the best one and then we're going to uh and then we're going to uh we're going to the top scoring ones are favored to P to populate the next gener generation uh best ones get to reproduce okay so that's how that's the basic logic of this game okay uh it's it's we're gonna use pure JavaScript it's going to use web workers web workers are and I haven't actually talked much about parallelism uh but but web workers are basically uh concurrency they're threads in JavaScript we want we want these things to be happening in parallel remember the demo I showed you guys at the beginning there were four games happening at the same time those are web workers are happening concurrently that means at the same time okay uh we are using tabs not spaces okay so let's go ahead and get started increase the font size I can do that b up up up how how about that how's that how's that for big as F okay so we're gonna we're GNA Define our neural net uh and we it's going to be called TTT for Tic Tac sorry we're going to Define our game which is called tic tactoe or TTT okay we're going to start off with a function and the function um uh is going to be TTT all right so that's the function and let's get started here and we're going to say you know this is going to get a opening and then that and then okay so all right let's get started bigger than that my Twitter handle is SJ rvel s i r a j r a v a l let me make that even bigger for you guys okay so this is as big as we are going okay so let's start off by defining um are are three states so uh we're going to say bar X bar um o which is the which is the sorry this that's the VAR o is you know X's and O's okay and then bar Tai and the and we're gonna be X and the computer is g to be o okay and Tai okay so now this is going to take about 45 minutes if I can if I can yeah that's right you guys don't need to see the chats that's true that's true all right I'm just gonna so I'm not I can't see what you guys are saying right now I'm just gonna like start coding because that really makes it easier for me so um and but I'll I'll pop it up once once in a while okay so let me move this all right so let me make this a little bigger okay so let's get started okay so the first thing we want to do is think about the squares the squares like we're going to iterate through through them and it's going to look like this the iteration is going to look like this it's going to be like it's going to be like zero one two three four five six seven eight okay there are eight the a game state is remember a Tic Tac Toe is there two vertical lines and then there's uh two horizontal lines okay so it's it's going to look at this and when we iterate them we're going to iterate at the top left and we're going to move to the bottom right okay top left to bottom right and it's going to look like that so I just wanted to show you that for a second okay okay so now let's go ahead and initialize our board so we're going to say function new board and we're we're going to start off by initializing our helper classes helper classes okay these are our helper classes that's what we all we want to initialize our helper classes so um what is a what is a new board well it's gonna get nothing right we're just initializing it so we're gonna return zero a new board gets absolutely no value we're gonna we're gonna add value to that new board what's another helper function we need we want to check if the board is empty right so let's check let's create a function called is empty and is empty checks uh what is the parameter the board itself right what are we checking we're checking if the board is empty right so let's check if the board is empty and we can return it's going to be a true or false value so do a in JavaScript there are three uh equal signs to for for this so we say return board zero okay so if it's zero then return that value it's gonna be true or false it's going to be a Boolean value okay so that's that's that helper function what what else do we need here let's say we're going to get the piece okay what is the piece the piece is what this is to retrieve uh what's what Square in the tic tac toe right which Square are we using here okay um say board we're gonna say Square okay those are our two parameters we got the board and the square which one so we want to get the value of that piece and what are we doing here well guess what guys we are defining each of these uh squares in our tic taac toe not as integers but as bits okay and why are we thinking of them as bits each of these are two numbers they're they're they're they're bits that are two numbers long like 0 one 02 03 like that and why are we doing that well it is faster to perform uh uh it's it's just faster if if we're operating at the bit level rather than abstraction which are integers if we're operating at the bit level all of our operations are faster okay and that's one of the great things about JavaScript is we can do that uh so we're going to operate at the bit level and what what does that mean that means that we're going to perform what are called bitwise operations bitwise operations so let me let me let me write this and then I'll explain what I mean by that okay so board okay um and I'm going to pull up the chat in a second because I want to see if you guys know what I mean when I when I do this because this is uh going to be interesting so up up up and three okay so what does this mean what does this mean this is this is a bitwise operation so this essentially like so what this is saying is so for this uh so for this like what whatever's in the parentheses here for the square this basically means uh take the square and multiply it by two to the one the first which is the one value and that's the value we get for that that's that's essentially a bitwise operator um and then what does it mean for the board well for the board that that the whole thing means we want to take the board and we want to divide it by uh whatever the value up here is okay whatever this value is so it's G to be this and then for the for the and sign we are going to do uh bit wives and multiplies uh those two values uh by three just because there are three squares uh both horizontally and vertically so what this is going to do is it GNA give us the bit value of whatever of wherever we are uh in the game that's it's gonna give us that raw bit value because that's what this whole game is operating on it's it's operating on bit values okay because it's just faster to run our neural net that way okay so that's what that's going to look like and I'm going to pull up the chat for a second I want to see if you guys are uh let's see hold on hold on let me pull up the chat where is we where are you guys okay all right okay you guys are on point you know I'm just gonna keep this chat window because I I want to see you guys because I I you know just in case okay all right thanks guys okay yes we understand this okay cool cool cool cool cool okay all right so so that's that's we're getting the piece uh it is an open AI t-shirt yes not that I work there but I love the T-shirt okay so so now we've got the piece uh and now we're gonna perform a move operation so now we're going to say move um uh so for the move operation what we want to do is um no actually I'll close that out okay so now for the move operation we're going to say we got our board we have our square and now we have our piece and we want to move that value to another position in the board okay so uh what does move look like well we want to say we're going to again we're going to perform a bitwise operation and I'm gon to explain how that works so we're gonna say board or uh let's see piece less than or not not bitwise left it's a shift operator it's not a less than it's a shift operation so Square uh d da one okay just like that and so we've got one two three okay so what is this doing it's going to take it's combining those values so that's what the or operation is doing it's combining those values so let me write a quick example of what I mean by that so if we had 0 one 01 and then we had um o uh if we had 01111 to combine what this is doing is it's combining those values to show the move so this would become one uh one one one right so it's taking it's it's it's that or operation so so just like that okay so that's the move value okay so that's what it's doing for move now um so those are our helper methods and let's go ahead and start writing our game uh functionality so we're going to say game functionality uh so we have our board we have the turn whoever turn it is and we have the history of the of the game State okay and this is stored using this is stored locally and we're using javascript's local storage uh functionality to do that it's storing it locally in a cache in a temporary cache okay and that the history of game States and it's going to be and and and it's going to be a stack a stack data structure so we can easily pop the top off and replace it and we can push something new on it every time that's why we we're using a stack for that okay so let's go ahead and uh initialize our values here uh this is one of the annoying things about JavaScript uh we just we we want to we want to initialize these uh these values okay so this do turn hold on this. turn equals turn and this. history equals history kind of annoying but we have to do that and it initializes those values so now what we're going to do is we're going to define a prototype so every JavaScript object has a prototype a prototype is also an object uh and all JavaScript objects inherent properties uh uh from their Pro and their methods from their prototype okay so this is kind of like the the god of the object so the the Prototype so we're going to call the Prototype of the game object and we're going to define a bunch of uh functions for this okay so the first one we want to do is Define the equals function what what's the equals function doing the equals function says let's take that um game equals function that we already have uh and we want to see if two places in the game state are equal to each other okay so we're going to say hold on on make sure that's all on point okay and okay so what we're going to do is going to say return the value um of whether or not it's equal so we're going to say we're going to check if the board if the board equals uh whatever The Other Board is and what is the other board The Other Board is going to be the next state okay we want to check if they're equal to each other okay um and uh the turn is the same as this other. turn Okay um and this is going to be so that's the equals value so we're checking if it's equal or not uh and want to see where you guys are at okay um [Music] um let's see okay cool okay so so that's so that's that you guys are keeping up more or less and now we are going to uh Define the get piece method or for the Prototype uh prototype. get piece and the get piece method is going to get the value of that piece from our prototype right uh and the value is remember it's a bitwise it's it's a it's it's a bit um so we we have um a local function for this and we're it's we're going to initialize the the Prototype using that local function the square is the actual the the game state or sorry the board and we're going to return we're going to return uh the piece which is going to be when we give it two parameters the board and the square right the board and the square because we've already defined that get piece method up there remember up here we defined it so we can just return that for the Prototype okay um uh let's see what else do we need we need to uh oh a move function right so prototype uh oh this is goingon to be fun to explain so prototype. move um and then we're g to say well what's the game move what is the game move given the that we are at given the square that we are at what does this look like okay here's the fun part here's the fun part so we're going to say given our history in our local cach which is a which is a stack remember I said it's a stack we're going to push the new value uh of this the new state of the board onto our stack remember it's a it's a stack of of game States and then we're gonna say uh let's get this uh let's take the board that we already have and perform form a move because we just moved it right uh given the board the square and the turn Okay the turn of where we are lastly we want to define the turn and it's going to be this uh xor operator this is going to do a a bitwise exor operation and it's going to assign the result to the first operate operand so what does that mean it's gonna basically it's Shifting the bit so it's either you or me you or me you or me right so that's it just shifts one one way or the other okay so whose move is it yours or the AI okay so now uh we're going to uh uh we also want an undo function right game. prototype. undo okay so function game undo so what does this mean so if we want to undo one of our moves this is what we have to do we've got to take the board we already had and this is why it's great to have a stack for this right the stack data structure is um great for this because we can easily we can uh it's it's a constant time retrieval of whatever we just push pushed on it it's a constant time retrieval okay so we're gonna say this. history. pop we're gonna pop it off the stack what was that last game State what we want that game State and we're gonna we're g to get it and then we're going to uh remember we're going to switch that turn back using that exort operating it's it's back to you or it's back to me whoever was the opposite okay and so now we want to declare our winner so how do we declare our winner well it we're going to do once again game. prototype. winner equals function and then we have a function internally for who is the winner of the game given the board right given the board we can Define who the winner is going to be and um the winner is going it's going to return uh the the value of who is going to be given the the board this. board okay so now um there's one more thing I want to do in this class there's one more thing I want to do we're gonna we're g to draw the board okay we're g to draw the board and before I draw the board I'm going to write this out and I'm going to explain what this is okay CTX CTX is our canvas this is how we're drawing in JavaScript don't be confused that think and think this is the context it's it's it's the it's the canvas which we can think of as an object that we're passing around and how we draw on the screen it's a canvas object uh and uh we're we're going to step by-step draw out this board okay uh and it's going to be a series of values here so we're GNA start off by we're going to initialize our canvas by saying begin path okay and then we're going to say uh we want to do a move two function and we're gonna actually do this several times so I'm G to copy and paste this one two three okay just like that and let me let me talk about what I'm doing here but first uh have my move to and then I have my line two line two okay I am iteratively right here I am iteratively drawing out this uh State can anyone okay so now I'm going to engage you guys for a second before I draw this I haven't done this before so what I'm going to do is I'm going to engage you guys can anyone tell me in their own words uh uh when when to use a canvas when to use a canvas in JavaScript like what is an example beside this okay when when is a good when is when would we use a canvas like besides a game what's like another application I just want to make sure I've got you guys uh no idea okay so another so another um example would be drawing guis exactly okay any any kind of GUI image proc image processing great example making animations Okay cool so so great to draw stuff okay so you guys are maps all right a lot of great great suggestions okay so exactly so we're gonna draw this canvas so this is the this is the kind of like uh this is the kind of a little boring part but you know I'm just going to write out these values and I'm going to explain what I'm doing uh which is always the you know that part about games where it's like you know you got to manually you know write up the the values for the the coordinates and and um yes it's 666 I know yes that's hilarious isn't it okay so want move to 0.95 0.33 and you'll notice that I'm uh using similar values here move to um 0.666 okay so move and then I want to end it with a stroke and that's G to be okay hold on okay so what's happening here okay so I'm drawing out each of those lines okay so I'm moving to a point and then I'm drawing a line and I'm saying what is the what is the XY coordinate that I want to draw that uh or or have that point and what is the end point so basically it's it's saying okay draw one line uh down vertically draw the next line down vertically draw the other two lines horizontally and then it does all that in one stroke and that's it's called by the stroke function okay so that is what I'm going to do for the uh Tic Tac Toe code and now I'm going to get straight into the neural network okay so let's go ahead and get started with the neural network um H the neural net code is going to use this this code um to uh get better over time so now we're going to write our neural net okay so I'm GNA use a neural Network okay code I'm going to write that out and let me make sure you know you guys are you guys are on point you guys are all good everybody's everybody's Gucci wooo all right okay uh we got people from all over the place and we're going to do a neural network now okay um cool so let's get started so first we're going to Define our neuronet variable okay and how does that work well we we're going to use this let me just so we're going to Define our neural net okay uh and we're goingon to write all of our functions inside of this neural net um and oh we also want to make sure that we are I forgot about this this is one of those JavaScript things about the browser use strict so what is UST strict ustri allows you to place a program or a function in a strict operating context what is that that mean uh basically it prevents certain actions from being taken and and throwing more exceptions so it's kind of like a safety check so you know when you're in the browser you're gonna have a certain things happen that you don't want like uh let's say I don't know some some kind of crash or like uh just an unexpected uh operation especially since we're using concurrency there can be problems like Deadlocks there can be problems like uh you know just like uh and uh just a lot of things can go wrong and strict helps prevent that okay so so so that's why we're using strict and uh let's go ahead and get started here so we're going to write our our first function here for our neural network which is the get sizes function what does this do so okay let me let me talk about this for a second and I'm gonna have the chat open for this while I while I explain a little bit about this so um uh so so what we're doing here is we are building the neural net right this is a feed forward neural net this is a feed forward W neural net and a feed forward neural net it's a three layer feedforward neural net with 128 hidden nodes they're 128 hidden nodes okay um and the and and why why are we doing this uh the brain had oh let's think about the brain by the way uh this is going to be a two-minute tangent where I talk about the brain and then I'm going to get back into the code okay so the brain has like aund it has a 100 billion neurons which communicate uh through electrochemical signals and these neurons are connected through Junctions known as synapses and each neuron receives thousands and thousands of connections with other neurons constantly signals to reach the cell body okay and the resulting sum of the signal if the resulting sum of the signals surpasses a certain threshold then a response is sent throughout the axon so we have to reach uh a certain threshold for that Val for the value to um to propagate forward through our through our neural network okay that's what we want to do and I'm going to talk about the threshold in a second okay so let's go ahead and start with our get sizes function so for get sizes and by the way guys check out write this down okay write this down MCB 180x I'll say it again MCB 180x it's a free course online by Harvard University which I took I absolutely love it's gonna it it talks about the brain it's an introduction to neuroscience and it provides a great Gateway into neural networks you see things like uh feed forward recurrent these terms that we talk about uh in in in U machine learning they are actually there in the brain and this is a great course to learn about the brain and kind of get biologically inspired by the beauty of our brain's internal neural networks remember mcb1 adx great course okay so let's get back to this so what we're going to do is we're going to return uh a collection of nodes nodes are uh we have nodes in each layer of our Network right a three- layer feed forward neural network each layer has an amount of nodes so what we what we want to do when this get sizes function is we want to get the size of each layer and what is the size of each layer it's going to be the number of nodes or neurons in each layer so we're going to say return nodes. map fun return nodes. map and we're going to say um uh for that layer let's uh return that layer length all right so we have the length is going to be the number of nodes in that layer so that's going to get give us the length this is a helper function so the next one is well we we we know how to get the sizes but let's make the actual node or neuron itself so let's write a function for that so we'll say make node given the layers index given uh the index itself the sizes and the nodes okay um let's so let's initialize this hold on damn it okay H let's initialize it right so each node let's let's start off by initializing uh our node our node is going to have an input of zero that's going to be the initial input they have nothing remember there's nothing in there uh it's going to start off as zero okay and now we want to Define our threshold fine threshold okay and remember I talked about that threshold right the threshold is the the the the the top it is the the threshold is the limit right and if and if the amount of the sum of all the activation functions on the data from one net to the other if it reaches that threshold then it can propagate but it has to reach that threshold for it to propagate just like in the brain okay so let's define the threshold and we're going to say if the layers index is less than the length of the sizes uh of of the layers then we're going to say get that threshold and we want to initialize it as um whatever comes out of this function okay what is the function we're going to say the type of nodes if it's undefined so as if as in there's nothing there or uh that value is one then uh the nodes it's uh get the layer index and the index the other index let me let me talk about this what what's happening here let's see so each node is connected to every other node in the next layer so it's a 2d matrix it's a 2d Matrix Matrix of values all right each no is connected to every other node remember those weights those connections those synapses and it's a 2d Matrix and we're going to define the threshold just like that okay so now speaking of Weights let's define our nodes weights we have so let's define those weights so those weights uh are a part of the node and we're going to say type of uh nodes if it's undefined again so if there's if there's nothing there or or if there's uh if there's if there's nothing there or uh hold on new array sizes layer index plus one let me just write this out and then I'm going to Define what I'm talking about here uh layer index and then I'm going to pull up the chat for a second to let's see where you guys are at um function W and return W okay okay so let me talk about what's happening here okay so hold on okay so we have our weights of our nodes and if it is or there's if we've uh if if the if the node is empty or there's there's an array with with with some value in it then we want to uh return the number of Weights in that in in that node and we're going to update the the the connections in that node uh via this weights. map function okay and we're and we're going to return the value so let me Define the weights that's what this is doing it's defining the weights okay now let me see if anybody has any questions here let's see what do we got here what do we got here um nothing in the chat all right cool cool cool okay all right cool cool okay so those are our functions and when we uh and now we can return that node okay that's the end of that uh function and we can return our node okay um boom so now let's define so that's the node now let's define the network itself the actual Network right it's a neural network so let's define that Network and for the input we could give it we could either create our we can either initialize our network via a sizes value or a node value as in uh the size of a layer is equal to the number of nodes so we're just going to say sizes or no nodes okay we can initialize it either way um and we'll Define we'll Define variables for both of these things for the sizes and for the nodes so we want to say if uh the array is an array of sizes or nodes that value we had for the parameter and the array is an array and if it so let me be more specific about what I'm doing here so um sizes or nodes hold on and if there's something in there then okay so first of all so what is this saying this is saying if first of all if there if the if the parameter is not empty then we want to take those values and we're going to initialize them the the the the local variables that we initialize we're going to use that get sizes function that we defined and we're going to use the sizes or nodes uh parameter and then we're going to initialize our nodes okay they're two values that we're thinking about here sizes or nodes um else uh sizes equals sizes or nodes okay um that's our else value and now we want to Define how many nodes we have okay so now we're going to Define how many notes we have so so how many nodes do we have the map function is going to help us get that that value uh and it's going to be a callback it's going to be via a function callback where we have a size and the um an iterator as the parameter okay which is which we're gonna we're going to use to to to iterate through this uh um which is we're going to use it to iterate through the array count the number of values and then that's going to be uh the the number of nodes that we have okay so we're going to say VAR each layer is going to be an array uh initialized by a size okay and so and so the layer is initialized as an array of sizes so let's do our iteration to count the number of nodes we're gonna we're going to count the number of nodes so say bar J starts off say zero and uh J and remember J is just arbitrary is less than the size because we're iterating through the size and we're going to say uh Plus+ J okay so we're gonna iterate through each uh and so and then I'm gonna pull up the chat and we'll answer a few questions or if any if there are and what is that so what is what is each layer uh we're going to we're we're giving those layers values here and we're saying make a node uh given the the length given where we are given the sizes and given the number of nodes okay and that's how we're going to Define each of our layers and when we're done with that when we've when we've uh initialized each of our layers we're doing two things here we're we're defining our nodes and we're initializing our layers at the same time okay that's so and then we'll return that value when it's done okay and uh so there's that um okay so who got that there where do I get the T-shirt uh guys just wait a second I'm G to I'm going to launch a a clothing line soon okay right no self-promotion guys okay so so that's that um okay one more function in this class one more function in this class so we want to uh we want to set the weight values okay we're going to set the weight values so we're going to say the Prototype of our neural network is going to set our weight values um right so we could also if we wanted to we could write a get function so we could get our weights but we're just going to write a set weights function okay um function is going to be the net. set weights um given the so we're going to give it weights as a parameter what are those weights going to be well they're going to be a 2d Matrix each layer for each node each node so in inside of each layer in our neural network there are a collection of nodes and each node has a 2d Matrix of Weights that connect it to every other node in the next layer and we want to set the values for those weights and how are we updating this well it's not through back propagation it's through genetic algorithm right so instead of just saying we're going to update the weights constantly um via back propagation uh uh and gradient descent we're going to update the weights by putting them by by having several neural Nets and just having them play like I showed you in the web app and then taking those weights uh and taking the weights of the best ones and and and updating the only the best neural Nets and then having those and then and then praying more from those Okay so so okay so so for for each node we're going to say well where are we where are we in terms of our layer and where are we in terms of uh the weights so there are two indexes here the index of where we are in the layer and then the index of where we are in the in the weights uh Matrix okay so um we're going to take the weights of our node and by the way this code is going on GitHub guys I'm gonna I'm gonna within an hour of me finishing this I'm going to put post the code on GitHub the complete code including the HTML and the CSS which is actually quite long uh which I'm not going to do in this I'm just doing the logic I'm just doing the hard Logic for this the logic that matters okay so um so our weights are going to be um we're going to take the the index that we were we were given and then we're going to take the index right both of those were given we're going to map it to um VI have this call back function and then we're going to return the value of w which is going to be our uh weight 2D Matrix okay uh now at the end of this we're going to uh say that our neural net equals the neural net that we've just initialized okay and um and then we could return that okay so now let me see the chat and okay okay so now so so so let me let me show you guys the Dem okay so that's so um so let's let's go over what I what I've done so far um so what I've done is I've initialized my board uh I've initialized the I've created a set of helper methods to get the pieces to check if it's empty uh to move it to get the pieces then I've initialized the game State and I've used the Prototype uh function of JavaScript of the game object to define a bunch of very important moves like checking if two pieces are equal to check if two squares are equal to get the piece to to move um to see who the winner is and then to draw the board using canvas I've created a ner neural net function or a neural net class uh that's that initializes a node what does a node look like well a node is a an object that has connections to every other node in every layer of our neural network um there is a library called convet JS created by Andre karpathy which I've met he's a cool guy um but um yeah but that's not what we're doing right now I I thought about doing that I might I I will do that in a future video um and uh We've we've initialized our node we've initialize our Network we've set the weight values for each of those okay and so that's how so that's how um that is working and and I will fully comment the code and post this on GitHub it's not a recurrent net it is a feed forward N Net so let me let me just show you guys this code okay so hold on what does this look like um so that's what that looks like and getting a call right now do not call me while I'm live you know what I'm saying okay so so that's what that what that has done and what I want to do is I want to show you guys uh what that looks like um so we're going to go to the um that value I could run this off a local server um um but I don't want to do that right now I want to uh hold on where was this I'm gonna open up Chrome for a second what was ITT was it what was it iot it was something but essentially um that was what it was and um oh guys all right okay so um so that's what that was and showing you guys my history here okay so that is our uh MacBook is promoting itself right okay so so that was the code and uh what I want to do is I wanna if I can get the link to this show you guys you know what I'm going to post the link in the description and uh then I'm going to the the link to the web app is going to be there but but what happened was I I had that I had the demo but I just can't find the uh link to it right now so what I'm going to do is I'm going to stop the uh screen share here and stop screen sharing and I'm gonna be do me okay so uh an ending five minute Q&A and then we can get started okay let's see am I I'm still up there yes yeah I'm still there okay so yeah Okay cool so uh five minute Q&A can you make the same Tic Tac Toe with Java yeah absolutely uh I I uh don't have a link to that uh but if you just search tict tac toe on GitHub and then just Java I promise you you will find results okay yes there we go thank you chaa he showed uh where it's where it's at where it's at exactly you know what I'm going to show you guys the the demo of this okay let me let me show you guys the demo you guys deserve a demo so so so so I'm going to screen share oh I'm so excited let me let me start the screen share okay guys check this out okay so what what's happening this what it's going to look like we take our we take our weights we take our weights okay which is it's going to learn via genetic programming and then we can we can we can test it so what does that look like hold on okay so we're gonna say AI neural net and we're gonna paste those weights in the weights that it learned from genetic Pro uh from from from this genetic environment and will past those weights in this remember these weights are a matrix of values of what it's learned through playing okay and I'll play something it's G to think and then it's G to play and then I'll play something it's going to think and then it's GNA play look at this smart thing it's trying to go in that bottom left corner I am not gonna let you because you are not smart enough to beat a human yet okay so that's what that looks like okay so I'm going to stop screen sharing again and back to my now three minute Q&A but uh whoever sent me the link to that thank you so much okay so three more minutes Jupiter notebook uh that I'm gonna you know what I'm gonna start using more Jupiter notebooks um for sure uh in the future great uh great thing uh thanks David for the compliment uh can you do more slow tutorials focus more on understanding the theory yes absolutely Max I I I'm getting better at this where did you go to schooling I went to Columbia I studied myself on the internet all this stuff on my own how is this better than simply writing a tic tac toe game using the Monte Carlo method um so the Monte Carlo tree search is actually quite um effective and uh Al and alphago alphago use Monty Carlo how is it better it's not better actually um it's just different did you add more gray to your hair Spar uh I didn't it's just it's just always like that now but I'm going to regr it in the future there training more with with more does training more with more layers make the AI better uh generally the more layers you add the better your neural network is uh but the trick is to just to just have as many layers as you need because the more layers you add the more computation there is right so like always there's that tradeoff SJ I would like you I would like to suggest to you a simple game I made and want to implement a bot into it us definitely post that in the comments which books are useful um Andrew Ang has a new book out called machine learning yearning uh Ian Goodfellow uh has a book out who's one of the researchers at open AI uh called Deep learning which I would definitely re uh read is the gray hair related to machine learning the gray hair is Just My Style you know what I mean Which laptop are you using a MacBook Pro 2015 no it is a MacBook Pro 2016 with the force touch could you make a simple tutorial about deep learning which learn you from Zero to Hero in that field that is coming up next year and by next year I mean in January it's going to be a from scratch series that I'm going to make um hi SJ from where can we get the data set for an AI girlfriend um look for chat logs for um uh chat chat room chat logs for like a dating site are you slowly building a self-learning AI for retro games maybe in the future uh yes I I mean I I I I am going to focus more on games in the future yes absolutely okay do you recommend JS or python for AI learning python python uh but but but the conet JS Library by Andre is actually pretty good Jake glad to have you here at my live uh live cast harsh thank you a a poor can we make more similar programs yes do a neural net for playing Tetris eventually make a neural net for assembly okay so um I I'm going to tell a short story and then we're done um End video with the song that's a great idea I you know what I'm live so I'm just gonna freestyle okay so uh by the way I had this uh my my mentor at Meetup when I worked at Meetup as an intern he was like I remember like being really confused I was it was I was an iOS programmer at Meetup for the summer and I was like trying to figure out this problem and and if any of you guys have used X code before you know that when something goes wrong and you have a breakpoint it shows a bunch of assembl which I've always kind of just like ignored but he was like I was like I I had I was focusing on this problem for like four or five hours and I just couldn't figure it out and then he comes in uh my mentor Michael Gray and he's just like he looks at the assembly and he's like within 30 seconds he's like that's your problem looking at the assembly how gangster is that so yeah a neural network for assembly maybe maybe in the future so I'm going to end this with a freestyle rap someone give and I'll freestyle about machine learning so some someone give me a topic I'll go it's going to be a One Minute Freestyle someone say a topic just someone say a topic I'll just wait for a topic here we go whatever is the first topic uh assembly is badass so let's just do assembly okay I love assembly I'm like it's like my enemy because it's so hard that I cannot see what is happening my mind is so free when I look at it it looks at me and it says you can't beat me because I'm better than you don't you see okay so that was my short 15-second freestyle okay so anyway uh thanks guys for watching for now I've got to go focus on this video I'm making so thanks for watching love you guys code is going up very soon join the slack Channel by the way there's a slack Channel continue the conversation there it's in the description of every one of my videos that wasn't even a minute I know I I'm getting better okay so thanks guys love you okay how do I end the session there we go stop
Original Description
I'll use pure Javascript to build a neural network that evolves via a genetic algorithm to eventually become amazing at Tic Tac Toe!
Code for this video is here:
https://github.com/llSourcell/tic_tac_toe_neural_network-LIVE
Join us in the Wizards Slack channel:
http://wizards.herokuapp.com/
Some things i mentioned in the video ---
Free Harvard Neuroscience course:
https://www.mcb80x.org/
Great newer Deep Learning Books:
http://www.deeplearningbook.org/
http://www.mlyearning.org/
Also tweet Andrew Ng directly and ask him if he will interview with @sirajraval . I'm trying to get him on my 'Really Quick Questions' series.
Please subscribe, like, and comment! That's what keeps me going.
and please support me on Patreon:
https://www.patreon.com/user?u=3191693
Follow me:
Twitter: https://twitter.com/sirajraval
Facebook: https://www.facebook.com/sirajology Instagram: https://www.instagram.com/sirajraval/ Instagram: https://www.instagram.com/sirajraval/
Signup for my newsletter for exciting updates in the field of AI:
https://goo.gl/FZzJ5w
Hit the Join button above to sign up to become a member of my channel for access to exclusive content! Join my AI community: http://chatgptschool.io/ Sign up for my AI Sports betting Bot, WagerGPT! (500 spots available):
https://www.wagergpt.co
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Siraj Raval · Siraj Raval · 60 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
▶
What is Bitcoin?
Siraj Raval
5 Ways to Use Bitcoin
Siraj Raval
BTC Fever - Siraj [Music Video]
Siraj Raval
5 Reasons to Build Decentralized Apps
Siraj Raval
The Interplanetary File System
Siraj Raval
How to Build a Dapp in 3 min
Siraj Raval
Life Before Smartphones
Siraj Raval
4 Ways to Use Smart Contracts
Siraj Raval
3 Dapps You HAVE to See
Siraj Raval
Char's Life as a BitTorrent Engineer
Siraj Raval
4 Reasons AlphaGo is a Huge Deal
Siraj Raval
Build a Neural Net in 4 Minutes
Siraj Raval
Sentiment Analysis in 4 Minutes
Siraj Raval
The Hackathon Life
Siraj Raval
Your First ML App - Machine Learning for Hackers #1
Siraj Raval
Build an AI Composer - Machine Learning for Hackers #2
Siraj Raval
Build a Game AI - Machine Learning for Hackers #3
Siraj Raval
Build a Movie Recommender - Machine Learning for Hackers #4
Siraj Raval
Build an AI Artist - Machine Learning for Hackers #5
Siraj Raval
Build a Chatbot - ML for Hackers #6
Siraj Raval
Build an AI Reader - Machine Learning for Hackers #7
Siraj Raval
Build an AI Writer - Machine Learning for Hackers #8
Siraj Raval
Build a Chatbot w/ an API - ML for Hackers #9
Siraj Raval
One-Shot Learning - Fresh Machine Learning #1
Siraj Raval
Generative Adversarial Nets - Fresh Machine Learning #2
Siraj Raval
Tone Analysis - Fresh Machine Learning #3
Siraj Raval
Generate Rap Lyrics - Fresh Machine Learning #4
Siraj Raval
Build an Autoencoder in 5 Min - Fresh Machine Learning #5
Siraj Raval
Build a Self Driving Car in 5 Min - Fresh Machine Learning #6
Siraj Raval
Build an Antivirus in 5 Min - Fresh Machine Learning #7
Siraj Raval
TensorFlow in 5 Minutes (tutorial)
Siraj Raval
Build a Recurrent Neural Net in 5 Min
Siraj Raval
Build a Simulation in 5 Min
Siraj Raval
Build a TensorFlow Image Classifier in 5 Min
Siraj Raval
Tensorboard Explained in 5 Min
Siraj Raval
Generate Music in TensorFlow
Siraj Raval
Build a Game Bot (LIVE)
Siraj Raval
Deep Learning Frameworks Compared
Siraj Raval
Introduction - Learn Python for Data Science #1
Siraj Raval
Build a Neural Network (LIVE)
Siraj Raval
Twitter Sentiment Analysis - Learn Python for Data Science #2
Siraj Raval
Recommendation Systems - Learn Python for Data Science #3
Siraj Raval
Predicting Stock Prices - Learn Python for Data Science #4
Siraj Raval
Pong Neural Network (LIVE)
Siraj Raval
Deep Dream in TensorFlow - Learn Python for Data Science #5
Siraj Raval
Visualizing Data with D3.js (LIVE)
Siraj Raval
Genetic Algorithms - Learn Python for Data Science #6
Siraj Raval
Enter Siraj [Music Video]
Siraj Raval
Build a Web Scraper (LIVE)
Siraj Raval
Why is P vs NP Important?
Siraj Raval
How to Make a Neural Network (LIVE)
Siraj Raval
How to Make an Amazing Tensorflow Chatbot Easily
Siraj Raval
How to Make an Amazing Video Game Bot Easily
Siraj Raval
How to Make a Tensorflow Neural Network (LIVE)
Siraj Raval
How to Make a Simple Tensorflow Speech Recognizer
Siraj Raval
Joel Shor - Really Quick Questions with an Awesome Google Engineer
Siraj Raval
How to Make a Path Planning Algorithm Easily (LIVE)
Siraj Raval
The Best Way to Prepare a Dataset Easily
Siraj Raval
Catherine Olsson - Really Quick Questions with an OpenAI Engineer
Siraj Raval
How to Make a Tic Tac Toe Neural Network Easily (LIVE)
Siraj Raval
More on: LLM Foundations
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
How to Learn a Hard Technical Skill Without Burning Out
Dev.to · Anas Kalthoum | FreeBrain
After interviewing over 100 ML Candidates. Last Week Someone Walked In and Made Me Take Notes.
Medium · Machine Learning
How AI Learns with Less Labeled Data
Medium · Machine Learning
Mastering TypeScript — Understanding the TypeScript Compiler (tsc) from Scratch — Lesson 2
Medium · JavaScript
🎓
Tutor Explanation
DeepCamp AI