SLIDE 1 Multi-Layer Networks and Backpropagation Algorithm
Sharif University of Technology Fall 2017 Most slides have been adapted from Fei Fei Li lectures, cs231n, Stanford 2017 and some from Hinton lectures, “NN for Machine Learning” course, 2015.
SLIDE 2 Reasons to study neural computation
- Neuroscience: To understand how the brain actually works.
– Its very big and very complicated and made of stuff that dies when you poke it around. So we need to use computer simulations.
- AI: To solve practical problems by using novel learning algorithms
inspired by the brain
– Learning algorithms can be very useful even if they are not how the brain actually works.
SLIDE 3
SLIDE 4 A typical cortical neuron
- Gross physical structure:
– There is one axon that branches – There is a dendritic tree that collects input from other neurons.
- Axons typically contact dendritic trees at synapses
– A spike of activity in the axon causes charge to be injected into the post-synaptic neuron.
– There is an axon hillock that generates outgoing spikes whenever enough charge has flowed in at synapses to depolarize the cell membrane.
SLIDE 5 A mathematical model for biological neurons
𝑦1 𝑥1 𝑥1𝑦1 𝑥2𝑦2 𝑥3𝑦3
SLIDE 6 How the brain works
- Each neuron receives inputs from other neurons
- The effect of each input line on the neuron is controlled by a synaptic weight
- The synaptic weights adapt so that the whole network learns to perform useful
computations
– Recognizing objects, understanding language, making plans, controlling the body.
- You have about 1011 neurons each with about 104 weights.
– A huge number of weights can affect the computation in a very short time. Much better bandwidth than a workstation.
SLIDE 7 Be very careful with your brain analogies!
– Many different types – Dendrites can perform complex non-linear computations – Synapses are not a single weight but a complex non-linear dynamical system – Rate code may not be adequate
[Dendritic Computation. London and Hausser]
SLIDE 8 Binary threshold neurons
- McCulloch-Pitts (1943): influenced Von Neumann.
– First compute a weighted sum of the inputs. – send out a spike of activity if the weighted sum exceeds a threshold. – McCulloch and Pitts thought that each spike is like the truth value of a proposition and each neuron combines truth values to compute the truth value of another proposition!
𝑗𝑜𝑞𝑣𝑢1 𝑗𝑜𝑞𝑣𝑢2 𝑗𝑜𝑞𝑣𝑢𝑒 𝑔
𝑗
𝑥𝑗𝑦𝑗 𝑔: Activation function … 𝑔 Σ 𝑥1 𝑥2 𝑥𝑒
SLIDE 9 McCulloch-Pitts neuron: binary threshold
9
- Neuron, unit, or processing element:
𝑦1 𝑦2 𝑦𝑒 𝑧 … 𝑧 = 1, 𝑨 ≥ 𝜄 0, 𝑨 < 𝜄 𝑧 𝜄: activation threshold 𝑥1 𝑥2 𝑥𝑒 𝑦1 𝑦2 𝑦𝑒 𝑧 … 𝑥1 𝑥2 𝑥𝑒 𝑐 1 𝑧
bias: 𝑐 = −𝜄
Equivalent to binary McCulloch-Pitts neuron
SLIDE 10 AND & OR networks
10
SLIDE 11 Sigmoid neurons
- These give a real-valued output that is a smooth and bounded
function of their total input.
- Typically they use the logistic function
– They have nice derivatives.
SLIDE 12 Rectified Linear Units (ReLU)
- They compute a linear weighted sum of their inputs.
- The output is a non-linear function of the total input.
SLIDE 13 Adjusting weights
- Types of single layer networks:
–Perceptron (Rosenblatt, 1962) –ADALINE (Widrow and Hoff, 1960)
SLIDE 14 The standard Perceptron architecture
- Learn how to weight each of the
feature activations to get desirable
- utputs.
- If output is above some threshold,
decide that the input vector is a positive example of the target class.
SLIDE 15 The perceptron convergence procedure
- Perceptron trains binary output neurons as classifiers
- Pick training cases (until convergence):
– If the output unit is correct, leave its weights alone. – If the output unit incorrectly outputs a zero, add the input vector to it. – If the output unit incorrectly outputs a 1, subtract the input vector from it.
- This is guaranteed to find a set of weights that gets the right answer
for all the training cases if any such set exists.
SLIDE 16 Adjusting weights
16
- Weight update for a training pair (𝒚 𝑜 , 𝑧(𝑜)):
– Perceptron: If 𝑡𝑗𝑜(𝒙𝑈𝒚(𝑜)) ≠ 𝑧(𝑜) then ∆𝒙 = 𝒚(𝑜)𝑧(𝑜) else ∆𝒙 = 𝟏 – ADALINE: ∆𝒙 = 𝜃(𝑧(𝑜) − 𝒙𝑈𝒚(𝑜))𝒚(𝑜)
- Widrow-Hoff, LMS, or delta rule
𝒙𝑢+1 = 𝒙𝑢 − 𝜃𝛼𝐹𝑜 𝒙𝑢 𝐹𝑜 𝒙 = 𝑧(𝑜) − 𝒙𝑈𝒚(𝑜) 2
SLIDE 17
How to learn the weights: multi class example
SLIDE 18 How to learn the weights: multi class example
- If correct: no change
- If wrong:
– lower score of the wrong answer (by removing the input from the weight vector of the wrong answer) – raise score of the target (by adding the input to the weight vector of the target class)
SLIDE 19 How to learn the weights: multi class example
- If correct: no change
- If wrong:
– lower score of the wrong answer (by removing the input from the weight vector of the wrong answer) – raise score of the target (by adding the input to the weight vector of the target class)
SLIDE 20 How to learn the weights: multi class example
- If correct: no change
- If wrong:
– lower score of the wrong answer (by removing the input from the weight vector of the wrong answer) – raise score of the target (by adding the input to the weight vector of the target class)
SLIDE 21 How to learn the weights: multi class example
- If correct: no change
- If wrong:
– lower score of the wrong answer (by removing the input from the weight vector of the wrong answer) – raise score of the target (by adding the input to the weight vector of the target class)
SLIDE 22 How to learn the weights: multi class example
- If correct: no change
- If wrong:
– lower score of the wrong answer (by removing the input from the weight vector of the wrong answer) – raise score of the target (by adding the input to the weight vector of the target class)
SLIDE 23 How to learn the weights: multi class example
- If correct: no change
- If wrong:
– lower score of the wrong answer (by removing the input from the weight vector of the wrong answer) – raise score of the target (by adding the input to the weight vector of the target class)
SLIDE 24 Single layer networks as template matching
- Weights for each class as a template (or sometimes also called a
prototype) for that class.
– The winner is the most similar template.
- The ways in which hand-written digits vary are much too complicated
to be captured by simple template matches of whole shapes.
- To capture all the allowable variations of a digit we need to learn the
features that it is composed of.
SLIDE 25 The history of perceptrons
- They were popularised by Frank Rosenblatt in the early 1960’s.
– They appeared to have a very powerful learning algorithm. – Lots of grand claims were made for what they could learn to do.
- In 1969, Minsky and Papert published a book called “Perceptrons”
that analyzed what they could do and showed their limitations.
– Many people thought these limitations applied to all neural network models.
SLIDE 26 What binary threshold neurons cannot do
- A binary threshold output unit cannot even tell if two single bit
features are the same!
- A geometric view of what binary threshold neurons cannot do
- The positive and negative cases cannot be separated by a plane
SLIDE 27 What binary threshold neurons cannot do
- Positive cases (same): (1,1)->1; (0,0)->1
- Negative cases (different): (1,0)->0; (0,1)->0
- The four input-output pairs give four inequalities that are impossible
to satisfy:
– w1 +w2 ≥θ – 0 ≥θ – w1 <θ – w2 <θ
SLIDE 28 Discriminating simple patterns under translation with wrap-around
- Suppose we just use pixels as the
features.
decision unit cannot discriminate patterns with the same number of on pixels
– if the patterns can translate with wrap- around!
SLIDE 29 Sketch of a proof
- For pattern A, use training cases in all possible translations.
– Each pixel will be activated by 4 different translations of pattern A. – So the total input received by the decision unit over all these patterns will be four times the sum of all the weights.
- For pattern B, use training cases in all possible translations.
– Each pixel will be activated by 4 different translations of pattern B. – So the total input received by the decision unit over all these patterns will be four times the sum of all the weights.
- But to discriminate correctly, every single case of pattern A must provide
more input to the decision unit than every single case of pattern B.
- This is impossible if the sums over cases are the same.
SLIDE 30 Networks with hidden units
- Networks without hidden units are very limited in the input-output
mappings they can learn to model.
– More layers of linear units do not help. Its still linear. – Fixed output non-linearities are not enough.
- We need multiple layers of adaptive, non-linear hidden units. But
how can we train such nets?
SLIDE 31 Feed-forward neural networks
- Also called Multi-Layer Perceptron (MLP)
SLIDE 32 General approximator
32
- If the decision boundary is smooth, then a 3-layer network (i.e. 2
hidden layer) can come arbitrarily close to the target classifier
SLIDE 33 33
MLP with Different Number of Layers
Structure Type of Decision Regions Interpretation Example of region Single Layer (no hidden layer) Half space Region found by a hyper-plane Two Layer (one hidden layer) Polyhedral (open or closed) region Intersection of half spaces Three Layer (two hidden layers) Arbitrary regions Union of polyhedrals MLP with unit step activation function Decision region found by an output unit.
SLIDE 34
Beyond linear models
SLIDE 35
Beyond linear models
SLIDE 36 MLP with single hidden layer
36
- Two-layer MLP (Number of layers of adaptive weights is counted)
𝑝𝑙 𝒚 = 𝜔
𝑘=0 𝑁
𝑥
𝑘𝑙 [2]𝑨 𝑘
⇒ 𝑝𝑙 𝒚 = 𝜔
𝑘=0 𝑁
𝑥
𝑘𝑙 [2]𝜚 𝑗=0 𝑒
𝑥𝑗𝑘
[1]𝑦𝑗
… … … Input Output 𝑦0 = 1 𝑦𝑒 𝑝1 𝑝𝐿 𝑥
𝑘𝑙 [2]
𝑥𝑗𝑘
[1]
𝜚 𝜔 𝑨0 = 1 𝑨1 𝑨𝑁 𝑨𝑘 𝜔 𝜚 𝜚 𝑦1 𝑗 = 0, … , 𝑒 𝑘 = 1 … 𝑁 𝑘 = 1 … 𝑁 𝑙 = 1, … , 𝐿
SLIDE 37 learns to extract features
37
- MLP with one hidden layer is a generalized linear model:
– 𝑝𝑙(𝒚) = 𝜔 𝑘=1
𝑁
𝑥
𝑘𝑙 [2]𝑔
𝑘(𝒚)
– 𝑔
𝑘 𝒚 = 𝜚 𝑗=0 𝑒
𝑥
𝑘𝑗 [1]𝑦𝑗
– The form of the nonlinearity (basis functions 𝑔
𝑘) is adapted from the training data
(not fixed in advance)
𝑘 is defined based on parameters which can be also adapted during training
- Thus, we don’t need expert knowledge or time consuming tuning of
hand-crafted features
SLIDE 38 Deep networks
- Deeper networks (with multiple hidden layers) can work better than a
single-hidden-layer networks is an empirical observation
– despite the fact that their representational power is equal.
- In practice usually 3-layer neural networks will outperform 2-layer
nets, but going even deeper may not help much more.
– This is in stark contrast to Convolutional Networks
SLIDE 39 How to adjust weights for multi layer networks?
- We need multiple layers of adaptive, non-linear hidden units. But
how can we train such nets?
– We need an efficient way of adapting all the weights, not just the last layer. – Learning the weights going into hidden units is equivalent to learning features. – This is difficult because nobody is telling us directly what the hidden units should do.
SLIDE 40
SLIDE 41 Gradient descent
- We want 𝛼𝑿𝑀 𝑿
- Numerical gradient:
– slow :( – approximate :( – easy to write :)
– fast :) – exact :) – error-prone :(
- In practice: Derive analytic gradient, check your implementation with
numerical gradient
SLIDE 42 Training multi-layer networks
42
– Training algorithm that is used to adjust weights in multi-layer networks (based on the training data) – The backpropagation algorithm is based on gradient descent – Use chain rule and dynamic programming to efficiently compute gradients
SLIDE 43
Computational graphs
SLIDE 44
Backpropagation: a simple example
SLIDE 45
Backpropagation: a simple example
SLIDE 46
Backpropagation: a simple example
SLIDE 47
Backpropagation: a simple example
SLIDE 48
Backpropagation: a simple example
SLIDE 49
Backpropagation: a simple example
SLIDE 50
Backpropagation: a simple example
SLIDE 51
How to propagate the gradients backward
SLIDE 52
How to propagate the gradients backward
SLIDE 53
Another example
SLIDE 54
Another example
SLIDE 55
Another example
SLIDE 56
Another example
SLIDE 57
Another example
SLIDE 58
Another example
SLIDE 59
Another example
SLIDE 60
Another example
SLIDE 61
Another example
SLIDE 62
Another example
SLIDE 63
Another example
SLIDE 64
Another example
SLIDE 65
Another example
SLIDE 66 Another example
[local gradient] x [upstream gradient] x0: [2] x [0.2] = 0.4 w0: [-1] x [0.2] = -0.2
SLIDE 67
Derivative of sigmoid function
SLIDE 68
Derivative of sigmoid function
SLIDE 69 Patterns in backward flow
- add gate: gradient distributor
- max gate: gradient router
SLIDE 70
Gradients add at branches
SLIDE 71 Simple chain rule
SLIDE 72
Multiple paths chain rule
SLIDE 73
Modularized implementation: forward / backward API
SLIDE 74
Modularized implementation: forward / backward API
SLIDE 75
Modularized implementation: forward / backward API
SLIDE 76
Caffe Layers
SLIDE 77
Caffe sigmoid layer
SLIDE 78 Output as a composite function
𝑃𝑣𝑢𝑞𝑣𝑢 = 𝑏[𝑀] = 𝑔 𝑨[𝑀] = 𝑔 𝑋[𝑀]𝑏[𝑀−1] = 𝑔 𝑋[𝑀]𝑔(𝑋[𝑀−1]𝑏[𝑀−2] = 𝑔 𝑋[𝑀]𝑔 𝑋[𝑀−1] … 𝑔 𝑋[2]𝑔 𝑋[1]𝑦
For convenience, we use the same activation functions for all layers. However, output layer neurons most commonly do not need activation function (they show class scores or real-valued targets.)
𝑋[1] 𝑦 × 𝑔 𝑋[2] × 𝑔 𝑋[𝑀] × 𝑔 𝑨[1] 𝑏[1] 𝑨[2] 𝑏[𝑀−1] 𝑨[𝑀] 𝑏[𝑀] 𝑏[𝑀] = 𝑝𝑣𝑢𝑞𝑣𝑢
SLIDE 79 Backpropagation: Notation
79
- 𝒃[0] ← 𝐽𝑜𝑞𝑣𝑢
- 𝑝𝑣𝑢𝑞𝑣𝑢 ← 𝒃[𝑀]
𝑔(. ) 𝑔(. ) 𝑔(. ) 𝑔(. ) 𝑔(. ) 𝒃[𝑚−1] 𝒃[𝑚] 𝒜[𝑚]
SLIDE 80 Backpropagation: Last layer gradient
𝜖𝐹𝑜 𝜖𝑋
𝑗𝑘 [𝑀] = 𝜖𝐹𝑜
𝜖𝑏[𝑀] 𝜖𝑏[𝑀] 𝜖𝑋
𝑗𝑘 [𝑀]
𝜖𝐹 𝜖𝑏[𝑀] = 2(𝑧 − 𝑏[𝑀]) 𝜖𝑏[𝑀] 𝜖𝑋
𝑗𝑘 [𝑀] = 𝑔′ 𝑨 𝑘 [𝑀]
𝜖𝑨
𝑘 [𝑀]
𝜖𝑋
𝑗𝑘 [𝑀]
= 𝑔′ 𝑨
𝑘 [𝑀] 𝑏𝑗 [𝑀−1]
𝑏𝑗
[𝑚−1]
𝑨𝑘
[𝑚]
𝑏𝑘
[𝑚]
𝑔 𝑏𝑗
[𝑚] = 𝑔 𝑨𝑗 [𝑚]
𝑨𝑘
[𝑚] = 𝑘=0 𝑁
𝑥𝑗𝑘
[𝑚]𝑏𝑗 [𝑚−1]
For squared error loss: 𝐹𝑜 = 𝑏[𝑀] − 𝑧 𝑜
2
𝑥𝑗𝑘
[𝑚]
SLIDE 81 Backpropagation:
81
𝜖𝐹𝑜 𝜖𝑥𝑗𝑘
[𝑚] = 𝜖𝐹𝑜
𝜖𝑏𝑘
[𝑚] ×
𝜖𝑏𝑘
[𝑚]
𝜖𝑥𝑗𝑘
[𝑚]
= 𝜀
𝑘 [𝑚] × 𝑏𝑗 [𝑚−1] × 𝑔′ 𝑨𝑗 [𝑚]
𝜀
𝑘 [𝑚] = 𝜖𝐹𝑜 𝜖𝑏𝑘
[𝑚] is the sensitivity of the output to 𝑏𝑘
[𝑚]
sensitivity vectors can be obtained by running a
backward process in the network architecture (hence the name backpropagation.)
𝑏𝑗
[𝑚−1]
𝑨𝑘
[𝑚]
𝑏𝑘
[𝑚]
𝑔 𝑏𝑗
[𝑚] = 𝑔 𝑨𝑗 [𝑚]
𝑨𝑘
[𝑚] = 𝑘=0 𝑁
𝑥𝑗𝑘
[𝑚]𝑏𝑗 [𝑚−1]
𝑥𝑗𝑘
[𝑚]
SLIDE 82 𝜀𝑗
[𝑚−1] from 𝜀𝑗 [𝑚]
82
We will compute 𝜺[𝑚−1] from 𝜺[𝑚]: 𝜀𝑗
[𝑚−1] =
𝜖𝐹𝑜 𝜖𝑏𝑗
[𝑚−1]
=
𝑘=1 𝑒[𝑚]
𝜖𝐹𝑜 𝜖𝑏𝑘
[𝑚] ×
𝜖𝑏𝑘
[𝑚]
𝜖𝑨
𝑘 [𝑚] ×
𝜖𝑨
𝑘 [𝑚]
𝜖𝑏𝑗
[𝑚−1]
=
𝑘=1 𝑒[𝑚]
𝜖𝐹𝑜 𝜖𝑏𝑘
[𝑚] × 𝑔′ 𝑨𝑗 [𝑚] × 𝑥𝑗𝑘 [𝑚]
=
𝑘=1 𝑒[𝑚]
𝜀
𝑘 [𝑚] × 𝑔′ 𝑨𝑗 [𝑚] × 𝑥𝑗𝑘 [𝑚]
= 𝑔′ 𝑨𝑗
[𝑚] × 𝑘=1 𝑒[𝑚]
𝜀
𝑘 [𝑚] × 𝑥𝑗𝑘 [𝑚]
𝑏𝑗
[𝑚−1] = 𝑔 𝑨𝑗 [𝑚−1]
𝑨𝑘
[𝑚] = 𝑘=0 𝑁
𝑥𝑗𝑘
[𝑚]𝑏𝑗 [𝑚−1]
𝑏𝑗
[𝑚−1]
𝑏𝑘
[𝑚]
𝑔′ 𝑨𝑗
[𝑚−1]
𝑏𝑗
[𝑚−1]
𝑨𝑘
[𝑚]
𝑏𝑘
[𝑚]
𝑔 𝑥𝑗𝑘
[𝑚]
𝑥𝑗𝑘
[𝑚]
𝜀
𝑘 [𝑚]
𝜀
𝑘 [𝑚−1]
SLIDE 83 Find and save 𝜺[𝑀]
83
- Called error, computed recursively in backward manner
- For the final layer 𝑚 = 𝑀:
𝜀
𝑘 [𝑀] = 𝜖𝐹𝑜
𝜖𝑏𝑘
[𝑀]
SLIDE 84 Backpropagation of Errors
84
𝜀𝑗
[1] = 𝑔′ 𝑨𝑗 [1] × 𝑘=1 𝑒[2]
𝜀
𝑘 [2] × 𝑥𝑗𝑘 [2]
𝜀
𝑘 [2] = 2 𝑏𝑘 [2] − 𝑧𝑘 (𝑜) 𝑔′ 𝑨𝑘 [2]
𝑥𝑗𝑘
[2]
𝜀𝑗
[1]
𝜀1
[2]
𝜀2
[2]
𝜀3
[2]
𝜀
𝑘 [2]
𝜀𝐿
[2]
𝐹𝑜 =
𝑘
𝑏𝑘
[𝑀] − 𝑧𝑘 𝑜 2
SLIDE 85
Gradients for vectorized code
SLIDE 86
Vectorized operations
SLIDE 87
Vectorized operations
SLIDE 88
Vectorized operations
SLIDE 89
Vectorized operations
SLIDE 90
Vectorized operations
SLIDE 91
SLIDE 92
SLIDE 93
SLIDE 94
SLIDE 95
SLIDE 96
SLIDE 97
Always check: The gradient with respect to a variable should have the same shape as the Variable
SLIDE 98
SLIDE 99
SVM example
SLIDE 100 Summary
- Neural nets may be very large: impractical to write down gradient formula by
hand for all parameters
- Backpropagation = recursive application of the chain rule along a computational
graph to compute the gradients of all inputs/parameters/intermediates
- Implementations maintain a graph structure, where the nodes implement the
forward() / backward() API
– forward: compute result of an operation and save any intermediates needed for gradient computation in memory – backward: apply the chain rule to compute the gradient of the loss function with respect to the inputs
SLIDE 101 Converting error derivatives into a learning procedure
- The backpropagation algorithm is an efficient way of computing the
error derivative dE/dw for every weight on a single training case.
- To get a fully specified learning procedure, we still need to make a lot
- f other decisions about how to use these error derivatives:
– Optimization issues: How do we use the error derivatives on individual cases to discover a good set of weights? – Generalization issues: How do we ensure that the learned weights work well for cases we did not see during training?
SLIDE 102 Optimization issues in using the weight derivatives
- How to initialize weights
- How often to update the weights
– Batch size
– Use a fixed learning rate? – Adapt the global learning rate? – Adapt the learning rate on each connection separately? – Don’t use steepest descent?
SLIDE 103 Overfitting: The downside of using powerful models
- A model is convincing when it fits a lot of data surprisingly well.
– It is not surprising that a complicated model can fit a small amount of data well.
SLIDE 104 Ways to reduce overfitting
- A large number of different methods have been developed.
– Weight-decay – Weight-sharing – Early stopping – Model averaging – Dropout – Generative pre-training
SLIDE 105 Resources
- Deep Learning Book, Chapter 6.
- Please see the following note:
– http://cs231n.github.io/optimization-2/