RB×N×d\mathbb{R}^{B \times N \times d}

Tensor — numbers on any number of axes

An array of numbers with any number of axes. In code every array is called a tensor: a scalar has 0 axes, a vector 1, a matrix 2. In prose the word usually means 3 or more.

Each extra axis is one more index. A stack of matrices is a 3-axis tensor: a batch of BB sentences, each NN tokens by dmodeld_{\text{model}} numbers, has shape B×N×dmodelB \times N \times d_{\text{model}}. A color image is another: height by width by 3 color channels (section 25.2).

Multiply the sizes to count the numbers: a 2×3×42 \times 3 \times 4 tensor holds 24.

Two meanings of rank. Some texts call the number of axes a tensor's rank or order. Section 20.2 uses rank for something else, the number of independent directions in a matrix. This course says "number of axes" to keep them apart; PyTorch calls it ndim.

Example

X∈R2×3×4  ⟹  2⋅3⋅4=24 numbers\mathcal{X} \in \mathbb{R}^{2 \times 3 \times 4} \;\Longrightarrow\; 2 \cdot 3 \cdot 4 = 24 \text{ numbers}

Two sentences, three tokens each, four numbers per token. PyTorch shape (2, 3, 4).

Where the course uses it

Related terms