Try TensorFlow.js in your browser (Coding TensorFlow)

TensorFlow · Beginner ·📐 ML Fundamentals ·7y ago

Key Takeaways

The video demonstrates how to use TensorFlow.js to build and train a simple machine learning model in the browser, using JavaScript and the TensorFlow.js library. The model is trained to predict a linear relationship between input data.

Full Transcript

[Music] [Applause] hi and welcome to coding tensorflow a show where we focus on coding machine learning and AI I'm Laurence Moroney a developer advocate for tensorflow and in this episode we're going to look at using JavaScript for machine learning in the browser this is achieved using tensorflow j/s a javascript library for training and deploying machine learn models in the browser and on no js' there's lots of great information about it on the jsr tensorflow dot org site including samples API Docs 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 in the next episodes we'll look at setting up a node environment and running more complex examples are you ready to go ok let's do this let's first create the simplest web page imaginable it's empty but for 1 div and we'll even leave that empty too the next thing of course is to add the tensorflow j/s libraries and these can be inserted using a script tag and I do that here always be sure to use the latest version which you can find at this URL 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 but powerful example of how it can work the goal of machine learning is to train a model from input data this model can be then used to in fare 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 Y value is when X is 5 I can infer that by looking at the line in 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 an asynchronous function called learn linear it's asynchronous because the learning will take some time so it's good to get into the habit of waiting for the learning to finished now I'm going to add a model I'm using a TF 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 in 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 node 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 loss function and the optimizer I'm setting the loss 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 to SGD which stands for a stochastic gradient descent this simply defines a methodology for the learning there are a bunch of them supported including SGD and the popular atom you can learn more about these in the training optimizer section of the API 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 and our 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 two tensors for the training values one for the X's and one for the Y's let's take a look at this in code I'll create a tensor for the X's by using TF tensor 2d you'll see that the first element in this is my array of X values minus 1 0 1 etc the second parameter is the shape of this array 6 rows and 1 column I'll then do the same for my y's giving my Y values and the same shape six rows on one column now all I have to do is Rane my model now this can 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 epochs you call the fit method here you can see I'm telling it my input values the X's my output values the Y's and then asking it to train for 250 iterations 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 y equals 2x minus 1 thus the value for X is 5 well then Y should be 9 you use the model to predict method to get a prediction so let's see that in action I'll refer to the div called output field that I created earlier and I'll load the results of the prediction into its inner text to do this I call model duck predict and I pass my input tensor 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 for more epochs giving the network more time to error correct so let's make it say 500 epochs and now when i refresh we'll see my value is 38.9 and another refresh keeps it there so let's see what it would predict for x equals 10 it gives us 18 point 9 7 where of course the correct value is 19 a refresh and retrain the network and now I get 18 point 9 8 we're pretty close and that's it you've just created a neural network using javascript in the browser and trained it to predict a linear relationship this was made possible by tensorflow Jas in the next episode of this series you'll learn a little about data science and 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 das on tensorflow org and don't forget to hit that subscribe button for more great videos on this channel thank you [Music] [Applause] [Music]

Original Description

Machine Learning in the Browser? You betcha! In this video, Laurence Moroney (@lmoroney) shows you just how easy it is to get up and running with creating a machine learning model in JavaScript that executes right in the browser. It's the first part of a three-part series that gets you deeper into both training and executing machine learned models with TensorFlow.js, so be sure to hit that subscribe button for more! Learn more at TensorFlow.js → http://bit.ly/2HN1wj5 Coding TensorFlow playlist → http://bit.ly/2zoZfvt Subscribe to the TensorFlow channel → http://bit.ly/TensorFlow1
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from TensorFlow · TensorFlow · 54 of 60

