MODULE 1
Fundamentals of Deep Learning
N
Why Do We Need Backpropagation?
The Complexity of Computational Graphs
∗ Difficult to differentiate!
Recursive Nesting is Ugly!
1
f (g(w)) =
)i |
(1)
1
1 + exp h−1+
exp(−w
– Increasing path length increases recursive nesting.
Backpropagation along Single Path (Univariate Chain Rule)
w
INPUT WEIGHT
g(w)=w2
f(y)=
cos(y)
O = f(g(w))=cos(w2)
OUTPUT
y=g(w)
= ·
derivatives.
∂f(g(w)) ∂f(y) ∂g(w)
= −2w · sin(
y) = −2
2
w · sin(w )
∂w ∂y ∂w
` ˛¸ x
−` si˛n¸(yx)
2w
Backpropagation along Multiple Paths (Multivariate Chain Rule)
k
∂f (g1(w), ...g (w))
k
Σ
=
∂w
i=1
∂f (g1(w), ...gk(w))
∂gi(w)
·
∂gi(w)
∂w
(2)
Example of Multivariable Chain Rule
g(y)=
cos(y)
O = [cos(w2)] + [sin(w2)]
w
INPUT WEIGHT
f(w)=w2
K(p,q)
=p+q
O
OUTPUT
h(z)=
sin(z)
=
∂o ∂K(p, q)
∂w
∗ ∗
∂K(p, q)
· g (y ) · f (
∂p
` ˛¸ x
-`si˛n¸(yx)
` 2˛w¸ x
w) +
∗
∗
· h (z) · f (
w)
∂q
` ˛¸ x
c`o˛s¸(zx)
` 2˛w¸ x
1
1
= −2w · sin(y)+ 2w · cos(z)
= −2w · sin(w2)+ 2w · cos(w2)
Pathwise Aggregation Lemma
– Local gradient of node with variable y(j) with respect to
variable y(i) for directed edge (i, j) is z(i, j) = ∂y(j)
∂y(i)
∂o
∂w
is given by computing the product of the
local gradients along each path in P, and summing these
products over all paths.
∂o =
∂w
Σ Y
z(i, j)
(3)
P ∈P (i,j)∈P
An Exponential Time Algorithm for Computing Partial Derivatives
– Use computational graph to compute each value y(i) of nodes i in a forward phase.
∂y(i)
– Compute local derivative z(i, j) = ∂y(j) on each edge (i, j)
in the network.
w to the output o.
Q(i,j)∈P z(i, j) of the local derivatives on that path.
Example: Deep Computational Graph with Product Nodes
w
INPUT WEIGHT
h(1,1) h(2,1)
w w2
h(3,1)
w4
h(4,1)
w8
h(5,1)
w16
O=w32
O
OUTPUT
w w2
h(1,2) h(2,2)
w4
h(3,2)
w8
h(4,2)
w16
h(5,2)
EACH NODE COMPUTES THE PRODUCT OF ITS INPUTS
Example of Increasing Complexity with Depth
w
INPUT WEIGHT
h(1,1) h(2,1)
w w2
h(3,1)
w4
h(4,1)
w8
h(5,1)
w16
O=w32
O
OUTPUT
w w2
h(1,2) h(2,2)
w4
h(3,2)
w8
h(4,2)
w16
h(5,2)
EACH NODE COMPUTES THE PRODUCT OF ITS INPUTS
∂O =
∂w
Σ
1 2 3 4 5
j ,j ,j ,j ,j ∈{1,2}
5
` ˛¸ x
Y h(1, j1) h (2, j2) h (3, j3) h (4, j4) h (5, j5)
` w˛¸2 x ` w˛¸4 x ` w˛¸8 x ` w˛1¸6 x
w
Σ
=
w31 = 32w31
All 32 paths
Observations on Exponential Time Algorithm
Differentiating Composition Functions
repetitiveness caused by a node appearing in multiple paths.
f (w) = sin(w2)+ cos(w2)
1
3
5
7
9
w INPUT WEIGHT
2 4 6 8 10
11
OUTPUT
O
y(4) y(6)
z(4, 6)
EACH NODE i CONTAINS y(i) AND EACH EDGE BETWEEN i AND j CONTAINS z(i, j)
EXAMPLE: z(4, 6)= PARTIAL DERIVATIVE OF y(6) WITH RESPECT TO y(4)
∂y(j)
∂o
Σ
Q
∂w
=
P ∈P
(i,j)∈P
z(i, j)
by explicit aggregation over all paths in P.
Dynamic Programming and Directed Acyclic Graphs
Dynamic Programming Update
Σ
S(i, o) ⇐ S(j, o) · z(i, j)
(4)
j∈A(i)
– Intermediate computations like S(i, o) are also useful for computing derivatives in other layers.
Σ
·
∂y(j)
j∈A(i)
∂o = ∂o ∂y
∂y(i)
(j)
∂y(i)
X {
How Does it Apply to Neural Networks?
∑ ɸ
h=ɸ(W.X)
BREAK UP
X {
∑
ɸ
∑
ɸ
PRE-ACTIVATION VALUE
ah = W.X
W
W
h=ɸ(ah)
POST-ACTIVATION VALUE
Pre-Activation Variables to Create Computational Graph
– Loss derivative with respect to weight wij from node i to node j is given by the product of δ(j, o) and hidden variable at i (why?)
∗
ij
i
·Φ , Initialize
S(o, o) = δ(
o, o) =
∂L
∂o
Φ
∗
o
Σ
Σ
δ(i, o) = S(i, o) = Φ∗i
wijS(j, o) = Φ∗i
wijδ(j, o)
j∈A(i)
j∈A(i)
(5)
Post-Activation Variables to Create Computation Graph
after activation function application.
ij
Δ(i, o) = S(i, o) =
∗
j
· Φ , Initialize
S(o, o) = Δ(o, o) =
∂L
∂o
Σ
Σ
wijS(j, o)Φ∗j =
j∈A(i)
j∈A(i)
wijΔ(j, o)Φ∗j
(6)
Variables for Both Pre-Activation and Post-Activation Values
– Update can be computed in vector and matrix multiplica-tions.
Losses at Arbitrary Nodes
– Similar treatment as the initialization of an output node, except that we add the contribution to existing value of S(i, o).
Handling Shared Weights
∂L
∂w
Σ
=
·
∂L ∂w
i
=
r r
Σ
∂L
(7)
∂w
i=1 ∂wi i=1 ∂wi
Multiple Computational Graphs from Same Neural Network
APPLY ɸ (ELEMENTWISE)
Effect on Linear Layer and Activation Functions
MULTIPLY WITH WT
DECOUPLED LAYER (i-1)
LINEAR TRANSFORM
ACTIVATION
SOME FUNCTION
DECOUPLED LAYER i
MULTIPLY WITH W
MULTIPLY ɸ’(ELEMENTWISE)
DECOUPLED LAYER (i+1)
DECOUPLED LAYER (i+2)
SOME FUNCTION
DECOUPLED LAYER (i+3)
SOME LOSS
matrix for linear layer.
Table of Forward Propagation and Backward Propagation
Function | Forward | Backward |
Linear | zi+1 = WT zi | gi = Wgi+1 |
Sigmoid | zi+1 =sigmoid(zi) | gi = gi+1 ⊙ zi+1 ⊙ (1 − zi+1) |
Tanh | zi+1 =tanh(zi) | gi = gi+1 ⊙ (1 − zi+1 ⊙ zi+1) |
ReLU | zi+1 = zi ⊙ I(zi > 0) | gi = gi+1 ⊙ I(zi > 0) |
Hard Tanh | Set to ±1 (/∈ [−1, +1]) Copy (∈ [−1, +1]) | Set to 0 (/∈ [−1, +1]) Copy ( ∈ [−1, +1]) |
Max | Maximum of inputs | Set to 0 (non-maximal inputs) Copy (maximal input) |
Arbitrary function fk(·) | z(k) = fk(zi) i+1 | gi = JT gi+1 J is Jacobian (Equation 10) |
How to Check Correctness of Backpropagation
∂L(w)
∂w
L(w + є) − L(w)
≈
(12)
є
What Does “Closely Enough” Mean?
ρ = |Ge − Ga|
(13)
|Ge + Ga|
Stochastic Gradient Descent
Mini-batch Stochastic Gradient Descent
Why Does Mini-Batching Work?
Feature Normalization
Transform the data to a de-correlated axis
system with principal component analysis (mean-centered SVD).
Weight Initialization
Symmetry Breaking
Sensitivity to Number of Inputs
Tuning Hyperparameters
Grid Search
How to Select Values for Each Parameter
– Not the best approach in many cases! intervals.
⇒ Log-uniform
Sampling versus Grid Search
Large-Scale Settings
Revisiting Feature Normalization
– Exact behavior depends on target variable and loss func-tion.
The Vanishing and Exploding Gradient Problems
Example
h1
h2
hm-1
x
w1
∑
∑
w2
w3
wm-1
∑
wm
∑
o
Activation Function Propensity to Vanishing Gradients
2
has a maximum value of 1 at o = 0, but saturation will still cause problems.
Exploding Gradients
Cliffs
30
25
20
15
GENTLE GRADIENT BEFORE CLIFF OVERSHOOTS
LOSS
1.4
1.2
1
0.8
0.6
0.4
0.2
0
0
5
10
15
10
20
5
25
300
x
y
A Partial Fix to Vanishing Gradients
– Neuron is permanently dead!
Leaky ReLU
– At the reduced rate of α < 1 times the learning case for nonnegative inputs:
v ≤ 0
Φ(v) = ⎧⎨α · v
(14)
⎩v
otherwise
Maxout
Gradient Clipping for Exploding Gradients
Other Comments on Vanishing and Exploding Gradients
First-Order Descent
Learning Rate Decay
and inverse decay.
αt = α0 exp(−k · t) [Exponential Decay]
α0
αt =
[Inverse Decay]
1+ k · t
The parameter k controls the rate of the decay.
Momentum Methods: Marble Rolling Down Hill
LOSS
GD SLOWS DOWN
IN FLAT REGION
GD GETS TRAPPED IN LOCAL OPTIMUM
VALUE OF NEURAL NETWORK PARAMETER
∂L
V ⇐ βV − α∂W ;
W ⇐ W + V
Avoiding Zig-Zagging with Momentum
STARTING POINT
WITH MOMENTUM
WITHOUT
MOMENTUM
STARTING
POINT
OPTIMUM
(b) WITHOUT MOMENTUM
STARTING POINT
OPTIMUM
(a) RELATIVE DIRECTIONS
(c) WITH MOMENTUM
Nesterov Momentum
V ⇐ βV −α
Mom`˛e¸nx tum
∂L(W + βV )
; W ⇐ W + V
∂W
AdaGrad
– The absolute value will increase over time.
∂L
!
2
Ai ⇐ Ai +
∀i
(15)
∂w
i
w
i
i
⇐ w − √A
i
α ∂L
!
; ∀i
(16)
∂wi
AdaGrad Intuition
relative movements along gently sloping directions.
RMSProp
Ai ⇐ ρAi + (1 − ρ)
∂L !
2
∀i
(17)
∂w
i
w
i
i
⇐ w −
√Ai
α
∂L
!
; ∀i
∂wi
RMSProp with Nesterov Momentum
v
i
i
⇐ βv − √A
i
α ∂L(W + βV )
!
∂wi
i
; wi ⇐ wi + v ∀i
Ai ⇐ ρAi + (1 − ρ)
∂L(W + βV )!
2
∀i (18)
∂w
i
AdaDelta and Adam
Why Second-Order Methods?
30
25
20
15
GENTLE GRADIENT BEFORE CLIFF OVERSHOOTS
LOSS
1.4
1.2
1
0.8
0.6
0.4
0.2
0
0
5
10
15
10
20
5
25
300
x
y
Revisiting the Bowl
40
40
30
VALUE OF y
30
20
20
10
10
0
VALUE OF y
0
−10
−10
−20
−20
−30
−30
−40
−40
−30
−20
−10
0
VALUE OF x
10
20
30
40
−40
−40
−30
−20
−10
0
VALUE OF x
10
20
30
40
(a) Loss function is circular bowl
L = x2 + y2
(b) Loss function is elliptical bowl
L = x2 + 4y2
A Valley
5
0
LEAST
CURVATURE DIRECTION
f(x, y)
4
3
2
1
0
−1
1
0.5
1
−1
−2
2
0
−0.5
y
−1
x
The Hessian
∂2L(W )
Hij =
∂wi∂wj
Hessian matrix H, for which the (i, j)th entry is Hij.
Quadratic Approximation of Loss Function
1
2
L(W ) ≈ L(W 0)+(W −W 0)T [∇L(W 0)]+ (
T
0
W −W ) H(
0
W −W )
(19)
Newton’s Update
∇L(W ) = 0 [Gradient of Loss Function]
∇L(W 0)+ H(W − W 0) = 0 [Gradient of Taylor approximation]
W ∗ ⇐ W 0 − H−1[∇L(W 0)] (20)
Why Second-Order Methods?
Basic Second-Order Algorithm and Approximations
Conjugate Gradient Method
T
qt+1 = −∇L(Wt+1)+
t
q H[∇L(W
t+1
)]
!
qt (21)
t
qT Hqt
Conjugate Gradients on 2-Dimensional Quadratic
Conjugate Gradient Algorithm
– Update Wt+1 ⇐ Wt + αtqt. Here, the step size αt is com-puted using line search.
T
t+1
t+1
– Set q = −∇L(W ) +
t
q H[∇L(W
t+1
)]
t
qT Hqt
t
q . Increment
t by 1.
Efficiently Computing Projection of Hessian
T
qt+1
t+1
= −∇L(W )+
t
q H[∇L(W
t+1
)]
!
qt
(22)
t
qT Hqt
∇L(W
Hv ≈
0
+ δv) − ∇
0
L(W )
(23)
δ
Other Second-Order Methods
which stands for the algorithm and its lim-
Problems with Second-Order Methods
1
0
−0.2
−0.4
−0.6
−0.8
−1
−1
−0.8
−0.6
−0.4
−0.2
0.2
0.4
0.6
0.8
1
1
0.8
0.6
0.4
0.2
f(x)
0.8
0
SADDLE POINT
0.6
0.4
0.2
g(x, y)
0
−0.2
−0.4
−0.6
−0.8
−1
1
0.5
0
−0.5
0.5
−0.5
1
−1
0
x
y
−1
x
(a) f (x) = x3
Degenerate
(b) f (x) = x2 − y2
Stationary
Revisiting the Vanishing and Exploding Gradient Problems
h1 h2
hm-1
x
w1
∑ ∑
w2
w3
wm-1
∑
wm
∑
o
Revisiting the Bowl
40
40
30
VALUE OF y
30
20
20
10
10
0
VALUE OF y
0
−10
−10
−20
−20
−30
−30
−40
−40
−30
−20
−10
0
VALUE OF x
10
20
30
40
−40
−40
−30
−20
−10
0
VALUE OF x
10
20
30
40
(a) Loss function is circular bowl
L = x2 + y2
(b) Loss function is elliptical bowl
L = x2 + 4y2
Input Shift
Solution: Batch Normalization
∑ ɸ
∑ ɸ
ADD BATCH NORMALIZATION
BREAK UP
∑ ɸ
BN
∑
ɸ
∑
vi
ai
ɸ
BN
Batch Normalization Node
Σ
m
v(r)
μi = r=1 i
∀i [Batch Mean]
m
Σ
m
(r)
2
σi
i
(v − μ )
2
m
= r=1 i + є
(r)
(r)
v − μ
ˆvi
i
= i
σi
∀i, r
∀i [Batch Variance]
[Normalize Batch Instances]
(r) (r)
ai = γi · ˆvi + βi ∀i, r
[Scale with Learnable Parameters]
– Most activations will be near zero (near-linear regime).
Changes to Backpropagation
Issues in Inference
test instance is available?
– One can also maintain exponentially weighted averages during training.
Batch Normalization as Regularizer