All Modules Why Activations Feedforward Cost & Gradients Exercise

Neural-Net Foundations

Why activations exist, how data feeds forward, and what a cost function is really doing.

Module 3 · Lecture notes by Dr. Abdulkarim Albanna

Core Neural Nets ~25 min

What You'll Learn

  • Explain why a network needs non-linear activations — and what happens without them
  • Write the feedforward step in matrix form: \(x^{(1)}=\sigma\!\left(Wx^{(0)}+b\right)\)
  • Understand cost functions, gradients, and why derivatives matter for training

Prerequisites: Module 1 (Linear Algebra) and Module 2 (Numerical Computation). You should be comfortable with matrix–vector multiplication and the idea of gradient descent before starting.

Why Do We Need Activation Functions?

A network with no activation functions collapsing into a single linear layer
Without an activation between them, stacked layers are just repeated matrix multiplications — and their product is a single matrix. The whole “deep” network collapses into one linear layer. (From the course notes.)

A neural network without activation functions is just a chain of linear operations. And here is the crucial fact from linear algebra: composing linear functions gives another linear function. If layer one computes \(W_1x\) and layer two computes \(W_2(W_1x)\), that is simply \((W_2W_1)x\) — a single matrix multiply. No matter how many layers you stack, the whole network collapses into a single linear layer.

Non-linear activations are what break this collapse. By inserting a non-linear function between the layers, each layer can transform its input in a way the next layer cannot simply undo or absorb. This is what lets a network model non-linear relationships — curves, boundaries, and interactions that no single line could ever capture.

An activation also squashes the weighted sum into a useful interval — for example, near \(1\) for class “Yes” and near \(0\) for class “No” — which keeps activations from growing without bound as signals travel through the network. A good activation is, on top of all that, simple and cheap to compute.

Concretely, each neuron computes a weighted sum of its inputs plus a bias, then passes the result through the activation \(\sigma\) to produce its output:

\[\hat y=\sigma\!\left(w_0x_0+w_1x_1+\dots+w_nx_n+b\right)\]

Two linear layers = one linear layer

Stacking depth buys nothing without a non-linearity between the layers. Two linear layers in a row are mathematically identical to a single linear layer — formally, \((W_2(W_1x)=(W_2W_1)x)\). The extra weights add parameters but no new expressive power. Depth only becomes meaningful once a non-linear activation sits between each pair of layers.

How Data Feeds Forward

Every neuron in a layer computes the same kind of expression — a weighted sum of its inputs, plus a bias, passed through an activation — each with its own weights and bias. For a single neuron that is \(\sigma(w\cdot x + b)\). Writing all the neurons of a layer together — stacking their weight vectors as the rows of a matrix — gives the compact matrix form of the feedforward step:

\[x^{(1)}=\sigma\!\left(Wx^{(0)}+b\right)\]

