Placeholder Image

Subtitles section Play video

  • hi and welcome to coding tensorflow, a show where we focus on coding machine learning an A I I'm Laurence Maroney at developer advocate for tensorflow.

  • On In this episode, we're going to look at using JavaScript for machine learning in the browser.

  • This is achieved using tensorflow dot Js a Java script library for training and deploying machine learn models in the browser andan node.js.

  • There's lots of great information about it on the J s stock tensorflow dot org's site, including samples, a P I docks and frequently asked questions.

  • In this first episode, I'll show you how easy it is to build and train a very simple model that executes entirely in the browser.

  • And then the next episodes will look at setting up a note environment and running more complex.

  • Examples.

  • Are you ready to go?

  • Okay, let's do this.

  • Let's first create the simplest Web page imaginable.

  • It's empty, but for one Dev on, we'll even leave that empty, too.

  • The next thing, of course, is to add the Tensorflow Js libraries, and these could be inserted using a script tag on.

  • I do that here always be sure to use the latest version, which you can find it.

  • This you, Earl.

  • Be sure to put the script loader in your head tag as shown here.

  • Great.

  • You now have your page set up for tensorflow.

  • So let's now show a simple, powerful example off how it can work.

  • The goal of machine learning is to train a model from input data.

  • This model can be then used to unfair or predict output data for future input values.

  • So, for example, take a look at this data.

  • Now.

  • It's pretty obvious to the human eye that there's a linear relationship in this data.

  • These dots can be joined by a straight line.

  • Thus, even though I don't know what the why value is when X is five, I could unfair that by looking at the line and machine learning.

  • We do this by training a model on the input data, so let's take a look at the code for this.

  • First of all, I'm going to create a new script block, and within that, I'll create in a synchronous function called Learn Linear.

  • It's a synchronous because the learning will take some time, so it's good to get into the habit of waiting for the learning to finish.

  • Now I'm going to add a model.

  • I'm using a T f dot sequential, where the outputs of one layer are the inputs to the next.

  • It's a simple stack of layers with no branching or any kind of skipping.

  • I will then add a dense layer to this, and dense means.

  • All of the nodes and each of the layers are connected to each other.

  • In this case, it's a little redundant, as I only have one layer and one note.

  • But it's the easiest way to define a simple neural network like this.

  • Now that my model is defined, it's time to compile it.

  • To do this, I have to specify some parameters, including the last function and the optimizer.

  • I'm setting the lost function to be a mean, squared error.

  • It's a pretty standard one, particularly for linear equations, and the optimizer is going to be said toe S G.

  • D, which stands for a stochastic Grady int descent.

  • This simply defines a methodology for the learning.

  • There are a bunch of them supported, including STD on the popular Adam.

  • You can learn more about these in the Training Optimizers section of the A p I, and we'll put a link below for the next step.

  • I'll define my X and Y values for the line.

  • Remember that graph we showed earlier?

  • Let's take a look at the points on that.

  • You can see that I've labeled them here with their X and Y coordinates.

  • From a machine learning perspective, we can consider the X values to be our inputs on are y values to be our desired outputs.

  • Thus, in the future, if we feed in a new X value would get a Y value back.

  • So to train a model to do this, we can create too tense sirs for the training values.

  • One for the exes on one for the wise.

  • Let's take a look at this in code.

  • I'll create a tensor for the exes by using T F dot tents or two D.

  • You'll see that the first element in this is my array of X values, minus 101 etcetera.

  • The second parameter is this shape of this array.

  • Six rows in one column.

  • How then do the same for my wise Giving my Y values on the same shape Six rows in one column Now all I have to do is train my model now.

  • This could take some time for complex models, but because this is really simple, it's going to be really quick either way, because it takes an indeterminate time, you will await its execution, which is why you made this function and a synchronous one to begin with.

  • So let's take a look at the code to train a model for a fixed number of iterations known as a pox You call the fits method.

  • Here you can see I'm telling at my input values the exes.

  • My output values the wise on, then asking it to train for 250 it orations.

  • Once the model is trained, I can try to do a prediction from it.

  • So if you're good at math, you'll see that the relationship here between X and Y is why equals two X minus one.

  • Those the value for X is five.

  • Where then why should be nine?

  • You use the modeled up predict method to get a prediction, so let's see that in action.

  • I'll refer to the Dave called Output Field that I created earlier on our load.

  • The results of the prediction into its inner text.

  • To do this, I called modeled up predict on I passed my input tenser, which is a single value in a one by one array.

  • Tensorflow will then give me back the value and you'll see that it predicted 38.5, which is pretty close to the correct value of 39.

  • If I refresh the value changes to 38.3 because I've retrained the neural network, I can impact the accuracy by training form or epochs, giving the network Maur timeto error correct.

  • So let's make it say, 500 a pox.

  • And now, when I refresh, we'll see.

  • My value is 38.9 on another.

  • Refresh keeps it there.

  • So let's see what it would protect for X equals 10.

  • It gives us 18.97 where, of course, the correct.

  • When he was 19 refresh and retrain the network, and now I get 18.98 were pretty close, and that's it.

  • You've just created a neural network using Java script in the browser and trained it to predict a linear relationship.

  • This was made possible by tensorflow dot Js in the next episode of this series, you'll learn a little about data science.

  • This is the process of preparing data for machine learning, and you'll do it by building a simple classifier for tensorflow, which runs completely in the browser.

  • You can learn more about tensorflow dot Js on tensorflow dot or GE.

  • And don't forget to hit that subscribe button for more great videos on this channel.

  • Thank you.

hi and welcome to coding tensorflow, a show where we focus on coding machine learning an A I I'm Laurence Maroney at developer advocate for tensorflow.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it