ArticlesMathematics for AI

How Linear Algebra Powers Neural Networks

Follow three numbers through a tiny neural network and see what vectors, matrices, and activations do at each step.

Yash Bafna · · 30 min with examples · Beginner

On this page
  1. Vectors represent information
  2. How one neuron calculates a score
  3. A matrix computes an entire layer
  4. What a layer does to space
  5. Rank shows what a matrix can keep
  6. Why the activation matters
  7. Build a tiny neural network in NumPy
  8. Why this matters in AI engineering
  9. The layer equation, revisited
  10. References

A neural network can take three numbers and turn them into a score. How does it get there? It multiplies, adds, and applies a few simple rules. We’ll follow the same three numbers from a single neuron through a small network you can run in Python.

Here’s the formula we’re working toward. Start with x, the input numbers. W holds the weights that tell each neuron how to combine those numbers. Then add b, called the bias. Finally, σ (sigma) stands for an activation function, a rule applied to each score. The result is y, the output. We’ll unpack each step below.

y = σ(Wx + b)

Vectors represent information

A vector is a list of numbers in a specific order. Say a sensor gives us three readings, already scaled for the model: temperature, pressure, and vibration. We’ll write them as x = [2, −1, 3]ᵀ. Each reading is a feature, and the ᵀ here tells us to read the list as a column. The order matters because the model uses a different weight for each reading.

An embedding is a vector that a model learns to represent something, such as a word or a piece of an image. Its numbers usually don’t have names like temperature or pressure. During training, the model learns numbers that help it with its task. Similar inputs can end up with similar vectors.

Python · NumPy
import numpy as np
x = np.array([2, -1, 3])
print(x, x.shape)
print(x[:, None].shape)
[ 2 -1  3] (3,)
(3, 1)

In NumPy, shape (3,) means a flat list of three numbers. The expression x[:, None] gives it an extra axis, so its shape becomes (3, 1): three rows and one column. We’ll use the flat version in the code and column notation in the equations.

How one neuron calculates a score

Give each input a weight. Our first neuron uses the weights [1, −2, 1]. Multiply each input by its matching weight, then add the results. That’s a dot product. The neuron adds one more number, the bias b, to get its score z. Here b is 1. Notice that the second pair gives +2 because two negatives multiply to a positive.

z = wᵀx + b
  = (1)(2) + (−2)(−1) + (1)(3) + 1 = 8
  1. Feature × weight2 × 1, −1 × −2, 3 × 1
  2. Sum + bias2 + 2 + 3 + 1
  3. Score8
Each feature contributes one product. The neuron sums the products and adds its bias.
Python · NumPy
import numpy as np
x = np.array([2, -1, 3])
w = np.array([1, -2, 1])
b = 1
print(w @ x + b)
8

The score z tells us how strongly the neuron responds to this set of inputs. Different inputs can give the same score because the neuron adds the weighted values together. In the code, @ does the dot product.

A matrix computes an entire layer

A layer is a group of neurons. Add a second neuron with weights [0, 1, −1] and a bias of 0. Put each neuron’s weights in its own row of W. Now W is a matrix, a table with two rows and three columns. Each row uses the same three inputs to make one score. The two bias values in b are added to those two scores.

    ⎡ 1  −2   1 ⎤
W = ⎣ 0   1  −1 ⎦

x = [2, −1, 3]ᵀ
b = [1, 0]ᵀ

Wx = [7, −4]ᵀ
z  = Wx + b = [8, −4]ᵀ
(2 × 3)(3 × 1) → (2 × 1)
  1. Input x[2, −1, 3]ᵀ
  2. Each row of WRow 1 → 7 · Row 2 → −4
  3. Add b[8, −4]ᵀ
Two neurons share the same input. Each takes the dot product with a different row of W.
Python · NumPy
import numpy as np
x = np.array([2, -1, 3])
W = np.array([[1, -2, 1], [0, 1, -1]])
b = np.array([1, 0])
print(W.shape, x.shape, b.shape)
print(W @ x + b)
(2, 3) (3,) (2,)
[ 8 -4]

Each neuron can calculate its score independently. NumPy can do both calculations in one operation, W @ x, which saves us from writing a Python loop over the neurons.

What a layer does to space

Picture a grid drawn on a sheet of paper. Applying a matrix to every point can stretch the grid, rotate it, tilt it sideways (a shear), or flatten it onto a line (a projection). To see what a two-by-two matrix does, follow two arrows: one step right and one step up. The two columns of the matrix tell you where those arrows land.

A = [ a  b ; c  d ]
A[1, 0]ᵀ = [a, c]ᵀ     A[0, 1]ᵀ = [b, d]ᵀ
Matrix entries
Gray: input grid. Green: transformed grid. Red and blue: transformed unit basis vectors. Determinant: 1

Try a preset in the grid above. The gray lines show where the grid started. The green lines show where it ended up. In the projection preset, several lines land on top of each other. If you add a bias afterward, the whole new grid shifts by the same amount. The math term for a linear transformation followed by a shift is affine.

