βMulti-layer Perceptron βForward Pass βBackpropagation
Multi-Layer Networks and Backpropagation Algorithm
Transcript of Multi-Layer Networks and Backpropagation Algorithm
Multi-Layer Networksand Backpropagation Algorithm
M. Soleymani
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.
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.
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.
β’ Spike generation:β There is an axon hillock that generates outgoing spikes whenever enough charge
has flowed in at synapses to depolarize the cell membrane.
A mathematical model for biological neurons
π₯1 π€1
π€1π₯1
π€2π₯2
π€3π₯3
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.
Be very careful with your brain analogies!
β’ Biological Neurons:β 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]
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
π€π
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
AND & OR networks
10
β’ For -1 and 1 inputs:
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.
Rectified Linear Units (ReLU)
β’ They compute a linear weighted sum of their inputs.
β’ The output is a non-linear function of the total input.
Adjusting weights
β’ Types of single layer networks:β Perceptron (Rosenblatt, 1962)
β ADALINE (Widrow and Hoff, 1960)
The standard Perceptron architecture
β’ Learn how to weight each of the feature activations to get desirable outputs.
β’ If output is above some threshold, decide that the input vector is a positive example of the target class.
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.
Adjusting weights
16
β’ Weight update for a training pair (π π , π¦(π)):
β Perceptron: If π πππ(πππ(π)) β π¦(π) then βπ = π(π)π¦(π) else βπ = π
β ADALINE: βπ = π(π¦(π) βπππ(π))π(π)
β’ Widrow-Hoff, LMS, or delta rule ππ‘+1 = ππ‘ β ππ»πΈπ ππ‘
πΈπ π = π¦(π) βπππ(π)2
How to learn the weights: multi class example
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)
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)
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)
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)
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)
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)
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.
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.
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
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 <ΞΈ
Discriminating simple patterns under translation with wrap-around
β’ Suppose we just use pixels as thefeatures.
β’ binary decision unit cannotdiscriminate patterns with the samenumber of on pixels
β if the patterns can translate with wrap-around!
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.
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?
Feed-forward neural networks
β’ Also called Multi-Layer Perceptron (MLP)
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
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 halfspaces
Three Layer (two hidden layers)
Arbitrary regions Union of polyhedrals
MLP with unit step activation function
Decision region found by an output unit.
Beyond linear models
Beyond linear models
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,β¦ , πΎ
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
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
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.
Gradient descent
β’ We want π»πΎπΏ πΎ
β’ Numerical gradient: β slow :(β approximate :(β easy to write :)
β’ Analytic gradient:β fast :)β exact :)β error-prone :(
β’ In practice: Derive analytic gradient, check your implementation with numerical gradient
Training multi-layer networks
42
β’ Backpropagationβ 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
Computational graphs
Backpropagation: a simple example
Backpropagation: a simple example
Backpropagation: a simple example
Backpropagation: a simple example
Backpropagation: a simple example
Backpropagation: a simple example
Backpropagation: a simple example
How to propagate the gradients backward
How to propagate the gradients backward
Another example
Another example
Another example
Another example
Another example
Another example
Another example
Another example
Another example
Another example
Another example
Another example
Another example
Another example
[local gradient] x [upstream gradient]
x0: [2] x [0.2] = 0.4
w0: [-1] x [0.2] = -0.2
Derivative of sigmoid function
Derivative of sigmoid function
Patterns in backward flow
β’ add gate: gradient distributor
β’ max gate: gradient router
Gradients add at branches
Simple chain rule
β’ π§ = π π π₯
β’ π¦ = π(π₯)
Multiple paths chain rule
Modularized implementation: forward / backward API
Modularized implementation: forward / backward API
Modularized implementation: forward / backward API
Caffe Layers
Caffe sigmoid layer
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]
π§[πΏ]
π[πΏ]
π[πΏ] = ππ’π‘ππ’π‘
Backpropagation: Notation
79
β’ π[0] β πΌπππ’π‘
β’ ππ’π‘ππ’π‘ β π[πΏ]
π(. )
π(. )
π(. )
π(. )
π(. )
π[πβ1] π[π]π[π]
Backpropagation: Last layer gradient
ππΈπ
ππππ[πΏ]
=ππΈπππ[πΏ]
ππ[πΏ]
ππππ[πΏ]
ππΈ
ππ[πΏ]= 2(π¦ β π[πΏ])
ππ[πΏ]
ππππ[πΏ]
= πβ² π§π[πΏ]
ππ§π[πΏ]
ππππ[πΏ]
= πβ² π§π[πΏ]
ππ[πΏβ1]
ππ[πβ1]
π§π[π]
ππ[π]
π
ππ[π]
= π π§π[π]
π§π[π]
=
π=0
π
π€ππ[π]ππ[πβ1]
For squared error loss:
πΈπ = π[πΏ]
β π¦π
2
π€ππ[π]
Backpropagation:
81
ππΈπ
ππ€ππ[π]
=ππΈπ
πππ[π]
Γπππ
[π]
ππ€ππ[π]
= πΏπ[π]
Γ ππ[πβ1]
Γ πβ² π§π[π]
πΏπ[π]
=ππΈπ
πππ[π] is the sensitivity of the output to ππ
[π]
sensitivity vectors can be obtained by running abackward process in the network architecture (hencethe name backpropagation.)
ππ[πβ1]
π§π[π]
ππ[π]
π
ππ[π]
= π π§π[π]
π§π[π]
=
π=0
π
π€ππ[π]ππ[πβ1]
π€ππ[π]
πΏπ[πβ1]
from πΏπ[π]
82
We will compute πΉ[πβ1]
from πΉ[π]
:
πΏπ[πβ1]
=ππΈπ
πππ[πβ1]
=
π=1
π[π]
ππΈπ
πππ[π]
Γπππ
[π]
ππ§π[π]
Γππ§π
[π]
πππ[πβ1]
=
π=1
π[π]
ππΈπ
πππ[π]
Γ πβ² π§π[π]
Γ π€ππ[π]
=
π=1
π[π]
πΏπ[π]
Γ πβ² π§π[π]
Γ π€ππ[π]
= πβ² π§π[π]
Γ
π=1
π[π]
πΏπ[π]
Γ π€ππ[π]
ππ[πβ1]
= π π§π[πβ1]
π§π[π]
=
π=0
π
π€ππ[π]ππ[πβ1]
ππ[πβ1]
ππ[π]
πβ² π§π[πβ1]
ππ[πβ1]
π§π[π]
ππ[π]
π
π€ππ[π]
π€ππ[π]
πΏπ[π]
πΏπ[πβ1]
Find and save πΉ[πΏ]
83
β’ Called error, computed recursively in backward manner
β’ For the final layer π = πΏ:
πΏπ[πΏ]
=ππΈπ
πππ[πΏ]
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
Gradients for vectorized code
Vectorized operations
Vectorized operations
Vectorized operations
Vectorized operations
Vectorized operations
Always check: The gradient with
respect to a variable should have
the same shape as the Variable
SVM example
Summary
β’ Neural nets may be very large: impractical to write down gradient formula byhand for all parameters
β’ Backpropagation = recursive application of the chain rule along a computationalgraph to compute the gradients of all inputs/parameters/intermediates
β’ Implementations maintain a graph structure, where the nodes implement theforward() / backward() API
β forward: compute result of an operation and save any intermediates needed for gradientcomputation in memory
β backward: apply the chain rule to compute the gradient of the loss function with respect tothe inputs
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 of 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?
Optimization issues in using the weight derivatives
β’ How to initialize weights
β’ How often to update the weightsβ Batch size
β’ How much to updateβ Use a fixed learning rate?
β Adapt the global learning rate?
β Adapt the learning rate on each connection separately?
β Donβt use steepest descent?
β’ β¦
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.
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
Resources
β’ Deep Learning Book, Chapter 6.
β’ Please see the following note:β http://cs231n.github.io/optimization-2/