SlideShare ist ein Scribd-Unternehmen logo
1 von 68
Diving into TensorFlow.js
Bill Stavroulakis
@bstavroulakis
https://www.fullstackweekly.com
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
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
Part 1 - What is Machine Learning,
how does Tensorflow.js fit in it?
Artificial
Intelligence
(A.I.)
Make computers “smart”. Visual
perception, speech recognition
and decision-making.
Computers mimic human
intelligence.
Artificial Intelligence
Machine Learning
Create a model through statistical
analysis
Y = W * X
Find the W so we can predict Y
X Y
1 2
2 4
3 6
4 8
5 10
Y = W * X
What is the best W?
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?
[ 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?
Artificial Intelligence
Machine Learning
Deep Learning
Y = W * X
Where W is a neural net with
multiple layers
W is a neural net with
multiple layers
X Y
1 2
2 4
3 6
4 8
5 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.
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.”
Part 2 - Intro to Linear Regression -
One of the simplest models
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. “
Fixed Price Prediction
Q: How much revenue when user plays for 5
minutes?
A: The price is fixed at $5 so
Y = 5
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
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
Going Back to the “Cranes” Revenue Prediction
For 5 minutes of play, how much
revenue will be generated by the
ad network?
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 ?
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 )
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
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
?
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)
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
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!
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
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
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
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
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
A bunch of iterations later
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
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
class LinearRegression {
constructor(params) {
this.iterations = 100; this.learningRate = 0.1;
this.xData = [0, 1, 2, 3, 4]; this.yData = [0, 0.015, 0.038,
0.058, 0.12]; this.weights = [0.0225, 0.04];
}
train() {
for (let i = 0; i < this.iterations; i++) {
const xLen = this.xData.length;
const errorValues = this.xData.map((curr, i) => {
return this.weights[0] * curr + this.weights[1] -
this.yData[i];
});
// Mean Squared Error Slopes
const slopeW1 = ... const slopeW2 = ...
// Optimizer
this.weights[0] = this.weights[0] - slopeW1 *
this.learningRate;
this.weights[1] = this.weights[1] - slopeW2 *
this.learningRate; }}}
Summary
Linear Regression
Mean Squared Error
Gradient Descent Algorithm
Learning Rate
Iterations
Part 3 - Intro to Tensorflow.js
https://www.tensorflow.org/js
Tensor
An matrix-like object that wraps a collection of numbers. It has the
following attributes: data, dimension, shape, type, valid transformations
Scalar
1
tf.scalar(1)
{"kept":false,"isDisposedIn
ternal":false,"shape":[],"d
type":"float32","size":1,"s
trides":[],"dataId":{},"id"
:5,"rankType":"0"}
Vector
[1,2]
tf.tensor([1,2])
{"kept":false,"isDisposedIn
ternal":false,"shape":[3],"
dtype":"float32","size":3,"
strides":[],"dataId":{},"id
":4,"rankType":"1"}
Tensor
[
[[1,2],[3,4]],
[[5,6],[7,8]]
]
Matrix
[
[1,2],
[3,4]
]
tf.tensor([[1,2],[3,4]])
Single Object Array 2D Array n-dimensional
array
Tensor - Dimensions (also Rank)
Parenthesis until first number
[
[1,2,3,4],
[5,6,7,8],
[1,3,4,5],
[8,7,5,4]
]
[
[ [1,3] ]
]
[
[
[[1,2,3,4]]
]
]
[1,3,4,2]
2 Dimensions 3 Dimensions 1 Dimension 4 Dimensions
Tensor - Shape
Length for every dimension
[
[1,2,3,4],
[5,6,7,8],
[1,3,4,5],
[8,7,5,4].length
].length
[
[
[1,3].length
].length
].length
[
[
[
[1,2,3,4].length
].length
].∂length
].length
[1,3,4,2].length
[4,4] [1,1,2] [4] [1,1,1,4]
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.
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.
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:
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
You can also get the values from a tf.Tensor using the
Tensor.array() or Tensor.data() methods:
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.
Operators
Example: computing x^2 of all
elements in a tf.Tensor
Operators
Example: adding elements of
two tf.Tensors element-wise
Importing Tensorflow.js
Browser
<script
src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.0.0/dist
/tf.min.js"></script>
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';
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());
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.
Import Example
Linear Regression in TF
for (let i = 0; i < this.iterations; i++) {
// Guess Errors
const errorValues = this.xData.map((curr, i) => {
return this.weights[0] * curr + this.weights[1] - this.yData[i];
});
// Mean Squared Error Slopes
const slopeW1 = (2 / xLen) * errorValues.map((guessError, i) => {
return this.xData[i] * guessError; }).reduce((acc, curr) => {
return acc + curr;
}, 0);
const slopeW2 = (2 / xLen) * errorValues.reduce((acc, guessError) => {
return acc + guessError;
}, 0);
// Optimizer
this.weights[0] = this.weights[0] - slopeW1 * this.learningRate;
this.weights[1] = this.weights[1] - slopeW2 * this.learningRate;
}
Vanilla JS Tensorflow JS
for (let i = 0; i < this.iterations; i++) {
// Guess Errors
let errorValues = this.xData.matMul(this.weights).sub(this.yData);
// Mean Squared Error Slopes
let slopes = this.xData .transpose() .matMul(errorValues)
.div(this.xData.shape[0]);
// Optimizer
this.weights = this.weights.sub(slopes.mul(this.learningRate));
}
[
[0, 1],
[1, 1],
[2, 1],
[3, 1],
[4, 1],
]
[
[w1],
[w2]
]
=
[
[0],
[0.015],
[0.038],
[0.058],
[0,12],
]
X
[[ 0 * w1 + w2 = 0 ],
[ 1 * w1 + w2 = 0.015 ],
[ 2 * w1 + w2 = 0.038 ],
[ 3 * w1 + w2 = 0.058 ],
[ 4 * w1 + w2 = 0.12 ]]
Part 4 - Multivariate Linear Regression
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]
]
The yValues, xValues are large and as a result the errorValues will be large as well.
console.log(
errorValues.dataSync(),
this.weights.dataSync());
Normalization vs Standardization
100 20000
0 1
-1 10
Normalization
Standardization
How can we improve our predictions?
1. Add/ Remove Features
2. Change Learning Rate
3. Change Iterations
4. Change Optimizer
5. Change Model
Part 5 - Transfer Learning
Image
Classification
Example
New Use Case
We won’t be training new
models and reinventing
the wheel!
How can we reuse already
trained models and extend
them to our use case?
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.”
Step 1 - Load Tensorflow.js & pre-trained model
<script src="https://unpkg.com/@tensorflow/tfjs"></script>
<script src="https://unpkg.com/@tensorflow-models/mobilenet">
</script>
<script src="https://unpkg.com/@tensorflow-models/knn-
classifier">
</script>
Step 2 - Train with new classifier
const epocs = Array.apply(null, { length: 50
}).map(Number.call, Number);
epocs.forEach(async iteration => {
const activation = net.infer(webcamElement, "conv_preds");
classifier.addExample(activation, classId);
await tf.nextFrame();
});
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];
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
Next Steps
https://www.tensorflow.org/js/
https://github.com/tensorflow/tfjs-examples
https://www.tensorflow.org/js/models
Neural Networks
Logistic Regression
Text/Image/Audio
Time-series Forecasting
….
Bill Stavroulakis
@bstavroulakis
https://www.fullstackweekly.com

