Purpose
Covers feedforward neural networks as classifiers for NLP. It works through units and activation functions, why nonlinearity is needed at all (the XOR problem), the matrix notation for multi-layer computation, and training with cross-entropy loss and backpropagation. These networks generalize the linear decision boundaries of multinomial logistic regression on the same classification problems, and they can learn complex patterns without hand-crafted features. Follows SLP3 ch. 7.
Activation Functions
A single computational unit is a linear function of the input with weights and bias . The output is a non-linear function , where is the activation function (typically one of , , or ).
In practice is rarely the best choice. SLP3 notes that , a scaled version of ranging from to , is similar and almost always works better.
The simplest activation function is the Rectified Linear Unit (ReLU), which is for negative inputs and linear for positive inputs.
ReLU is cheap to compute, and it doesn’t saturate for large positive inputs the way and do. Saturation causes the vanishing gradient problem, where gradients near stop the network from learning.
The XOR Problem
A single computational unit cannot solve XOR, because a single unit draws a linear decision boundary and XOR is not linearly separable. A two-layer network can solve it, since the hidden layer re-represents the input in a space where the problem becomes linearly separable.
To see why XOR is not linearly separable, look at the four inputs. and are in one class, while and are in the other. No straight line separates the two classes.
Feedforward Neural Networks
A feedforward NN is a multi-layer network where the output of each layer is the input to the next layer, with no cycles. They are sometimes called multilayer perceptrons (MLPs), although that term technically applies only to networks whose units use a step function as the activation.
The network has three types of nodes.
Input units
The vector of input units is , with one node for each feature of the input.
Hidden layers
One or more layers of hidden units, each with a non-linear activation function. In the standard architecture, each node is connected to all nodes in the previous layer, so each hidden unit sums over all input values.
For a given hidden layer, combine the weights and bias for each computational unit into a weight matrix and bias vector . Each element of the weight matrix is the weight from the th input unit to the th hidden unit .
The output of a hidden layer with activation function is:
Dimensionality
Call the input layer, layer , and let be the number of input units, so the input is a column vector with dimension .
The first hidden layer has hidden units, so and .
Output units
The output layer is the final layer of the network. Its output , with , is an estimate of the probability distribution over classes.
Normalization
To get a probability distribution, normalize the output of the network with the softmax function:
Comparison with MLR
A NN is like MLR with a few differences:
- many layers, since a deep NN is like layer after layer of MLR classifiers
- intermediate layers have non-linear activation functions. Without these, the network would just be a linear classifier, since the composition of linear functions is still linear
- instead of feature selection, previous layers build up a representation of the input that is useful for the final layer
Details/Notation
- denotes a quantity associated with the th layer, e.g. is the weight matrix for the th layer. These indices are 1-indexed.
- is the number of units in layer .
- is the activation function, which tends to be or ReLU for hidden layers, and softmax for the output layer.
- is the output from layer
- is the input to the activation function in layer , e.g.
- is the input vector
Example: 2-layer NN
Feedforward Computation
For :
then return .
def feedforward(x):
a = x
for l in range(1, L):
z = W[l] @ a + b[l]
a = g[l](z)
return aReplacing the Bias
Often the bias term is folded into the weight matrix by appending a constant to the input vector.
With , we can write , where the column of that multiplies the constant plays the role of :
FF networks for NLP: Classification
Instead of manually designed features, use word embeddings (e.g. word2vec, GloVe). This constitutes “pre-training”, i.e. relying on already computed values/embeddings. One simple way to represent a sentence is to sum the embeddings of its words, or to average them.
To classify many examples at once, pack the inputs into a single matrix where each row is an input vector . If each input has features, then where is the number of examples.
is the weight matrix for the hidden layer, is the bias vector, and is the output layer weight matrix. is the output matrix.
Training Neural Nets
We want to learn the parameters and for each layer that make as close as possible to the true .
Loss Function
Same as the one used for MLR, the cross-entropy loss.
For binary classification:
For multi-class classification with a one-hot true label , the sum collapses to the log probability of the correct class :
Written in terms of the logit for the correct class:
Backpropagation
Gradients pass backward through the network to update the weights, using the chain rule. Each node in a computation graph takes an upstream gradient and computes its local gradient, multiplying the two to get the downstream gradient. A node may have multiple local gradients, one for each incoming edge.
A very simple example
Consider the function . Create a computation graph with nodes for the inputs, and for the intermediate computations.
flowchart LR a((a)) --> e(("e = a + d")) b((b)) --> d(("d = 2b")) d --> e e --> L(("L = ce")) c((c)) --> L style L fill:#e3f2fd,stroke:#1565c0
Learning details
NN optimization is a non-convex problem, so it needs a few techniques to work well:
- Initialize weights and biases to small random values instead of all zeros
- Normalize input values to
- Dropout: randomly (with probability ) set some hidden units to 0, then renormalize other inputs to prevent overfitting
- Hyperparameters: learning rate, mini-batch size, number of hidden units, number of layers, choice of activation function, etc.
Related notes
- Neural Networks from Scratch
- Neural Networks from Scratch and the MNIST experiment
- Recurrent Neural Networks
- Manual Gradients and Autodiff