Placeholder Image

字幕列表 影片播放

  • Heil and welcome to this new tutorial, Siri's, to create a neural network in JavaScript, which we will hopefully use to automate the game Asteroids.

  • Now we made this game.

  • Asteroids in another tutorial, Siri's so feel free to check that out and download the code from the links below.

  • There should be Sounds folder and an HTML file, so that's up in the game up.

  • So there's a triangular ship that can be moved around the string, and it can shoot yesterday.

  • I'll just meet that.

  • So the goal of this tutorial, Siri's, is a very simple one, and intentionally so because new neural networks can be quite challenging.

  • The goal is to rotate towards the closest asteroid and shoot, rotate to the next closest asteroid and shoot.

  • It's not gonna be an algorithm.

  • We're going to train and Neural Network to do this.

  • So what is a neural network?

  • Well, here it is.

  • Basically, they're made up of a number of layers, so in the import layer, a hidden layer and an output layer, you can't have multiple hidden layers, But we'll focus on just having a single hidden layer to keep things simple.

  • Now, the input layer it will take out game information so we could pass things such as the angle to the nearest asteroid or the current angle of the ship.

  • The hidden layers more like a processing layer, I guess.

  • And the output?

  • Well, we'll probably have a single output whether to turn left or turn right.

  • So that'll be a single Bullen.

  • So zero will mean left.

  • One will mean right.

  • We'll keep things nice and simple because these things can be quite complicated.

  • In between each of these layers is a whole bunch of black errors.

  • Now each of these black errors represent, await, await between minus one and one.

  • I believe so.

  • For each neuron in the previous layer, it sends out information to each of the neurons in the next layer.

  • So ultimately will be sending in some training data for example, the asteroid position and the ship's current angle or something similar.

  • And we'll feed that through with unexpected output, so either turn left or turn right now.

  • If it gets it wrong, will send that information back to the be in port, I guess, and use that to correct these whites so ultimately will be sending in thousands of points of information.

  • Correcting these waits until finally, we have a unique set of whites that can take any of our gain information and give us an accurate and reliable output.

  • So here's the position of an asteroid.

  • What do I do?

  • Turn left.

  • Here's another position of a different asteroid.

  • What do I do?

  • Turn right.

  • Let's just close that down.

  • We'll have to create a new job script file.

  • Just a plain text document called saying Neural Network Euro network.

  • And give that the Js extension.

  • Yes, and open both of those in your favorite editor arm using visual CEO code here.

  • The first thing we should do is set up a flag to let us know that automation is on.

  • So currently we have a whole bunch of flags in here.

  • How about we pull them out and we'll put them in a different section just to make it a little bit Tidier?

  • How about developer flags?

  • Paste them in there?

  • Sorry, just doing some housekeeping.

  • So let's create a new constant called automation automation on.

  • We'll set that to true initially, So when the automation is on, will want to set up a neural network So just after we set up the game parameters here, So after new game here, sit up the neural network.

  • For if automation is on, uh, let's leave ourselves to do here because we can't do it at the moment to dio Neural network.

  • Good.

  • Now, also, when the automation is on, we don't want the player to be able Thio Intro ll the ship, Dewey.

  • So down in our key press functions.

  • So kit key down yet Here we go, kid down and keep up.

  • So if the ship is dead or automation is on return, don't do anything so they can press the keys as much as I like, nothing will happen.

  • Similarly, in the key up method, If ship is dead or automation is on return, we'll let the computer control the ship.

  • Speaking of computers controlling ships, let's head down to the update function.

  • That's where all the objects drawn and moved, and so on.

  • So just the start of the update function to stop me here somewhere, how about we say, use the neural network thio control to rotate the ship and shoot and shoot?

  • So again, we'll just leave ourselves.

  • So if automation is on, we'll leave ourselves to do.

  • He has to dio control ship.

  • Okay, good.

  • Let's just make sure that we haven't broken anything, so I head up the top.

  • First of all, let's try it with the automation flag on.

  • Okay, so I'm moving the arrow keys.

  • I'm pushing the space bar.

  • Nothing's happened.

  • And just to make sure that we haven't broken anything that senator false.

  • Yeah, I can control the ship in that situation.

  • So when it's turned on, when it's true, I've lost control of the ship or soon head over to our new file, a neural network dot Js At the top of that file, let's use a flag used strict and what you strict does.

  • It basically enforces good coding practice things like you must declare variables and so on.

  • We're also gonna focus on E s six JavaScript.

  • Now, years six is like the latest major revision to jab scripts in tax things like class and constructors and so on.

  • So we're gonna use that will try to use that throughout this tutorial.

  • Siri's now neural networks use a lot of math, specifically matrix maths.

  • So how about we create a heading here called matrix functions.

  • A matrix is essentially just a two dimensional array.

  • So it has a number of rows and a number of columns.

  • So I'm just making to stand out a little bit.

  • My tricks functions.

  • There we go.

  • So happened, we represent a matrix.

  • Well, how about we represented as a class the class matrix?

  • Give it a constructor.

  • The constructor will take a number of rows and a number of columns.

  • And how about optionally Will have darter here as well.

  • So if we put an equal sign here, it said default value.

  • So the by default, it will be an empty array.

  • So inside the constructor, let's set the variables now, usually private variables.

  • Private instance.

  • Variables are prefixed with an underscore.

  • So we'll follow that convention.

  • Do the same sort of thing for the columns and the data, calls and data and access those variables.

  • We should put some getters, so just get, uh, Rose.

  • And all that does is return this start, underscore Rose and do a similar thing for each of the other variables.

  • Cole's data, this calls and this data.

  • So they use that when we declare an instance of this matrix.

  • Would go and make Chick Stock rose and that would return this stock rose.

  • So inside the constructor, let's initialize uh, you socialize with zeros initialized with zeros if no data provided.

  • So if data equals now, somebody could pass now, couldn't they or data dot length equal zero.

  • So, in the default case, that's true.

  • If either of those things occur, then we want to populate this stock data.

  • This Datta will equal a new empty array and let the loop through each of the rose.

  • So four let I equals zero.

  • So let's is one of these new ear six words.

  • Best way.

  • It means that Isa only visible within the four loop that Michael's here eyes less than this rise I plus plus lower case I plus plus but want to initialize wth e road.

  • So this starter I will equal and empty all right, and we'll loop through each off the columns That J equals zero Jay is less than this, uh, calls J plus.

  • Plus this data will this starter I j So we'll set each of the elements Thio equal zero Good.

  • If data is past will probably check its integrity.

  • For example, somebody could put three rows three columns but provide data with any two rows.

  • So that's not good, is it?

  • So that's what else here.

  • Elf school, Chick Datta integrity.

  • So if the daughter's length doesn't equal, the row number doesn't equal rows or dot ah, maybe dot a zero dot length.

  • So that's the column.

  • Length isn't it doesn't equal calls.

  • Then let's throw a new era.

  • So throat New Era and the era takes a message.

  • You could just put something like incorrect data dimensions.

  • That should be enough.

  • So let's test this.

  • Let's go back to our main file, Okay?

  • We'll have to import that for I also do that up in the style stripped credit script tags the script and we'll need a type type Feli who'll, UH text Java script and the source will equal dot sorry dot slash.

  • That means the current directory on neural network.

  • Whatever we named your file, a neural network dot Js closed the script tags, so head down to where we will initialize set up on neural network just in here.

  • Let's try to set up some matrices, so let em zero equal.

  • New Matrix rose say to rose three columns and lets out.

  • Put that to make sure that it's working.

  • We could probably use the table command here.

  • Is that outputs table data?

  • Tabula data.

  • So M zero dot data That's the daughter of our Matrix.

  • Let's take a look at that.

  • Just need to open the console.

  • You know, we have a two by three matrix full of zeros.

  • That's good.

  • Let's try toe.

  • Pass some data so we can do that by setting up in a ray here.

  • So an array of a raise I should say so to one.

  • Just having some values here and the next road for three 430 to say, Okay, let's give that a guy there.

  • We go to one minus 1430 to one minus 14 threes here.

  • Now What if we declared the incorrect dimension here, too?

  • So he said that a three by three matrix instead of two by three let's check out area checking Good era, incorrect data dimensions.

  • Put that back, too, also head to our other file to create another function.

  • So let's create a function that randomize is the whites or creates a random set of white sort of between our neurons.

  • There's gonna be some whites between minus one and one, so let's create a function for that.

  • So apply Brandon whites between minus one and one, so we'll call this random rights.

  • So this is inside our class four.

  • Let's I equals zero.

  • I is less than this dot Rose I plus plus for columns for Let J Equals Zero.

  • Jay is less than this.

  • Stock columns Jake Plus Plus this start Datta I J will equal math random.

  • That's another between zero and one times.

  • Two times two minus one.

  • So a random number between zero and two minus one is minus one between modest one and positive one.

  • Good.

  • Let's test it in our other file.

  • So our name zero we can discuss m zero dot random White and hopefully that should work.

  • So currently that's what it looks like.

  • Let's run it.

  • There we go yet that we're screwed if we got some random waits, negatives, positives and everything in between.

  • Great head Thio.

  • Other file on this created on next method Add two matrices ese so we'll make this a static method That just means we don't have to.

  • Instead, she ate the class we could just call it directly.

  • We can go in matrix dot ad.

  • It will require two metre sees m zero in m one.

  • Um, probably want to check them to make sure that they have the same dimension.

  • So we can go.

  • We'll create a new method.

  • A new static method matrix Start check time mentions.

  • It will require m zero Name one.

  • Let's create that now, actually, so check matrices.

  • These have the same dimension.

  • Same dimensions.

  • So static chick dimensions in zero and him one if ems it.

  • If m zero dot rose doesn't equal in one dot rose.

  • And what in zero dot culls doesn't equal em one dot calls.

  • Then let's just throw a new era, throw a new era, The message we could.

  • How about maitresse?

  • Eez?

  • Aah!

  • Off different dimensions.

  • Okay, so we check that now we'll need to create you.

  • Now admit that we'll need to create a new return matrix.

  • So let em equal New matrix.

  • The roads will be just be the same as what's past m zero dot rose m zero dot columns and really to look through each of those air.

  • Let's I equals zero eyes less than m dot rose I plus plus andan of lip full.

  • Let's j equals zero j is less than in dot culls j plus plus what we do here, so m dot data I jay will equal or this copy that m zero m zero dot data i j plus m one dot data i j.

  • So it is adding the each individual cells together and assigning it to the New Matrix and will have to return that matrix return in.

  • Okay, let's test that you now other file.

  • First, we need a couple more matrices to test with.

  • So let's create two more M one, which will be two by three.

  • And just change up some of these numbers here.

  • That will do.

  • And em, too will be a different sized matrix.

  • A two by two.

  • So let's just delete that.

  • Thank you.

  • Good.

  • Uh, let's try it.

  • We don't need to use this random whites anymore.

  • Let's try to ad M one into.

  • It.

  • Shouldn't work because there are being invalid, sir.

  • They're of different dimensions.

  • Matrix dot ad in one.

  • Em, too.

  • Let's try that Aron Maitresse, Caesar of different dimensions.

  • And that's in the online 81 Yep, that's correct.

  • How about we try to ad M zero?

  • Well, then M zero and em one they should add.

  • You probably want to print out the daughter before we add them in zero Donna in one, Datta and we'll print out this starter.

  • There we go.

  • So add that and show the data off the result matrix, I guess.

  • The first matrix to 11430 next metric.

  • Syria one blubber.

  • So adding each of these two plus zero equals zero Sorry to plus zero equals +24 plus two equal six.

  • That's right.

  • One plus one equals +23 plus minus +30 minus one plus minus one minus two and zero plus +00 plus zero equals your good adding works head back to our other file.

  • We also need to be able to subtract matrices ese.

  • So it'll be very similar to adding.

  • So this copy all of that pasted down here.

  • Subtract to make sure sees, subtract almost identical.

  • Except that we just need to change this plus to minus.

  • So it'll be a M zero data minus m one data.

  • So the order is important here.

  • Let's test that right.

  • So instead of adding EMS urine and one How about we subtract?

  • Okay, so two minus zero is 24 minus twos to one minus one is 03 minus minus three is six minus one minus one is zero and zero minus 00 Good.

  • It looks like it's working.

  • Just make sure the area checking is working.

  • Okay, so M to Linus in one should not work.

  • Give your hair a major Caesar of different dimensions.

  • Great.

  • We also need to be able to multiply two matrices together.

  • So in a similar way that we've subjected them that set up, multiply two matrices multiply to make sure sees multiply.

  • If you know anything about mattresses, this is not the DOT product.

  • So it's making no, not the DOT product.

  • That's a separate thing.

  • We'll need to do that, too, but it's a different operation.

  • It's just multiplying each cell by each cell.

  • It's all we have to do is change that minus there, too.

  • At times.

  • Ah, head back and test it.

  • So matrix multiply mole multiply into and in one shouldn't work questions.

  • Make sure Yeah, it doesn't work and m zero and em one should work good two times 004 times 281 times one is 13 times minus three is minus nine minus one times minus one is 10 times heroes.

  • You're a looking good.

  • Next, let's calculate the dot products.

  • Create a function to calculate the dot product off two mattresses, so dot product off two matrices these so static dot m zero and and one now for two matrices to be dot compatible.

  • As such, the first one's columns must be the same as the second one's rose, for example, of four by two matrix and a two by four metrics there dot compatible the inside numbers are the same.

  • So, for example, three by two and two by one, they're compatible.

  • So let's test for that.

  • If I am zero dot Cole's doesn't equal m one dot rose, then we have to throw an error here.

  • So throw throw a new era and their message can be just be matrices are not, uh, dot dot compatible.

  • Hey, Dicky.

  • Now the size of the resulting metrics.

  • So that's created new Matt Matrix.

  • You let em equal new night, Trix.

  • The size of the resulting mattress is equal to the outside numbers.

  • For example, four by two and two by four.

  • The resulting matrix will be a four by 43 by two times, or to buy one would be a three by one.

  • So that will be the M zero dot rose on the number of columns will be the M one dot calls.

  • Right Next, we have to do the similar sort of loops that we've been doing in all of these.

  • So that so for Lead Eichel's Zero Eyes Listen, the m dot rose I plus plus end for Let Jaeckel zero Jay's Listen, the M Cole's J Plus plus.

  • So inside here is where we need to do are tricky stuff.

  • Basically, we need to add.

  • So we need to multiply each sell off the first matrices row by each cell of the second matrices column, and then add up those products opposed to link below.

  • How to do this by hand if you're interested, so we'll need to create a variable called some Let's Sum Equal zero and another full loop for Let K equals zero.

  • Que is less than the inner number, which will be the M zero dot calls que plus plus and then we'll have some plus equal.

  • So continually add this together.

  • Some plus equals the M zero uh, Datta I The Ice Row and then the case column multiplied by the M ones Datta and it will be the case.

  • Roe Remember the inside numbers of what's matching them by the J's.

  • Sorry, the J F column.

  • I think that's right someplace.

  • Eagles at.

  • And then finally, we need to sit the i j so m dot data I, ah j will equal the sum.

  • Thank you, Dokey.

  • And then finally will need to return that matrix.

  • Return em.

  • Let's test it out now.

  • Other file.

  • So matrix dots dot m zero m one m zero is a two by three matrix in ones or two by three as well, so they're not dot compatible.

  • Let's just prove that yet ere a metre Cesaire not dot compatible.

  • So which of these are dot compatible?

  • Well, how about m too?

  • Em two times in one.

  • So two by two times a two by three Is doc compatible?

  • Because, you know, numbers of the same.

  • Let's just update this here.

  • Give that a guy.

  • Right.

  • So the first row entries times the first search times.

  • The 2nd 1st column interests.

  • So one times 00 plus minus one times two is minus two.

  • Yep, the 2nd 1 The one times one is one plus minus, one times minus three is positive.

  • Three's positive.

  • Four in total.

  • Yep, looks like it's working fairly well.

  • X function that we'd like to do is to create some sort of conversion function to convert a regular array to a matrix.

  • Because when we pass in, our import data from our game into the neural network will probably just pass.

  • It is a regular one dimensional array, so convert array, thio one road my chicks so static convertir a.

  • We'll call it convert from array, who, Passing, array and all the Jews return a new Matrix New Matrix with one right.

  • The columns will equal the array dot length, and the data will just be the array itself.

  • Okay, let's try that.

  • First of all, let's create an array, so let array equal, just making array of any length with some random numbers inside.

  • We'll want to print that out to the consul's a console log array, and we also want to print out the converted array for using table matrix thought convert from Array Parcel Array.

  • And we want to see the daughter of that.

  • Okay, we don't see the rest of this stuff, so let's just comment that out.

  • Let's see that again.

  • Incorrect started dimensions.

  • Okay, something's gone wrong here.

  • 25 now Matrix function.

  • Where are we?

  • So at the top of here, Page line number 25.

  • I guess we failed this starter Integrity check.

  • I can see why.

  • Because it's checking the dot a dot length now the length of a single dimension.

  • DeRay is the total length, so it would be four in our case.

  • But we don't want that.

  • We want that to equal the rose, which is one.

  • So to get around that all we have to do is created us two dimensional array in our convertir A to convert from array here.

  • We just need to put square brackets around that.

  • So essentially we're putting an array inside of an array A to D array.

  • Let's give that a that's better.

  • So our regular a 4567 and our converted array to a matrix 4567 So it's a four columned single row array.

  • Great.

  • Next, we'll need to create a function that can apply a function to each sell off the given matrix.

  • So we'll call that map.

  • So just down here, apply a function to each sell off.

  • The given my chase.

  • The reason we need to do that I do this is because in neural networks will be using a function called the sigmoid function, and we'll need to apply it to every cell.

  • So that's creative static method static map, which takes in zero matrix and a function.

  • Let's call that in function, and we'll need to create a new ah magic.

  • So let em equal.

  • New Matrix will be of the same size as the past for m zero dot rose m zero Don't Coles.

  • We'll have to loop over in similar fashion to what we've done up here.

  • So let I equals zero eyes.

  • Listen in zero dot What?

  • When Rose is okay too in Rose, I plus plus J Yeah, and inside here all we need to do is get em.

  • Datta i j m dot i j will equal the function off that data.

  • So the function of that love itself effectively in Datta I J.

  • And then always to do is return that matrix.

  • Hang on.

  • We don't wanna sign the M data because that's empty.

  • We want to assign the zero data.

  • So the one that's been passed so that data, we will apply the function, too.

  • And then we'll assign that result to our empty array.

  • So are empty matrix.

  • Let's give that a guy.

  • We don't want to see that array anymore.

  • So Consul Table Matrix start.

  • Matt, we want a map.

  • Let's just choose one of these array so we'll choose in two.

  • And let's apply a function.

  • We can use what I call an arrow function, so that's just X equals greater than it looks like an arrow pointing right words.

  • And then we can just What do we want to do?

  • The x o x times to so that that means, well, times every cell by two.

  • And also, let's copy this just above that show, the original table.

  • Okay, let's do that again.

  • So the usual type of one modest 130 one times two is 23 times to a six, and so on.

  • That's working well and one more functions X divided by to say that 0.53 develop Butters 1.50 yet minus one, divided by two is minus 0.5.

  • Great!

  • That's our map function handled.

  • And the last function for today is to find the transpose of a matrix.

  • Now the transpose off a metric is simply when we switch the columns with the rose and vice versa.

  • So find the transposed off the given metrics.

  • So a 41 matrix will become a 14 matrix that static trains blows.

  • It'll just take a single matrix says its perimeter and basically will be doing something very similar to the ISS.

  • Just needed modify.

  • If you think so, let Chemical New Matrix.

  • We need to switch the columns for the rose and we need to make sure that we loop over the put the original.

  • The past metrics for EMS here arose in zero columns.

  • Now M darter, I j we want em data J I in data J.

  • I will equal m zero datta i j Let's test that out.

  • I think it's a matrix start transpose let's transpose say matrix won the m one and we'll just put the n one dotted before it.

  • Okay, so the original.

  • The original Matrix 01 minus one and in the column down here is 01 minus 12 minus 302 minus 30 So a 23 matrix has become a 32 matrix.

  • Excellent.

  • Last time we created these matrix functions there like, for example, toe add matrices and multiply them and so on.

  • So today we're focusing entirely on creating the neural network.

  • So festival Let's create the class.

  • Neural networks, a class neural network, create the constructor.

  • Now the constructor needs to take the number of inputs, the number of hidden in the number of outputs, numb inputs, numb, hidden and numb outputs.

  • Let's set the private variables the properties of such.

  • So what is court given the same name?

  • Just prefix it with an underscore.

  • Numb in fort Sequels, numb inputs numb, hidden, numb, hidden equals numb, hidden and numb Outputs equals numb outputs.

  • Now going back to our diagram.

  • Remember, we have all these weights that are in between the input in the hidden layer.

  • Oh, let's call that white zero and we'll call these weights here between the hidden and the output whites one.

  • So this underscore weights zero will equal a new matrix.

  • The number of rows will be the number of inputs.

  • So this is the number.

  • This numb inputs and number of columns will be the number of hidden.

  • So this numb, hidden, similarly whites one will equal a new matrix.

  • Except that it's number of rows will be the number of hidden and the number of columns will be the number of outputs.

  • Okay, for the white zero, what's one will need to set that will need Thio update them during our training process.

  • So let's create getters and said his form.

  • So get whites zero.

  • That's simply return.

  • Uh, this under school weights zero.

  • And we also want to set that So sit, Wait zero Ah, wait.

  • Whites sit.

  • Wait says you're a weight so we can just go.

  • This underscore whites.

  • Zero equals whites, right?

  • And we'll do a similar thing for whites.

  • One waits one.

  • This weight's one white sit waits.

  • One.

  • This weight's one equals whites.

  • Great.

  • So we've done this one here.

  • Next, we need to randomize the initial wait, Celeste, just use that as our next comment.

  • It's a randomized initial waits.

  • We've already created a function in our metrics functions actually where we can call randomize, waits on any on any metrics, await zero dot random waits and do a similar thing for whites.

  • One good.

  • That's that step done.

  • We should probably test this.

  • I go to our other file that html file and let's go down to where we set up here he had to do.

  • A neural network will have to create a variable here, variable, and then we'll call it.

  • And here we can go in n equals new neural network.

  • Now we need to pass the number of imported and outputs here.

  • I think we should set them.

  • Is Constance up where we're sitting at Constance?

  • Let's create a section called a neural network Parameters Constance.

  • None of inputs.

  • Not entirely sure how many imports will need.

  • But let's just say, to begin with constant, numb, hidden, um, again is what's the theory behind what number this should be.

  • The higher usually means the more complicated processing it conducive.

  • Let's just start with five.

  • We can always adjust it later and constant um, outputs.

  • Now this one I am confident about, we just want one bully and flag.

  • Zero turn left.

  • One turn right for one output.

  • So head back down to where we're setting up a neural network.

  • Put in these is there the arguments and numb inputs numb, hidden and numb outputs.

  • So we don't need these major season in more sick.

  • We can get rid of them to test that a neural networks constructor is working.

  • All right, how about we out?

  • Put the weights so in in weights zero dot data.

  • And we'll do a similar thing for whites.

  • One, we don't need this transpose anymore.

  • So open that up in now.

  • Web browser.

  • Okay, so we have one.

  • Uh, first mattress e of whites is to buy 34 yet to buy five.

  • That's right.

  • And output by now Waits one is a five by one with random rights inside.

  • Awesome to head over to our neural network class.

  • Let's go down and see what we need to do next.

  • Feed forward.

  • Now all that means is that we're going to import some information into our network.

  • It's going to feed through it, and some output is going to be created.

  • So let's create a new method called feed forward in our class.

  • It will take an importer Ray, as an input as a president must place these comments in here.

  • So the first step is to convert input array to a matrix that's easy enough.

  • So that's declare a new array.

  • Let inputs equal matrix dot convert convert from array.

  • We made this earlier so we can just put the input array in there and that will convert it to a single road matrix.

  • That sign next one.

  • Find the hidden values and apply the activation function.

  • Okay, to find the hidden values, we can look at this diagram here.

  • You see that each hidden neuron is made up of these weights, one coming from each of the previous imports.

  • So to calculate that it's just the first input multiplied by the first weight, plus the second import times.

  • The second wait times the third input.

  • So it plus the third import times the third way and that can be handled using a matrix dot product.

  • We have a function for that as well, So let's hidden equal matrix dot dot, which takes two matrices, the 1st 1 being the inputs and the 2nd 1 will be the weights which will have already determined it's being randomly determined so white zero.

  • Okay, now the activation function is simply well, with lots of neural networks.

  • The activation function is something called a sigmoid function.

  • Let's take a look at that.

  • Now, a signal it function is simply this one divided by one plus E to the minus t.

  • Now what?

  • That does weaken input, any value into this system.

  • And it will return a value between zero and one, no matter how big that value is.

  • So if you put one trillion into this, we'll get a little output of value.

  • Close toe one.

  • Okay, which suits our needs very well.

  • So let's go ahead and create a function down here just outside our class function.

  • We'll just call it sigmoid seem oId.

  • It'll take some value, will call that X, and it will return one divided by one plus the exponents.

  • Oh, that's e a new American expression.

  • Represent the power of E.

  • Yep.

  • Samantha E x p e to the minus X.

  • That's right.

  • Now, in order to apply that function to each of the cells of the Matrix, we made our function for that.

  • To me.

  • Just go hidden will equal matrix darts map Okay, so it takes the the Matrix, which is hidden, and the function to apply to it.

  • What we can use an arrow function here So ex equals greater than fix our Seymour index So sigmoid x so that should apply the sigmoid function to each of the values with enough matrix good Similarly so for the second part, finally, output values and apply the activation function.

  • It's the same sort of thing so we can disco let outputs equal matrix dot Hidden will be the inputs.

  • So this will be from here from the hidden layer to the output layer so hidden will be the inputs and outputs will be the outputs So Matrixx stopped hidden this stop So this stock whites one that's the second set of weights outputs, metrics map so itself outputs and apply the sigmoid function to each value.

  • And finally we want to return returned those outputs to the sender the next step applied bias.

  • I don't want to do that right now and the reason is because not applying bias can cause issues and I'd like to see what those issues are.

  • This first test, this feed forward function.

  • So let's do some console output console log inputs, and we'll just put the data in the array so we can use table to that console that table inputs dot data and we'll do something similar each step of the way here.

  • So when we work out our hidden neurons, we can just do that.

  • And we'll also want to see what happens after we apply the sigmoid function similarly, for our outputs and after the outputs have had the sigmoid function applied to them.

  • So just call that output sig that will be hidden Sig que head over to our other file political that function.

  • So all we have to do is go in in dot feed forward.

  • It requires an input array.

  • So something like zero comma one remembering that the number of imports that we declared for a neural network is too.

  • So we need to pass to allow an array of length to hear.

  • Now, If we didn't, let's just try that.

  • If we didn't, it should break.

  • Justo, open a file.

  • Open up the console.

  • Yes, sir.

  • Input Scott through a single length away.

  • But there's Nero.

  • Mattresses are not dot compatible.

  • So to fix that, all we have to do is put the second element put the second ah, importing their Let's give that a go Yet that works fine.

  • So we got to imports of your own one.

  • The hidden values calculated.

  • I'll just believe that the sigmoid is applied the AP foot and the signal it is applied.

  • Let's just keep resetting that until we get an output that is negative.

  • Here we go.

  • Here's a negative output.

  • Can you see that the sigmoid function has made that positive?

  • So that seems to be working okay.

  • To head over to our other file, we can probably get rid of some of these console logs, so let's just delayed a few of them.

  • We'll get one set.

  • Will probably need it for our next function.

  • And that is train, train using input and target arrays.

  • Right?

  • So currently our system doesn't do very much.

  • We give it some inputs and outputs some randomly generated outputs, not very useful.

  • So in order to make it do something, how about we creative function that allows us to put in some training data?

  • So we'll call that train so up inside our class train, it will take an input.

  • All right and the target array.

  • So, for example, if we had the input zero in one and we expect one to be the answer, then that's what we would put in here.

  • Let's paste those comments in there The first step.

  • Feed the input data through the network.

  • Well, we already have a function for that called feed forward so that, let's say, let outputs equal feed forward this stock.

  • Feed forward the input array.

  • Okay, that's that done.

  • Next, calculate the output errors target minus output I can order to subtract.

  • Matrices will have to convert the target array first so that let targets equal matrix dot convert.

  • Convert from aware from a rain target array can good and let's output errors.

  • So calculating the output errors here that output eras equal matrix dot Subtract and we'll subtract the first, which is the target that targets minus the second, which is the output e outputs this test, that's all.

  • Grab this console data that we kept up here, uh, put targets and output errors, so outputs that'll be fine targets, targets and output areas, output areas.

  • Let's print out every step que head back over to our other file in end up train, we need an input array 01 and an output arraign remembering it has to match the number of outputs.

  • If it doesn't, we're gonna get an error.

  • We'll just give that a go.

  • We'll put two column one there, I guess, Aaron matrices that are of different dimensions.

  • So when it tried to subtract them, it failed.

  • So let's put that to a single one, right?

  • So we have our outputs, which is 0.639 the the expected.

  • The target is one, so the difference is 10.36 That sounds correct.

  • So, on to our next step, calculate the deltas, which are equal to the area's times, the derivative off the output.

  • So the derivative of the output is actually the derivative off the sigmoid function.

  • So that's update our sigmoid function to handle derivatives.

  • How about we just pass a flag?

  • The riv equals false will set.

  • We'll give it a default values that you don't have to provide it.

  • And if you don't, it'll just return what we always have.

  • If derivative, then we'll return the derivative.

  • Now the derivative of this is X Times one minus X.

  • That's not the true derivative off the sigmoid function.

  • But we've already applied the sigmoid function, so we don't want to return the truth.

  • So open a note here where X equals sigmoid X.

  • Right.

  • So the truth is the sigmoid X times one minus a signal index.

  • But we've already applied it, so this will be a good representation.

  • X times one minus x Let's create a variable.

  • Let output derives equal.

  • We can use our map function to apply this matrix stop map.

  • The outputs and thief function will be we can use narrow function again will be the sigmoid X But this time we'll set the drift flag to true.

  • It'll apply the derivative to each of these outputs and assign it to that.

  • And finally, output errors.

  • No Kappa Delta output deltas is what we're trying to find here.

  • Output deltas will equal matrix dot So areas times derivatives and multiply.

  • We can use our multiply function.

  • The 1st 1 is the errors output errors and the 2nd 1 will be the output derivatives output derivatives And what is Copy this console long here so we can test this output.

  • Just the deltas will do output deltas.

  • It's just that.

  • Just run it again.

  • So without puts targets up what areas and put deltas, who am I to refute that it looks okay?

  • Account to the next step.

  • Calculate hidden layer errors.

  • So it's the deltas dot the transpose of whites one.

  • Right.

  • So let's calculate the transpose first.

  • So call that waits.

  • One capital T for transpose will equal.

  • We have a function for this metric start transpose it just takes a single matrix.

  • So that'll be the this dot waits one.

  • Okay.

  • And then we'll need to Catholic The hidden lie eras hidden there is hidden errors will equal delta dot matrix dots.

  • Uh, what was the 1st 1?

  • The deltas dot the transpose.

  • Okay, so that will be the output.

  • Delta's output deltas dot What?

  • We just grow it out.

  • The transposed their weight's one t again.

  • Let's outputs, um, just to make sure it's not throwing error.

  • Basically, we don't really know exactly what we're after here.

  • Uh, the Heaton Harris is what well, wantto put here in areas.

  • Let's give it a gay right.

  • So we get some output deltas, and then we get the hidden errors across ways that looks to be Okay, so the next step calculate the hidden Delta's errors times to relieve off hidden sounds very similar to what we've done up here.

  • So that's just copy and paste that that hidden Juries equal metrics met hidden.

  • I just had a thought.

  • We don't have actually have visibility to hear them here hidden because hidden wasn't was used within this feed forward function.

  • Here we go.

  • Let hidden equals that.

  • So how about instead of creating a local variable here, we use a property on our neural network Will say this dot hidden this stock hidden so anywhere we use hidden inside, he will go this stock hidden.

  • It's stuck hidden.

  • You start hidden and we have to go create a property on the neural network so we could do that easily enough to set the top of it.

  • This start underscore hidden what is equal and empty array.

  • And we have to set up some getters and sedatives for it so that we can access it, get hidden well, has returned.

  • This underscore hidden and sets hidden.

  • Just pasta.

  • Typical hidden.

  • This hidden will equal hidden.

  • That should access.

  • Yep, that's the same.

  • Is that right?

  • So that shit'll working out feed forward now and back down where we have back in the train function where we have hidden he has a hindrance will remain chicks map this start hidden So we're accessing the actual human values at this state.

  • At this point in time, X sigmoid X is true yet we're applying the derivative of it capital in Delta.

  • So the hidden deltas matrix multiply the hidden eras times the hidden derivatives.

  • Right, So that's out.

  • Put those hidden deltas give it a guy.

  • We have some hidden areas with some hidden deltas.

  • Good.

  • Now, for the final step in our little neural network, update the weights with the ad the transpose of liars dot deltas through.

  • Add that to the whites, I guess.

  • Let's first calculate the trains pose so let hidden transposed equal matrix dot trains pose Uh, we start hidden.

  • Sorry.

  • Um next we have to add the transposed to the wait So this waits.

  • The Associated Waits is one.

  • It's the second set of weights Will equal matrix ad ad itself with add to itself.

  • So this white one the lay the transfers layers dot Delta's matrix Don't the transpose he didn t hidden T and the deltas, which will be the output deltas of the social, are associated with the hidden layer output deltas.

  • Now I have to do a similar thing for the first white so white zero.

  • So we need to Catholic transpose of the imports inputs t What metrics transfers in?

  • We have inputs.

  • Do we have visibility on inputs?

  • No, we don't cause again within the feed forward where converting it there and keeping it as a local variable.

  • So there's a couple things we could do.

  • We could just converted again within our trainer Ray But we have to remember that this train functional probably called like thousands of times, if not hundreds of thousands of times.

  • So it's probably better just to assign it to what?

  • How we're doing with the hidden here.

  • So let's do that.

  • This inputs this inputs and let the garden created property same as what we've done for hidden.

  • So this inputs will just equal on empty array and we have to create a couple of with Craig together on a set up get inputs This return this inputs set inputs, input, Sequels, inputs.

  • This inputs were legal inputs that's looking good.

  • There are feed.

  • Forward function should work.

  • Now we're just assign instead of creating a local variable, which is a sign it as a property now down in our train function.

  • This is inputs should work.

  • Now that's fine.

  • And we're updating weights.

  • Zero At this point, wait zero weights, zero dot the inputs, teeth inputs T and the deltas that are associated with out of the hidden Delta's the next layer deltas to run that make sure it's not broken.

  • Okay, it's not breaking, so that's good enough test for me.

  • I think we should actually try this training and see if it actually works or not.

  • How about we get rid of all these comments first?

  • Because they're just not.

  • We don't want them printing out every it's oration when we've got, like, 1000 or £10.

  • Alterations.

  • Mazel land, Rezulin.

  • This weight's updating.

  • Great.

  • Go back to our other file, right, so we'll be calling train.

  • But we want to loot over the number of training samples that we have.

  • Something go for all.

  • Title is trained, the network full.

  • Let I equals zero eyes less than, say, Let's set up a flag for this a numb samples.

  • I Plus plus in there will do our training for something.

  • Ah, number of samples.

  • Let's go up and create that now.

  • Numb samples.

  • Not exactly sure how many will need, but let's start at, say, 1000.

  • I know in other examples is 110,000 and so on.

  • What?

  • We'll start with 1000 some samples.

  • 1000.

  • And how about we use the classic example to test neural networks?

  • Is the Zohar, sir the X or gate?

  • The exclusive all gate, so X.

  • Call it It's a test test X or gate logic.

  • Let's take a look at that excellent gate Here it is just this table down here.

  • So when you have to imports 00 output 001 or 10 out.

  • Put one and 11 Output 0000 Okay, let's write that in here.

  • I think 00 equals zero.

  • 01 equals one.

  • 10 equals one and one one equal zero.

  • So only when there's one of them flag will it be one.

  • Otherwise, when they both the same will be zero.

  • So let's create some random input will create 101 There and 101 There's all input.

  • One input to or input zero and input one input zero will equal well, it'll be a random number, and we want around that.

  • So math round his rounds to the nearest integer, and we want around a random number.

  • Math random, which is a number between 01 So that should produce a zero or one Leif Input one so we can pass them into L ah training array here in Port Huron.

  • Input one.

  • And we also need an output.

  • So let output but output equal.

  • What's the logic behind it?

  • If input one.

  • It puts your own and put one of the same thing.

  • The output is zero right.

  • Otherwise the output is one.

  • So I'm just to a conditional here.

  • Input zero equals input one. 01:03:51.430 --> 01:03:54.

Heil and welcome to this new tutorial, Siri's, to create a neural network in JavaScript, which we will hopefully use to automate the game Asteroids.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

使用神經網絡打敗小行星遊戲 - JavaScript教程 (Beat Asteroids Game Using a Neural Network - JavaScript Tutorial)

  • 4 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字