Weitere ähnliche Inhalte

Ähnlich wie Diving into Tensorflow.js

Machine Learning Tutorial Part - 1 | Machine Learning Tutorial For Beginners ...
Machine Learning Tutorial Part - 1 | Machine Learning Tutorial For Beginners ...Machine Learning Tutorial Part - 1 | Machine Learning Tutorial For Beginners ...
Machine Learning Tutorial Part - 1 | Machine Learning Tutorial For Beginners ...
Simplilearn
 

Ähnlich wie Diving into Tensorflow.js (20)

Chapter 1: Linear Regression
Chapter 1: Linear RegressionChapter 1: Linear Regression
Chapter 1: Linear Regression
 
Support Vector Machines Simply
Support Vector Machines SimplySupport Vector Machines Simply
Support Vector Machines Simply
 
Behind the Performance of Quake 3 Engine: Fast Inverse Square Root
Behind the Performance of Quake 3 Engine: Fast Inverse Square RootBehind the Performance of Quake 3 Engine: Fast Inverse Square Root
Behind the Performance of Quake 3 Engine: Fast Inverse Square Root
 
2013 - Andrei Zmievski: Machine learning para datos
2013 - Andrei Zmievski: Machine learning para datos2013 - Andrei Zmievski: Machine learning para datos
2013 - Andrei Zmievski: Machine learning para datos
 