Follow the point (2, 1), two steps right and one up. The rotation preset sends it to (−1, 2). The projection preset sends it to (2, 0) by dropping the vertical coordinate. Try predicting where the two arrows will land before switching presets.

Rank shows what a matrix can keep

Rank counts how many independent directions a matrix can produce. On a flat grid, rank 2 means the output can still spread across the plane. Rank 1 means every output lies on a line. In the example below, the two columns of R are identical, so moving one step right or one step up gives the same result. The output loses the information needed to tell those inputs apart.

R[1, 0]ᵀ = R[0, 1]ᵀ = [1, 2]ᵀ
Rank 2 · planeRank 1 · line
On the right, the two transformed basis vectors overlap. The two independent directions on the left span a plane.
Python · NumPy
import numpy as np
R = np.array([[1, 1], [2, 2]])
print(np.linalg.matrix_rank(R))
print(R @ np.array([1, 0]), R @ np.array([0, 1]))
1
[1 2] [1 2]

A layer that turns three numbers into two has rank at most two. A narrow layer like this is called a bottleneck. It can help compress information if it learns to keep the details that matter for the task.

Rank also shows up in LoRA, a way to adapt a trained model. LoRA keeps the original weights fixed and learns a smaller change using two smaller matrices. Their product is a low-rank update, which limits what can change and reduces how many numbers the model needs to learn during adaptation.

Why the activation matters

Suppose we put two layers in a row and skip the activation. Each layer multiplies by a matrix and adds a bias. The equation below shows how we can combine both steps into a single matrix multiplication and one addition.

W₂(W₁x + b₁) + b₂
= (W₂W₁)x + W₂b₁ + b₂

ReLU gives the network more flexibility. It turns negative scores into zero and keeps positive scores as they are. So [8, −4] becomes [8, 0]. Which scores survive depends on the input, so the network can respond differently to different inputs in a way that one fixed matrix and bias generally can’t match.

ReLU(t) = max(0, t)
Python · NumPy
import numpy as np
z = np.array([8, -4])
print(np.maximum(0, z))
[8 0]

Build a tiny neural network in NumPy

Let’s build a network with three inputs, two neurons in the middle, and one final score. The middle layer is called a hidden layer because it sits between the input and output. Its two scores are [8, −4]. After ReLU, they become [8, 0]. The final layer uses weights [2, −1] and a bias of 1, so the score is 2 × 8 + (−1) × 0 + 1 = 17. Running an input through these steps is called a forward pass.

  1. Input[2, −1, 3]ᵀ
  2. Hidden affine map[8, −4]ᵀ
  3. ReLU[8, 0]ᵀ
  4. Output affine map17
A 3 → 2 → 1 forward pass. The hidden activation removes the negative coordinate.
Python · NumPy
import numpy as np
x = np.array([2, -1, 3])
W1, b1 = np.array([[1, -2, 1], [0, 1, -1]]), np.array([1, 0])
W2, b2 = np.array([[2, -1]]), np.array([1])
z1 = W1 @ x + b1
h = np.maximum(0, z1)
y = W2 @ h + b2
print(x.shape, W1.shape, h.shape, W2.shape, y.shape)
print(z1, h, y)
(3,) (2, 3) (2,) (1, 2) (1,)
[ 8 -4] [8 0] [17]

If your code gives an error, print the shapes after each step. Check how many features you feed in, how many weights each neuron has, and whether there’s one bias per neuron. Then compare the intermediate scores with the ones you worked out by hand.

Why this matters in AI engineering

Models often process several examples together in a batch. Put one example on each row of X, with three features per row. In NumPy, W.T swaps the rows and columns of W so those three input features line up with each neuron’s three weights. The result has one row per example and two scores per row. NumPy adds the same two bias values to each row automatically, which is called broadcasting.

Z = XWᵀ + b     (B × 3)(3 × 2) → B × 2
Python · NumPy
import numpy as np
X = np.array([[2, -1, 3], [1, 0, 2]])
W = np.array([[1, -2, 1], [0, 1, -1]])
b = np.array([1, 0])
Z = X @ W.T + b
print(Z.shape)
print(Z)
(2, 2)
[[ 8 -4]
 [ 4 -2]]

Working on a whole array at once is called vectorization. Batching can help a GPU process more examples per second because it can reuse the same weights. A larger batch also needs more memory, and collecting enough examples to fill it can make a request wait longer.

The layer equation, revisited

Read y = σ(Wx + b) again. Start with a list of input numbers. Each row of W combines those numbers into one score. Add a bias to each score, then apply the activation. Rank tells you how many independent directions the matrix step can pass along.

The same ideas show up in larger models. For example, a transformer uses dot products in attention to score how strongly one token should pay attention to another. When you meet a new operation, ask what numbers go in, what shape they have, and what comes out.

References