Skip to the lesson
FixTheRobot Home / VLA / Prerequisites / M3.1

VLA · Prerequisite · Deep Learning and Transformers

M3.1 — Neural Network Fundamentals

Layers, activations and what a network can represent.

  • Prerequisite module 3
  • Deep Learning and Transformers
  • Essential
  • About 13 minutes to read

M2.4 showed how a network is trained. This lesson is about what it is: why the nonlinearity matters, how a softmax output works, and what depth, width and capacity do. These are the terms every later model description assumes.

Learning objectives

After this lesson, you should be able to:

  • Explain why a network needs a nonlinear activation, using XOR.
  • Name common activation functions and where each is used.
  • Compute softmax and explain what it produces.
  • Count a network’s parameters from its layer sizes.
  • State what the universal approximation theorem does and does not say.
  • Describe residual connections, normalisation and regularisation at a high level.
  1. Linear layers alone can only draw straight lines
  2. A nonlinear activation lets them bend
  3. More units and layers capture more complicated shapes
  4. A softmax output turns scores into probabilities
  5. Capacity is set by depth, width and parameter count
The idea of this lesson, from why networks need nonlinearity to what sets their capacity.

The one-sentence idea

A neural network is stacked linear layers with nonlinear activations between them, and its size and depth set how complicated a function it can represent.

Without the nonlinearity, depth adds nothing.

Where M2.4 left off

M2.4 showed how a small network computes an output and how backpropagation trains it. This lesson steps back to ask a different question: what can such a network represent at all, and what are the design choices (depth, width, activation, output layer) that decide the answer? Those choices are the vocabulary used to describe every model in the rest of the roadmap.

Why the activation function is essential

A stack of linear layers collapses into one linear layer (M1.1), and a single linear layer can only draw straight boundaries. The classic example is XOR: output 1 when exactly one of two inputs is 1. No straight line separates the 1s from the 0s, and the best a linear model can do is predict 0.5 everywhere:

import numpy as np

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=float)
t = np.array([0.0, 1.0, 1.0, 0.0])
A = np.hstack([X, np.ones((4, 1))])                   # add a column of ones for the bias
coef, *_ = np.linalg.lstsq(A, t, rcond=None)          # best linear fit in the least-squares sense
print((A @ coef).round(2))

Output:

[0.5 0.5 0.5 0.5]

One hidden layer with a ReLU is enough. Here the weights are chosen by hand (no training) so that you can see how it works:

W1 = np.array([[1.0, 1.0],
               [1.0, 1.0]])
b1 = np.array([0.0, -1.0])
w2 = np.array([1.0, -2.0])

h = np.maximum(0, X @ W1.T + b1)       # hidden layer, one row per input
y = h @ w2
print(h)
print(y)

Output:

[[0. 0.]
 [1. 0.]
 [1. 0.]
 [2. 1.]]
[0. 1. 1. 0.]

The first hidden unit measures “how many inputs are on”, and the second switches on only when both are, and the output subtracts twice that second signal. The nonlinearity lets the network bend its decision rule. Without the ReLU the same weights would produce a straight-line result.

Common activation functions:

Common activation functions and where they are used
ActivationWhat it doesTypical use
ReLUmax(0, z)Default for many hidden layers
GELUA smooth version of ReLUCommon inside transformers
TanhSquashes to −1…1Bounded outputs, such as normalised actions
SigmoidSquashes to 0…1A single probability
SoftmaxTurns a vector into probabilities that sum to 1Choosing among several classes or tokens

The output layer: softmax

When a network chooses among several options, such as the next word or the next action token, its last layer produces one raw score (a logit) per option, and softmax converts them into probabilities. It exponentiates each score and divides by the total, so larger scores get larger probabilities and the results add up to 1.

z = np.array([2.0, 1.0, 0.1])
p = np.exp(z) / np.exp(z).sum()
print(p.round(3), p.sum().round(3))

Output:

[0.659 0.242 0.099] 1.0

Softmax appears again in the next lessons: attention uses it to decide how much weight each position receives, and language models use it to pick the next token.

What a network can represent: depth, width and capacity

Width is the number of units in a layer and depth is the number of layers. Together with the sizes of the layers they determine the number of parameters, which is often called the model’s capacity:

def n_params(sizes):
    """Weights plus biases for a stack of linear layers."""
    return sum(a * b + b for a, b in zip(sizes[:-1], sizes[1:]))

print(n_params([10, 64, 64, 2]))
print(n_params([10, 128, 128, 2]))

Output:

4994
18178