Machine Learning Essentials Demystified part2 | Big Data Demystified
Machine Learning Essentials Demystified part2 | Big Data DemystifiedMachine Learning Essentials Demystified part2 | Big Data Demystified
Machine Learning Essentials Demystified part2 | Big Data Demystified
 
Begin with Machine Learning
Begin with Machine LearningBegin with Machine Learning
Begin with Machine Learning
 
Shap
ShapShap
Shap
 
Startup Jungle Cambodia | How to Build your First Machine Learning Application
Startup Jungle Cambodia | How to Build your First Machine Learning ApplicationStartup Jungle Cambodia | How to Build your First Machine Learning Application
Startup Jungle Cambodia | How to Build your First Machine Learning Application
 
A few solvers for portfolio selection
A few solvers for portfolio selectionA few solvers for portfolio selection
A few solvers for portfolio selection
 
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- ButkarComputer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
 
Machine Learning Tutorial Part - 1 | Machine Learning Tutorial For Beginners ...
Machine Learning Tutorial Part - 1 | Machine Learning Tutorial For Beginners ...Machine Learning Tutorial Part - 1 | Machine Learning Tutorial For Beginners ...
Machine Learning Tutorial Part - 1 | Machine Learning Tutorial For Beginners ...
 
Explore ml day 2
Explore ml day 2Explore ml day 2
Explore ml day 2
 
Describe Machine learning with math.
Describe Machine learning with math.Describe Machine learning with math.
Describe Machine learning with math.
 
Support vector machine
Support vector machineSupport vector machine
Support vector machine
 
Money Manager Presentation-2.pptx
Money Manager Presentation-2.pptxMoney Manager Presentation-2.pptx
Money Manager Presentation-2.pptx
 
Gans
GansGans
Gans
 
GANs
GANsGANs
GANs
 
Machine Learning.pdf
Machine Learning.pdfMachine Learning.pdf
Machine Learning.pdf
 
8. Vectors data frames
8. Vectors data frames8. Vectors data frames
8. Vectors data frames
 
Deep learning simplified
Deep learning simplifiedDeep learning simplified
Deep learning simplified
 

Kürzlich hochgeladen

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Kürzlich hochgeladen (20)

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 

