Concepts / Tensor Shapes and Dimensions in PyTorch

Tensor Shapes and Dimensions in PyTorch

A tensor is a multi-dimensional data container, and PyTorch tensors are data containers for array-like structures.

  • Programming

Predicting a Tensor's Dimension Count

When you start working with PyTorch, the first data structure you meet is the tensor. Before worrying about any particular operation, it helps to have a clear picture of what kind of object a tensor is and how the different names used for it relate to one another. This article approaches that picture by asking one question again and again: how many dimensions does this data have?

What do you think happens?

Before reading on, guess how many dimensions a tensor created from a single number such as 5 has. Is it zero, one, or two?

  • Zero dimensions

  • One dimension

  • Two dimensions

Reveal answer
Answer:

Zero dimensions

A tensor with zero dimensions is called a scalar. A single value has no arrangement at all, so there is no position you need to point at. The value 5 on its own is a scalar.

Tensors as Data Containers

Definition

A tensor is a multi-dimensional data container. PyTorch tensors are data containers for array-like structures, meaning they hold values arranged in a structured way. PyTorch provides a Tensor class for creating and working with such objects.

Tensors are the basic container you will pass around in nearly everything that follows in this module. The key idea is that a tensor is not just a pile of numbers. It is a container whose values sit in a definite arrangement, and that arrangement is what the dimension count describes.

Climbing the Dimension Ladder

The names scalar, vector, and matrix are not separate kinds of containers with unrelated rules. They are labels for tensors that have a particular number of dimensions. A tensor with zero dimensions is called a scalar, a tensor with one dimension is called a vector, and a tensor with two dimensions is called a matrix. Each name is simply a way of reading off the dimension count.

Seen this way, the vocabulary becomes a single ladder rather than a list of unrelated terms. Start at the bottom with zero dimensions and climb one dimension at a time. Each step up produces the next named case. The diagram below shows the same ladder read from the top down, where each level holds the level beneath it.

3D tensor3 dimensionsMatrix2 dimensionsVector1 dimensionScalar0 dimensionsholds matricesholds vectorsholds scalars
How does adding a dimension wrap the previous structure, so that a vector holds scalars, a matrix holds vectors, and a 3D tensor holds matrices?

Read the diagram as a rule you can apply to any tensor. Adding one dimension means the new structure is a collection of the previous structure. A vector is a collection of scalar values. A matrix is a collection of vectors. A 3D tensor is a collection of matrices.

Scalars, Vectors, and Matrices Side by Side

A scalar is a zero-dimensional tensor. It is the value itself with no arrangement around it. A vector is a one-dimensional tensor. Its values lie along a single dimension, so you need only one position to point at any one entry. A matrix is a two-dimensional tensor. Its values are arranged along two dimensions, so picking out one entry takes two positions.

NameNumber of dimensionsPositions needed to pick one entryTypical description
Scalar00A single value with no arrangement
Vector11Values lying along one dimension
Matrix22Values arranged along two dimensions

The name is determined by the dimension count, and the number of positions needed always matches it

Scalar0 dimensions, one valueVector1 dimension, values in alineMatrix2 dimensions, values in agrid
How do a scalar, a vector, and a matrix differ in their number of dimensions and shape, and what does each look like side by side?

The number of positions you need is the same as the number of dimensions, which is a useful way to check your reasoning. If you are unsure whether a tensor is a vector or a matrix, ask how many positions you would need to point at a single entry. One position means a vector. Two positions means a matrix.

Counting Positions to Address an Entry

Worked example

Locating one entry in a vector and in a matrix

A vector holds the values 10, 20, and 30. A matrix holds two rows, where the first row is 1, 2, 3 and the second row is 4, 5, 6. How many positions does each tensor need to pick out a single entry, and what does that tell you about its dimensions?

  1. 1
    Identify the dimension count of the vector

    The values lie along a single line, so the vector is a one-dimensional tensor. Only one position is needed to point at any entry, such as the position holding 20.

  2. 2
    Identify the dimension count of the matrix

    The values are arranged along two directions, the row and the column. The matrix is a two-dimensional tensor, and picking out the value 5 takes two positions: row 2 and column 2.

  3. 3
    Check the rule

    In both cases the number of positions equals the number of dimensions. One position for the vector and two positions for the matrix.

