Search

Search papers and lessons

Feedforward Neural Network

Level 3 · 10 min read

A feedforward network is a stack of layers, each one a matrix multiply followed by a bend, that turns an input into an output with no loops, and the bends are what let it fit curved functions.

On this page

  • Colour = topic mix
  • Linear algebra
  • Neural network basics
  • Discrete math
  • Sign in to see your progress

TL;DR

A feedforward neural network passes an input through a sequence of layers. Each layer does a matrix multiplication of its input by a weight matrix, adds a bias vector, and applies an activation function elementwise. Information only flows forward, from input to output, with no loops. The activation functions are what matter: they stop the layers from collapsing into one linear map, so the whole stack can represent curved relationships.

This is the basic architecture behind most of deep learning. Convolutional networks, transformer feed-forward blocks, and the output heads of most larger models are all variations on it.

Where you'll see it: as the classifier that sits on top of image features, as the per-token feed-forward block inside every transformer layer, and as the function approximator inside reinforcement learning agents.

Intuition

Folding a sheet of paper

Think of the input as a point on a sheet of paper. A linear layer (a Linear Transformation) can stretch, rotate, shear, or flip the sheet, but it always keeps straight lines straight. An activation function bends the sheet at a crease. Each layer adds another round of stretching and folding. After enough rounds, a flat sheet can be folded into a complicated shape, and the output is read off where each point ends up.

Training is the process of choosing where to stretch and where to fold, so that points that should share a label land together and points that should differ land apart.

flowchart LR
  x1[x1] --> h1[h1]
  x1 --> h2[h2]
  x1 --> h3[h3]
  x2[x2] --> h1
  x2 --> h2
  x2 --> h3
  h1 --> y[y]
  h2 --> y
  h3 --> y

The diagram shows a network with two inputs, a hidden layer of three neurons, and one output. Every arrow carries a weight, and every neuron in one layer is connected to every neuron in the next. That is why these networks are called fully connected or dense. The hidden layer is hidden only because we never look at its values directly; the network is free to use them however it likes.

Why the bend matters

Suppose there were no activation functions. Then layer one computes W1x+b1W_1 \mathbf{x} + \mathbf{b}_1, and layer two computes W2(W1x+b1)+b2W_2(W_1 \mathbf{x} + \mathbf{b}_1) + \mathbf{b}_2. Expanding gives (W2W1)x+(W2b1+b2)(W_2 W_1)\mathbf{x} + (W_2 \mathbf{b}_1 + \mathbf{b}_2), which is a single linear map with a single bias. A hundred such layers would still be one matrix and one vector. Adding nonlinearity between layers is what makes depth useful.

The math

Definition: Feedforward network

A feedforward network with LL layers is a sequence of functions. Set h(0)=x\mathbf{h}^{(0)} = \mathbf{x}, the input vector. For each layer ℓ=1,…,L\ell = 1, \dots, L:

z(ℓ)=W(ℓ)h(ℓ−1)+b(ℓ),h(ℓ)=σ ⁣(z(ℓ))\mathbf{z}^{(\ell)} = W^{(\ell)} \mathbf{h}^{(\ell-1)} + \mathbf{b}^{(\ell)}, \qquad \mathbf{h}^{(\ell)} = \sigma\!\left(\mathbf{z}^{(\ell)}\right)

Here W(ℓ)W^{(\ell)} is a weight matrix of size nℓ×nℓ−1n_\ell \times n_{\ell-1}, where nℓn_\ell is the number of neurons in layer ℓ\ell. The vector b(ℓ)\mathbf{b}^{(\ell)} is the bias, of length nℓn_\ell. The function σ\sigma is an activation applied to each entry separately, and z(ℓ)\mathbf{z}^{(\ell)} is called the pre-activation. The last layer is usually left without an activation, or with one suited to the task, so that y^=z(L)\hat{\mathbf{y}} = \mathbf{z}^{(L)} or y^=softmax(z(L))\hat{\mathbf{y}} = \text{softmax}(\mathbf{z}^{(L)}) (the Softmax turns scores into probabilities) for classification.

The layers between input and output are called hidden layers. A network with several of them is called deep.

The forward pass

Computing y^\hat{\mathbf{y}} from x\mathbf{x} is the forward pass. It is just the definition above, run from ℓ=1\ell = 1 to ℓ=L\ell = L. Each layer costs one matrix-vector product, which is nℓ×nℓ−1n_\ell \times n_{\ell-1} multiply-adds.

Counting parameters

