This document provides an introduction to diving into TensorFlow.js, including:
Part 1 discusses what artificial intelligence, machine learning, and deep learning are and how TensorFlow.js fits into machine learning for linear regression.
Part 2 demonstrates how to perform linear regression in JavaScript, including finding the weights using the gradient descent algorithm.
Part 3 introduces TensorFlow.js, including how to work with tensors, perform operations like addition and multiplication, and import TensorFlow.js in both browsers and Node.js.
2. Prerequisites
No knowledge of Machine Learning expected
Some experience with JavaScript would be helpful.
- Adding dependencies in packages.json
- Map, Reduce, Filter operators
- Working with modules
3. After this presentation we should be able to answer
these questions
Part 1 - What is AI, how does Tensorflow.js fit in it?
Part 2 - How do I do Linear Regression in JavaScript?
Part 3 - Intro to Tensorflow.js (Tensors, Operations,
Simple Model Creation)
Part 4 - Multivariate Linear Regression
Part 5 - Transfer Learning
4. Part 1 - What is Machine Learning,
how does Tensorflow.js fit in it?
7. Artificial Intelligence
Machine Learning
Create a model through statistical
analysis
Y = W * X
Find the W so we can predict Y
X Y
1 bedroom
3 years old
100 feet from subway
$700/month
2 bedroom
4 years old
50 feet from subway
$1000/month
4 bedroom
10 years old
500 feet from subway
$1000/month
Y = W * X
What is the best W?
8. [ 1,3,100 ]
[
[W1],
[W2],
[W3]
]
X = [$700]
Matrices
Is there a scientific way to figure out the matrix W1, W2, W3?
Methods that we can use to mathematically prove that one set of numbers if
better than others?
10. Artificial Intelligence
Machine Learning
Deep
Learning
Y = W * X
Where W is a neural net
Where does Tensorflow.js fit
in here? What does it do?
1. A library offering functions and APIs for
matrix manipulation
[ 1,3,100 ] * [[W1],[W2],[W3]]
2. Tools for training models with/without
neural nets
(finding the “W” a.k.a. “training”)
3. Tools for assessing the models
(how good is the W)
4. Tools to run and extend existing models,
even ones that have been trained in
Python or other languages.
(reuse a W)
Y = W * X
Find the W so
we can
predict Y
Computers mimic human
intelligence.
11. Why Tensorflow.js? - Advantages over other
libraries1. Use the browser or node.js to train/evaluate/run/reuse models! JavaScript!
2. Fast - WebGL acceleration out of the box
a. TensorFlow.js automatically supports WebGL, and will accelerate your code behind the
scenes when a GPU is available
3. Model and data run on the client
a. Lower latency
b. Better privacy
4. TensorFlow.js APIs offer consistency and flexibility
a. The Core API or Low-level API for linear algebra.
b. High-level layers API for defining models and neural nets.
c. TensorFlow.js supports importing TensorFlow predefined and Keras models.
d. “The Layers API of TensorFlow.js is modeled after Keras and we strive to make the Layers API as similar to
Keras as reasonable given the differences between JavaScript and Python. This makes it easier for users with
experience developing Keras models in Python to migrate to TensorFlow.js Layers in JavaScript.”
12. Part 2 - Intro to Linear Regression -
One of the simplest models
13. Linear Regression
“ Let us say that you work for a mobile app gaming company.
You are going to release an awesome new game named
“Cranes”.
The company has asked for you to make an estimation on
how much profit will they make if a player spends X minutes
of playing the game. “
14. Fixed Price Prediction
Q: How much revenue when user plays for 5
minutes?
A: The price is fixed at $5 so
Y = 5
15. Prediction with Ads
Q: How much revenue when user plays for 5
minutes?
A: Hmm, the ad network looks like it is
generating revenue linearly. There should be
a trendline that I can find over here...
Y = W * X
16. How to find the weights of a line (in general)
Y = W1 * X + W2
X Y
0 2
1 4
2 6
3 8
W2
W1 = Y / X
With the following data:
For X = 0, Y = 2
2 = W1 * 0 + W2
2 = W2
For X = 1, Y = 4
4 = W1 * 1 + 2
2 = W1 * 1
W2 = 2
W1 = 2
Y = 2 * X + 2
17. Going Back to the “Cranes” Revenue Prediction
For 5 minutes of play, how much
revenue will be generated by the
ad network?
18. Trend Line
There are multiple lines we
can guess.
Which line can give us the
best prediction, so we can
predict the revenue for 5
mins?
?
?
?
0 0
1 0.015
2 0.038
3 0.058
4 0.12
5 ?
19. Trend Line
Line A is obviously worse than
Line B
How can we say that with math?
?
?
X Values Real Y
0 0
1 0.015
2 0.038
3 0.058
4 0.12
5 ?
Line A ( Y = 0.0225 * X + 0.04 )
Line B ( Y = 0.025 * X )
20. Line A is “worse” than Line B!
The Mean Squared Error of Line A is greater than the Mean Squared Error of Line B
0.0017231 > 0.0001866
?
?
The mean distance between the line and the real values is greater in Line A than Line B
21. The Best Line is out there
Line A and Line B were 2 random lines we chose.
How can we find the best line?
?
Line B
?
22. W1 MSE
0.000005 0.00388511015
0.00005 0.003871715
0.0005 0.0037391
0.001 0.0035946
0.015 0.0007666
0.02 0.0003266
0.025 0.0001866
0.05 0.0039866
Y = W1 * X + W2
Minimum Error Here!
0.025
Let us create a graph with every Mean Square Error for different W values
(We will only consider W1 not W2 for now)
23. So the BEST line Y = 0.025 * X
So for X = 5 then Y = 0.125
We estimate that the player that spends 5
minutes in the game generates $0.125
24. The famous Gradient Descent algorithm!
Pick random W1 and W2 Step 1
Find Mean Squared Error Slope for W1 and W2 Step 2
Multiply slopes with learning rate and nudge in the right direction Step 3
Step 4Go to Step 2 - Repeat Until Slope W1 and W2 are very small!
25. Multiply Slopes with Learning Rate & Nudge in the
right direction - Step 3
New_W1 = 0.0225 - 0.132 * 0.1 = 0.0093
New_W2 = 0.04 - 0.0776 * 0.1 = 0.0376
W1 = 0.0225
W2 = 0.04
Learning Rate = 0.1
1 2
3
26. Go to Step 2 - Repeat until slope W1 (MSE1) and
slope W2 (MSE2) are very small or 0!
Slope MSE1 Slope MSE2 New W1 New W2
-0.05744 0.00928 0.0093 0.03224
Y = 0.0225 * X + 0.04
27. Go to Step 2 - Repeat until slope W1 (MSE1) and
slope W2 (MSE2) are very small or 0!
Slope MSE1 Slope MSE2 New W1 New W2
-0.05744 0.00928 0.0093 0.03224
0.007776 0.0304 0.015044 0.031312
Y = 0.015044 * X + 0.031312
28. Go to Step 2 - Repeat until slope W1 (MSE1) and
slope W2 (MSE2) are very small or 0!
Slope MSE1 Slope MSE2 New W1 New W2
-0.05744 0.00928 0.0093 0.03224
0.007776 0.0304 0.015044 0.031312
-0.0137152 0.0212096 0.0142664 0.028272
Y = 0.0142664 * X + 0.028272
29. Go to Step 2 - Repeat until slope W1 (MSE1) and
slope W2 (MSE2) are very small or 0!
Slope MSE1 Slope MSE2 New W1 New W2
-0.05744 0.00928 0.0093 0.03224
0.007776 0.0304 0.015044 0.031312
-0.0137152 0.0212096 0.0142664 0.028272
-0.0057408 0.02245376 0.01563792 0.02615104
Y = 0.01563792 * X + 0.02615104
31. Go to Step 2 - Repeat until slope W1 (MSE1) and
slope W2 (MSE2) are very small or 0!
Slope MSE1 Slope MSE2 New W1 New W2
-0.05744 0.00928 0.0093 0.03224
0.007776 0.0304 0.015044 0.031312
-0.0137152 0.0212096 0.0142664 0.028272
-0.0057408 0.02245376 0.01563792 0.02615104
... ... ... ...
-
0.000000076758
17921
0.000000218820
7633 0.0282998714 -0.01039963339
Y = 0.0282998714 * X + -0.01039963339
32. Coding Time
1) List of a X,Y dataset
2) Train with gradient descent to find the W1 and W2
a) Learning Rate
b) Iterations
No Tensorflow.js quite yet, only JavaScript.
Time to implement Linear Regression and Gradient Descent
39. const a = tf.tensor([[1,2],[3,4]]);
console.log('shape:',a.shape);
a.print();
Create a rank-2 tensor
(matrix) matrix tensor
from a multidimensional
array.
40. const shape = [2, 2];
const b = tf.tensor([1, 2, 3, 4],
shape);
console.log('shape:', b.shape);
b.print();
Or you can create a
tensor from a flat array
and specify a shape.
41. const a = tf.tensor([[1, 2], [3,
4]], [2, 2], 'int32');
console.log('shape:', a.shape);
console.log('dtype', a.dtype);
a.print();
By default, tf.Tensors will have
a float32 dtype.
tf.Tensors can also be created
with bool, int32, complex64,
and string dtypes:
42. const a = tf.tensor([[1, 2], [3,
4]]);
console.log('a shape:', a.shape);
// a shape: (2) [2, 2]
a.print();
// Tensor
[[1, 2],
[3, 4]]
const b = a.reshape([4, 1]);
console.log('b shape:', b.shape);
// b shape: (2) [4, 1]
b.print();
// Tensor
[[1],
[2],
[3],
[4]]
It's often useful to be able to
reshape a tf.Tensor to another
shape with the same size.
This can be achieved with the
reshape() method
43. You can also get the values from a tf.Tensor using the
Tensor.array() or Tensor.data() methods:
44. We also provide synchronous versions of these methods which are simpler to
use, but will cause performance issues in your application. You should always
prefer the asynchronous methods in production applications.
48. Importing Tensorflow.js
Node.js
const tf = require("@tensorflow/tfjs");
// Optional Load the binding:
// Use '@tensorflow/tfjs-node-gpu' if running with GPU. If your system has a NVIDIA® GPU with CUDA support, use the GPU
package even for higher performance
require("@tensorflow/tfjs-node");
Instead of Loading @tensorflow/tfjs you can load @tensorflow/tfjs-core for no layer functionality
----------------------------------------------------------------------
// You have the Core API: tf.matMul(), tf.softmax(), …
// You also have Layers API: tf.model(), tf.layers.dense(), …
import * as tf from '@tensorflow/tfjs';
//You have the Core API: tfc.matMul(), tfc.softmax(), …
//No Layers API.
import * as tfc from '@tensorflow/tfjs-core';
49. TensorFlow.js support multiple different backends that implement tensor storage
and mathematical operations. At any given time, only one backend is active.
Most of the time, TensorFlow.js will automatically choose the best backend for
you given the current environment. However, sometimes it's important to know
which backend is being used and how to switch it.
console.log(tf.getBackend());
50. The WebGL backend, 'webgl', is currently the most powerful backend for the
browser. This backend is up to 100x faster than the vanilla CPU backend.
One caveat when using the WebGL backend is the need for explicit memory management.
WebGLTextures, which is where Tensor data is ultimately stored, are not automatically garbage
collected by the browser.
56. Multi-feature dataset
X Y
1000 sqrt
2 county score
$200K
400 sqrt
5 county score
$300K
20000 sqrt
5 county score
1M
[
[ 1000, 2, 1 ],
[ 400, 5, 1 ],
[ 20000, 5, 1 ],
]
[
[W1],
[W2],
[W3]
]
X =
[
[200K],
[300K],
[1M]
]
57. The yValues, xValues are large and as a result the errorValues will be large as well.
console.log(
errorValues.dataSync(),
this.weights.dataSync());
62. Transfer Learning
“Sophisticated deep learning models have millions of
parameters (weights) and training them from scratch often
requires large amounts of data of computing resources.
Transfer learning is a technique that shortcuts much of this by
taking a piece of a model that has already been trained on a
related task and reusing it in a new model.”
65. Step 3 - Predict
// Get the activation from mobilenet from the webcam.
const activation = net.infer(webcamElement, "conv_preds");
// Get the most likely class and confidences from the
classifier module.
const result = await classifier.predictClass(activation);
return names[result.classIndex];
66. Summary
Linear Regression
Mean Squared Error
Gradient Descent
Learning Rate
Iterations
Tensor (Dimensions, Shape, Type)
Importing Tensorflow.js
Training with Tensorflow.js
Multivariate Linear Regression
Transfer Learning