NLP Neural
NLP Neural
1
Table of contents
2. Neural Networks
2
Linear Models Recap
Linear models
f (⃗ x · W +⃗
x) = ⃗ b
x ∈ Rdin
⃗ W ∈ Rdin ×dout ⃗
b ∈ Rdout
3
Linear models
• Nevertheless, we can map the values using a non-linear function, and then we
can solve the problem
4
Linear models
• In some cases, we can find the mapping function relatively easily. For instance,
for the XOR problem, we can use ϕ(x1 , x2 ) = [x1 × x2 , x1 + x2 ] and then train a
classifier to solve ŷ = f (⃗
x ) = ϕ(⃗ ⃗ +b
x) · w
• But we would like to find the ϕ function during training, i.e., to be a trainable
function.
• We would like something like
ŷ = f (⃗
x ) = ϕ(⃗ ⃗ +b
x) · w
xW ′ + ⃗
x ) = g(⃗
ϕ(⃗ b ′)
5
Neural Networks
Introduction to Neural Networks
6
Neuron
0
Source:[Goldberg, 2017]
7
Neural network
• We can connect many neurons, creating a network (hence the name neural
networks)
• Neurons are arranged in layers, reflecting the flow of information
• The bottom layer has no incoming arrows, as is the input to the network
• The top-most layer has no outgoing arrows, as is the output of the network
• The other layers are called hidden layers.
• When each neuron in one layer is connected to all neurons in the next layer, we
are talking about a fully connected layer. A network comprised of such layers is
called a feedforward neural network (also called multilayer perception (MLP))
8
Feedforward Network with two Layers
0
Source:[Goldberg, 2017]
9
Feedforward Network Neural Networks
• The feedforward network from the picture is a stack of linear models separated
by nonlinear functions.
• The values of each row of neurons in the network can be thought of as a vector.
• The input layer is a 4-dimensional vector (⃗
x ), and the layer above it is a
6-dimensional vector (⃗ h1 ).
• The fully connected layer can be thought of as a linear transformation from 4
dimensions to 6 dimensions.
• A fully connected layer implements a vector-matrix multiplication, ⃗
h =⃗xW + ⃗ b.
• The weight of the connection from the i-th neuron in the input row to the j-th
neuron in the output row is W[i,j] .
• The values of ⃗
h are transformed by a nonlinear function g that is applied to each
value before being passed on as input to the next layer.
0
Vectors are assumed to be row vectors and superscript indices correspond to
network layers.
10
Fully connected layers as vector-matrix multiplications
�h1 �h2
W1,1 W1,2
�x = [�x1 , �x2 , �x3 ] W = W2,1 W2,2
�h = �xW W3,1 W3,2
W32
�xW = [�x1 ∗ W11 + �x2 ∗ W21 + x3 ∗ W31 , �x1 ∗ W12 + �x2 ∗ W22 + x3 ∗ W32 ]
W11 W12
W22
�h = [�h1 , �h2 ]
W21
W31
11
Neural Networks as Mathematical Functions
• The Multilayer Perceptron (MLP) from the figure is called MLP2 because it has
two hidden layers.
• A simpler model would be MLP1, a multilayer perceptron of one hidden layer:
⃗
ŷ = NNMLP1 (⃗ xW 1 + ⃗
x ) = g(⃗ b1 )W 2 + ⃗
b2
x ∈ Rdin , W 1 ∈ Rdin ×d1 , ⃗
⃗ b2 ∈ Rdout , ⃗
b1 ∈ Rd1 , W 2 ∈ Rd1 ×dout , ⃗ ŷ ∈ Rdout
• Here W 1 and ⃗
b1 are a matrix and a bias term for the first linear transformation of
the input.
• The function g is a nonlinear function that is applied element-wise (also called a
nonlinearity or an activation function ).
• W 2 and ⃗
b2 are the matrix and bias term for a second linear transform.
• When describing a neural network, one should specify the dimensions of the
layers (d1 ), the input (din ), and the output (dout ).
12
Neural Networks as Mathematical Functions
x) = ⃗
NNMLP2 (⃗ ŷ
⃗ xW 1 + ⃗
h1 = ⃗ b1
⃗
h2 = g 1 (⃗
h1 )W 2 + ⃗
b2
y = g 2 (⃗
⃗ h2 )W 3 + b3
xW 1 + ⃗
y = (g 2 (g 1 (⃗
⃗ b1 )W 2 + ⃗
b2 ))W 3 + b3 .
• The matrices and the bias terms that define the linear transformations are the
parameters of the network.
• Like in linear models, it is common to refer to the collection of all parameters as
Θ.
13
Representation Power
• [Hornik et al., 1989] and [Cybenko, 1989] showed that a multilayer perceptron of
one hidden later (MLP1) is a universal approximator.
• MLP1 can approximate all continuous functions on a closed and bounded subset
of Rn .
• This may suggest there is no reason to go beyond MLP1 to more complex
architectures.
• The result does not say how easy or hard it is to set the parameters based on
training data and a specific learning algorithm.
• It also does not guarantee that a training algorithm will find the correct function
generating our training data.
• Finally, it does not state how large the hidden layer should be.
0
For intuition about this theorem, see [Nielsen, 2015], Chapter 4.
14
Representation Power
15
Activation Functions
16
Sigmoid
17
Hyperbolic tangent (tanh)
2x
• The hyperbolic tangent tanh(x) = ee2x +1
−1
activation function is an S-shaped
function, transforming the values x into the range[−1, 1].
18
Hard tanh
−1 x < −1
hardtanh(x) = 1 x >1
x
otherwise.
19
ReLU
• The rectifier activation function [Glorot et al., 2011], also known as the rectified
linear unit is a very simple activation function.
• It is easy to work with and was shown many times to produce excellent results.
• The ReLU unit clips each value x < 0 at 0.
ReLU(x) = max(0, x)
• It performs well for many tasks, especially when combined with the dropout
regularization technique (to be explained later).
20
Activation Functions
• As a rule of thumb, both ReLU and tanh units work well, and significantly
outperform the sigmoid.
• You may want to experiment with both tanh and ReLU activations, as each one
may perform better in different settings.
• The figure from below shows the shapes of the different activations functions,
together with the shapes of their derivatives.
0
Source:[Goldberg, 2017] 21
Embedding Layers
• In NLP the input to the neural network contains symbolic categorical features
(e.g., words from a closed vocabulary, character n-grams, POS tags).
• In linear models we usually represent the input with sparse vectors e.g., as the
sum, average, or the concatenation of one-hot encoded vectors (the sum or the
average can produce bag-of words representation).
• In neural networks, it is common to associate each possible feature value (i.e.,
each word in the vocabulary, each POS tag category) with a d−dimensional
dense vector for some d.
• These vectors are then considered parameters of the model, and are trained
jointly with the other parameters.
• The mapping from a symbolic feature values such as “word number 1249” to
d−dimensional vectors is performed by an embedding layer (also called a
lookup layer ).
22
Embedding Layers
23
The Embedding Matrix
24
Neural Network Training
Neural Network Training
25
Neural Network Training
• We know that we can use gradient descent to iteratively update the value of each
∂L
parameter: Θi ← Θi − η ∂Θ (ŷ, y) (for all parameters Θi and with a learning
i
parameter η).
∂L
• But how can we compute ∂Θi ?
26
Neural Network Training
• Maybe we can compute the loss directly for each parameter Θi , either weight or
∂L L(Θ1 ,...,Θi +ϵ,...,ΘM )−L(Θ1 ,...,Θi ,...,ΘM )
bias, with the formula ∂Θ ≈ ϵ .
i
• But then, for each Θi we would need to compute L(Θ1 , . . . , Θi + ϵ, . . . , ΘM ). If
we have a million parameters, we would need to do a million computations.
• We also need to compare with L(Θ1 , . . . , ΘM ), so that’s one million and one
computations. All of that just to perform one update to the parameters of the
network, with one training example.
• We would like to compute all the partial derivatives in a faster way.
27
Backpropagation
28
Derivative Chain Rule Recap
∂z ∂z ∂y
= × = ey × 2 = 2e2x
∂x ∂y ∂x
0
Figure taken from:
[Link]
29
Derivative Chain Rule Recap
∂z 3
= (ey1 ×y2 × y2 ) × 2 + (ey1 ×y2 × y1 ) × 2x = e2x × 6x 2
∂x
0
Figure taken from:
[Link]
30
Derivative Chain Rule Recap
The general version of the multiple path chain rule would be:
∂z X ∂z
n
∂y
= × i
∂x ∂yi ∂x
i=1
31
0
Figure taken from:
Backpropagation: notation
• dl = dimension of layer l
• h⃗l = z l−1
⃗ W l + b⃗l
�ŷ1 �ŷ2 �ŷ3 �y13 �y23 �y33
• Equivalently,
Pdl−1 l l−1 Softmax Gold output
hjl = i=1 wij zi + bjl
zjl = g(hjl ). 3
W11 3
W12
3
W23
3
W13 3
W22
z M = [z1M , . . . , zdM ]. 2 2 2
W22
M W11 W12
2
W21
• Just for now, assume that zjM �z11 �z21
depends only on hjM . For instance, g1 g1
g on the final layer could be a �h1
1
�h1
2
∂L ′ M
δM
j = g (hj ) (BP1)
∂zjM
dl+1
X
l+1 ′ l
δlj = δl+1
k wjk g (hj ) (BP2)
k=1
∂L
= δlj (BP3)
∂bjl
∂L
= δlj zil−1 (BP4)
∂wijl
0
All the following derivation of backpropagation is adapted from
[Nielsen, 2015], Chapter 2.
33
Backpropagation: equation 1
∂L
δM
j =
∂hjM
XdM
∂L ∂zkM
=
k =1
∂zkM ∂hjM
M
∂L ∂zj ∂zkM
= (since zkM only depends on hkM , then = 0 for k ̸= j)
∂zj ∂hjM
M ∂hjM
∂L ′ M
= g (hj ) (since zjM = g(hjM ))
∂zjM
Then:
∂L ′ M
δM
j = g (hj ) (BP1)
∂zjM
34
Backpropagation: equation 2
∂L
δlj =
∂hjl
dl+1
X ∂L ∂hkl+1
= (since the output of the layer l can affect all the outputs in layer (l + 1)
k=1
∂hkl+1 ∂hjl
dl+1
X ∂hkl+1
= δl+1
k (by definition of δl+1
k )
k=1
∂hjl
35
Backpropagation: equation 2
∂hkl+1
Let’s focus on .
∂hjl
We know that:
X
dl
hkl+1 = wikl+1 zil + bkl
i=1
X
dl
= wikl+1 g(hil ) + bkl (by definition of zil )
i=1
∂hkl+1
We note that hjl only appears multiplied by wjk . Then, when we compute all the
∂hjl
terms with i ̸= j will be 0.
∂hkl+1
= wjkl+1 g ′ (hjl )
∂hjl
36
Backpropagation: equation 2
dl+1
X ∂hkl+1
δlj = δl+1
k
k=1
∂hjl
(BP2)
dl+1
X
l+1 ′ l
= δl+1
k wjk g (hj )
k=1
⃗l = δl+1
Alternatively, it can be written in vector form as δ ⃗ (W l+1 )T ⊙ g ′ (h⃗l ), where ⊙
37
Backpropagation: equation 3
∂L
Goal: find an expression for (i.e., the derivative of L with respect to the bias of
∂bjl
neuron j in layer l)
∂L Xdl
∂L ∂hkl
=
∂bjl k=1
∂hkl ∂bjl
l
∂L ∂hj
= (since bjl only affects hjl )
∂hjl ∂bjl
∂hjl
= δlj (by definition of δlj )
∂bjl
X
= δlj × 1 (since hjl = wi j l zil−1 + bjl )
Then:
∂L
= δlj (BP3)
∂bjl
38
Backpropagation: equation 4
∂L
Goal: find an expression for (i.e., the derivative of L with respect to the weight that
∂wijl
connects input i-th to neuron j-th in layer l)
∂L X dl
∂L ∂hkl
=
∂wijl k =1
∂hkl ∂wijl
l
∂L ∂hj
= (since wijl only affects hjl )
∂hjl ∂wijl
∂hjl
= δlj (by definition of δlj )
∂wijl
X
= δlj zil−1 (since hjl = wkjl zkl−1 + bjl )
Then:
∂L
= δlj zil−1 (BP4)
∂wijl
39
Backpropagation: algorithm
Let ⃗
x be a training input example, and ⃗
y its corresponding value.
Now we can update the weights with just one forward and one backward pass :)
40
Backpropagation: softmax?
• When deriving the previous equations, me assume that zjM = g(hjM ); i.e., the
activation for the final layer depends only on the value for that neuron.
• That assumption doesn’t hold for the softmax function, often used in multi-class
classification problems
zM
e j
• softmax(zjM ) = P zM .
ke k
• It’s easy to derive the equation BP1 in that case, without our previous
assumption (see problem Backpropagation with softmax and the log-likelihood
cost, in [Nielsen, 2015], Chapter 3).
• But it’s not that easy to extend this demonstration to other architectures (residual
connections, shared weights, etc).
• We need a better abstraction.
41
The Computation Graph
Abstraction
The Computation Graph Abstraction
• One can compute the gradients of the various parameters of a network by hand
and implement them in code.
• This procedure is cumbersome and error prone.
• For most purposes, it is preferable to use automatic tools for gradient
computation [Bengio, 2012].
• A computation graph is a representation of an arbitrary mathematical
computation (e.g., a neural network) as a graph.
• This abstraction will allow us computing the gradients from any kind of neural
network architecture using the backpropagation algorithm.
• Previous formulation was restricted to feedforward networks.
42
The Computation Graph Abstraction
43
The Computation Graph Abstraction
44
The Computation Graph Abstraction
0
Figure taken from: [Goldberg, 2017] 45
The Computation Graph Abstraction
• The figure above shows the computation graph for an MLP with one hidden-layer
and a softmax output transformation.
• Oval nodes represent mathematical operations or functions, and shaded
rectangle nodes represent parameters (bound variables).
• Network inputs are treated as constants, and drawn without a surrounding node.
• Input and parameter nodes have no incoming arcs, and output nodes have no
outgoing arcs.
• The output of each node is a matrix, the dimensionality of which is indicated
above the node.
46
The Computation Graph Abstraction
47
Forward Computation
• The forward pass computes the outputs of the nodes in the graph.
• Since each node’s output depends only on itself and on its incoming edges, it is
trivial to compute the outputs of all nodes.
• This is done by traversing the nodes in a topological order and computing the
output of each node given the already computed outputs of its predecessors.
• More formally, in a graph of N nodes, we associate each node with an index i
according to their topological ordering.
• Let fi be the function computed by node i (e.g., multiplication, addition , etc.).
48
Forward Computation
• Let π(i) be the parent nodes of node i , and π−1 (i) = {j|i ∈ π(j)} the children
nodes of node i (these are the arguments of fi ).
• Denote by v (i) the output of node i , that is, the application of fi to the output
values of its arguments π−1 (i).
• For variable and input nodes, fi is a constant function and π−1 (i) is empty.
• The computation-graph forward pass computes the values v (i) for all i ∈ [1, N].
49
Backward Computation (Backprop)
50
Backward Computation (Backprop)
51
Summary of the Computation Graph Abstraction
0
A comprehensive tutorial on the backpropagation algorithm over the
computational graph abstraction can be found here:
[Link]
52
Derivatives of “non-mathematical” functions
∂fj
• Defining ∂i for mathematical functions such is as log or + is straightforward.
• It may be challenging to think about the derivative of operations as as pick(⃗
x , 5)
that selects the fifth element of a vector.
• The answer is to think in terms of the contribution to the computation.
• After picking the i-th element of a vector, only that element participates in the
remainder of the computation.
x , 5) is a vector ⃗
• Thus, the gradient of pick(⃗ v with the dimensionality of ⃗
x where
⃗
v[5] = 1 and ⃗
v[i̸=5] = 0.
• Similarly, for the function max(0, x) the value of the gradient is 1 for x > 0 and 0
otherwise.
53
More on neural networks
Regularization and Dropout
• Multi-layer networks can be large and have many parameters, making them
especially prone to overfitting.
• Model regularization is just as important in deep neural networks as it is in linear
models, and perhaps even more so.
• The regularizers discussed for linear models, namely L2 , L1 , and the elastic-net,
are also relevant for neural networks.
• Another effective technique for preventing neural networks from overfitting the
training data is dropout training [Srivastava et al., 2014].
54
Regularization and Dropout
• The dropout method is designed to prevent the network from learning to rely on
specific weights.
• It works by randomly dropping (setting to 0) half of the neurons in the network (or
in a specific layer) in each training example in the stochastic-gradient training.
0
Figure taken from: [Link]
uploads/[Link]
55
Deep Learning Frameworks
56
Questions?
57
References i
Bengio, Y. (2012).
Practical recommendations for gradient-based training
of deep architectures.
In Neural networks: Tricks of the trade, pages 437–478.
Springer.
Cybenko, G. (1989).
Approximation by superpositions of a sigmoidal
function.
Mathematics of control, signals and systems, 2(4):303–314.
58
References ii
59
References iii
60
References iv
61