The trainable parameters are every weight and bias. A layer with ninn_{\text{in}} inputs and noutn_{\text{out}} outputs has nout⋅nin+noutn_{\text{out}} \cdot n_{\text{in}} + n_{\text{out}} parameters. The total is the sum over layers. This count matters because it sets memory use, and a network with many more parameters than training examples can memorise the training set.

Training

The network is trained by choosing all weights and biases to minimise a Loss Function averaged over the training set. For classification the usual loss is the Cross-Entropy Loss on the softmax output. The gradient of the loss with respect to every parameter is computed with the chain rule, running backwards through the layers. That procedure is Backpropagation, and the parameters are updated with a rule such as Gradient Descent or a variant of it.

Universal approximation

A single hidden layer with enough neurons and a suitable bend can approximate any continuous function on a bounded region to any accuracy. Cybenko proved this for sigmoid activations in 1989, and Hornik extended it to a broader class of activations in 1991. The theorem is an existence result. It says nothing about how many neurons are needed, how to find the weights, or whether the network will generalise to new inputs. Depth often gets the same accuracy with far fewer parameters, which is why practical networks are deep.

Worked examples

Example
XOR, which no linear model can solve

The XOR function takes two bits and outputs 1 when they differ: (0,0)→0(0,0) \to 0, (0,1)→1(0,1) \to 1, (1,0)→1(1,0) \to 1, (1,1)→0(1,1) \to 0. No single line separates the 1s from the 0s, so no linear model computes it.

Use two ReLU hidden units, ReLU(z)=max⁡(0,z)\text{ReLU}(z) = \max(0, z):

  • h1=ReLU(x1+x2)h_1 = \text{ReLU}(x_1 + x_2)
  • h2=ReLU(x1+x2−1)h_2 = \text{ReLU}(x_1 + x_2 - 1)
  • output y=h1−2h2y = h_1 - 2h_2

Check each input:

  1. (0,0)(0,0): h1=ReLU(0)=0h_1 = \text{ReLU}(0) = 0, h2=ReLU(−1)=0h_2 = \text{ReLU}(-1) = 0, so y=0y = 0. Correct.
  2. (0,1)(0,1): h1=ReLU(1)=1h_1 = \text{ReLU}(1) = 1, h2=ReLU(0)=0h_2 = \text{ReLU}(0) = 0, so y=1y = 1. Correct.
  3. (1,0)(1,0): same as the previous case, so y=1y = 1. Correct.
  4. (1,1)(1,1): h1=ReLU(2)=2h_1 = \text{ReLU}(2) = 2, h2=ReLU(1)=1h_2 = \text{ReLU}(1) = 1, so y=2−2=0y = 2 - 2 = 0. Correct.

The hidden layer folds the input so that (1,1)(1,1) lands on the same side as (0,0)(0,0). Remove the ReLU and the same arithmetic gives y=(x1+x2)−2(x1+x2−1)=2−(x1+x2)y = (x_1 + x_2) - 2(x_1 + x_2 - 1) = 2 - (x_1 + x_2). That is linear, and it gets three inputs right but outputs 2 at (0,0)(0,0) instead of 0. No choice of linear weights fixes all four cases at once, so the bend is essential.

Example
A forward pass by hand

Take x=(1,2)\mathbf{x} = (1, 2), one hidden layer of two ReLU units, and one linear output.

W(1)=(1−10.51),b(1)=(0−1),W(2)=(21),b(2)=0.5W^{(1)} = \begin{pmatrix} 1 & -1 \\ 0.5 & 1 \end{pmatrix}, \quad \mathbf{b}^{(1)} = \begin{pmatrix} 0 \\ -1 \end{pmatrix}, \quad W^{(2)} = \begin{pmatrix} 2 & 1 \end{pmatrix}, \quad b^{(2)} = 0.5
  1. Hidden pre-activations: z(1)=W(1)x+b(1)\mathbf{z}^{(1)} = W^{(1)}\mathbf{x} + \mathbf{b}^{(1)}. The first entry is 1⋅1+(−1)⋅2+0=−11 \cdot 1 + (-1) \cdot 2 + 0 = -1. The second is 0.5⋅1+1⋅2−1=1.50.5 \cdot 1 + 1 \cdot 2 - 1 = 1.5.
  2. Apply ReLU: h(1)=(ReLU(−1),ReLU(1.5))=(0,1.5)\mathbf{h}^{(1)} = (\text{ReLU}(-1), \text{ReLU}(1.5)) = (0, 1.5).
  3. Output: y=2⋅0+1⋅1.5+0.5=2.0y = 2 \cdot 0 + 1 \cdot 1.5 + 0.5 = 2.0.
  4. Interpretation: the first hidden unit was switched off by the bend, so the output depends only on the second unit. The network used the nonlinearity to make one neuron inactive for this input.
