AI Run Lab

All chapters

Scalars, vectors and tensors

The shape of an array tells you what can be multiplied. The meaning of each axis tells you what that multiplication does.

Open this chapter with experiments and audio

What does each position mean?

A scalar is one number. A vector is an ordered set of numbers. A matrix arranges numbers along two axes, and a tensor may have additional axes. For a house, a vector might record [floor area, bedroom count]. The vector [80, 3] has two positions with different units. Rearranging them without adapting the weights changes the prediction.

For language, common axes are batch, token position and feature. A tensor with shape (4, 16, 32) holds four sequences with sixteen positions and thirty-two values per position. It contains 4 × 16 × 32 = 2,048 numbers. The shape alone does not say whether those numbers are model parameters or the temporary output of a layer.

How do two vectors produce one score?

In a dot product you multiply matching entries and add the results. With inputs [1, 2] and weights [5, 7], the first contribution is 1 × 5 = 5, the second is 2 × 7 = 14. Summing them gives 19. A positive weight makes that feature raise the score when its input increases. A negative weight makes the same increase lower the score.

Keep the weights fixed and change one input by one unit. The change in score equals the corresponding weight. Then set that input to zero. Its contribution disappears, even if its weight is large. This is a direct way to distinguish the current input from the persistent setting applied to it.

Why multiply matrices?

A layer often needs several scores for the same example. Put three weight vectors side by side as columns of a matrix. A single row of two inputs can then produce three outputs. Stack four input rows and the same weight matrix transforms all four examples at once. This reuse matters because the learned rule must apply consistently across examples.

(4 × 2) · (2 × 3) → (4 × 3)

The matching dimensions of size two identify the features that get paired and summed. Four rows and three columns survive. The twelve outputs are newly computed values. The weight matrix still contains six parameters. Doubling the batch length doubles the output size while leaving these six numbers shared.

A bias vector can contain one number per output column. In the four-by-three output, adding a length-three bias to every row shifts the same three features in each example. Frameworks call compatible reuse across axes broadcasting. One set of three learned biases suffices for all four examples. This is why the batch size disappears when you count the parameters of a layer.

At the initial settings, row one of A is [1, 2, 3] and column one of B is [1, 2, 3]. Their dot product gives 14. Change only the number of rows in B to 2. The missing partner for the third input explains exactly why this product is undefined.

Verify one row in Python

features = [1, 2]
columns = [[5, 7], [1, 0], [-2, 3]]
scores = [sum(x * w for x, w in zip(features, col))
          for col in columns]
print(scores)

The result is [19, 1, 4]. The middle output simply copies the first input. The last subtracts twice the first input and adds three times the second. In a model, training can change those combinations. The computation that evaluates all three retains the same structure.

Can a 4 × 2 matrix directly multiply a 3 × 2 matrix in this order?

The two inner dimensions would be 2 and 3. Transposing the second matrix yields shape 2 × 3 and makes this operation possible if the axes then have the intended meanings.

PyTorch · Tensors

What does a tensor axis preserve?

Two rooms contain a door sensor and a window sensor. In one minute they report [2, 3] openings in room A and [4, 1] in room B. Store the readings as two rows. The first axis identifies rooms, the second identifies sensors. A score of door openings minus window openings uses the same weights [1, −1] in both rooms.

rooms = [[2, 3], [4, 1]]
weights = [1, -1]
scores = [sum(x * w for x, w in zip(row, weights)) for row in rooms]
print(scores)

Room A contributes 2 × 1 + 3 × (−1) = −1. Room B contributes 4 × 1 + 1 × (−1) = 3. The resulting vector has one value per room. Multiplication pairs matching sensor positions, and summation removes the sensor axis. The room axis survives. This is the operation hidden in the compact expression Xw.

If you transpose this square matrix, will a shape check catch the mistake?

Both shapes are 2 × 2. The new rows [2, 4] and [3, 1] yield [−2, 2], comparing rooms within one sensor type. The arithmetic is valid but answers a different question. A third room makes the axis swap visible from the shape. Axis names and one hand-calculated example work even for a square.

Change the matrix dimensions and follow the axis that disappears →

Further reading · PyTorch · Tensors