SlideShare a Scribd company logo
Gentlest Intro to Tensorflow (Part 3)
Khor Soon Hin, @neth_6, re:Culture
In collaboration with Sam & Edmund
Overview
● Multi-feature Linear Regression
● Logistic Regression
○ Multi-class prediction
○ Cross-entropy
○ Softmax
● Tensorflow Cheatsheet #1
Review: Predict from Single Feature with Linear Regression
Quick Review: Predict from Single Feature (House Size)
Quick Review: Use Linear Regression
Quick Review: Predict using Linear Regression
Linear Regression: Predict from Two (or More) Features
Two Features: House Size, Rooms
Source: teraplot.com
House Price, $
Rooms, unit
House Size, sqm
Same Issue: Predict for Values without Datapoint
Source: teraplot.com
House Price, $
Rooms, unit
House Size, sqm
Same Solution: Find Best-Fit
Source: teraplot.com
House Price, $
House Size, sqm
Rooms, unit
Prediction!
Review: Tensorflow Code
Tensorflow Code
# Model linear regression y = Wx + b
x = tf.placeholder(tf.float32, [None, 1])
W = tf.Variable(tf.zeros([1,1]))
b = tf.Variable(tf.zeros([1]))
product = tf.matmul(x,W)
y = product + b
y_ = tf.placeholder(tf.float32, [None, 1])
# Cost function 1/n * sum((y_-y)**2)
cost = tf.reduce_mean(tf.square(y_-y))
# Training using Gradient Descent to minimize cost
train_step = tf.train.GradientDescentOptimizer(0.0000001).minimize(cost)
Multi-feature: Change in Model & Cost Function
Model
1 Feature
y = W.x + b
y: House price prediction
x: House size
Goal: Find scalars W,b
Model
1 Feature
y = W.x + b
y: House price prediction
x: House size
Goal: Find scalars W,b
2 Features
y = W.x + W2.x2 + b
y: House price prediction
x: House size
x2: Rooms
Goal: Find scalars W, W2, b
Tensorflow Graph
1 Feature
y = tf.matmul(x, W) + b
W = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1])
Tensorflow Graph
1 Feature
y = tf.matmul(x, W) + b
W = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1])
2 Features
y = matmul(x, W) + matmul(x2, W2) + b
W = tf.Variable(tf.zeros[1,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1])
Tensorflow Graph: Train
1 Feature
y = tf.matmul(x, W) + b
W = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1])
2 Features
y = matmul(x, W) + matmul(x2, W2) + b
W = tf.Variable(tf.zeros[1,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1])
Train: feed = { x: … , y_: … }
Train: feed = { x: … , x2: ... y_: … }
Tensorflow Graph: Scalability Issue
1 Feature
y = tf.matmul(x, W) + b
W = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1])
2 Features
y = tf.matmul(x, W) + tf.matmul(x2,
W2) + b
W = tf.Variable(tf.zeros[1,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1]Train: feed = { x: … , y_: … }
Train: feed = { x: … , x2: ... y_: … }
3 Features
y = tf.matmul(x, W) + tf.matmul(x2,
W2) + tf.matmul(x3, W3) +b
W = tf.Variable(tf.zeros[1,1])
W2 = tf.Variable(tf.zeros[1,1])
W3 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
x2 = tf.placeholder(tf.float, [None, 1])
x3 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1]
Train: feed = { x: … , x2: ... , x3: … , y_: … }
Model gets messy!
Data Representation
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Feature values Actual outcome
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Lots of Data Manipulation
2 Features
y = tf.matmul(x, W) + tf.matmul(x2,
W2) + b
W = tf.Variable(tf.zeros[1,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1]
Train: feed = { x: … , x2: ... y_: … }
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Lots of Data Manipulation 2
2 Features
y = tf.matmul(x, W) + tf.matmul(x2,
W2) + b
W = tf.Variable(tf.zeros[1,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1]
Train: feed = { x: … , x2: ... y_: … }
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Lots of Data Manipulation 3
2 Features
y = tf.matmul(x, W) + tf.matmul(x2,
W2) + b
W = tf.Variable(tf.zeros[1,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1]
Train: feed = { x: … , x2: ... y_: … }
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Lots of Data Manipulation 4
2 Features
y = tf.matmul(x, W) + tf.matmul(x2,
W2) + b
W = tf.Variable(tf.zeros[1,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1]
Train: feed = { x: … , x2: ... y_: … }
Data manipulation
gets messy!
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Lots of Data Manipulation 4
2 Features
y = tf.matmul(x, W) + tf.matmul(x2,
W2) + b
W = tf.Variable(tf.zeros[1,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1]
Train: feed = { x: … , x2: ... y_: … }
y = W.x + W2.x2 + b
Find better way
Matrix: Cleaning Up Representations
Matrix Representation
House #0 Size_0 Rooms_0 Price_0
House #1 Size_1 Rooms_1 Price_1
… … … ...
House #m Size_m Rooms_m Price_m
Matrix Representation
House #0 Size_0 Rooms_0 Price_0
House #1 Size_1 Rooms_1 Price_1
… … … ...
House #m Size_m Rooms_m Price_m
House #0
House #1
…
House #m
Size_0 Rooms_0
Size_1 Rooms_1
… …
Size_m Rooms_m
Price_0
Price_1
...
Price_m
ActualFeaturesLabels
Matrix Representation
House #0 Size_0 Rooms_0 Price_0
House #1 Size_1 Rooms_1 Price_1
… … … ...
House #m Size_m Rooms_m Price_m
House #0
House #1
…
House #m
Size_0 Rooms_0
Size_1 Rooms_1
… …
Size_m Rooms_m
Price_0
Price_1
...
Price_m
... ...
Align all data by row so NO need for label
ActualFeaturesLabels
Matrix Representation
House #0 Size_0 Rooms_0 Price_0
House #1 Size_1 Rooms_1 Price_1
… … … ...
House #m Size_m Rooms_m Price_m
House #0
House #1
…
House #m
Size_0 Rooms_0
Size_1 Rooms_1
… …
Size_m Rooms_m
Price_0
Price_1
...
Price_m
... ...
Align all data by row so NO need for label
Let’s Focus Here
ActualFeaturesLabels
Matrix: Cleaning Up Models
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Better Model Equation
2 Features
y = tf.matmul(x, W) + tf.matmul(x2,
W2) + b
W = tf.Variable(tf.zeros[1,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1]
Train: feed = { x: … , x2: … , y_: …
}
y = W.x + W2.x2 + b
Find better way
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Better Model Equation
2 Features
y = tf.matmul(x, W) + tf.matmul(x2,
W2) + b
W = tf.Variable(tf.zeros[1,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1]
Train: feed = { x: … , x2: … , y_: … }
y = W.x + W2.x2 + b
Find better way
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Better Model Equation
2 Features
y = tf.matmul(x, W) + tf.matmul(x2,
W2) + b
W = tf.Variable(tf.zeros[1,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1]
Train: feed = { x: [size_i, rooms_i], … , x2: … y_: … }
y = W.x + W2.x2 + b
Find better way
2 Features
y = tf.matmul(x, W) + tf.matmul(x2,
W2) + b
W = tf.Variable(tf.zeros[1,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 2])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1]
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Better Model Equation
Train: feed = { x: [size_i, rooms_i], … , x2: …, y_: …
}
y = W.x + W2.x2 + b
Find better way
2 Features
y = tf.matmul(x, W) + tf.matmul(x2,
W2) + b
W = tf.Variable(tf.zeros[2,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 2])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1]
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Better Model Equation
Train: feed = { x: [size_i, rooms_i], … , x2: …, y_: …
}
y = W.x + W2.x2 + b
Find better way
2 Features
y = tf.matmul(x, W) + tf.matmul(x2,
W2) + b
W = tf.Variable(tf.zeros[2,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 2])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1]
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Better Model Equation
Train: feed = { x: [size_i, rooms_i], … , x2: …, y_: …
}
y = W.x + W2.x2 + b
Find better way
2 Features
y = tf.matmul(x, W) + tf.matmul(x2,
W2) + b
W = tf.Variable(tf.zeros[2,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 2])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1]
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Better Model Equation
Train: feed = { x: [size_i, rooms_i], … , x2: …, y_: …
}
y = W.x + W2.x2 + b
Find better way
Tensorflow Graph (Messy)
1 Feature
y = tf.matmul(x, W) + b
W = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1])
2 Features
y = matmul(x, W) + matmul(x2, W2) + b
W = tf.Variable(tf.zeros[1,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1])
Tensorflow Graph (Clean)
1 Feature
y = tf.matmul(x, W) + b
W = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1])
2 Features
y = matmul(x, W) + matmul(x2, W2) + b
W = tf.Variable(tf.zeros[2,1])
W2 = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 2])
x2 = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1])
Tensorflow Graph (Clean and Formatted)
1 Feature
y = tf.matmul(x, W) + b
W = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1])
2 Features
y = matmul(x, W) + b
W = tf.Variable(tf.zeros[2,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 2])
y_ = tf.placeholder(tf.float, [None, 1])
Tensorflow Graph (Illustration)
1 Feature
y = tf.matmul(x, W) + b
W = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1])
2 Features
y = matmul(x, W) + b
W = tf.Variable(tf.zeros[2,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 2])
y_ = tf.placeholder(tf.float, [None, 1])
scalar scalar.. 1 feature ..
..1coeff..
scalar scalar.. 2 features ..
..2coeffs..
Logistic Regression
Linear vs. Logistic Regression
Size
1
42
3 5
Rooms
ML
price (scalar)
Linear Regression
predict
Linear vs. Logistic Regression
Size
1
42
3 5
Rooms
ML
price (scalar)
ML
0
1
9
2
.
.
.
number (discrete classes)
Linear Regression
Logistic Regression
predict
predict
Image
Image Features
Image Features 1
23 53 … 33
53 20 … 88
… … … ....
62 2 … 193
Grayscale value of each pixel
Image Features 2
23 53 … 33
53 20 … 88
… … … ....
62 2 … 193
Grayscale value of each pixel
Image Features 3
23 53 … 33
53 20 … 88
… … … ....
62 2 … 193
Grayscale value of each pixel
Logistic Regression: Change in Models
Model
Linear Regression
y = W.x + b
y: House price (scalar) prediction
x: [House size, Rooms]
Goal: Find scalars W,b
Logistic Regression
y = W.x + b
y: Discrete class [0,1,...9] prediction
x: [2-Dim pixel grayscale colors]
Goal: Find scalars W, b
Data Representation Comparison
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Feature values Actual outcome
23 53 … 33
53 20 … 88
… … … ....
62 2 … 193
Image #1
Image #2
250 10 … 33
103 5 … 88
… … … ...
5 114 … 193
Feature values Actual outcome
Image #m ...
5
2
3
x x y_y_
Data Representation Comparison
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Feature values Actual outcome
23 53 … 33
53 20 … 88
… … … ....
62 2 … 193
Image #1
Image #2
250 10 … 33
103 5 … 88
… … … ...
5 114 … 193
Feature values Actual outcome
Image #m ...
5
2
3
2 Features 1-Dim X x Y 2-Dim Features
x x y_y_
Data Representation Comparison
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Feature values Actual outcome
23 53 … 33
53 20 … 88
… … … ....
62 2 … 193
Image #1
Image #2
250 10 … 33
103 5 … 88
… … … ...
5 114 … 193
Feature values Actual outcome
Image #m ...
5
2
3
2 Features 1-Dim X x Y 2-Dim Features
x x y_y_
Data Representation Comparison
House #1 Size_1 Rooms_1 Price_1
House #2 Size_2 Rooms_2 Price_2
… … … ...
House #m Size_m Rooms_m Price_m
Feature values Actual outcome
Image #1
Image #2 250 10 … 33 103 5 … 88 … 5 114 … 193
Feature values Actual outcome
Image #m ...
5
2
3
2 Features 1-Dim
X.Y 2 1-Dim Features
x x y_y_
23 53 … 33 53 20 … 88 … 62 2 … 193
Generalize into a multi-feature problem
Model
Linear Regression
y = W.x + b
y: House price (scalar) prediction
x: [House size, Rooms]
Goal: Find scalars W,b
Logistic Regression
y = W.x + b
y: Discrete class [0,1,...9] prediction
x: [2-Dim pixel grayscale colors]
x: [Pixel 1, Pixel 2, …, Pixel X.Y]
Goal: Find scalars W, b
Model
Linear Regression
y = W.x + b
y: House price (scalar) prediction
x: [House size, Rooms]
Goal: Find scalars W,b
Logistic Regression
y = W.x + b
y: Discrete class [0,1,...9] prediction
x: [2-Dim pixel grayscale colors]
x: [Pixel 1, Pixel 2, …, Pixel X.Y]
Goal: Find scalars W, b
This needs change as well!
Why Can’t ‘y’ be left as scalar of 0-9?
HINT: Beside the model, when doing ML we need this function!
Logistic Regression: Change in Cost Function
Linear Regression: Cost
Scalar
Linear Regression: Cost, NOT
Number
6
7
5
4
3
2
0
1
9
8
Pixel grayscale values
Discrete
The magnitude of difference (y_ - y) does NOT matter
Wrongly predicting 4 as 3 in as bad as 9 as 3
Logistic Regression: Prediction
ML
Logistic Regression
Image
1
Logistic Regression: Prediction
ML
Logistic Regression
Image
Probability
0 1 2 3 4 5 6 7 8 9
Logistic Regression: Prediction
ML
Logistic Regression
Image
Probability
0 1 2 3 4 5 6 7 8 9
Probability
0 1 2 3 4 5 6 7 8 9
Actual
Compare
Cross-Entropy: Cost Function 1
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
Cross-Entropy: Cost Function 2
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
Prediction
Probability
0 1 2 3 4 5 6 7 8 9
Probability
0 1 2 3 4 5 6 7 8 9
Actual
Cross-Entropy: Cost Function 3
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
Prediction
-log (Probability)
0 1 2 3 4 5 6 7 8 9
Probability
0 1 2 3 4 5 6 7 8 9
Actual
Almost inverse because Probability < 1
Graph: -log(probability)
-log(probability)
probability
10
Probability always between 0 and 1.
Thus -log(probability) is inversely proportional to probability
Cross-Entropy: Cost Function x x xx
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
Prediction
0 1 2 3 4 5 6 7 8 9
Probability
0 1 2 3 4 5 6 7 8 9
Actual
-log (Probability)
Cross-Entropy: Cost Function
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
Prediction
0 1 2 3 4 5 6 7 8 9
Probability
0 1 2 3 4 5 6 7 8 9
Actual
-log (Probability)
Cross-Entropy: Cost Function
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
Prediction
0 1 2 3 4 5 6 7 8 9
Probability
0 1 2 3 4 5 6 7 8 9
Actual
-log (Probability)
Cross-Entropy: Cost Function
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
Prediction
0 1 2 3 4 5 6 7 8 9
Probability
0 1 2 3 4 5 6 7 8 9
Actual
-log (Probability)
● Only y’i = 1 and log(yi) matters
● The smaller the log(yi) the lower cost
● The higher the yi the lower the cost
Logistic Regression: Prediction
ML
Logistic Regression
Image
Probability
0 1 2 3 4 5 6 7 8 9
ML
Linear Regression
Image
1
Tensorflow Graph (Review)
1 Feature
y = tf.matmul(x, W) + b
W = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1])
2 Features
y = matmul(x, W) + b
W = tf.Variable(tf.zeros[2,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 2])
y_ = tf.placeholder(tf.float, [None, 1])
Tensorflow Graph (Review 2)
1 Feature
y = tf.matmul(x, W) + b
W = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1])
2 Features
y = matmul(x, W) + b
W = tf.Variable(tf.zeros[2,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 2])
y_ = tf.placeholder(tf.float, [None, 1])
scalar scalar.. 1 feature ..
..1coeff..
scalar scalar.. 2 features ..
..2coeffs..
Tensorflow Graph (Review 2)
1 Feature
y = tf.matmul(x, W) + b
W = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1])
2 Features
y = matmul(x, W) + b
W = tf.Variable(tf.zeros[2,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 2])
y_ = tf.placeholder(tf.float, [None, 1])
scalar scalar.. 1 feature ..
..1coeff..
scalar scalar.. 2 features ..
..2coeffs..
Apply multi-feature linear regression
Logistic Regression: Prediction
ML
Logistic Regression
Image
Probability
0 1 2 3 4 5 6 7 8 9
ML
Linear Regression
Image
1
y = tf.matmul(x,W) + b
scalar scalar.. n features ..
..ncoeffs..
Logistic Regression: Prediction
ML
Logistic Regression
Image
Probability
0 1 2 3 4 5 6 7 8 9
ML
Linear Regression
Image
1
y = tf.matmul(x,W) + b
scalar scalar.. n features ..
.. k class ..
..ncoeffs..
Logistic Regression: Prediction
ML
Logistic Regression
Image
Probability
0 1 2 3 4 5 6 7 8 9
ML
Linear Regression
Image
1
y = tf.matmul(x,W) + b
scalar scalar.. n features ..
.. k class ..
y = tf.matmul(x,W) + b
..ncoeffs..
.. k class .... n features ..
..ncoeffs..
.. k class ..
Tensorflow Graph: Basic, Multi-feature, Multi-class
1 Feature
y = tf.matmul(x, W) + b
W = tf.Variable(tf.zeros[1,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 1])
y_ = tf.placeholder(tf.float, [None, 1])
2 Features
y = matmul(x, W) + b
W = tf.Variable(tf.zeros[2,1])
b = tf.Variable(tf.zeros[1])
x = tf.placeholder(tf.float, [None, 2])
y_ = tf.placeholder(tf.float, [None, 1])
scalar scalar.. 1 feature ..
..1coeff..
scalar scalar.. 2 features ..
..2coeffs..
2 Features, 10 Classes
y = matmul(x, W) + b
W = tf.Variable(tf.zeros[2,10])
b = tf.Variable(tf.zeros[10])
x = tf.placeholder(tf.float, [None, 2])
y_ = tf.placeholder(tf.float, [None, 10])
k class k class.. 2 features ..
..2coeffs..
k class
Logistic Regression: Prediction
ML
Logistic Regression
Image
Probability
0 1 2 3 4 5 6 7 8 9
Probability
0 1 2 3 4 5 6 7 8 9
Actual
Compare
Great but….sum of all prediction ‘probability’ NOT 1
ML
Logistic Regression
Image
Value
0 1 2 3 4 5 6 7 8 9
ML
Logistic Regression
Image
Value
0 1 2 3 4 5 6 7 8 9
exp(Value)
0 1 2 3 4 5 6 7 8 9
ML
Logistic Regression
Image
Value
0 1 2 3 4 5 6 7 8 9
exp(Value)
0 1 2 3 4 5 6 7 8 9
SUM
ML
Logistic Regression
Image
Value
0 1 2 3 4 5 6 7 8 9
exp(Value)
0 1 2 3 4 5 6 7 8 9
exp(Value)
0 1 2 3 4 5 6 7 8 9
SUM
SUM
ML
Logistic Regression
Image
Value
0 1 2 3 4 5 6 7 8 9
exp(Value)
0 1 2 3 4 5 6 7 8 9
exp(Value)
0 1 2 3 4 5 6 7 8 9
SUM
SUM
sum of all prediction ‘probability’ is 1
Before softmax
y = tf.matmul(x, W) + b
With softmax
y = tf.nn.softmax(tf.matmul(x, W) + b)
y = tf.matmul(x, W) + b
Summary
● Cheat sheet: Single feature, Multi-feature, Multi-class
● Logistic regression:
○ Multi-class prediction: Ensure that prediction is one of a discrete set of values
○ Cross-entropy: Measure difference between multi-class prediction and actual
○ Softmax: Ensure the multi-class prediction probability is a valid distribution (sum = 1)
Congrats!
You can now understand Google’s Tensorflow Beginner’s Tutorial
(https://www.tensorflow.org/versions/r0.7/tutorials/mnist/beginners/index.html)
References
● Perform ML with TF using multi-feature linear regression (the wrong way)
○ https://github.
com/nethsix/gentle_tensorflow/blob/master/code/linear_regression_multi_feature_using_mini_
batch_with_tensorboard.py
● Perform ML with TF using multi-feature linear regression
○ https://github.
com/nethsix/gentle_tensorflow/blob/master/code/linear_regression_multi_feature_using_mini_
batch_without_matrix_with_tensorboard.py
● Tensorflow official tutorial for character recognition
○ https://www.tensorflow.org/versions/r0.7/tutorials/mnist/beginners/index.html
● Colah’s excellent explanation of cross-entropy
○ http://colah.github.io/posts/2015-09-Visual-Information/

More Related Content

PDF
Gentlest Introduction to Tensorflow
PDF
Gentlest Introduction to Tensorflow - Part 2
PPTX
TensorFlow in Practice
PPTX
Explanation on Tensorflow example -Deep mnist for expert
PDF
Google TensorFlow Tutorial
PDF
TensorFlow Tutorial
PPTX
TensorFlow
PPTX
Introduction to Tensorflow
Gentlest Introduction to Tensorflow
Gentlest Introduction to Tensorflow - Part 2
TensorFlow in Practice
Explanation on Tensorflow example -Deep mnist for expert
Google TensorFlow Tutorial
TensorFlow Tutorial
TensorFlow
Introduction to Tensorflow

What's hot (18)

PDF
Tensor board
PDF
About RNN
PDF
About RNN
PDF
Python book
PDF
Introduction to TensorFlow, by Machine Learning at Berkeley
PDF
Pythonbrasil - 2018 - Acelerando Soluções com GPU
PPTX
Intro to Python (High School) Unit #3
PPTX
30 分鐘學會實作 Python Feature Selection
PDF
Baby Steps to Machine Learning at DevFest Lagos 2019
PDF
Additive model and boosting tree
PDF
What is TensorFlow and why do we use it
PDF
Sergey Shelpuk & Olha Romaniuk - “Deep learning, Tensorflow, and Fashion: how...
PDF
Pybelsberg — Constraint-based Programming in Python
PDF
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
PDF
Neural networks with python
PPTX
深層学習後半
PPTX
Font classification with 5 deep learning models using tensor flow
PDF
[신경망기초] 합성곱신경망
Tensor board
About RNN
About RNN
Python book
Introduction to TensorFlow, by Machine Learning at Berkeley
Pythonbrasil - 2018 - Acelerando Soluções com GPU
Intro to Python (High School) Unit #3
30 分鐘學會實作 Python Feature Selection
Baby Steps to Machine Learning at DevFest Lagos 2019
Additive model and boosting tree
What is TensorFlow and why do we use it
Sergey Shelpuk & Olha Romaniuk - “Deep learning, Tensorflow, and Fashion: how...
Pybelsberg — Constraint-based Programming in Python
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
Neural networks with python
深層学習後半
Font classification with 5 deep learning models using tensor flow
[신경망기초] 합성곱신경망
Ad

Similar to Gentlest Introduction to Tensorflow - Part 3 (20)

PDF
Tensor flow description of ML Lab. document
PDF
The TensorFlow dance craze
PDF
Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...
PPTX
Introduction to TensorFlow
PPTX
Wrangle 2016: (Lightning Talk) FizzBuzz in TensorFlow
PPTX
Machine Learning with Tensorflow
PDF
Developing a ML model using TF Estimator
PPTX
Introduction to Neural Networks and Deep Learning from Scratch
DOCX
When running the code below I am getting some errors (see image)- The.docx
PPTX
TensorFlow for IITians
PDF
maXbox starter65 machinelearning3
PDF
Introducton to Convolutional Nerural Network with TensorFlow
PDF
Gradient Descent Code Implementation.pdf
PDF
Assignment 5.3.pdf
PDF
딥러닝 교육 자료 #2
PPTX
Machine Learning - Introduction to Tensorflow
DOCX
Please help fill in the missing code below in order for it run correct.docx
PDF
Introduction to TensorFlow 2.0
PDF
TensorFlow 2: New Era of Developing Deep Learning Models
PPTX
wk5ppt1_Titanic
Tensor flow description of ML Lab. document
The TensorFlow dance craze
Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...
Introduction to TensorFlow
Wrangle 2016: (Lightning Talk) FizzBuzz in TensorFlow
Machine Learning with Tensorflow
Developing a ML model using TF Estimator
Introduction to Neural Networks and Deep Learning from Scratch
When running the code below I am getting some errors (see image)- The.docx
TensorFlow for IITians
maXbox starter65 machinelearning3
Introducton to Convolutional Nerural Network with TensorFlow
Gradient Descent Code Implementation.pdf
Assignment 5.3.pdf
딥러닝 교육 자료 #2
Machine Learning - Introduction to Tensorflow
Please help fill in the missing code below in order for it run correct.docx
Introduction to TensorFlow 2.0
TensorFlow 2: New Era of Developing Deep Learning Models
wk5ppt1_Titanic
Ad

More from Khor SoonHin (6)

PDF
The Many Flavors of OAuth - Understand Everything About OAuth2
PDF
Rails + Webpack
PDF
RMSLE cost function
PDF
Tokyo React.js #3: Missing Pages: ReactJS/Flux/GraphQL/RelayJS
PDF
Tokyo React.js #3 Meetup (ja): Missing Pages: ReactJS/GraphQL/RelayJS
PDF
From Back to Front: Rails To React Family
The Many Flavors of OAuth - Understand Everything About OAuth2
Rails + Webpack
RMSLE cost function
Tokyo React.js #3: Missing Pages: ReactJS/Flux/GraphQL/RelayJS
Tokyo React.js #3 Meetup (ja): Missing Pages: ReactJS/GraphQL/RelayJS
From Back to Front: Rails To React Family

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
sap open course for s4hana steps from ECC to s4
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
KodekX | Application Modernization Development
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation theory and applications.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Spectroscopy.pptx food analysis technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
cuic standard and advanced reporting.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
Unlocking AI with Model Context Protocol (MCP)
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
sap open course for s4hana steps from ECC to s4
The AUB Centre for AI in Media Proposal.docx
KodekX | Application Modernization Development
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation theory and applications.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Spectral efficient network and resource selection model in 5G networks
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
MYSQL Presentation for SQL database connectivity
Spectroscopy.pptx food analysis technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
cuic standard and advanced reporting.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Per capita expenditure prediction using model stacking based on satellite ima...
NewMind AI Weekly Chronicles - August'25 Week I

Gentlest Introduction to Tensorflow - Part 3

  • 1. Gentlest Intro to Tensorflow (Part 3) Khor Soon Hin, @neth_6, re:Culture In collaboration with Sam & Edmund
  • 2. Overview ● Multi-feature Linear Regression ● Logistic Regression ○ Multi-class prediction ○ Cross-entropy ○ Softmax ● Tensorflow Cheatsheet #1
  • 3. Review: Predict from Single Feature with Linear Regression
  • 4. Quick Review: Predict from Single Feature (House Size)
  • 5. Quick Review: Use Linear Regression
  • 6. Quick Review: Predict using Linear Regression
  • 7. Linear Regression: Predict from Two (or More) Features
  • 8. Two Features: House Size, Rooms Source: teraplot.com House Price, $ Rooms, unit House Size, sqm
  • 9. Same Issue: Predict for Values without Datapoint Source: teraplot.com House Price, $ Rooms, unit House Size, sqm
  • 10. Same Solution: Find Best-Fit Source: teraplot.com House Price, $ House Size, sqm Rooms, unit Prediction!
  • 12. Tensorflow Code # Model linear regression y = Wx + b x = tf.placeholder(tf.float32, [None, 1]) W = tf.Variable(tf.zeros([1,1])) b = tf.Variable(tf.zeros([1])) product = tf.matmul(x,W) y = product + b y_ = tf.placeholder(tf.float32, [None, 1]) # Cost function 1/n * sum((y_-y)**2) cost = tf.reduce_mean(tf.square(y_-y)) # Training using Gradient Descent to minimize cost train_step = tf.train.GradientDescentOptimizer(0.0000001).minimize(cost)
  • 13. Multi-feature: Change in Model & Cost Function
  • 14. Model 1 Feature y = W.x + b y: House price prediction x: House size Goal: Find scalars W,b
  • 15. Model 1 Feature y = W.x + b y: House price prediction x: House size Goal: Find scalars W,b 2 Features y = W.x + W2.x2 + b y: House price prediction x: House size x2: Rooms Goal: Find scalars W, W2, b
  • 16. Tensorflow Graph 1 Feature y = tf.matmul(x, W) + b W = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1])
  • 17. Tensorflow Graph 1 Feature y = tf.matmul(x, W) + b W = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1]) 2 Features y = matmul(x, W) + matmul(x2, W2) + b W = tf.Variable(tf.zeros[1,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1])
  • 18. Tensorflow Graph: Train 1 Feature y = tf.matmul(x, W) + b W = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1]) 2 Features y = matmul(x, W) + matmul(x2, W2) + b W = tf.Variable(tf.zeros[1,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1]) Train: feed = { x: … , y_: … } Train: feed = { x: … , x2: ... y_: … }
  • 19. Tensorflow Graph: Scalability Issue 1 Feature y = tf.matmul(x, W) + b W = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1]) 2 Features y = tf.matmul(x, W) + tf.matmul(x2, W2) + b W = tf.Variable(tf.zeros[1,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1]Train: feed = { x: … , y_: … } Train: feed = { x: … , x2: ... y_: … } 3 Features y = tf.matmul(x, W) + tf.matmul(x2, W2) + tf.matmul(x3, W3) +b W = tf.Variable(tf.zeros[1,1]) W2 = tf.Variable(tf.zeros[1,1]) W3 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) x2 = tf.placeholder(tf.float, [None, 1]) x3 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1] Train: feed = { x: … , x2: ... , x3: … , y_: … } Model gets messy!
  • 20. Data Representation House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Feature values Actual outcome
  • 21. House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Lots of Data Manipulation 2 Features y = tf.matmul(x, W) + tf.matmul(x2, W2) + b W = tf.Variable(tf.zeros[1,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1] Train: feed = { x: … , x2: ... y_: … }
  • 22. House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Lots of Data Manipulation 2 2 Features y = tf.matmul(x, W) + tf.matmul(x2, W2) + b W = tf.Variable(tf.zeros[1,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1] Train: feed = { x: … , x2: ... y_: … }
  • 23. House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Lots of Data Manipulation 3 2 Features y = tf.matmul(x, W) + tf.matmul(x2, W2) + b W = tf.Variable(tf.zeros[1,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1] Train: feed = { x: … , x2: ... y_: … }
  • 24. House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Lots of Data Manipulation 4 2 Features y = tf.matmul(x, W) + tf.matmul(x2, W2) + b W = tf.Variable(tf.zeros[1,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1] Train: feed = { x: … , x2: ... y_: … } Data manipulation gets messy!
  • 25. House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Lots of Data Manipulation 4 2 Features y = tf.matmul(x, W) + tf.matmul(x2, W2) + b W = tf.Variable(tf.zeros[1,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1] Train: feed = { x: … , x2: ... y_: … } y = W.x + W2.x2 + b Find better way
  • 26. Matrix: Cleaning Up Representations
  • 27. Matrix Representation House #0 Size_0 Rooms_0 Price_0 House #1 Size_1 Rooms_1 Price_1 … … … ... House #m Size_m Rooms_m Price_m
  • 28. Matrix Representation House #0 Size_0 Rooms_0 Price_0 House #1 Size_1 Rooms_1 Price_1 … … … ... House #m Size_m Rooms_m Price_m House #0 House #1 … House #m Size_0 Rooms_0 Size_1 Rooms_1 … … Size_m Rooms_m Price_0 Price_1 ... Price_m ActualFeaturesLabels
  • 29. Matrix Representation House #0 Size_0 Rooms_0 Price_0 House #1 Size_1 Rooms_1 Price_1 … … … ... House #m Size_m Rooms_m Price_m House #0 House #1 … House #m Size_0 Rooms_0 Size_1 Rooms_1 … … Size_m Rooms_m Price_0 Price_1 ... Price_m ... ... Align all data by row so NO need for label ActualFeaturesLabels
  • 30. Matrix Representation House #0 Size_0 Rooms_0 Price_0 House #1 Size_1 Rooms_1 Price_1 … … … ... House #m Size_m Rooms_m Price_m House #0 House #1 … House #m Size_0 Rooms_0 Size_1 Rooms_1 … … Size_m Rooms_m Price_0 Price_1 ... Price_m ... ... Align all data by row so NO need for label Let’s Focus Here ActualFeaturesLabels
  • 32. House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Better Model Equation 2 Features y = tf.matmul(x, W) + tf.matmul(x2, W2) + b W = tf.Variable(tf.zeros[1,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1] Train: feed = { x: … , x2: … , y_: … } y = W.x + W2.x2 + b Find better way
  • 33. House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Better Model Equation 2 Features y = tf.matmul(x, W) + tf.matmul(x2, W2) + b W = tf.Variable(tf.zeros[1,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1] Train: feed = { x: … , x2: … , y_: … } y = W.x + W2.x2 + b Find better way
  • 34. House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Better Model Equation 2 Features y = tf.matmul(x, W) + tf.matmul(x2, W2) + b W = tf.Variable(tf.zeros[1,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1] Train: feed = { x: [size_i, rooms_i], … , x2: … y_: … } y = W.x + W2.x2 + b Find better way
  • 35. 2 Features y = tf.matmul(x, W) + tf.matmul(x2, W2) + b W = tf.Variable(tf.zeros[1,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 2]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1] House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Better Model Equation Train: feed = { x: [size_i, rooms_i], … , x2: …, y_: … } y = W.x + W2.x2 + b Find better way
  • 36. 2 Features y = tf.matmul(x, W) + tf.matmul(x2, W2) + b W = tf.Variable(tf.zeros[2,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 2]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1] House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Better Model Equation Train: feed = { x: [size_i, rooms_i], … , x2: …, y_: … } y = W.x + W2.x2 + b Find better way
  • 37. 2 Features y = tf.matmul(x, W) + tf.matmul(x2, W2) + b W = tf.Variable(tf.zeros[2,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 2]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1] House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Better Model Equation Train: feed = { x: [size_i, rooms_i], … , x2: …, y_: … } y = W.x + W2.x2 + b Find better way
  • 38. 2 Features y = tf.matmul(x, W) + tf.matmul(x2, W2) + b W = tf.Variable(tf.zeros[2,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 2]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1] House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Better Model Equation Train: feed = { x: [size_i, rooms_i], … , x2: …, y_: … } y = W.x + W2.x2 + b Find better way
  • 39. Tensorflow Graph (Messy) 1 Feature y = tf.matmul(x, W) + b W = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1]) 2 Features y = matmul(x, W) + matmul(x2, W2) + b W = tf.Variable(tf.zeros[1,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1])
  • 40. Tensorflow Graph (Clean) 1 Feature y = tf.matmul(x, W) + b W = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1]) 2 Features y = matmul(x, W) + matmul(x2, W2) + b W = tf.Variable(tf.zeros[2,1]) W2 = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 2]) x2 = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1])
  • 41. Tensorflow Graph (Clean and Formatted) 1 Feature y = tf.matmul(x, W) + b W = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1]) 2 Features y = matmul(x, W) + b W = tf.Variable(tf.zeros[2,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 2]) y_ = tf.placeholder(tf.float, [None, 1])
  • 42. Tensorflow Graph (Illustration) 1 Feature y = tf.matmul(x, W) + b W = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1]) 2 Features y = matmul(x, W) + b W = tf.Variable(tf.zeros[2,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 2]) y_ = tf.placeholder(tf.float, [None, 1]) scalar scalar.. 1 feature .. ..1coeff.. scalar scalar.. 2 features .. ..2coeffs..
  • 44. Linear vs. Logistic Regression Size 1 42 3 5 Rooms ML price (scalar) Linear Regression predict
  • 45. Linear vs. Logistic Regression Size 1 42 3 5 Rooms ML price (scalar) ML 0 1 9 2 . . . number (discrete classes) Linear Regression Logistic Regression predict predict Image
  • 47. Image Features 1 23 53 … 33 53 20 … 88 … … … .... 62 2 … 193 Grayscale value of each pixel
  • 48. Image Features 2 23 53 … 33 53 20 … 88 … … … .... 62 2 … 193 Grayscale value of each pixel
  • 49. Image Features 3 23 53 … 33 53 20 … 88 … … … .... 62 2 … 193 Grayscale value of each pixel
  • 51. Model Linear Regression y = W.x + b y: House price (scalar) prediction x: [House size, Rooms] Goal: Find scalars W,b Logistic Regression y = W.x + b y: Discrete class [0,1,...9] prediction x: [2-Dim pixel grayscale colors] Goal: Find scalars W, b
  • 52. Data Representation Comparison House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Feature values Actual outcome 23 53 … 33 53 20 … 88 … … … .... 62 2 … 193 Image #1 Image #2 250 10 … 33 103 5 … 88 … … … ... 5 114 … 193 Feature values Actual outcome Image #m ... 5 2 3 x x y_y_
  • 53. Data Representation Comparison House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Feature values Actual outcome 23 53 … 33 53 20 … 88 … … … .... 62 2 … 193 Image #1 Image #2 250 10 … 33 103 5 … 88 … … … ... 5 114 … 193 Feature values Actual outcome Image #m ... 5 2 3 2 Features 1-Dim X x Y 2-Dim Features x x y_y_
  • 54. Data Representation Comparison House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Feature values Actual outcome 23 53 … 33 53 20 … 88 … … … .... 62 2 … 193 Image #1 Image #2 250 10 … 33 103 5 … 88 … … … ... 5 114 … 193 Feature values Actual outcome Image #m ... 5 2 3 2 Features 1-Dim X x Y 2-Dim Features x x y_y_
  • 55. Data Representation Comparison House #1 Size_1 Rooms_1 Price_1 House #2 Size_2 Rooms_2 Price_2 … … … ... House #m Size_m Rooms_m Price_m Feature values Actual outcome Image #1 Image #2 250 10 … 33 103 5 … 88 … 5 114 … 193 Feature values Actual outcome Image #m ... 5 2 3 2 Features 1-Dim X.Y 2 1-Dim Features x x y_y_ 23 53 … 33 53 20 … 88 … 62 2 … 193 Generalize into a multi-feature problem
  • 56. Model Linear Regression y = W.x + b y: House price (scalar) prediction x: [House size, Rooms] Goal: Find scalars W,b Logistic Regression y = W.x + b y: Discrete class [0,1,...9] prediction x: [2-Dim pixel grayscale colors] x: [Pixel 1, Pixel 2, …, Pixel X.Y] Goal: Find scalars W, b
  • 57. Model Linear Regression y = W.x + b y: House price (scalar) prediction x: [House size, Rooms] Goal: Find scalars W,b Logistic Regression y = W.x + b y: Discrete class [0,1,...9] prediction x: [2-Dim pixel grayscale colors] x: [Pixel 1, Pixel 2, …, Pixel X.Y] Goal: Find scalars W, b This needs change as well!
  • 58. Why Can’t ‘y’ be left as scalar of 0-9? HINT: Beside the model, when doing ML we need this function!
  • 59. Logistic Regression: Change in Cost Function
  • 61. Linear Regression: Cost, NOT Number 6 7 5 4 3 2 0 1 9 8 Pixel grayscale values Discrete The magnitude of difference (y_ - y) does NOT matter Wrongly predicting 4 as 3 in as bad as 9 as 3
  • 63. Logistic Regression: Prediction ML Logistic Regression Image Probability 0 1 2 3 4 5 6 7 8 9
  • 64. Logistic Regression: Prediction ML Logistic Regression Image Probability 0 1 2 3 4 5 6 7 8 9 Probability 0 1 2 3 4 5 6 7 8 9 Actual Compare
  • 65. Cross-Entropy: Cost Function 1 cross_entropy = -tf.reduce_sum(y_*tf.log(y))
  • 66. Cross-Entropy: Cost Function 2 cross_entropy = -tf.reduce_sum(y_*tf.log(y)) Prediction Probability 0 1 2 3 4 5 6 7 8 9 Probability 0 1 2 3 4 5 6 7 8 9 Actual
  • 67. Cross-Entropy: Cost Function 3 cross_entropy = -tf.reduce_sum(y_*tf.log(y)) Prediction -log (Probability) 0 1 2 3 4 5 6 7 8 9 Probability 0 1 2 3 4 5 6 7 8 9 Actual Almost inverse because Probability < 1
  • 68. Graph: -log(probability) -log(probability) probability 10 Probability always between 0 and 1. Thus -log(probability) is inversely proportional to probability
  • 69. Cross-Entropy: Cost Function x x xx cross_entropy = -tf.reduce_sum(y_*tf.log(y)) Prediction 0 1 2 3 4 5 6 7 8 9 Probability 0 1 2 3 4 5 6 7 8 9 Actual -log (Probability)
  • 70. Cross-Entropy: Cost Function cross_entropy = -tf.reduce_sum(y_*tf.log(y)) Prediction 0 1 2 3 4 5 6 7 8 9 Probability 0 1 2 3 4 5 6 7 8 9 Actual -log (Probability)
  • 71. Cross-Entropy: Cost Function cross_entropy = -tf.reduce_sum(y_*tf.log(y)) Prediction 0 1 2 3 4 5 6 7 8 9 Probability 0 1 2 3 4 5 6 7 8 9 Actual -log (Probability)
  • 72. Cross-Entropy: Cost Function cross_entropy = -tf.reduce_sum(y_*tf.log(y)) Prediction 0 1 2 3 4 5 6 7 8 9 Probability 0 1 2 3 4 5 6 7 8 9 Actual -log (Probability) ● Only y’i = 1 and log(yi) matters ● The smaller the log(yi) the lower cost ● The higher the yi the lower the cost
  • 73. Logistic Regression: Prediction ML Logistic Regression Image Probability 0 1 2 3 4 5 6 7 8 9 ML Linear Regression Image 1
  • 74. Tensorflow Graph (Review) 1 Feature y = tf.matmul(x, W) + b W = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1]) 2 Features y = matmul(x, W) + b W = tf.Variable(tf.zeros[2,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 2]) y_ = tf.placeholder(tf.float, [None, 1])
  • 75. Tensorflow Graph (Review 2) 1 Feature y = tf.matmul(x, W) + b W = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1]) 2 Features y = matmul(x, W) + b W = tf.Variable(tf.zeros[2,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 2]) y_ = tf.placeholder(tf.float, [None, 1]) scalar scalar.. 1 feature .. ..1coeff.. scalar scalar.. 2 features .. ..2coeffs..
  • 76. Tensorflow Graph (Review 2) 1 Feature y = tf.matmul(x, W) + b W = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1]) 2 Features y = matmul(x, W) + b W = tf.Variable(tf.zeros[2,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 2]) y_ = tf.placeholder(tf.float, [None, 1]) scalar scalar.. 1 feature .. ..1coeff.. scalar scalar.. 2 features .. ..2coeffs.. Apply multi-feature linear regression
  • 77. Logistic Regression: Prediction ML Logistic Regression Image Probability 0 1 2 3 4 5 6 7 8 9 ML Linear Regression Image 1 y = tf.matmul(x,W) + b scalar scalar.. n features .. ..ncoeffs..
  • 78. Logistic Regression: Prediction ML Logistic Regression Image Probability 0 1 2 3 4 5 6 7 8 9 ML Linear Regression Image 1 y = tf.matmul(x,W) + b scalar scalar.. n features .. .. k class .. ..ncoeffs..
  • 79. Logistic Regression: Prediction ML Logistic Regression Image Probability 0 1 2 3 4 5 6 7 8 9 ML Linear Regression Image 1 y = tf.matmul(x,W) + b scalar scalar.. n features .. .. k class .. y = tf.matmul(x,W) + b ..ncoeffs.. .. k class .... n features .. ..ncoeffs.. .. k class ..
  • 80. Tensorflow Graph: Basic, Multi-feature, Multi-class 1 Feature y = tf.matmul(x, W) + b W = tf.Variable(tf.zeros[1,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 1]) y_ = tf.placeholder(tf.float, [None, 1]) 2 Features y = matmul(x, W) + b W = tf.Variable(tf.zeros[2,1]) b = tf.Variable(tf.zeros[1]) x = tf.placeholder(tf.float, [None, 2]) y_ = tf.placeholder(tf.float, [None, 1]) scalar scalar.. 1 feature .. ..1coeff.. scalar scalar.. 2 features .. ..2coeffs.. 2 Features, 10 Classes y = matmul(x, W) + b W = tf.Variable(tf.zeros[2,10]) b = tf.Variable(tf.zeros[10]) x = tf.placeholder(tf.float, [None, 2]) y_ = tf.placeholder(tf.float, [None, 10]) k class k class.. 2 features .. ..2coeffs.. k class
  • 81. Logistic Regression: Prediction ML Logistic Regression Image Probability 0 1 2 3 4 5 6 7 8 9 Probability 0 1 2 3 4 5 6 7 8 9 Actual Compare Great but….sum of all prediction ‘probability’ NOT 1
  • 83. ML Logistic Regression Image Value 0 1 2 3 4 5 6 7 8 9 exp(Value) 0 1 2 3 4 5 6 7 8 9
  • 84. ML Logistic Regression Image Value 0 1 2 3 4 5 6 7 8 9 exp(Value) 0 1 2 3 4 5 6 7 8 9 SUM
  • 85. ML Logistic Regression Image Value 0 1 2 3 4 5 6 7 8 9 exp(Value) 0 1 2 3 4 5 6 7 8 9 exp(Value) 0 1 2 3 4 5 6 7 8 9 SUM SUM
  • 86. ML Logistic Regression Image Value 0 1 2 3 4 5 6 7 8 9 exp(Value) 0 1 2 3 4 5 6 7 8 9 exp(Value) 0 1 2 3 4 5 6 7 8 9 SUM SUM sum of all prediction ‘probability’ is 1
  • 87. Before softmax y = tf.matmul(x, W) + b
  • 88. With softmax y = tf.nn.softmax(tf.matmul(x, W) + b) y = tf.matmul(x, W) + b
  • 89. Summary ● Cheat sheet: Single feature, Multi-feature, Multi-class ● Logistic regression: ○ Multi-class prediction: Ensure that prediction is one of a discrete set of values ○ Cross-entropy: Measure difference between multi-class prediction and actual ○ Softmax: Ensure the multi-class prediction probability is a valid distribution (sum = 1)
  • 90. Congrats! You can now understand Google’s Tensorflow Beginner’s Tutorial (https://www.tensorflow.org/versions/r0.7/tutorials/mnist/beginners/index.html)
  • 91. References ● Perform ML with TF using multi-feature linear regression (the wrong way) ○ https://github. com/nethsix/gentle_tensorflow/blob/master/code/linear_regression_multi_feature_using_mini_ batch_with_tensorboard.py ● Perform ML with TF using multi-feature linear regression ○ https://github. com/nethsix/gentle_tensorflow/blob/master/code/linear_regression_multi_feature_using_mini_ batch_without_matrix_with_tensorboard.py ● Tensorflow official tutorial for character recognition ○ https://www.tensorflow.org/versions/r0.7/tutorials/mnist/beginners/index.html ● Colah’s excellent explanation of cross-entropy ○ http://colah.github.io/posts/2015-09-Visual-Information/