Introduction: why linear algebra?
Machine learning (ML) is about manipulating numbers. Lots of numbers. A language model like GPT processes billions of parameters, an image contains millions of pixels, a dataset can have thousands of columns.
To organize and manipulate all these numbers efficiently, you need structures. That's exactly what vectors, matrices, and tensors provide. Without them, no ML.
Linear algebra isn't a boring prerequisite. It's the native language of deep learning.
Vectors: ordered lists of numbers
Geometric view
A vector is an arrow in space. In 2D, the vector represents a displacement of 3 units to the right and 2 upward.
In 3D, you add a component: .
So far, nothing complicated. But the powerful idea is that you can generalize to any dimension. A vector in dimension 768 is just a list of 768 numbers. You can no longer draw it, but you can still manipulate it mathematically.
Numerical view
More concretely, a vector of dimension is an element of :
It's an ordered list of real numbers.
Vectors in machine learning
In ML, vectors are everywhere:
-
Feature vector: each example in the dataset is described by a vector. An apartment described by (area, number of rooms, floor, distance to subway) = a 4-dimensional vector.
-
Embeddings: a word, a sentence, or an image is represented by a dense vector. The word "cat" in Word2Vec is a 300-dimensional vector. In GPT, a token is a 12,288-dimensional vector.
-
Neuron weights: each neuron has a weight vector that determines the importance of each input.
-
Gradients: during training, the gradient of the loss with respect to parameters is a vector pointing in the direction of steepest ascent.
The useful analogy: a vector is a point in a space. Two words close in meaning have close vectors in the embedding space. It's this idea that allows a model to understand that "king" and "queen" are related.

Matrices: tables of numbers
Definition
A matrix is a rectangular table of numbers, with rows and columns. We denote it :
The element at row and column is denoted .
A column vector is a matrix of size . A row vector is a matrix of size . A scalar is a matrix. Matrices encompass everything.
Matrices in machine learning
Matrices are the natural format for data and models:
-
Dataset: a tabular dataset with examples and features is a matrix . Each row is an example, each column is a feature. 1,000 patients with 50 measurements each? A matrix.
-
Weight matrix: in a neural network, a linear layer transforms an input vector of dimension into an output vector of dimension . This transformation is entirely defined by a matrix .
-
Attention matrix: in a Transformer, attention scores form a matrix indicating how much each token "attends to" each other token.
-
Confusion matrix: in evaluation, it summarizes the correct and incorrect predictions of a classifier.
Square matrices and special types
Some types of matrices you'll encounter often:
- Square matrix (): same number of rows and columns
- Identity matrix : 1s on the diagonal, 0s everywhere else. It's the "neutral element" of matrix multiplication:
- Diagonal matrix: nonzero only on the diagonal
- Symmetric matrix: for all . Covariance matrices are symmetric.
Tensors: the generalization
A scalar is a dimension-0 tensor. A vector, dimension 1. A matrix, dimension 2.
A tensor is the generalization to any number of dimensions (called axes or ranks).

Why tensors are essential
In deep learning, you rarely work with simple matrices:
- Color image: height width 3 channels (RGB). That's a 3D tensor. A image contains about 150,000 numbers.

-
Batch of images: when you train a network, you process a batch of images at once. That gives a 4D tensor: .
-
Token sequence: in a Transformer, a batch of sequences of length with embeddings of dimension forms a 3D tensor: .
-
Video: a 5D tensor if you add time: .
Notation and conventions
In practice, with PyTorch or NumPy:
-
Dimensions are described by their shape. A tensor of shape
(32, 128, 768)represents a batch of 32 sequences of 128 tokens, each encoded by a 768-dimensional vector. -
The order of dimensions follows conventions. In PyTorch, images are often in
(B, C, H, W)format, batch, channels, height, width. In TensorFlow, it's often(B, H, W, C). -
The memory size is the product of dimensions. A
(32, 128, 768)tensor of type float32 (4 bytes) occupies MB.
Notation and general conventions
Some standard conventions in ML:
| Notation | Meaning |
|---|---|
| or | Scalar (lowercase, italic) |
| or | Vector (lowercase, bold) |
| or | Matrix (uppercase, bold) |
| Higher-order tensor (calligraphic) | |
| The -th example in the dataset | |
| The -th feature | |
| Weight matrix of layer |
These conventions vary by author, but this base covers the majority of papers and courses.
Why this matters for AI
All information in a deep learning model is stored in tensors. Input data, model weights, intermediate activations, gradients, everything is a tensor.
When you read that GPT-4 has hundreds of billions of parameters, those are numbers organized in gigantic weight matrices. When we talk about semantic embeddings, those are vectors in a high-dimensional space.
Mastering vectors, matrices, and tensors means understanding the data format that AI manipulates. It's the essential first step.
Where this is used. The perceptron, the smallest network building block, is already a vector-matrix product.