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

VLA · Prerequisite · Mathematics for Deep Learning

M1.1 — Linear Algebra: Vectors, Matrices and Matrix Multiplication

How data is represented as arrays, and how a neural network layer transforms it.

  • Prerequisite module 1
  • Mathematics for Deep Learning
  • Essential
  • About 12 minutes to read

Every model in the VLA roadmap, from a vision encoder to a transformer to an action head, is built from one operation repeated many times: multiplying arrays of numbers. This lesson gives you that operation and the habit of checking its shapes. No robotics knowledge is needed.

Learning objectives

After this lesson, you should be able to:

  • Describe a vector and a matrix, and read their shapes.
  • Compute a dot product.
  • Multiply a matrix by a vector, and a matrix by a matrix, by hand on small examples.
  • Say whether two shapes can be multiplied, and what shape the result has.
  • Explain a neural network layer as a matrix multiplication plus an offset.
  • Explain why the order of matrix multiplication matters.
  1. Data as a vector (a list of numbers)
  2. A layer’s weights as a matrix
  3. Matrix multiplication transforms the vector
  4. A nonlinearity, then the next layer
  5. The output: a new vector
The picture to keep in mind: a neural network passes a vector through a chain of matrix multiplications.

The one-sentence idea

A neural network layer takes a vector of numbers, multiplies it by a matrix of learned numbers, and returns a new vector.

Learn to read shapes, and you can read most model code.

Why this matters for VLA models

A Vision-Language-Action model works with very different kinds of information. Linear algebra gives all of it the same form:

  • A camera image is an array of pixel values, and an image encoder turns it into vectors.
  • A word or instruction becomes a list of vectors, one per token.
  • The robot’s state, such as its joint angles, is a vector.
  • The action the model outputs, such as the next joint targets or gripper command, is a vector.

Once everything is a vector or a matrix, the model can transform it with the same operation everywhere. The attention mechanism you will meet in module 3 is also built from matrix multiplications and dot products.

Vectors

A vector is an ordered list of numbers. The number of entries is its dimension, and its shape is written (n,) for n entries. The order matters: the first number always means the same thing.

A robot arm’s joint angles (radians)
[0.10, -0.50, 1.20]: three joints, so a 3-dimensional vector.
One RGB pixel
[255, 0, 0]: red, green and blue values, so a pure red pixel.
A word embedding
A list of, for example, a few hundred numbers that a model has learned to represent a word. Module 3 covers how.

You can add two vectors of the same size entry by entry, and scale a vector by multiplying every entry by one number:

Vector addition and scaling
[1, 2, 3]+[4, 5, 6]=[5, 7, 9][1,\,2,\,3]+[4,\,5,\,6]=[5,\,7,\,9]
2×[1, 2, 3]=[2, 4, 6]2\times [1,\,2,\,3]=[2,\,4,\,6]

The dot product

The dot product of two vectors of the same size multiplies matching entries and adds the results into a single number:

A dot product example
[1, 2, 3]⋅[4, 5, 6]=1×4+2×5+3×6=32[1,\,2,\,3]\cdot [4,\,5,\,6]=1\times 4+2\times 5+3\times 6=32

The dot product measures how much two vectors point the same way. Two vectors that line up give a large positive value, and two that are perpendicular give zero. This is why models use it as a similarity score: comparing an image embedding with a text embedding, or a query with a key inside attention, comes down to dot products.

Matrix multiplication, which comes next, is just many dot products arranged in a table.

Matrices

A matrix is a table of numbers with rows and columns. Its shape is written (rows, columns), so a matrix with 2 rows and 3 columns is (2, 3), often said “2 by 3”.

A 2 by 3 matrix
W=[120013]W = \begin{bmatrix}1 & 2 & 0\\ 0 & 1 & 3\end{bmatrix}
Shape (2, 3): two rows, three columns.

Matrices appear in two main roles:

  • As data. A grayscale image is a matrix of brightness values. A batch of vectors, one per row, is a matrix too.
  • As a transformation. A matrix can act on a vector and produce a new one. The weights of a neural network layer are exactly such a matrix.