where \(W\) is the weight matrix (one row per neuron of the next layer), \(b\) is the bias vector (one entry per neuron), \(x^{(0)}\) is the input vector (the previous layer's outputs), and \(\sigma\) is the activation applied elementwise. The result \(x^{(1)}\) becomes the input to the next layer.

A multi-layer feedforward network showing each layer transforming its input
A multi-layer feedforward network. Data enters on the left and flows layer by layer to the right; each layer \(k\) applies \(h^{(k)}=g(W^{(k-1)}h^{(k-1)}+b^{(k)})\) — a matrix multiply, a bias, and an elementwise non-linearity. (From the course notes.)

Running this rule from layer to layer is forward propagation:

\[h^{(1)}=g\!\left(W^{(0)}x+b^{1}\right),\quad h^{(2)}=g\!\left(W^{(1)}h^{(1)}+b^{2}\right),\quad\dots\quad h^{(n)}=g\!\left(W h^{(n-1)}+b^{n}\right)\]

Feedforward propagation means information flows forward — the inputs produce hidden-layer values, which in turn produce the output. Backpropagation is the reverse: the weights are then repeatedly adjusted to minimise the difference between the network's output and the desired output.

Connect back to Module 1

Strip away the activation and the feedforward step is just matrix–vector multiplication plus a bias — exactly the operation you practised in the Linear Algebra module. A neural network layer is nothing more exotic than \(Wx + b\) with a non-linear function wrapped around it. Everything you know about matrices carries straight over.

Cost Functions, Gradients & Why Derivatives Matter

Training a network means finding the weights that minimise an error measure — a number telling us how far the network's predictions are from the true answers. That number is the cost function (also called the loss). For regression, the usual choice is the Mean Squared Error:

\[J=\frac{1}{n}\sum_i\left(\hat y_i-y_i\right)^2\]

Here \(\hat y_i\) is the network's prediction for example \(i\), \(y_i\) is the true target, and we average the squared differences over all \(n\) examples. Squaring makes every error positive and penalises large errors more heavily.

A convex bowl-shaped MSE cost curve plotted against a weight, with a single minimum
Plotted against the weights, the MSE of a linear model forms a convex “bowl” with a single minimum. Training is the act of rolling downhill to the bottom of that bowl. (From the course notes.)

Because that bowl is convex, gradient descent — following the derivative downhill — is guaranteed to find the global minimum. Deep networks have non-convex error surfaces with local minima, but the gradient still tells us which direction reduces the error.

The gradient is the vector of partial derivatives of the cost with respect to each weight. It points in the direction of steepest increase of the cost — so to reduce the cost we step in the opposite direction. That is exactly the gradient descent you met in Module 2: measure the slope, take a small step downhill, repeat until you reach the bottom of the bowl.

Terminology

  • Loss function — error of the model on a single data instance.
  • Cost function — error over a group of instances (e.g. the average loss).
  • Objective function — what we actually optimise: the cost plus any regulariser.

Derivatives are the whole game

Every single weight update in a neural network is driven by \(\partial J/\partial w\) — the derivative of the cost with respect to that weight. This is precisely why activation functions must be differentiable: backpropagation needs their derivative to update the weights. If we could not take the derivative through the activation, we could not compute the gradient, and without a gradient there is nothing to descend. Derivatives are not a side detail of training — they are training.

Exercises

Work each one by hand before opening the solution — tracing a neuron on paper cements everything above. These mirror the “solve it at home” problems from the course notes.

1

B1 — One neuron with a sigmoid activation

A single neuron receives inputs \(x=[2,\,1]\) with weights \(w=[0.4,\,0.6]\) and bias \(b=-0.5\), and uses a sigmoid activation \(\sigma(z)=\dfrac{1}{1+e^{-z}}\).

(a) Compute the pre-activation \(z=w\cdot x+b\).
(b) Compute \(\sigma(z)\), given \(e^{-0.9}\approx 0.407\).
(c) If we removed the activation, what single operation would two stacked layers reduce to?

(a) The pre-activation:

\[z=0.4\cdot 2+0.6\cdot 1-0.5=0.8+0.6-0.5=0.9\]

(b) Applying the sigmoid with \(e^{-0.9}\approx 0.407\):

\[\sigma(0.9)=\frac{1}{1+e^{-0.9}}=\frac{1}{1+0.407}=\frac{1}{1.407}\approx 0.711\]

(c) Two layers with no activation between them reduce to a single linear layer — the composition \(W_2(W_1x)=(W_2W_1)x\) is just one matrix multiply, so the depth adds no expressive power. This is exactly the collapse from the “Why activations” section.

2

B2 — Feedforward through a 2-neuron layer

A layer of two neurons has weight matrix \(W=\begin{bmatrix}0.2 & 0.8\\ 0.5 & -0.4\end{bmatrix}\), input \(x=[1,\,2]\), and bias \(b=[0.1,\,0]\). Compute \(z=Wx+b\) for both neurons, then apply a ReLU activation \(\operatorname{ReLU}(z)=\max(0,z)\). Which neuron is switched off, and why?

Compute each pre-activation from its row of \(W\):

\[z_1=0.2\cdot 1+0.8\cdot 2+0.1=0.2+1.6+0.1=1.9\] \[z_2=0.5\cdot 1+(-0.4)\cdot 2+0=0.5-0.8+0=-0.3\]

So \(z=[1.9,\,-0.3]\). Applying ReLU elementwise:

\[\operatorname{ReLU}([1.9,\,-0.3])=[\max(0,1.9),\,\max(0,-0.3)]=[1.9,\,0]\]

Neuron 2 is switched off. Its pre-activation \(z_2=-0.3\) is negative, and ReLU maps every negative input to \(0\) — so that neuron outputs nothing and passes no signal to the next layer.

3

B3 — Mean Squared Error cost

Given predictions \(\hat y=[0.6,\,0.9,\,0.2]\) and targets \(y=[1,\,1,\,0]\), compute the MSE cost \(J\). Which single prediction contributes most to the cost, and what does that tell the training algorithm?

Compute the squared error for each example:

\[(0.6-1)^2=(-0.4)^2=0.16\] \[(0.9-1)^2=(-0.1)^2=0.01\] \[(0.2-0)^2=(0.2)^2=0.04\]

Average over the \(n=3\) examples:

\[J=\frac{1}{3}\left(0.16+0.01+0.04\right)=\frac{0.21}{3}=0.07\]

The first prediction contributes most (its squared error \(0.16\) is the largest term). That tells the training algorithm to adjust the weights hardest in the direction that fixes that example — the largest error dominates the gradient, so it drives the biggest weight update.

Recap & Where Next

You now know

  • Why activations exist: without a non-linearity, stacked layers collapse into a single linear layer — depth buys nothing.
  • Feedforward in matrix form: each layer computes \(x^{(1)}=\sigma(Wx^{(0)}+b)\) — matrix–vector multiply, add a bias, apply the activation elementwise.
  • Cost functions: training minimises an error measure such as MSE, \(J=\frac{1}{n}\sum_i(\hat y_i-y_i)^2\), whose convex bowl has a single minimum.
  • Gradients & derivatives: the gradient points uphill, so we step opposite it (gradient descent); every weight update is \(\partial J/\partial w\), which is why activations must be differentiable.

Next up: Module 4 — Activation Functions. Now that you know why a network needs a non-linearity and where it sits in the feedforward step, we will meet the specific functions themselves — sigmoid, tanh, ReLU and friends — and see what each one buys you (and costs you) during training.

NN Foundations

Objectives Why Activations Feedforward Cost & Gradients Exercise Recap