Professional Documents
Culture Documents
DeepLearning.AI makes these slides available for educational purposes. You may not use or distribute
these slides for commercial purposes. You may make copies of these slides and use or distribute them for
educational purposes as long as you cite DeepLearning.AI as the source of the slides.
Welcome!
Advanced learning algorithms
Neural Networks
inference (prediction)
training
Practical advice for building machine learning systems
Decision Trees
Andrew Ng
Neural Networks Intuition
Andrew Ng
Neurons in the brain
Andrew Ng
Why Now?
large neural network
traditional AI
amount of data
Andrew Ng
Neural Network Intuition
Demand Prediction
Demand Prediction
𝑥 = 𝑝𝑟𝑖𝑐𝑒 input
1 output
𝑎=𝑓 𝑥 =
top seller? 1 + 𝑒 − 𝑤𝑥+𝑏
yes/no
𝑥 𝑎
price
Andrew Ng
Demand Prediction
layer
price
layer
shipping cost
probability of
marketing being a top seller
material
Andrew Ng
Demand Prediction
layer
price
layer
shipping cost a a
probability of
marketing being a top seller
material
Andrew Ng
Demand Prediction
layer
price
layer
shipping cost a a
probability of
marketing being a top seller
material
Andrew Ng
Multiple hidden layers
a a
x
x
input
output
layer
hidden hidden
layer layer hidden hidden
hidden
layer layer layer
Example:
Recognizing Images
Face recognition
x=
Andrew Ng
Face recognition
output layer
⋮ probability of being
⋮ ⋮
person ‘XYZ’
x
input
source: Convolutional Deep Belief Networks for Scalable Unsupervised Learning of Hierarchical Representations
by Honglak Lee, Roger Grosse, Ranganath Andrew Y. Ng
Andrew Ng
Car classification
output layer
⋮ probability of
⋮ ⋮
car detected
source: Convolutional Deep Belief Networks for Scalable Unsupervised Learning of Hierarchical Representations
by Honglak Lee, Roger Grosse, Ranganath Andrew Y. Ng
Andrew Ng
Neural network model
Andrew Ng
Neural network layer 1
𝑔(𝑧) =
1 + 𝑒− 𝑧
a[1]
x [1] [1] [1]
𝑤1 , 𝑏1 𝑎1 = 𝑔(w1 ∙ x + 𝑏1 )
vector of
layer 0 layer 2 activation
[1] values from
w2 , 𝑏2 𝑎2 = 𝑔(w2[1] ∙ x + 𝑏2[1] ) [1] layer 1
x a
layer 1
w3 , 𝑏3 𝑎3 = 𝑔(w3 ∙ x + 𝑏3 )
Andrew Ng
Neural network layer 1
𝑔(𝑧) =
1 + 𝑒− 𝑧
a[1] 𝑎 [2]
x
layer 1
Andrew Ng
Neural network layer
predict category 1 or 0 (yes/no)
a[1] 𝑎 [2]
x
2
𝑖𝑠 𝑎 ≥ 0.5?
layer 2
layer 1 𝑦ො = 1 𝑦ො = 0
Andrew Ng
Neural Network Model
layer 0
Andrew Ng
More complex neural network
[3] [3] [3]
[3]
𝑤1 , 𝑏1
[3] 𝑎1 = 𝑔(w1 ∙ a[2] + 𝑏1 )
[3] [3] [3]
a[1] a[2] a[3] a[4]
[3]
a[2] w2 , 𝑏2
[3]
𝑎2 = 𝑔(w2 ∙ a[2] + 𝑏2 ) a[3]
x
input [3] [3] [3] [3] [3]
w3 , 𝑏3 𝑎3 = 𝑔(w3 ∙ a[2] + 𝑏3 )
Andrew Ng
Notation
[3] [3] [3]
[3]
𝑤1 , 𝑏1
[3] 𝑎1 = 𝑔(w1 ∙ a[2] + 𝑏1 )
[3] [3] [3]
x a[1] a[2] a[3] a[4]
[3]
a[2] w2 , 𝑏2
[3]
𝑎2 = 𝑔(w2 ∙ a[2] + 𝑏2 ) a[3]
input
[3] [3] [3] [3] [3]
w3 , 𝑏3 𝑎3 = 𝑔(w3 ∙ a[2] + 𝑏3 )
Andrew Ng
Notation
[3] [3] [3]
[3]
𝑤1 , 𝑏1
[3] 𝑎1 = 𝑔(w1 ∙ a[2] + 𝑏1 )
x
[3] [3] [3]
a[1] a[2] a[3] a[4]
[3]
a[2] w2 , 𝑏2
[3]
𝑎2 = 𝑔(w2 ∙ a[2] + 𝑏2 ) a[3]
x
input
[3] [3] [3] [3] [3]
w3 , 𝑏3 𝑎3 = 𝑔(w3 ∙ a[2] + 𝑏3 )
layer 3 layer 4
layer 1 layer 2
[3]
𝑎2 = 𝑔(w2[3] ∙ a[2] + 𝑏2[3] )
output of layer 𝑙 − 1
Activation value of (previous layer)
layer 𝑙, unit(neuron) 𝑗 [𝑙]
𝑎𝑗 = 𝑔(w𝑗[𝑙] ∙ a[𝑙−1] + 𝑏𝑗[𝑙] )
sigmoid
Parameters w & b of layer 𝑙, unit 𝑗
Andrew Ng
Neural Network Model
label 0 1
Andrew Ng
Handwritten digit recognition
Digit images
label 0 1
Andrew Ng
Handwritten digit recognition
forward propagation
Andrew Ng
TensorFlow implementation
Inference in Code
Coffee roasting
x a[1] a[2]
Duration 2
(minutes) 𝑖𝑠 𝑎1 ≥ 0.5?
𝑦ො = 1 𝑦ො = 0
Temperature
(Celsius)
Andrew Ng
x a[1] a[2]
x = np.array([[200.0, 17.0]])
layer_1 = Dense(units=3, activation=‘sigmoid’)
a1 = layer_1(x)
Andrew Ng
Build the model using TensorFlow
x a[1] a[2]
x = np.array([[200.0, 17.0]])
layer_1 = Dense(units=3, activation=‘sigmoid’)
a1 = layer_1(x)
Andrew Ng
Build the model using TensorFlow
x a[1] a[2]
2
𝑖𝑠 𝑎1 ≥ 0.5?
𝑦ො = 1 𝑦ො = 0
x = np.array([[200.0, 17.0]])
layer_1 = Dense(units=3, activation=‘sigmoid’)
a1 = layer_1(x) if a2 >= 0.5:
yhat = 1
else:
yhat = 0
layer_2 = Dense(units=1, activation=‘sigmoid’)
a2 = layer_2(a1)
Andrew Ng
Model for digit classification
x = np.array([[0.0,...245,...240...0]])
layer_1 = Dense(units=25, activation=‘sigmoid’)
a1 = layer_1(x)
x ⋮ a[1] a[2] a[3]
⋮
1 unit
15 units
25 units
Andrew Ng
Model for digit classification
x = np.array([[0.0,...245,...240...0]])
layer_1 = Dense(units=25, activation=‘sigmoid’)
a1 = layer_1(x)
x ⋮ a[1] a[2] a[3] layer_2 = Dense(units=15, activation=‘sigmoid’)
⋮ a2 = layer_2(a1)
1 unit
15 units
25 units
Andrew Ng
Model for digit classification
x = np.array([[0.0,...245,...240...0]])
layer_1 = Dense(units=25, activation=‘sigmoid’)
a1 = layer_1(x)
x ⋮ a[1] a[2] a[3] layer_2 = Dense(units=15, activation=‘sigmoid’)
⋮ a2 = layer_2(a1)
Andrew Ng
Model for digit classification
x = np.array([[0.0,...245,...240...0]])
layer_1 = Dense(units=25, activation=‘sigmoid’)
a1 = layer_1(x)
x ⋮ a[1] a[2] a[3] layer_2 = Dense(units=15, activation=‘sigmoid’)
⋮ a2 = layer_2(a1)
Andrew Ng
TensorFlow implementation
Data in TensorFlow
Feature vectors
temperature duration Good coffee? x = np.array([[200.0, 17.0]])
(Celsius) (minutes) (1/0)
[[200.0, 17.0]]
200.0 17.0 1
425.0 18.5 0
… … …
Andrew Ng
Note about numpy arrays
x = np.array([[1, 2, 3],
1 2 3 [4, 5, 6]])
4 5 6
[[1, 2, 3],
[4, 5, 6]]
2x3
x = np.array([[0.1, 0.2],
[-3.0, -4.0,], 4x2
[-0.5, -0.6,],
[7.0, 8.0,]]) 1x2
[[0.1, 0.2], 2x1
[-3.0, -4.0,],
[-0.5, -0.6,],
[7.0, 8.0,]]
Andrew Ng
Note about numpy arrays
x = np.array([[200, 17]]) 200 17 1x2
x = np.array([200,17])
Andrew Ng
Feature vectors
temperature duration Good coffee? x = np.array([[200.0, 17.0]])
(Celsius) (minutes) (1/0)
[[200.0, 17.0]]
200.0 17.0 1
425.0 18.5 0 1x2
… … …
200.0 17.0
Andrew Ng
Activation vector
x a[1] a[2]
x = np.array([[200.0, 17.0]])
layer_1 = Dense(units=3, activation=‘sigmoid’)
a1 = layer_1(x)
1 x 3 matrix
tf.Tensor([[0.2 0.7 0.3]], shape=(1, 3), dtype=float32)
a1.numpy()
array([[1.4661001, 1.125196 , 3.2159438]], dtype=float32)
Andrew Ng
Activation vector
x a[1] a[2]
a2.numpy()
array([[0.8]], dtype=float32)
Andrew Ng
TensorFlow implementation
Andrew Ng
Building a neural network architecture
x a[1] a[2] layer_1 = Dense(units=3, activation="sigmoid")
layer_2 = Dense(units=1, activation="sigmoid")
model = Sequential([layer_1, layer_2])
x = np.array([[200.0, 17.0],
y [120.0, 5.0],
4x2
[425.0, 20.0],
200 17 1 [212.0, 18.0])
y = np.array([1,0,0,1])
120 5 0
model.compile(...)
425 20 0 model.fit(x,y)
212 18 1 model.predict(x_new)
Andrew Ng
Digit classification model
layer_1 = Dense(units=25, activation="sigmoid")
layer_2 = Dense(units=15, activation="sigmoid")
layer_3 = Dense(units=1, activation="sigmoid")
x [1] [2] [3]
⋮ a a a
⋮ model = Sequential([layer_1, layer_2, layer_3]
model.compile(...)
1 unit
x = np.array([[0..., 245, ..., 17],
15 units [0..., 200, ..., 184])
25 units y = np.array([1,0])
model.fit(x,y)
model.predict(x_new)
Andrew Ng
Digit classification model
model = Sequential([
Dense(units=25, activation="sigmoid"),
x ⋮ a[1] a[2] a[3]
Dense(units=15, activation="sigmoid"),
Dense(units=1, activation="sigmoid")])
⋮
model.compile(...)
1 unit
x = np.array([[0..., 245, ..., 17],
15 units [0..., 200, ..., 184])
25 units y = np.array([1,0])
model.fit(x,y)
model.predict(x_new)
Andrew Ng
Neural network implementation
in Python
Andrew Ng
Neural network implemenation
in Python
General implementation of
forward propagation
[1]
𝑤1 , 𝑏1
[1]
Forward prop in NumPy
x [1] [1] a [𝑙]
w2 , 𝑏2
def dense(a_in,W,b, g): def sequential(x):
[1] [1]
w3 , 𝑏3 units = W.shape[1] a1 = dense(x,W1,b1,g)
a_out = np.zeros(units) a2 = dense(a1,W2,b2,g)
[1] 1 −3 [1][1] 5 for j in range(units): a3 = dense(a2,W3,b3,g)
𝑤1 = w2 = w3 =
2 4 −6 w = W[:,j] a4 = dense(a3,W4,b4,g)
W = np.array([ z = np.dot(w,a_in) + b[j] f_x = a4
[1, -3, 5] a_out[j] = g(z) return f_x
[2, 4, -6]]) return a_out
[𝑙] [𝑙] [𝑙]
𝑏1 = −1 𝑏2 = 1 𝑏3 = 2
b = np.array([-1, 1, 2])
a [0] = x
a_in = np.array([-2, 4])
Andrew Ng
Speculations on artificial general
intelligence (AGI)
ANI AGI
(artificial narrow intelligence) (artificial general intelligence)
E.g., smart speaker, Do anything a human can do
self-driving car, web search,
AI in farming and factories
Andrew Ng
Biological neuron Simplified mathematical
model of a neuron
inputs outputs inputs outputs
Andrew Ng
Neural network and the brain
Can we mimic the human brain?
vs x 𝑓(x)
Andrew Ng
The ”one learning algorithm” hypothesis
Auditory cortex
Andrew Ng
The ”one learning algorithm” hypothesis
Somatosensory cortex
Andrew Ng
Sensor representations in the brain
Andrew Ng
Vectorization
(optional)
[1,0,1]
Andrew Ng
Vectorization
(optional)
Matrix multiplication
Dot products
Andrew Ng
Vector matrix multiplication
[2]
→ 1
a =
[4 6]
3 5
→ 𝐙= →
a 𝐖 [← →]
𝑻 𝑻
a = [1 2] 𝐖= →
a
𝑻
→
w1 →
w2
𝐙 = [→
a →
𝑻
w 2]
w 1 𝑎𝑻 →
(1 ∗ 3) + (2 ∗ 4) (1 ∗ 5) + (2 ∗ 6)
𝐙 = [11 17]
Andrew Ng
matrix matrix multiplication
[2 − 2]
1 −1
𝐀=
[4 6]
→ 𝑇
[− 1 − 2]
1 2 𝐖 = 3 5 𝐙 = 𝐀𝑻 𝐖 = ← a1 →
→
𝐀𝑻 =
→ 𝑇 w1 →
w2
← a 2 →
→
a 1→
𝑇
w1 →
a 1→
𝑇
w2
=
→
a 2→
𝑇
w1 →
a 2→
𝑇
w2
[− 11 − 17]
11 17
=
Andrew Ng
Vectorization
(optional)
Andrew Ng
Matrix multiplication rules
1 −1 0.1 1 2
𝐀= 𝐀𝑻 = −1 −2 3 5 7 9
2 −2 0.2 𝐖= 𝒁 = 𝐀𝑻 𝐖 =
0.1 0.2 4 6 8 0
Andrew Ng
Matrix multiplication rules
1 −1 0.1 1 2 11 17 23 9
𝐀= 𝐀𝑻 = −1 −2 3 5 7 9
2 −2 0.2 𝐖= 𝑻
𝒁 = 𝐀 𝐖 = −11 −17 −23 −9
0.1 0.2 4 6 8 0
1.1 1.7 2.3 0.9
Andrew Ng
Matrix multiplication rules
1 −1 0.1 1 2 11 17 23 9
𝐀= 𝐀𝑻 = −1 −2 3 5 7 9
2 −2 0.2 𝐖= 𝑻
𝒁 = 𝐀 𝐖 = −11 −17 −23 −9
0.1 0.2 4 6 8 0
1.1 1.7 2.3 0.9
Andrew Ng
Vectorization
(optional)
Andrew Ng
Dense layer vectorized
[1] [1]
𝑤1 , 𝑏1 AT = np.array([[200, 17]])
𝐀𝑻
[1] [1]
[1]
𝐀 𝐀[2] W = np.array([[1, -3, 5],
w2 , 𝑏2
[-2, 4, -6])
[1] [1]
w3 , 𝑏3 b = np.array([[-1, 1, 2]])
def dense(AT,W,b,g):
z = np.matmul(AT,W) + b
𝐀𝑻 = 200 17 𝐙 = 𝐀𝑻 𝐖 + 𝐁
a_out = g(z)
165 −531 900 return a_out
1 −3 5
𝐖= [1] [1] [1]
−2 4 −6 𝑧1 𝑧2 𝑧3 [[1,0,1]]
Ԧ𝐛 = −1 1 2
𝐀 = g(𝐙)
1 0 1
Andrew Ng