Concepts / Manipulating Tensor Shapes

Manipulating Tensor Shapes

torch.tensor() creates a PyTorch tensor from a Python value that you pass to it as an argument.

  • Programming

Why Tensor Creation Comes First

A tensor is the basic data container you will work with throughout PyTorch, so the first practical skill is creating one. Everything you need to understand about creation can be read from a short listing that creates four tensors using one function, torch.tensor(). Before reading on, take a guess: what do you think the input to torch.tensor() controls?

The core definition is short. torch.tensor() creates a PyTorch tensor from a Python value that you pass to it as an argument. Every creation follows the same path: you write a Python value, pass it to torch.tensor(), and assign the returned tensor to a name. The shape of the tensor is decided at the moment of creation, by the value you pass in. This article shows how to read that value so you can predict the dimensions before you run the code.

Tracing Four Tensors from One Function

The listing begins by importing the library and then makes four assignments. Read the lines from top to bottom and notice what changes from one line to the next.

python

The first line, tensor0d = torch.tensor(1), passes a single Python integer. The result is a zero-dimensional tensor, also called a scalar. A scalar holds one value and has no axes of its own. A Python integer goes in and a tensor comes out.

The second line passes a Python list, [1, 2, 3]. The result is a one-dimensional tensor, or vector. The three values sit side by side in a single row of data. The third and fourth lines extend the same idea by nesting lists inside lists. A list of lists produces a two-dimensional tensor, and a list of lists of lists produces a three-dimensional tensor. The four inputs are the exact inputs you can check against the code.

Read the four inputs from left to right. Each step adds one level of nesting, and the dimension count rises with it. The leftmost input has no brackets at all, so it is the only one that is not a list.

Counting Brackets to Predict Dimensions

The listing gives you a reliable method for predicting dimensions. Count the levels of square brackets in the input before you run the code. A bare number has no brackets and yields a zero-dimensional tensor. One level of brackets, as in a flat list, yields a one-dimensional tensor. Two levels yield a two-dimensional tensor, and three levels yield a three-dimensional tensor.

Reading the input from left to right, the outer pair of brackets wraps the whole value, and each inner pair wraps one row. The number of bracket levels that wrap the innermost numbers is the dimension count. In this input the innermost numbers sit two levels deep, so the tensor is two-dimensional.

Finding Where an Input Diverges

Sometimes the tensor you get is not the one you planned. Bracket counting helps you locate the exact point where the input departs from your expectation. The rule is the same as before: the number of bracket levels around the innermost numbers sets the dimension count, so any extra or missing pair of brackets changes the result.

Worked example

Locating an Unexpected Extra Dimension

You want a one-dimensional tensor holding the numbers 4, 5, and 6, so you write torch.tensor([[4, 5, 6]]). What dimension count does this produce, and which bracket is responsible?

  1. 1
    State your expectation

    A flat list of three values, as in [4, 5, 6], has one level of brackets and produces a one-dimensional tensor.

  2. 2
    Count the bracket levels in the actual input

    The input [[4, 5, 6]] has an outer pair of brackets and an inner pair around the three values, so there are two levels.

  3. 3
    Find the divergence

    The extra outer pair of brackets is the one that does not match your expectation. It adds a second level of nesting.

  4. 4
    Predict the result

    Two levels of brackets yield a two-dimensional tensor, so the tensor is two-dimensional rather than one-dimensional.

Result

The input produces a two-dimensional tensor. The extra outer pair of brackets is where the structure diverges from the one-dimensional tensor you expected. Removing that outer pair restores a one-dimensional tensor.

The same method works in the other direction. If you expected a two-dimensional tensor but the input has only one level of brackets, the missing pair is where the structure diverges. Always compare the bracket depth of the input with the depth you intended, and look for the first level where the two disagree.

Testing Your Bracket Reading

What do you think happens?

How many dimensions does torch.tensor([[[7]]]) produce?

  • 0

  • 1

  • 2

  • 3

Reveal answer
Answer:

3, a three-dimensional tensor

The input has three levels of square brackets wrapping the innermost number 7. Three levels yield a three-dimensional tensor.

EASY

Without running the code, predict the dimension count for each of these inputs: torch.tensor(5), torch.tensor([5, 6]), and torch.tensor([[5, 6]]). Then write one sentence explaining which bracket, if any, makes the second and third inputs differ.

Hints
  • A bare number has no brackets, so it has no dimensions beyond the scalar level.
  • Count the levels of square brackets wrapped around the innermost numbers, not the number of values.
  • Compare the bracket depth of torch.tensor([5, 6]) with torch.tensor([[5, 6]]) and look for the extra pair.

Common Mistakes with Bracket Depth

  • Common mistake

    Counting the values instead of the brackets

    What it looks like

    Assuming torch.tensor([1, 2, 3, 4, 5, 6]) is two-dimensional because it holds six numbers.

    Why it fails

    The dimension count depends on how many levels of square brackets wrap the innermost numbers. Six values inside one pair of brackets still form one level.

    Do this instead

    Count only the bracket levels. This input has one level, so it is one-dimensional.

  • Common mistake

    Overlooking an outer pair of brackets

    What it looks like

    Writing torch.tensor([[4, 5, 6]]) when you meant a one-dimensional tensor, and being surprised by the result.

    Why it fails

    The extra outer pair adds a second level of nesting, which raises the dimension count to two.

    Do this instead

    Before running the code, trace each opening bracket and make sure the depth matches the dimension count you intended.

  • Common mistake

    Treating a bare number as a list

    What it looks like
    Expecting torch.tensor(1) to behave like a one-element list.
    Why it fails

    A bare Python integer has no brackets and yields a zero-dimensional tensor, also called a scalar.

    Do this instead

    Use a bare number for a scalar, and add one pair of brackets only when you want a one-dimensional tensor.

Key Takeaways

  • torch.tensor() creates a PyTorch tensor from the Python value you pass to it, and that value determines the tensor's shape at creation time.
  • A bare Python integer produces a zero-dimensional tensor, or scalar; a flat list produces a one-dimensional tensor, or vector.
  • Each added level of nested lists raises the dimension count, so a list of lists gives a two-dimensional tensor and a list of lists of lists gives a three-dimensional tensor.
  • To predict dimensions, count the levels of square brackets around the innermost numbers before running the code.
  • When a tensor's dimensions surprise you, compare the bracket depth of the input with the depth you intended to find the exact point of divergence.

Related concepts

More concepts starting with M