An MLP consists of an input layer, hidden layers, and an output layer. Every node in one layer is connected to every node in the next, so each layer is called a fully connected layer.
Information flows as follows.
- The input is turned into a vector x of length d.
- For an image, the pixels are flattened into a single row (length 784 for a 28×28 image).
- A hidden layer multiplies by weights and sums (Wx+b), then applies an activation function φ.
- Step 2 is repeated once per hidden layer (L times).
- The last fully connected layer (the output layer) maps to the number of outputs k.
- For classification, the outputs are turned into probabilities with the softmax function, and the model is trained with cross-entropy.
An MLP with one hidden layer can be written as follows.
h=φ(W1x+b1),y=W2h+b2
Here, h is the hidden layer output (a vector of length h), W1 is an h×d weight matrix, W2 is a k×h weight matrix, and b1,b2 are biases. The weights and biases are updated with an optimizer such as Adam, using gradients computed by backpropagation.
Without activation functions, any number of stacked fully connected layers is equivalent to a single fully connected layer, because the matrices can be combined, as in W2(W1x)=(W2W1)x. Stacking layers is meaningful only because the activation functions in between allow curved decision boundaries.
MLPs also have the following weaknesses.
- Because the input is flattened into a single row, structure such as the spatial arrangement of image pixels or the order of a time series cannot be exploited. Models that exploit the data's structure are commonly used instead, such as a CNN for images and a Transformer for sequences.
- Because every node is connected, large inputs mean many parameters. Connecting 784 inputs to 256 nodes alone requires about 200,000 weights.
- Deeper networks become harder to train because of vanishing gradients and more prone to overfitting. ReLU and dropout are used to mitigate these problems.