Example
Parameters in an image classifier

A classifier for 28×2828 \times 28 grayscale digits flattens each image to 784784 numbers, then uses two layers: 784→128784 \to 128 and 128→10128 \to 10, one output per digit.

  1. First layer: 128⋅784+128=100,352+128=100,480128 \cdot 784 + 128 = 100{,}352 + 128 = 100{,}480 parameters.
  2. Second layer: 10⋅128+10=1,280+10=1,29010 \cdot 128 + 10 = 1{,}280 + 10 = 1{,}290 parameters.
  3. Total: 100,480+1,290=101,770100{,}480 + 1{,}290 = 101{,}770 parameters.
  4. Cost of a forward pass on a batch of 64 images: roughly 101,770⋅64≈6.5101{,}770 \cdot 64 \approx 6.5 million multiply-adds, which a laptop does in well under a millisecond with optimised matrix routines. Most of the parameters sit in the first layer, which is where the flattening throws away spatial structure. That is one reason convolutional networks replaced dense first layers for images.

Tradeoffs and failure modes

A feedforward network is a general, flexible function approximator. It is also a poor fit for some structures, and it is easy to overfit with it.

SituationProblemBetter choice
Data has spatial structure (images)Flattening throws away neighbourhood information, and the parameter count grows fastConvolutional Neural Network
Data is a sequence of variable lengthA fixed input size cannot handle it directlyRecurrent Neural Network, or a transformer
Small tabular datasetA large dense network memorises the training setGradient-boosted trees, or a small network with L2 Regularization
Very deep stackGradients vanish or explode during trainingResidual connections, and careful Xavier (Glorot) Initialization or He (Kaiming) Initialization
Activation saturates (sigmoid at large inputs)Gradients become near zero and learning stallsRectified Linear Unit (ReLU) or a similar activation
Layer outputs drift in scale during trainingLearning becomes unstableLayer Normalization

Common failure modes include forgetting the activation between layers, which silently makes the whole stack linear, and choosing a hidden width so large that the network memorises noise. A good first check is to compare training loss with validation loss. A large gap means the network is memorising.

History

The idea of a neuron as a thresholded weighted sum comes from McCulloch and Pitts in 1943. Rosenblatt's perceptron, a single trainable layer, followed in 1958. In 1969 Minsky and Papert showed that single-layer perceptrons cannot compute XOR, and many researchers moved away from the approach for over a decade.

Multilayer networks trained by backpropagation came back into focus in the mid-1980s. Rumelhart, Hinton, and Williams popularised the method for hidden layers in 1986. The universal approximation results of Cybenko and Hornik followed in 1989 and 1991, which gave a theoretical reason to believe the architecture was expressive enough.

Practical progress was slower than the theory suggested. Dense networks were hard to train with sigmoid units, because gradients shrank through many layers. The switch to ReLU units and better initialisation in the 2010s made deep feedforward networks trainable at scale, and they became the building blocks of the architectures that followed.

Check yourself

QuestionWhy does a stack of layers with no activation function reduce to a single linear layer?Show answer

Each layer computes a matrix times the previous output plus a bias. Substituting one into the next gives (W2W1)x+(W2b1+b2)(W_2 W_1)\mathbf{x} + (W_2 \mathbf{b}_1 + \mathbf{b}_2), which is one matrix and one bias. Any number of linear layers collapses the same way, so depth adds nothing without a nonlinearity between layers.

QuestionA network has layers of size 10→20→510 \to 20 \to 5. How many parameters does it have?Show answer

The first layer has 20⋅10+20=22020 \cdot 10 + 20 = 220 parameters. The second has 5⋅20+5=1055 \cdot 20 + 5 = 105. The total is 220+105=325220 + 105 = 325.

QuestionIn the XOR example, why does the output use h1−2h2h_1 - 2h_2 rather than h1+h2h_1 + h_2?Show answer

The second unit has to cancel the first one exactly when both inputs are 1. At (1,1)(1,1), h1=2h_1 = 2 and h2=1h_2 = 1, so h1−2h2=0h_1 - 2h_2 = 0, which is the XOR answer. With h1+h2h_1 + h_2 the output at (1,1)(1,1) would be 3, which is wrong.

QuestionYou train a dense network on 500 labelled photographs and it scores 99% on training data but 60% on new photos. What is happening, and what would you change first?Show answer

The network is overfitting: it has memorised the training photos rather than learning patterns that transfer. Dense layers on raw pixels have many parameters and little built-in structure, so this is common. The first changes to try are a smaller network, stronger regularisation such as weight decay or dropout, data augmentation, or a convolutional architecture that builds in the right structure for images.

Sources