Matrix times vector

To multiply a matrix by a vector, take the dot product of each row of the matrix with the vector. Each row produces one entry of the result.

Matrix times vector worked example
[120013][211]=[1×2+2×1+0×10×2+1×1+3×1]=[44]\begin{bmatrix}1 & 2 & 0\\ 0 & 1 & 3\end{bmatrix}\begin{bmatrix}2\\ 1\\ 1\end{bmatrix} = \begin{bmatrix}1\times2 + 2\times1 + 0\times1\\ 0\times2 + 1\times1 + 3\times1\end{bmatrix} = \begin{bmatrix}4\\ 4\end{bmatrix}

The input vector has 3 entries and the matrix has 3 columns, so they fit. The matrix has 2 rows, so the result has 2 entries. A (2, 3) matrix has turned a 3-dimensional vector into a 2-dimensional one. In general, a matrix with m rows and n columns takes an n-dimensional vector to an m-dimensional one.

The matrix is a function: vector in, vector out.

Matrix times matrix

Multiplying two matrices repeats this idea: each entry of the result is the dot product of a row of the first matrix with a column of the second.

Matrix times matrix worked example, A times B
A=[1234]B=[0110]A = \begin{bmatrix}1 & 2\\ 3 & 4\end{bmatrix}\qquad B = \begin{bmatrix}0 & 1\\ 1 & 0\end{bmatrix}
A times B
AB=[1⋅0+2⋅11⋅1+2⋅03⋅0+4⋅13⋅1+4⋅0]=[2143]AB = \begin{bmatrix}1\cdot0+2\cdot1 & 1\cdot1+2\cdot0\\ 3\cdot0+4\cdot1 & 3\cdot1+4\cdot0\end{bmatrix} = \begin{bmatrix}2 & 1\\ 4 & 3\end{bmatrix}

Now reverse the order:

B times A
BA=[3412]BA = \begin{bmatrix}3 & 4\\ 1 & 2\end{bmatrix}

A × B and B × A are different. Here B swaps columns when it is on the right, and swaps rows when it is on the left. Matrix multiplication is not commutative: the order is part of the meaning. (It is associative, so (A×B)×C = A×(B×C), which is why a chain of layers can be grouped in any way.)

Checking shapes

You can check a multiplication without calculating anything. The inner dimensions must match, and the outer dimensions give the result shape:

The shape rule for matrix multiplication
(m, n)×(n, p)→(m, p)(m,\,n)\times (n,\,p)\to (m,\,p)
└─ must match ─┘
Shape checks for four multiplications
MultiplicationValid?Result shape
(4, 3) × (3, 5)Yes, inner sizes are both 3(4, 5)
(2, 3) × (3,)Yes(2,)
(4, 3) × (4, 5)No, 3 does not equal 4Error
(3, 5) × (4, 3)No, 5 does not equal 4Error

In practice, “shapes cannot be multiplied” is one of the most common errors in deep learning code, and reading the shapes is how you find the cause.

The same operation in NumPy is written with the @ operator. This is the example from above, and you can run it:

import numpy as np

W = np.array([[1, 2, 0],
              [0, 1, 3]])      # shape (2, 3)
x = np.array([2, 1, 1])        # shape (3,)
print(W @ x, (W @ x).shape)

Output:

[4 4] (2,)

A neural network layer

A basic linear layer applies a weight matrix W to its input vector x and then adds a vector of offsets b called the bias:

A linear layer
y=W x+by=W\,x+b

If x has 512 entries and the layer should produce 256 outputs, then W has shape (256, 512) and b has 256 entries. The entries of W and b are the learned parameters: training adjusts them, which later modules cover. The layer itself is nothing more than the multiplication you just did.

Models normally process many examples at once, called a batch. The batch is stored as a matrix with one example per row, and one multiplication handles all of them. One practical note: some libraries, including PyTorch’s nn.Linear, store the weights as (out, in) and compute x Wᵀ + b on row-vector batches. It is the same maths with the arrays arranged differently, which is one more reason to read shapes carefully.

