Start studying
Module 2 · Deep Learning Module contents
Module 2  ·  Card 6 / 25beginner

Forward Propagation

Forward propagation is the pass where data flows from input to output.

Each layer computes its weighted sum and applies its activation.

The result becomes the input to the next layer.

The final layer produces the network's prediction.

It is exactly what happens at inference time when you actually use the model.

Learn in depth ↓
Module 02 · Deep Learning · 25 concepts
In depth  /  15 lines

Forward Propagation, in more detail

Forward propagation is the pass where an input travels through the network to an answer.

It starts with your features written as a row of numbers.

The first layer multiplies them by its weight matrix and adds its biases.

The activation function is applied to every value in that result.

The output becomes the input of the next layer, and the same thing happens again.

That repeats until the last layer, which produces the prediction.

In code this is a handful of matrix multiplications in a row.

Writing it as matrices is what lets a GPU do the whole batch at once.

A batch of 64 examples travels through together, not one after another.

During training every intermediate result is kept, because the backward pass needs them.

That storage is why training needs far more memory than prediction does.

At inference nothing needs keeping, so the intermediates are thrown away as they go.

The same pass is what runs when a trained model is actually used.

So the cost of one prediction is exactly the cost of one forward pass.

Training is this pass, then a backward pass, repeated a few hundred thousand times.

Want the full technical article? Wikipedia ↗
Deep dive

What is underneath Forward Propagation

A layer is a matrix multiply and a bend

z[l]=W[l]a[l−1]+b[l]\mathbf{z}^{[l]} = W^{[l]} \mathbf{a}^{[l-1]} + \mathbf{b}^{[l]}
a[l]=g(z[l])\mathbf{a}^{[l]} = g\big( \mathbf{z}^{[l]} \big)
  • a[l−1]\mathbf{a}^{[l-1]}: the activations arriving from the layer below.
  • W[l]W^{[l]}: the weight matrix of layer ll.
  • gg: the activation function, applied to each element on its own.

Those two lines are the entire forward pass. Every architecture is a choice of what WW looks like and where the bends go.

The input layer is a[0]=x\mathbf{a}^{[0]} = \mathbf{x}, and the output of the last layer is the prediction.

The shapes have to agree

W[l]∈Rnl×nl−1,b[l]∈RnlW^{[l]} \in \mathbb{R}^{n_l \times n_{l-1}}, \quad \mathbf{b}^{[l]} \in \mathbb{R}^{n_l}

A layer taking 784 inputs to 128 units holds a 128×784128 \times 784 matrix and a vector of 128 biases, which is 100,480 numbers.

Z[l]=W[l]A[l−1]+b[l]Z^{[l]} = W^{[l]} A^{[l-1]} + \mathbf{b}^{[l]}

A batch changes nothing but a shape: stack mm examples as columns and the same multiply does all of them at once.

The bias is a vector added to every column, which is broadcasting, and it is the one step where the shapes do not match exactly on purpose.

One pass, in order

  1. Put the input batch in A[0]A^{[0]}.
  2. Multiply by the layer's weights and add the bias.
  3. Apply the activation elementwise.
  4. Hand the result to the next layer as its input.
  5. At the last layer, apply softmax or the identity instead, depending on the task.
  6. Keep every ZZ and AA, because the backward pass needs them.

Why the nonlinearity is not optional

W2(W1x+b1)+b2=(W2W1)x+(W2b1+b2)W_2(W_1 \mathbf{x} + \mathbf{b}_1) + \mathbf{b}_2 = (W_2 W_1)\mathbf{x} + (W_2 \mathbf{b}_1 + \mathbf{b}_2)

Two linear layers with nothing between them collapse into one linear layer, because the product of two matrices is a matrix.

A hundred layers collapse the same way. Without gg, depth buys exactly nothing.

What it costs

FLOPs≈2∑lnl nl−1\mathrm{FLOPs} \approx 2 \sum_{l} n_l \, n_{l-1}

Each weight is used once for a multiply and once for an add, so the forward cost is about twice the parameter count per example.

Memory is the other half of the bill: every activation is stored until the backward pass reads it, which is why training needs far more memory than inference.

Inference can free each layer's activations as soon as the next layer has consumed them, and that single difference is most of the gap.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
When does forward propagation happen?
Deep Learning

25 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close