Result

The vector is one-dimensional and the matrix is two-dimensional. The number of positions needed to address one entry always equals the dimension count.

Naming Higher-Dimensional Tensors

Once you pass two dimensions, there is no specific term in the source material. Rather than inventing a new word for each case, the convention is to name a tensor by its dimension count. A tensor with three dimensions is called a 3D tensor, and the same pattern extends to any higher count. The naming scheme stays consistent as the data grows more complex.

The source presents tensors being created with the torch.tensor function. The naming ideas apply directly to the objects you create that way. Whatever values you pass to torch.tensor, the resulting object is classified by how many levels of arrangement it has. The code below uses nested brackets to build tensors of increasing dimension count.

python
Notes on the code
  • scalar_t has no brackets around its value, so it is zero-dimensional
  • vector_t has one level of brackets, so it is one-dimensional
  • matrix_t has two levels of brackets, so it is two-dimensional
  • tensor3d_t has three levels of brackets, so it is a 3D tensor
torch.tensor(creates a Tensor objectouter bracketsdimension 0: the twomatricesmiddle bracketsdimension 1: the rowsinside one matrixinner bracketsdimension 2: the valuesinside one row
Which level of brackets corresponds to which dimension, and what does each level group together?

Each level of brackets corresponds to one dimension. The outermost level groups the whole 3D tensor into its matrices, the middle level separates the rows within one matrix, and the innermost level holds the individual values of one row. Counting the bracket levels gives you the same dimension count you would get by counting positions.

Counting Dimensions, Not Values

  • Common mistake

    Treating a single value in brackets as a scalar

    What it looks like

    torch.tensor([7]) is written with brackets, so it looks like a single number but is not one.

    Why it fails

    A list holding a single value still has an arrangement of its own, so it counts as one-dimensional. The scalar is the case with no arrangement at all.

    Do this instead

    Use torch.tensor(7) without brackets for a scalar, and torch.tensor([7]) for a one-dimensional tensor.

  • Common mistake

    Classifying by the number of values

    What it looks like

    torch.tensor([1, 2, 3, 4, 5, 6]) and torch.tensor([[1, 2, 3], [4, 5, 6]]) both hold six values.

    Why it fails

    The first has one arrangement, so it is a vector. The second has two arrangements, so it is a matrix. Both hold six values, so the value count cannot tell them apart.

    Do this instead

    Count the levels of arrangement, meaning the dimensions, before deciding the name.

  • Common mistake

    Inventing a special word for each higher-dimensional case

    Why it fails

    The source gives no specific term beyond two dimensions, so the convention is to name the tensor by its dimension count.

    Do this instead

    Call a tensor with three dimensions a 3D tensor, and apply the same pattern to any higher count.

Classifying Tensors by Dimension Count

EASY

For each of these tensors, decide whether it is a scalar, a vector, a matrix, or a 3D tensor, based on the number of bracket levels: torch.tensor(9), torch.tensor([9, 8, 7, 6]), torch.tensor([[9, 8], [7, 6], [5, 4]]), and torch.tensor([[[9]]]). Explain your reasoning in terms of dimensions rather than values.

Hints
  • Count the bracket levels around the values, starting from zero for a bare number.
  • The second tensor holds four values but has only one level of brackets.
  • The last tensor has three levels of brackets even though it holds only one value.

Key Takeaways

  • A tensor is a multi-dimensional data container, and PyTorch tensors are data containers for array-like structures.
  • A scalar is a zero-dimensional tensor, a vector is one-dimensional, and a matrix is two-dimensional. These names are labels for the dimension count.
  • Higher-dimensional tensors have no specific term in the source, so they are named by dimension, such as a 3D tensor.
  • To classify a tensor, count its dimensions of arrangement rather than the number of values it holds.

Related concepts

More concepts starting with T