Why layers need a nonlinearity

Suppose you stack two linear layers with no extra step between them:

Two stacked linear maps collapse into one
W2(W1 x)=(W2 W1) xW_{2}(W_{1}\,x)=(W_{2}\,W_{1})\,x

Because matrix multiplication is associative, the two layers collapse into a single matrix W₂W₁. Stacking more would not make the model any more expressive. Real networks therefore apply a nonlinear function after each linear layer, such as the common ReLU, which sets negative values to zero. That is what lets a deep network represent complicated patterns. Module 2 returns to this when it covers neural networks.

A robotics example: rotating a point

Matrices are also how robots describe geometry. In two dimensions, rotating a point by 90° counter-clockwise about the origin is a matrix multiplication:

Rotating the point (1, 0) by 90 degrees
[0−110][10]=[01]\begin{bmatrix}0 & -1\\ 1 & 0\end{bmatrix}\begin{bmatrix}1\\ 0\end{bmatrix} = \begin{bmatrix}0\\ 1\end{bmatrix}
Rotating (1, 0) by 90° counter-clockwise.

The point (1, 0) on the x-axis moves to (0, 1) on the y-axis. Changing coordinate frames and moving a robot’s hand are built from matrices like this one, and module 5 covers them properly. For now, the useful observation is that the same tool describes both a neural network layer and a geometric transformation.

Common misconceptions

Misconception 1 “Matrix multiplication multiplies matching entries.”

Correction Multiplying matching entries is a different operation, called element-wise multiplication. Matrix multiplication takes dot products of rows with columns.

Misconception 2 “The order doesn’t matter, as with ordinary numbers.”

Correction In general A×B and B×A differ, and one of them may not even be defined. The shapes are different.

Misconception 3 “A matrix is only a picture or a table of data.”

Correction Matrices store data, but they are also functions that transform vectors. A layer’s weights are a matrix used as a function.

Misconception 4 “More linear layers always make a more powerful model.”

Correction Without a nonlinearity between them, any number of linear layers is equivalent to one matrix.

Engineering takeaways

  1. A vector is an ordered list of numbers, and a matrix is a table of numbers. Both have a shape.
  2. The dot product multiplies matching entries and sums them, and works as a similarity score.
  3. Matrix times vector takes the dot product of each row with the vector.
  4. Matrix times matrix needs matching inner dimensions, and the result takes the outer ones.
  5. The order of multiplication matters.
  6. A linear layer is y = Wx + b, with W and b learned.
  7. Nonlinearities between layers stop a stack of layers collapsing into one.
  8. When code fails with a shape error, write the shapes down and check the inner dimensions.

Read the shapes first: they tell you what a multiplication can do before you calculate a single number.

Knowledge check

Try each question first, then reveal the explanation.

Question 1

A matrix A has shape (4, 3) and a matrix B has shape (3, 5). Can you compute A × B? What is the result shape? What about B × A?

Explanation

A × B is valid and has shape (4, 5). The inner dimensions are both 3. B × A is not valid: its inner dimensions are 5 and 4, which do not match.

Question 2

A layer maps a 100-dimensional input to a 20-dimensional output with y = Wx + b. What are the shapes of W and b?

Explanation

W is (20, 100) and b has 20 entries. A (20, 100) matrix takes a 100-dimensional vector to a 20-dimensional one, and the bias is added to the 20 outputs.

Question 3

Why is a network made of ten linear layers with no nonlinearity no more expressive than a single linear layer?

Explanation

Because the ten matrices multiply together into one matrix. Matrix multiplication is associative, so W₁₀ ⋯ W₂ W₁ is a single matrix, and the network can only do what that one matrix does. A nonlinear step between layers prevents this.

What’s next

You can now read vectors, matrices and shapes. Next is how a network learns the numbers inside its matrices.

Next lesson

M1.2 — Calculus: derivatives, gradients and the chain rule

How a small change in a parameter changes the loss, and how that flows backwards through a network.

Back to the VLA roadmap prerequisites