Skip to main content

How to Create a Tensor in PyTorch with torch.tensor

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A tensor is PyTorch's basic object: a grid of numbers, all of one type, that PyTorch can compute with on the processor or the GPU. The simplest way to make one is from a Python list. This guide does that with torch.tensor in PyTorch 2.15.0.

Syntax​

Syntax
torch.tensor(data, dtype=None, device=None)
PartWhat it means
dataThe values: a number, a list of numbers, or a list of lists of numbers, such as [[1, 2], [3, 4]]. PyTorch copies them into the new tensor
dtypeOptional, given by name. The data type of the values, such as torch.float32. Left out, PyTorch picks one from the values
deviceOptional, given by name. Where the tensor is made, such as "cpu". Left out, PyTorch uses its default device, the CPU

PyTorch's reference lists two more options, requires_grad and pin_memory, which this guide doesn't need.

The Example​

A script makes two tensors from lists: three temperatures, and a small table of two rows and three columns.

Prerequisites​

  • A uv project with PyTorch and NumPy. uv add torch numpy adds both; without NumPy, PyTorch 2.15.0 prints a warning each time it starts. This guide uses PyTorch 2.15.0 and NumPy 2.5.5 on Python 3.15.2, with uv 0.13.10, on an Apple M1 Pro.

Step 1: Write the Script​

create_tensor.py
import torch

temperatures = torch.tensor([21.5, 23.0, 19.8])
print(temperatures)

grid = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(grid)

The create_tensor.py script in a macOS-style code window: torch.tensor turns a list of three temperatures and a list of two lists into tensors, and print shows each one.

import torch loads PyTorch, whose package is called torch. A list of numbers gives a tensor with one row of values. A list of lists gives a table, one inner list per row, so grid has two rows of three numbers.

Step 2: Run It​

Terminal
uv run python create_tensor.py
Output
tensor([21.5000, 23.0000, 19.8000])
tensor([[1, 2, 3],
[4, 5, 6]])

The terminal after uv run python create_tensor.py: tensor([21.5000, 23.0000, 19.8000]) and a two-row tensor holding 1, 2, 3 and 4, 5, 6.

PyTorch prints a tensor as tensor(...) around its values. The temperatures print with four decimals, because PyTorch prints all the values of a tensor in one format. The table prints row under row, and its numbers have no decimal point: the list held only whole numbers, so PyTorch stored whole numbers. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Rows of different lengths, such as [[1, 2], [3]]ValueError: expected sequence of length 2 at dim 1 (got 1)Give every row the same number of values: a tensor is a full grid
Mixing whole numbers and decimals, such as [1, 2.5]Every value becomes a decimal: tensor([1.0000, 2.5000]), of type torch.float32Expect it: one tensor holds one data type
torch.Tensor([1, 2]), with a capital TIt printed tensor([1., 2.]): decimals from whole numbersUse torch.tensor, which printed tensor([1, 2])
torch.tensor(other_tensor) to copy a tensorUserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.detach().clone() or sourceTensor.detach().clone().requires_grad_(True), rather than torch.tensor(sourceTensor).other_tensor.detach().clone()

FAQ​

What's the difference between torch.tensor and torch.Tensor?​

torch.tensor is the function that builds a tensor from data. torch.Tensor is the tensor class itself, and calling it as a constructor is the old way: PyTorch's reference says "There is a legacy constructor torch.Tensor whose use is discouraged. Use torch.tensor() instead."

How do I check what PyTorch made?​

Every tensor knows its size, data type and device: temperatures.shape, temperatures.dtype and temperatures.device.

Conclusion​

torch.tensor([21.5, 23.0, 19.8]) made a tensor of three decimals, and torch.tensor([[1, 2, 3], [4, 5, 6]]) a table of two rows of whole numbers. PyTorch picked each tensor's data type from the values in the list.

Additional Resources​