Introducing TensorFlow and ML
Introducing TensorFlow and ML
INTRODUCING TENSORFLOW
Overview
Understand the basics of machine learning, deep learning and neural networks
Mammals Fish
Members of the infraorder Cetacea Look like fish, swim like fish, move with fish
Rule-based Binary Classifier
Whale Mammal
Rule-based Classifier
Human Experts
ML-based Binary Classifier
Corpus
“Traditional” ML-based Binary Classifier
Corpus
ML-based Binary Classifier
Corpus
“Traditional” ML-based Binary Classifier
Corpus
“Traditional” ML-based Binary Classifier
Output: Label
Corpus
“Traditional” ML-based Binary Classifier
Corpus
“Traditional” ML-based Binary Classifier
Corpus
“Traditional” ML-based Binary Classifier
Output: Label
Corpus
The attributes that the ML algorithm focuses on are
called features
Corpus
“Representation” ML-based Binary Classifier
Corpus
“Representation” ML-based Binary Classifier
Algorithms that learn what The most common class of deep Simple building blocks that actually
features matter learning algorithms “learn”
“Deep Learning”-based Binary Classifier
Object Parts
Corners
Edges
Pixels
Object Parts
Corners
Edges
Pixels
Object Parts
Corners
Edges
Pixels
Layer 2
Layer 1
Layer N
…
Corpus of Layers in a neural network ML-based Classifier
Images
Neural Net works Introduced
https://www.tensorflow.org/
TensorFlow
TensorFlow™ is an open source software library for numerical computation using
data flow graphs.
https://www.tensorflow.org/
TensorFlow
TensorFlow™ is an open source software library for numerical computation using
data flow graphs.
https://www.tensorflow.org/
TensorFlow
TensorFlow™ is an open source software library for numerical computation using
data flow graphs.
https://www.tensorflow.org/
Advantages of TensorFlow
A network
Everything Is a Graph
Computations
Operators
Everything Is a Graph
Tensors
Data
Tensors Flow Through the Graph
5 + 15
+
3
3.6
floor 5 abs 5
TensorFlow
Demo
A network
Everything Is a Graph
Computations
Operators
Everything Is a Graph
Tensors
Data
Tensors Flow Through the Graph
5 + 15
+
3
3.6
floor 5 abs 5
TensorFlow
Tensors Flow Through the Graph
A round
x
+
+
B
floor abs
TensorFlow
Tensors Flow Through the Graph
A round
x
+
+
B floor abs
round
x
+
+
floor abs
Directed-Acyclic Graph
round
x
+
+
floor abs
Directed-Acyclic Graph
round
x
+
+
floor abs
round
x
round
floor
+
Indirect dependency +
floor
Directed-Acyclic Graph
round
x
+
+
floor abs
round
x
+
+
floor abs
round
x
+
+
floor abs
Calculate loss
Feedback
The Process of Machine Learning
…
Modelling Cyclical Dependencies
Modelling Cyclical Dependencies
How much you unroll depends on the number of iterations you want to run
Unroll graphs to model cyclic dependencies
Building and Running Graphs
2 Steps in a TensorFlow Program
Specify the operations and the Execute the graph to get the final result
data
2 Steps in a TensorFlow Program
TensorBoard
Visualize how data flows and what
computations operate on it
Modeling Computations as Graphs
Computation Graphs
Computation Graphs
Computation Graphs
Computation Graphs
Computation Graphs
Computation Graphs
Computation Graphs
TensorFlow calculates only that portion of
the graph which is required
Computation Graphs
Running Graphs on a Distributed System
Multiple portions of the graph can be run
in parallel across machines in the cluster
Demo
https://www.tensorflow.org/
Tensor
https://www.tensorflow.org/
Tensor
https://www.tensorflow.org/
Data Is Represented as Tensors
3, 6.7, “a”
Data Is Represented as Tensors
[1, 3, 5, 7, 9]
Data Is Represented as Tensors
[[1, 3, 5],
[7, 9, 11]]
Data Is Represented as Tensors
4 0
[1, 2, 3] 1
4 []
[1, 2, 3] [3]
int
float
string
boolean
Rank, shape and data types are 3
important characteristics which define a
Tensor
Summary
Cause Effect
Cause Effect
Cause Effect
Distance in miles from the city Price per square foot of homes
center
X Causes Y
Cause Effect
X
Distance from the city center
Simple Regression
Regression Equation:
y = A + Bx
y1 A + Bx1
=
y2 = A + Bx2
y3 = A + Bx3
… …
yn =
A + Bxn
The “Best” Regression Line
Y
Line 1: y = A1 + B1x
Line 2: y = A2 + B2x
X
Let’s compare two lines, Line 1 and Line 2
The “Best” Regression Line
Y
A1 Line 1: y = A1 + B1x
Line 2: y = A2 + B2x
x increases by 1
y decreases by B1
Line 1: y = A1 + B1x
Line 2: y = A2 + B2x
Line 1: y = A1 + B1x
Line 2: y = A2 + B2x
A2
X
Line 1: y = A1 + B1x
y decreases by B2
Line 2: y = A2 + B2x
x increases by 1
X
In the second line, if x increases by 1 unit, y decreases by B2 units
Minimising Least Square Error
Y
Line 1: y = A1 + B1x
Line 2: y = A2 + B2x
X
Drop vertical lines from each point to the lines 1 and 2
Minimising Least Square Error
Y
Line 1: y = A1 + B1x
Line 2: y = A2 + B2x
X
Drop vertical lines from each point to the lines 1 and 2
Minimising Least Square Error
Y
Line 1: y = A1 + B1x
Line 2: y = A2 + B2x
The “best fit” line is the one where the sum of the
squares of the lengths of these dotted lines is minimum
Minimising Least Square Error
Y
Line 1: y = A1 + B1x
Line 2: y = A2 + B2x
The “best fit” line is the one where the sum of the
squares of the lengths of these dotted lines is minimum
Minimising Least Square Error
Y
Line 1: y = A1 + B1x
Line 2: y = A2 + B2x
The “best fit” line is the one where the sum of the
squares of the lengths of the errors is minimum
Minimising Least Square Error
Y
Line 1: y = A1 + B1x
Line 2: y = A2 + B2x
The “best fit” line is the one where the sum of the
squares of the lengths of the errors is minimum
Minimising Least Square Error
Y
Regression Line: y = A
+ Bx
X
Linear Regression
Y
X
Distance from the city center
Linear Regression Algorithms in Practice
y = Astart + Bstartx
X
Start off with some values for A and B
The “Best” Regression Line
Y
y = Astart + Bstartx
y = A i + B ix
y = A i + B ix
y = A i + B ix
X
Distance from the city center
Linear Regression
Y
Price of
GOOG Regression Line: y = A
+ Bx
X
Dow Jones index
Linear Regression
Y
Life expectancy
Regression Line: y = A
+ Bx
Wealth
Machine learning algorithms can
be applied to a variety of problems
Abrahams, Sam; Hafner, Danijar; Erwitt, Erik; Scarpinelli, Ariel (2016-07-23). TensorFlow For
Machine Intelligence: A hands-on introduction to learning algorithms
Demo
y = Astart + Bstartx
y = Astart + Bstartx
X
Calculate the least square error and feed that back
The “Best” Regression Line
Y
y = A i + B ix
X
This will give us new values for A and B
The “Best” Regression Line
Y
y = A i + B ix
y = A i + B ix
Regression Line: y = A
+ Bx
Constants Placeholders
Variables
Immutable values which do Assigned once and do not
Are constantly recomputed
not change change after
Variables
Mutable Tensor values that persist across multiple calls to Session.run()
Abrahams, Sam; Hafner, Danijar; Erwitt, Erik; Scarpinelli, Ariel (2016-07-23). TensorFlow For
Machine Intelligence: A hands-on introduction to learning algorithms
Summary
Object Parts
Corners
Edges
Pixels
Object Parts
Corners
Edges
Pixels
Object Parts
Corners
Edges
Pixels
Layer 2
Layer 1
Layer N
…
Corpus of Layers in a neural network ML-based Classifier
Images
Neural Net works Introduced
R, G, B: 0-255
RGB Images
255, 0, 0
RGB Images
0, 255, 0
RGB Images
0, 0, 255
0.0 - 1.0
Grayscale Images
0.5
Grayscale Images
0.5
The number of channels specifies the number of elements in the 3rd dimension
Images as Tensors
(6, 6, 1) (6, 6, 3)
Demo
(10, 6, 6, 3)
(10, 6, 6, 3)
(10, 6, 6, 3)
= 784 pixels
MNIST Dataset
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
MNIST Dataset
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
MNIST Dataset
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
MNIST Dataset
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
MNIST Dataset
5 0 4 1
MNIST for machine learning is the
equivalent of the “Hello World” for
programming
The K-nearest-neighbors Algorithm
Types of ML Algorithms
Supervised Unsupervised
Labels associated with the training data The model has to be set up right to learn
is used to correct the algorithm structure in the data
Supervised Learning
Input variable x and output variable y
Corpus
KNN is an supervised learning algorithm
which uses training data to find what is
most similar to the current sample
K-nearest-neighbors
Distance measures
K-nearest-neighbors
4
Euclidean Distance
3
0 1 2 3 4 5
L1 Distance
Distance Measure
5
L1 distance
4
Snake distance
3
Manhattan distance 1
0 1 2 3 4 5
L1 Distance
5, 4
5 5-1 = 4
4
1, 0 1
0 1 2 3 4 5
L1 Distance
5, 4
5 5-1 = 4
4-0 = 4
4
1, 0 1
0 1 2 3 4 5
L1 Distance
5, 4
5 5-1 = 4
4-0 = 4
4
2
=8
1, 0 1
0 1 2 3 4 5
Demo
Handwritten image recognition using the k-nearest-neighbors ML algorithm
Calculating L1 distance
Find the distance between the test digit and all
training digits
KNN Implemented in TensorFlow
5 0 4 1
MNIST Dataset
= 784 pixels
4
Representing Labels
4
Vector 0 0 0 0 1 0 0 0 0 0
Index 0 1 2 3 4 5 6 7 8 9
Representing Labels
5
Vector 0 0 0 0 0 1 0 0 0 0
Index 0 1 2 3 4 5 6 7 8 9
Representing Images
28
28
= 784 pixels
Representing Images
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
= 784 pixels
Representing Images
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
Representing Images
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
Representing Images
0 0 0 0 0 0 0.2 0.8 0 0.3 0.6 0
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
Representing Images
0 0 0 0 0 0 0.2 0.9 0 0.3 0.8 0 0.2 0.8 0 0.3 0.6 0
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
Representing Images
0 0 0 0 0 0 .. .. .. .. .. .. 0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
Representing Images
0 0 0 0 0 0 .. .. .. .. .. .. 0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
Representing Images
0 0 0 0 0 0 .. .. .. .. .. .. 0 0 0 0.2 0.2 0
0 0 0 0 0 0
0 0 0 0.2 0.8 0
0 0 0 0.2 0.2 0
Representing Images
0 0 0 0 0 0 .. .. .. .. .. .. 0 0 0 0.2 0.2 0
= 784 pixels
KNN Implemented in TensorFlow
Calculating L1 distance
Find the distance between the test digit and all
training digits
L1 Distance
Training Test
tf.negative()
tf.add()
0.2 0.2 -0.2 0 0.3 -0.1 0 0.2 0.3 0.6 -0.3 -0.1 -0.2 0.4 -0.2 0 -0.3 -0.1
0.2 0.2 0.2 0 0.3 0.1 0 0.2 0.3 0.6 0.3 0.1 0.2 0.4 0.2 0 0.3 0.1
1 1.5 1.2
index = 0
KNN Implemented in TensorFlow
Calculating L1 distance
Find the distance between the test digit and all
training digits
Representing Labels
4
0 0 0 0 1 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9
np.argmax()
Summary