Diving into Tensorflow.js

  • 1. Diving into TensorFlow.js Bill Stavroulakis @bstavroulakis https://www.fullstackweekly.com
  • 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?
  • 5. Artificial Intelligence (A.I.) Make computers “smart”. Visual perception, speech recognition and decision-making. Computers mimic human intelligence.
  • 6. Artificial Intelligence Machine Learning Create a model through statistical analysis Y = W * X Find the W so we can predict Y X Y 1 2 2 4 3 6 4 8 5 10 Y = W * X What is the best W?
  • 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?
  • 9. Artificial Intelligence Machine Learning Deep Learning Y = W * X Where W is a neural net with multiple layers W is a neural net with multiple layers X Y 1 2 2 4 3 6 4 8 5 10
  • 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
  • 30. A bunch of iterations later
  • 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
  • 33. class LinearRegression { constructor(params) { this.iterations = 100; this.learningRate = 0.1; this.xData = [0, 1, 2, 3, 4]; this.yData = [0, 0.015, 0.038, 0.058, 0.12]; this.weights = [0.0225, 0.04]; } train() { for (let i = 0; i < this.iterations; i++) { const xLen = this.xData.length; const errorValues = this.xData.map((curr, i) => { return this.weights[0] * curr + this.weights[1] - this.yData[i]; }); // Mean Squared Error Slopes const slopeW1 = ... const slopeW2 = ... // Optimizer this.weights[0] = this.weights[0] - slopeW1 * this.learningRate; this.weights[1] = this.weights[1] - slopeW2 * this.learningRate; }}}
  • 34. Summary Linear Regression Mean Squared Error Gradient Descent Algorithm Learning Rate Iterations
  • 35. Part 3 - Intro to Tensorflow.js https://www.tensorflow.org/js
  • 36. Tensor An matrix-like object that wraps a collection of numbers. It has the following attributes: data, dimension, shape, type, valid transformations Scalar 1 tf.scalar(1) {"kept":false,"isDisposedIn ternal":false,"shape":[],"d type":"float32","size":1,"s trides":[],"dataId":{},"id" :5,"rankType":"0"} Vector [1,2] tf.tensor([1,2]) {"kept":false,"isDisposedIn ternal":false,"shape":[3]," dtype":"float32","size":3," strides":[],"dataId":{},"id ":4,"rankType":"1"} Tensor [ [[1,2],[3,4]], [[5,6],[7,8]] ] Matrix [ [1,2], [3,4] ] tf.tensor([[1,2],[3,4]]) Single Object Array 2D Array n-dimensional array
  • 37. Tensor - Dimensions (also Rank) Parenthesis until first number [ [1,2,3,4], [5,6,7,8], [1,3,4,5], [8,7,5,4] ] [ [ [1,3] ] ] [ [ [[1,2,3,4]] ] ] [1,3,4,2] 2 Dimensions 3 Dimensions 1 Dimension 4 Dimensions
  • 38. Tensor - Shape Length for every dimension [ [1,2,3,4], [5,6,7,8], [1,3,4,5], [8,7,5,4].length ].length [ [ [1,3].length ].length ].length [ [ [ [1,2,3,4].length ].length ].∂length ].length [1,3,4,2].length [4,4] [1,1,2] [4] [1,1,1,4]
  • 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.
  • 45. Operators Example: computing x^2 of all elements in a tf.Tensor
  • 46. Operators Example: adding elements of two tf.Tensors element-wise
  • 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.
  • 53. for (let i = 0; i < this.iterations; i++) { // Guess Errors const errorValues = this.xData.map((curr, i) => { return this.weights[0] * curr + this.weights[1] - this.yData[i]; }); // Mean Squared Error Slopes const slopeW1 = (2 / xLen) * errorValues.map((guessError, i) => { return this.xData[i] * guessError; }).reduce((acc, curr) => { return acc + curr; }, 0); const slopeW2 = (2 / xLen) * errorValues.reduce((acc, guessError) => { return acc + guessError; }, 0); // Optimizer this.weights[0] = this.weights[0] - slopeW1 * this.learningRate; this.weights[1] = this.weights[1] - slopeW2 * this.learningRate; } Vanilla JS Tensorflow JS for (let i = 0; i < this.iterations; i++) { // Guess Errors let errorValues = this.xData.matMul(this.weights).sub(this.yData); // Mean Squared Error Slopes let slopes = this.xData .transpose() .matMul(errorValues) .div(this.xData.shape[0]); // Optimizer this.weights = this.weights.sub(slopes.mul(this.learningRate)); }
  • 54. [ [0, 1], [1, 1], [2, 1], [3, 1], [4, 1], ] [ [w1], [w2] ] = [ [0], [0.015], [0.038], [0.058], [0,12], ] X [[ 0 * w1 + w2 = 0 ], [ 1 * w1 + w2 = 0.015 ], [ 2 * w1 + w2 = 0.038 ], [ 3 * w1 + w2 = 0.058 ], [ 4 * w1 + w2 = 0.12 ]]
  • 55. Part 4 - Multivariate Linear Regression
  • 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());
  • 58. Normalization vs Standardization 100 20000 0 1 -1 10 Normalization Standardization
  • 59. How can we improve our predictions? 1. Add/ Remove Features 2. Change Learning Rate 3. Change Iterations 4. Change Optimizer 5. Change Model
  • 60. Part 5 - Transfer Learning
  • 61. Image Classification Example New Use Case We won’t be training new models and reinventing the wheel! How can we reuse already trained models and extend them to our use case?
  • 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.”
  • 63. Step 1 - Load Tensorflow.js & pre-trained model <script src="https://unpkg.com/@tensorflow/tfjs"></script> <script src="https://unpkg.com/@tensorflow-models/mobilenet"> </script> <script src="https://unpkg.com/@tensorflow-models/knn- classifier"> </script>
  • 64. Step 2 - Train with new classifier const epocs = Array.apply(null, { length: 50 }).map(Number.call, Number); epocs.forEach(async iteration => { const activation = net.infer(webcamElement, "conv_preds"); classifier.addExample(activation, classId); await tf.nextFrame(); });
  • 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