1 The TensorFlow YouTube Channel is Here!
The TensorFlow YouTube Channel is Here!
TensorFlow
2 Answering Your TF Questions #AskTensorFlow
Answering Your TF Questions #AskTensorFlow
TensorFlow
3 Chatting With the TensorFlow Community (TensorFlow Meets)
Chatting With the TensorFlow Community (TensorFlow Meets)
TensorFlow
4 All About TensorFlow Code (Coding TensorFlow)
All About TensorFlow Code (Coding TensorFlow)
TensorFlow
5 TensorFlow: an ML platform for solving impactful and challenging problems
TensorFlow: an ML platform for solving impactful and challenging problems
TensorFlow
6 Keynote (TensorFlow Dev Summit 2018)
Keynote (TensorFlow Dev Summit 2018)
TensorFlow
7 tf.data: Fast, flexible, and easy-to-use input pipelines (TensorFlow Dev Summit 2018)
tf.data: Fast, flexible, and easy-to-use input pipelines (TensorFlow Dev Summit 2018)
TensorFlow
8 Eager Execution (TensorFlow Dev Summit 2018)
Eager Execution (TensorFlow Dev Summit 2018)
TensorFlow
9 Machine Learning in JavaScript (TensorFlow Dev Summit 2018)
Machine Learning in JavaScript (TensorFlow Dev Summit 2018)
TensorFlow
10 Training Performance: A user’s guide to converge faster (TensorFlow Dev Summit 2018)
Training Performance: A user’s guide to converge faster (TensorFlow Dev Summit 2018)
TensorFlow
11 The Practitioner's Guide with TF High Level APIs (TensorFlow Dev Summit 2018)
The Practitioner's Guide with TF High Level APIs (TensorFlow Dev Summit 2018)
TensorFlow
12 Distributed TensorFlow (TensorFlow Dev Summit 2018)
Distributed TensorFlow (TensorFlow Dev Summit 2018)
TensorFlow
13 Debugging TensorFlow with TensorBoard plugins (TensorFlow Dev Summit 2018)
Debugging TensorFlow with TensorBoard plugins (TensorFlow Dev Summit 2018)
TensorFlow
14 TensorFlow Lite (TensorFlow Dev Summit 2018)
TensorFlow Lite (TensorFlow Dev Summit 2018)
TensorFlow
15 Searching Over Ideas (TensorFlow Dev Summit 2018)
Searching Over Ideas (TensorFlow Dev Summit 2018)
TensorFlow
16 Reconstructing Fusion Plasmas (TensorFlow Dev Summit 2018)
Reconstructing Fusion Plasmas (TensorFlow Dev Summit 2018)
TensorFlow
17 Nucleus: TensorFlow toolkit for Genomics (TensorFlow Dev Summit 2018)
Nucleus: TensorFlow toolkit for Genomics (TensorFlow Dev Summit 2018)
TensorFlow
18 Open Source Collaboration (TensorFlow Dev Summit 2018)
Open Source Collaboration (TensorFlow Dev Summit 2018)
TensorFlow
19 Swift for TensorFlow - TFiwS (TensorFlow Dev Summit 2018)
Swift for TensorFlow - TFiwS (TensorFlow Dev Summit 2018)
TensorFlow
20 TensorFlow Hub (TensorFlow Dev Summit 2018)
TensorFlow Hub (TensorFlow Dev Summit 2018)
TensorFlow
21 Applied AI at The Coca-Cola Company (TensorFlow Dev Summit 2018)
Applied AI at The Coca-Cola Company (TensorFlow Dev Summit 2018)
TensorFlow
22 Real-World Robot Learning (TensorFlow Dev Summit 2018)
Real-World Robot Learning (TensorFlow Dev Summit 2018)
TensorFlow
23 TensorFlow Extended (TFX) (TensorFlow Dev Summit 2018)
TensorFlow Extended (TFX) (TensorFlow Dev Summit 2018)
TensorFlow
24 Project Magenta (TensorFlow Dev Summit 2018)
Project Magenta (TensorFlow Dev Summit 2018)
TensorFlow
25 TensorFlow Dev Summit 2018 - Livestream
TensorFlow Dev Summit 2018 - Livestream
TensorFlow
26 Introducing TensorFlow Lite (Coding TensorFlow)
Introducing TensorFlow Lite (Coding TensorFlow)
TensorFlow
27 TensorFlow Dev Summit 2018 Highlights
TensorFlow Dev Summit 2018 Highlights
TensorFlow
28 Jeff Dean, Head of AI at Google discusses the impact of ML (TensorFlow Meets)
Jeff Dean, Head of AI at Google discusses the impact of ML (TensorFlow Meets)
TensorFlow
29 TensorFlow Mobile vs. TF Lite and More! #AskTensorFlow
TensorFlow Mobile vs. TF Lite and More! #AskTensorFlow
TensorFlow
30 Using TensorFlow to enable research & production across many fields (TensorFlow Meets)
Using TensorFlow to enable research & production across many fields (TensorFlow Meets)
TensorFlow
31 Teaching TensorFlow for Deep Learning at Stanford University (TensorFlow Meets)
Teaching TensorFlow for Deep Learning at Stanford University (TensorFlow Meets)
TensorFlow
32 TensorFlow Lite for Android (Coding TensorFlow)
TensorFlow Lite for Android (Coding TensorFlow)
TensorFlow
33 Using the tf.data API to build input pipelines (TensorFlow Meets)
Using the tf.data API to build input pipelines (TensorFlow Meets)
TensorFlow
34 Training Models in the Cloud & the Benefits of AI Toolkits #AskTensorFlow
Training Models in the Cloud & the Benefits of AI Toolkits #AskTensorFlow
TensorFlow
35 Execute operations immediately with TensorFlow's Eager Execution (TensorFlow Meets)
Execute operations immediately with TensorFlow's Eager Execution (TensorFlow Meets)
TensorFlow
36 TensorFlow Lite for iOS (Coding TensorFlow)
TensorFlow Lite for iOS (Coding TensorFlow)
TensorFlow
37 Get started with TensorFlow's High-Level APIs (Google I/O '18)
Get started with TensorFlow's High-Level APIs (Google I/O '18)
TensorFlow
38 TensorFlow for JavaScript (Google I/O '18)
TensorFlow for JavaScript (Google I/O '18)
TensorFlow
39 TensorFlow in production: TF Extended, TF Hub, and TF Serving (Google I/O '18)
TensorFlow in production: TF Extended, TF Hub, and TF Serving (Google I/O '18)
TensorFlow
40 Get started with TensorFlow's High-Level APIs in 5 mins |  Google I/O 2018
Get started with TensorFlow's High-Level APIs in 5 mins | Google I/O 2018
TensorFlow
41 TensorFlow and deep reinforcement learning, without a PhD (Google I/O '18)
TensorFlow and deep reinforcement learning, without a PhD (Google I/O '18)
TensorFlow
42 TensorFlow Lite for mobile developers (Google I/O '18)
TensorFlow Lite for mobile developers (Google I/O '18)
TensorFlow
43 Advances in machine learning and TensorFlow (Google I/O '18)
Advances in machine learning and TensorFlow (Google I/O '18)
TensorFlow
44 Distributed TensorFlow training (Google I/O '18)
Distributed TensorFlow training (Google I/O '18)
TensorFlow
45 Classification using neural networks & ML regression models #AskTensorFlow
Classification using neural networks & ML regression models #AskTensorFlow
TensorFlow
46 TensorFlow and Keras in R - Josh Gordon meets with J.J. Allaire (TensorFlow Meets)
TensorFlow and Keras in R - Josh Gordon meets with J.J. Allaire (TensorFlow Meets)
TensorFlow
47 Focus on your experiment with TensorFlow Estimators (TensorFlow Meets)
Focus on your experiment with TensorFlow Estimators (TensorFlow Meets)
TensorFlow
48 How to get started with AI/ML, retraining models, & more! #AskTensorFlow
How to get started with AI/ML, retraining models, & more! #AskTensorFlow
TensorFlow
49 TensorFlow - the deep learning solution for mobile platforms (TensorFlow Meets)
TensorFlow - the deep learning solution for mobile platforms (TensorFlow Meets)
TensorFlow
50 MiniGo: TensorFlow Meets Andrew Jackson (TensorFlow Meets)
MiniGo: TensorFlow Meets Andrew Jackson (TensorFlow Meets)
TensorFlow
51 The growth of TensorFlow with added support for JS & Swift (TensorFlow Meets)
The growth of TensorFlow with added support for JS & Swift (TensorFlow Meets)
TensorFlow
52 At the intersection of TensorFlow & nuclear physics (TensorFlow Meets)
At the intersection of TensorFlow & nuclear physics (TensorFlow Meets)
TensorFlow
53 NVidia TensorRT: high-performance deep learning inference accelerator (TensorFlow Meets)
NVidia TensorRT: high-performance deep learning inference accelerator (TensorFlow Meets)
TensorFlow
Try TensorFlow.js in your browser (Coding TensorFlow)
Try TensorFlow.js in your browser (Coding TensorFlow)
TensorFlow
55 TensorFlow Hub: reusing machine learning modules (TensorFlow Meets)
TensorFlow Hub: reusing machine learning modules (TensorFlow Meets)
TensorFlow
56 How to use TensorFlow in PyCharm (TensorFlow Tip of the Week)
How to use TensorFlow in PyCharm (TensorFlow Tip of the Week)
TensorFlow
57 Training models faster with TensorFlow Hub (TensorFlow Meets)
Training models faster with TensorFlow Hub (TensorFlow Meets)
TensorFlow
58 Prepare your dataset for machine learning (Coding TensorFlow)
Prepare your dataset for machine learning (Coding TensorFlow)
TensorFlow
59 Using ML to predict insulin use for Type 1 Diabetes (TensorFlow Meets)
Using ML to predict insulin use for Type 1 Diabetes (TensorFlow Meets)
TensorFlow
60 TFX: an end-to-end machine learning platform for TensorFlow (TensorFlow Meets)
TFX: an end-to-end machine learning platform for TensorFlow (TensorFlow Meets)
TensorFlow

This video teaches how to use TensorFlow.js to build and train a simple machine learning model in the browser, using JavaScript. The model is trained to predict a linear relationship between input data. The video covers the basics of machine learning, neural networks, and JavaScript.

Key Takeaways
  1. Create a simple web page
  2. Add the TensorFlow.js library
  3. Define a model using TF.sequential
  4. Compile the model
  5. Define input and output data
  6. Train the model using the fit method
  7. Make predictions using the predict method
💡 TensorFlow.js allows you to build and train machine learning models directly in the browser, using JavaScript.

Related AI Lessons

Mastering TypeScript — Understanding the TypeScript Compiler (tsc) from Scratch — Lesson 2
Learn the basics of the TypeScript compiler to write better JavaScript code
Medium · JavaScript
Stop Overfitting With Basically One Line of Code
Learn to prevent overfitting with a simple code tweak and understand the difference between Ridge and Lasso regression
Medium · AI
Stop Overfitting With Basically One Line of Code
Learn to prevent overfitting in machine learning models with a simple code tweak and understand the difference between Ridge and Lasso regression
Medium · Machine Learning
Stop Overfitting With Basically One Line of Code
Prevent overfitting in models with a simple code tweak, understanding the difference between Ridge and Lasso regression
Medium · Data Science
Up next
Learn Deep Learning by Hand (Beginner's Guide - Part 1)
Thu Vu
Watch →