Doubling the width roughly quadruples the parameters of the middle layers. A famous result, the universal approximation theorem, says that a network with one hidden layer and enough units can approximate any reasonable continuous function on a bounded region as closely as you like. The theorem says an approximation exists. It does not say gradient descent will find it, how many units are needed, or that the network will behave well on inputs it never saw.

A ReLU network is built from bent line segments, and you can watch the approximation improve as units are added. Below, a sum of shifted ReLUs approximates x² on 0 to 1, with 4 units and then 8:

def relu_fit(f, n, xs):
    knots = np.linspace(0, 1, n + 1)
    slopes = np.diff(f(knots)) / np.diff(knots)
    coef = np.concatenate([[slopes[0]], np.diff(slopes)])   # change of slope at each knot
    return f(0.0) + sum(c * np.maximum(0, xs - k) for c, k in zip(coef, knots[:-1]))

xs = np.linspace(0, 1, 1601)
for n in (4, 8):
    err = np.abs(relu_fit(lambda x: x ** 2, n, xs) - xs ** 2).max()
    print(n, round(err, 4))

Output:

4 0.0156
8 0.0039

With twice as many units the worst-case error falls to a quarter. Real networks learn the knots and slopes from data instead of being given them, and use many more dimensions, but the principle is the same: more units can follow more complicated shapes.

What matters in practice

Representing a function and learning it are different problems. Practical networks add a few ingredients that you will meet in the transformer lessons:

  • Residual connections add a layer’s input to its output, so gradients can flow through very deep networks.
  • Normalisation layers (such as LayerNorm) keep activations at a stable scale.
  • Dropout and other regularisers reduce overfitting by making the network less able to memorise.

Depth tends to help networks build up features in stages, for example edges, then shapes, then objects in vision. Very large models also need very large datasets: capacity without enough data leads to memorisation, which is why M2.5 held out validation data. In PyTorch, a network like those above is a sequence of layers:

import torch
from torch import nn

net = nn.Sequential(nn.Linear(4, 16), nn.ReLU(), nn.Linear(16, 2))
print(net(torch.ones(4)).shape)
print(sum(p.numel() for p in net.parameters()))

Output:

torch.Size([2])
114

The parameter count, 4×16 + 16 + 16×2 + 2 = 114, matches what the n_params formula gives for sizes [4, 16, 2].

Common misconceptions

Misconception 1 “A deeper network is always better.”

Correction More layers add capacity and can make training harder. Whether depth helps depends on the task, the data and design choices such as residual connections.

Misconception 2 “The universal approximation theorem means a one-layer network can solve any problem.”

Correction It says a good approximation exists. It does not say the network is small, that training finds it, or that it generalises to new data.

Misconception 3 “Softmax outputs are the model’s confidence.”

Correction They are normalised scores that sum to 1. A model can be confidently wrong, so the numbers are not guaranteed to be calibrated probabilities.

Misconception 4 “More parameters always means more intelligence.”

Correction Parameters measure capacity, not ability. Quality depends on the data, the objective and the training as well.

Engineering takeaways

  1. A stack of linear layers is still linear, so networks need nonlinear activations.
  2. XOR cannot be solved by a linear model but can by a network with one ReLU hidden layer.
  3. ReLU, GELU, tanh, sigmoid and softmax are the activations to recognise.
  4. Softmax turns a vector of scores into probabilities that sum to 1.
  5. Parameters are weights plus biases, and grow quickly with width.
  6. Universal approximation says a good fit exists, not that it will be found.
  7. Residual connections, normalisation and regularisation make large networks trainable.
  8. Capacity needs enough data, or the network memorises.

Linear layers do the arithmetic, and the nonlinearity gives the network its shape.

Knowledge check

Try each question first, then reveal the explanation.

Question 1

Why does stacking five linear layers with no activation function not give a more powerful model than one?

Explanation

Because the product of matrices is still a single matrix. Five linear maps compose into one linear map, which can only represent straight-line relationships. A nonlinearity between the layers prevents this collapse.

Question 2

A network has layer sizes [8, 32, 4]. How many parameters does it have?

Explanation

420. The first layer has 8×32 + 32 = 288 parameters and the second has 32×4 + 4 = 132, and 288 + 132 = 420.

Question 3

A model outputs logits [3.0, 1.0, 1.0] for three classes. After softmax, which class has the highest probability, and do the probabilities sum to 1?

Explanation

The first class, and yes, they sum to 1. Softmax is monotonic, so the largest logit gets the largest probability, and it normalises by the total so the outputs always add up to 1.

What’s next

You now know what a network is and what sets its capacity. Next is how text and other inputs become the vectors a network can use.

Next lesson

M3.2 — Embeddings and tokenization

Turning text and other inputs into vectors a model can use.

Back to the VLA roadmap prerequisites