Lesson 9.2 · 2 of 5 · 6 min read · Reviewed 24 Nov 2026
Neurons, weights, biases and activations
By the end, you can
- Compute a neuron's output from its inputs, weights, bias and activation.
- Explain why a network needs non-linear activations.
- Count the parameters in a dense layer.
A neural network sounds mysterious, but its building block is a few lines of arithmetic. Once you can compute one neuron by hand, a transformerTransformer: The neural network architecture behind modern LLMs, built from stacked attention and feed-forward layers. is “only” a very large, very well organised pile of them.
One neuron
A neuron takes some numbers in and gives one number out:
- Multiply each input by its weight.
- Add the results together.
- Add the bias, a number that shifts the result up or down.
- Pass the total, often called z, through an activation function.
z = w1*x1 + w2*x2 + b
output = activation(z)
A worked example
Inputs x = [2, 1], weights w = [0.5, -1], bias b = 0.25:
z = 2*0.5 + 1*(-1) + 0.25 = 1 - 1 + 0.25 = 0.25
With different activations:
| Activation | Rule | Output for z = 0.25 | Output for z = -0.5 |
|---|---|---|---|
| ReLU | max(0, z) | 0.25 | 0 |
| Sigmoid | 1 / (1 + e^-z) | 0.56 | 0.38 |
| Tanh | squashes to -1..1 | 0.24 | -0.46 |
| GELU | a smooth ReLU, common in transformers | 0.15 | -0.15 |
The weights decide what the neuron pays attentionAttention: The mechanism that lets each token weigh every other token in the context when building its representation. to: a big positive weight means “this input pushes me up”, a negative one means “this input pushes me down”. The bias decides how easily it switches on.
Why the activation matters
Suppose you drop the activation. One layer computes W1 x + b1. A second layer computes W2 (W1 x + b1) + b2, which simplifies to one matrix times x plus one bias. However many layers you stack, the result is still a single straight-line function. It could never learn a curved boundary, or anything as tangled as language.
A non-linear activation such as ReLU or GELU breaks that collapse. Each layer can bend the space a little, and many layers together can represent very complex functions.
From neurons to layers
Put several neurons side by side, all reading the same inputs, and you have a dense (fully connected) layer. In code it is one matrix multiply: a weight matrix with one row per neuron, plus a bias vector.
A layer with 4 inputs and 3 neurons has 4 x 3 = 12 weights and 3 biases, so 15 parameters. When people say a model has “8 billion parameters”, they mean roughly this count added up over every layer, plus the embeddingEmbedding: A list of numbers (a vector) that represents the meaning of a piece of text, so similar meanings land close together. tables. Training is the process of finding good values for all of them (lesson 9.3).
The final layer of an LLMLarge language model (LLM): A neural network trained on large amounts of text to predict the next token, which lets it generate and transform language. has one output per tokenToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. in the vocabulary. A softmax turns those raw scores (logitsLogits: The raw scores a model outputs for every token in its vocabulary before they are turned into probabilities.) into probabilities that sum to 1, which is where the next-token chart in lesson 2.2 comes from.
Common mistakes
- Adding the bias to each input instead of once to the sum.
- ThinkingExtended thinking: A mode in which the model reasons step by step in thinking blocks before giving its final answer. one negative weight switches a ReLU off. Only the total sum decides.
- Confusing parameters with hyperparameters. Weights and biases are learned. The number of layers, the activation and the learning rate are chosen by people.
Key points
- A neuron multiplies each input by a weight, adds them up, adds a bias, then passes the sum through an activation function.
- Weights and biases are the parameters: the numbers training adjusts. An LLM's parameter count is mostly these.
- Without a non-linear activation, any stack of layers collapses into one straight-line layer.
- A dense layer with n inputs and m neurons has n x m weights plus m biases.
Build a neuron and swap its activation
Compute a neuron by hand, compare activations and prove that linear layers collapse without one.
You will have: A script that prints the lesson neuron under four activations and checks the linear-collapse claim on four points.
Steps
- Fill in neuron: weighted sum, plus the bias once, then the activation.
- Fill in gelu with math.erf and compare all four activations at z = 0.25.
- Work out the combined weights and bias of two linear layers (W2 times W1, W2 times b1 plus b2).
- Run it and confirm the stacked layers and the single layer agree on every point.
- Change one weight in W1 and check the collapse still holds; then compare the ReLU outputs.
Done when
- The ReLU output for the lesson example is 0.250 and sigmoid is 0.562.
- The script prints that the linear stack equals one layer.
- The network's parameter count prints as 9.
Open in the Playground No API key needed.
Starter code (Python)
import math
def relu(z): return max(0.0, z)
def sigmoid(z): return 1 / (1 + math.exp(-z))
def tanh(z): return math.tanh(z)
def gelu(z):
# TODO: GELU is 0.5 * z * (1 + erf(z / sqrt(2))). Use math.erf.
return z
def neuron(x, w, b, act):
# TODO: weighted sum of inputs times weights, plus the bias once, then the activation.
z = 0.0
return z, act(z)
x, w, b = [2, 1], [0.5, -1], 0.25
for name, act in [("relu", relu), ("sigmoid", sigmoid), ("tanh", tanh), ("gelu", gelu)]:
z, out = neuron(x, w, b, act)
print(f"z = {z:.2f} {name:7} {out:.3f}")
# A dense layer is one neuron per row of a weight matrix.
def dense(x, W, bias, act=lambda z: z):
return [neuron(x, row, bi, act)[1] for row, bi in zip(W, bias)]
W1, b1 = [[1.0, 2.0], [-1.0, 0.5]], [0.0, 1.0]
W2, b2 = [[0.5, -1.0]], [0.25]
params = sum(len(r) for r in W1) + len(b1) + sum(len(r) for r in W2) + len(b2)
print("Parameters in the 2-2-1 network:", params)
# Without activations, two layers collapse into one: W = W2 @ W1, b = W2 @ b1 + b2.
# TODO: compute the combined weights Wc (W2 times W1) and bias bc (W2 times b1, plus b2).
Wc = [[0.0, 0.0]]
bc = [0.0]
same = True
for pt in ([0, 0], [1, 2], [-3, 0.5], [2, -2]):
two = dense(dense(pt, W1, b1), W2, b2)[0]
one = dense(pt, Wc, bc)[0]
same = same and abs(two - one) < 1e-9
print("Linear stack equals one layer:", same)
with_relu = [dense(dense(pt, W1, b1, relu), W2, b2)[0] for pt in ([0, 0], [1, 2], [-3, 0.5], [2, -2])]
print("With ReLU outputs:", [round(v, 2) for v in with_relu])
Show a solution
import math
def relu(z): return max(0.0, z)
def sigmoid(z): return 1 / (1 + math.exp(-z))
def tanh(z): return math.tanh(z)
def gelu(z): return 0.5 * z * (1 + math.erf(z / math.sqrt(2)))
def neuron(x, w, b, act):
z = sum(xi * wi for xi, wi in zip(x, w)) + b # weighted sum, then the bias once
return z, act(z)
x, w, b = [2, 1], [0.5, -1], 0.25
for name, act in [("relu", relu), ("sigmoid", sigmoid), ("tanh", tanh), ("gelu", gelu)]:
z, out = neuron(x, w, b, act)
print(f"z = {z:.2f} {name:7} {out:.3f}")
# A dense layer is one neuron per row of a weight matrix.
def dense(x, W, bias, act=lambda z: z):
return [neuron(x, row, bi, act)[1] for row, bi in zip(W, bias)]
W1, b1 = [[1.0, 2.0], [-1.0, 0.5]], [0.0, 1.0]
W2, b2 = [[0.5, -1.0]], [0.25]
params = sum(len(r) for r in W1) + len(b1) + sum(len(r) for r in W2) + len(b2)
print("Parameters in the 2-2-1 network:", params)
# Without activations, two layers collapse into one: W = W2 @ W1, b = W2 @ b1 + b2.
Wc = [[sum(W2[i][k] * W1[k][j] for k in range(2)) for j in range(2)] for i in range(1)]
bc = [sum(W2[i][k] * b1[k] for k in range(2)) + b2[i] for i in range(1)]
same = True
for pt in ([0, 0], [1, 2], [-3, 0.5], [2, -2]):
two = dense(dense(pt, W1, b1), W2, b2)[0]
one = dense(pt, Wc, bc)[0]
same = same and abs(two - one) < 1e-9
print("Linear stack equals one layer:", same)
with_relu = [dense(dense(pt, W1, b1, relu), W2, b2)[0] for pt in ([0, 0], [1, 2], [-3, 0.5], [2, -2])]
print("With ReLU outputs:", [round(v, 2) for v in with_relu])