Neural Network Layer: 4 Steps Worked Out by Hand

⚡ TL;DR: A neural network layer does four things in order: multiply the inputs by a weight matrix, add a bias vector, apply an activation function, pass the result on. That is one matrix multiplication and one function call — nothing more.

The previous post explained what a neural network is. This one opens up a single layer and works through the arithmetic by hand, because once you have done it once, deep learning stops feeling mysterious.

The four steps of a neural network layer

Neural network layer shown as four steps: inputs, multiply by weights, add bias, apply activation function
Every layer in every neural network, from a toy example to a language model, is this same four-step pipeline.

Written as one line:

$$a = f(Wx + b)$$

✅ Reading the symbols: x is the input vector. W is the weight matrix — one row per neuron in this layer. b is the bias vector, one number per neuron. f is the activation function, applied to each element separately. a is the output, which becomes the next layer’s x.

Working one layer out by hand

Take a layer with 3 inputs and 2 neurons. The weight matrix is 2 × 3 — two rows because there are two neurons, three columns because each neuron has one weight per input.

🧪 Worked example: one layer, start to finish

Step 0 — what we have
Input x = [2, 1, 3]. Weights W = [[0.5, −1.0, 0.2], [1.5, 0.0, −0.5]]. Biases b = [1.0, −2.0]. Activation: ReLU.

Step 1 — multiply (the dot products)
Neuron 1: (0.5×2) + (−1.0×1) + (0.2×3) = 1.0 − 1.0 + 0.6 = 0.6
Neuron 2: (1.5×2) + (0.0×1) + (−0.5×3) = 3.0 + 0 − 1.5 = 1.5

Step 2 — add the bias
Neuron 1: 0.6 + 1.0 = 1.6  ·  Neuron 2: 1.5 − 2.0 = −0.5

Step 3 — apply ReLU, which is max(0, z)
Neuron 1: max(0, 1.6) = 1.6  ·  Neuron 2: max(0, −0.5) = 0

Output: a = [1.6, 0]. That vector is what the next layer receives.

Notice what happened to neuron 2: it produced a negative value, and ReLU flattened it to zero. That neuron contributes nothing to the next layer for this particular input. On a different input it might fire strongly. Different neurons switching on and off for different inputs is how a neural network layer represents varied patterns.

Why the shapes must line up

Most errors beginners hit in NumPy or PyTorch are shape errors, and they all come from this rule:

ObjectShapeWhy
Input x3 × 1One value per input feature
Weights W2 × 3Rows = neurons out, columns = inputs in
Wx2 × 1The inner 3s cancel
Bias b2 × 1One per neuron
Output a2 × 1Becomes the next layer’s input

The rule is the one from matrix multiplication dimensions: the columns of W must equal the rows of x. If a framework complains about a shape mismatch, this is almost always what it means.

Doing a whole batch at once

In practice you do not push one example through at a time. You stack many rows into a matrix and process them together — this is why GPUs help so much, and why the linear algebra matters more than the calculus in day-to-day work.

With 32 examples at once, x becomes 32 × 3, and one matrix multiplication produces all 32 outputs. The arithmetic per example is unchanged; it just happens in parallel.

⚠️ Why the bias matters more than it looks

Without a bias, every neural network layer is forced through the origin — when all inputs are zero, the output must be zero. The bias lets a neuron activate even on a weak input, or stay quiet on a strong one. It is one extra number per neuron and it meaningfully increases what the layer can represent. Never remove it to “simplify”.

What the layer is actually learning

Each row of W is one neuron’s opinion about which inputs matter. A large positive weight means “this input strongly increases my output”; a large negative one means the opposite; near zero means “I ignore this”.

Training adjusts every one of those numbers. In the worked example the weights were given — in reality they start random and are corrected repeatedly by backpropagation, which is the next post in this sequence.

🔑 Key Takeaways

  • Every neural network layer computes a = f(Wx + b) — multiply, add, activate.
  • The weight matrix has one row per neuron and one column per input.
  • Shape errors in code almost always mean those dimensions do not line up.
  • ReLU setting a neuron to zero is normal — different neurons fire for different inputs.
  • The bias frees the layer from being forced through the origin.

Further reading

PyTorch’s nn.Linear documentation is exactly the Wx + b step described here, and NumPy’s matmul reference covers the batching rules if you want to implement a layer yourself.

Where to go next

Scroll to Top