Creating Tensors with torch.tensor
A tensor is a multi-dimensional data container, and PyTorch tensors are data containers for array-like structures.
Asking How Many Dimensions
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. This article returns to one question again and again: how many dimensions does this data have? Once you can answer that question, the names scalar, vector, and matrix stop being a list of unrelated terms and become one simple idea.
What do you think happens?
How many dimensions does the object created by torch.tensor(5) have?
Reveal answer
Zero
The value 5 has no arrangement at all. A tensor with no arrangement is a scalar, which is a zero-dimensional tensor. Notice that the input is a bare number, not a bracketed list.
A Tensor as a Data Container
A tensor is a multi-dimensional data container. PyTorch tensors are data containers for array-like structures, and PyTorch provides a Tensor class for creating and working with such objects.
In plain terms, a tensor holds values arranged in a structured way. The arrangement is the important part. Two tensors can hold exactly the same number of values and still be different kinds of object, because one may arrange those values along a single dimension while the other arranges them along two. Tensors are the basic container you will pass around in nearly everything that follows in this module.
The source material also notes that Tensor objects can be created with the torch.tensor function. Whatever values you give to torch.tensor, the object it returns is classified by how many dimensions its arrangement has. The rest of this article follows that classification step by step.
Tracing a Scalar Through torch.tensor
Start at the bottom of the ladder. A scalar is a tensor with zero dimensions. It has no arrangement, just the value itself. When you pass a bare number to torch.tensor, the result is a scalar.
tensor(5)- The input 5 has no brackets, so there is no arrangement to describe
- The result is a scalar: a zero-dimensional tensor holding one value
Notice that the scalar holds exactly one value and needs zero positions to point at it. There is nothing to index into. This is the case the other shapes are compared against.
Reading Brackets as Dimensions
Brackets are the visible clue to a tensor's arrangement. Each bracket pair that wraps values adds an arrangement layer, and the number of layers you can read off the input is the dimension count of the result. The diagram below breaks down a two-level input so you can see which part of the bracket structure creates which dimension.
The outer pair of brackets creates the first arrangement. The inner pairs create the second. Because there are two arrangements, picking out a single value requires two positions, and the result is a two-dimensional tensor. The count of positions always matches the count of dimensions, which makes it a reliable check on your reasoning.
Vectors and Matrices Side by Side
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, with values arranged along two dimensions, so picking out one entry takes two positions. The names are simply readings of the dimension count, not separate kinds of container.
tensor([1, 2, 3])
tensor([[1, 2],
[3, 4]])- One level of brackets gives a vector: one position picks out an entry
- Two levels of brackets give a matrix: two positions pick out an entry
| Name | Dimensions | Positions needed for one entry | Example input |
|---|---|---|---|
| Scalar | 0 | 0 | 5 |
| Vector | 1 | 1 | [1, 2, 3] |
| Matrix | 2 | 2 | [[1, 2], [3, 4]] |
Each name is a reading of the dimension count
Naming a 3D Tensor and Beyond
The source gives no specific term for tensors with more than two dimensions, so the convention is to name them by dimension count. A tensor with three dimensions is called a 3D tensor, and the same pattern extends to any higher count. The diagram below shows the full ladder, starting from zero dimensions and climbing one step at a time.
tensor([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])- Three levels of brackets mean three arrangements
- Picking out one value requires three positions, so this is a 3D tensor
Read the input from the outside in. The outermost brackets hold two blocks, each block is a matrix-shaped group of values, and the innermost brackets hold the individual numbers. Three arrangements mean three positions to point at any single entry, so the result is a 3D tensor. Adding another level of brackets would move the result one step further up the ladder.
Mistakes When Counting Dimensions
Common mistake Classifying a tensor by how many values it holds
What it looks liketorch.tensor([1, 2, 3, 4, 5, 6]) holds six valuesWhy it failsThe source says to classify a tensor by its dimensions of arrangement, not by the number of values. Six values in one row of brackets still have a single arrangement.
Do this insteadThis is a vector, a one-dimensional tensor, because it has one arrangement with six entries along it.
Common mistake Treating a single value in brackets as a scalar
What it looks liketorch.tensor([5]) looks like it holds just the number 5
Why it failsA list holding one value still has an arrangement of its own. The scalar is the case with no arrangement at all.
Do this insteadtorch.tensor([5]) is a one-dimensional tensor, a vector. Only torch.tensor(5) without brackets is a scalar.
Common mistake Inventing a new name for every higher-dimensional case
What it looks likeCalling a 3-dimensional tensor a cube or a volume
Why it failsThe source names higher-dimensional tensors by dimension count rather than by new special words.
Do this insteadCall it a 3D tensor, and use the same pattern for any higher count.
Predicting the Dimension Count
Without running any code, predict the number of dimensions of the tensor created by torch.tensor([[7, 8, 9]]). Then name the tensor using the dimension ladder.
Hints
- Count the layers of brackets wrapping the values
- Ask how many positions you would need to point at the value 8
- Match your count to the scalar, vector, matrix, or 3D tensor names
Key Takeaways
- A tensor is a multi-dimensional data container, and PyTorch tensors are data containers for array-like structures created and handled through the Tensor class.
- Scalar, vector, and matrix are labels for zero, one, and two dimensions, forming a single ladder rather than unrelated terms.
- Classify a tensor by its dimensions of arrangement, not by how many values it holds.
- Higher-dimensional tensors have no special term in the source, so they are named by dimension count, such as a 3D tensor.
- Each level of brackets in a torch.tensor input adds one arrangement, and the number of positions needed to pick out one value equals the number of dimensions.