Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Class

torch.Tensor

torch.Tensor is PyTorch’s multidimensional container for values sharing one data type. Create tensors from existing data with torch.tensor() or use factory operations to control shape, dtype, device, and related properties.

How it works

A tensor stores values of one data type in a multidimensional structure. Create one from existing array-like data with torch.tensor(), or use factory functions for a chosen shape, dtype, device, or related tensor; indexing and slicing access or modify its contents, while requires_grad=True enables autograd tracking.

Syntax

class torch.Tensor(*args, **kwargs)

Key points

  • A tensor is a multidimensional value container whose elements use one dtype.
  • Use torch.tensor() to build a tensor from nested Python sequences or other existing array-like data.
  • Creation operations can explicitly select the tensor's dtype and device.
  • Python indexing and slicing provide element and sub-tensor access, including mutation.
  • Enabling requires_grad makes operations on the tensor eligible for automatic-differentiation recording.
  • Tensor data is backed by an associated storage object.

Common mistakes

  • Do not use torch.Tensor(...) as the general-purpose data constructor; prefer torch.tensor(...) or an appropriate tensor factory.
  • Passing a tensor to the legacy constructor can produce an alias rather than an independent copy.
  • Sequence input through the legacy constructor uses the default dtype unless another creation approach specifies the dtype.

Example: Create and differentiate a tensor

import torch

x = torch.tensor([[1.0, 2.0], [3.0, 4.0]], requires_grad=True)
y = (x * 2).sum()
y.backward()
print(x.shape)
print(x.grad)

Creates a floating-point tensor, records operations for automatic differentiation, and computes gradients.

When to use it

Deep-learning data

Representing model inputs, parameters, intermediate activations, and outputs.

Numerical computation

Performing vector, matrix, and higher-dimensional numeric operations.

Automatic differentiation

Building differentiable computations for training.

Device and dtype control

Selecting storage placement and numeric representation for computation.

Interview questions

QWhat is a torch.Tensor?

torch.Tensor represents a multidimensional matrix whose elements share one data type. A tensor also provides indexing, slicing, numeric operations, and metadata such as dtype, device, and layout.

Follow-up: How would you preserve an existing tensor's properties while changing its shape? Use a suitable tensor operation or a *_like creation operation when creating a related tensor.

QHow should tensors be created?

Use torch.tensor(data) when you already have array-like values. For a specified shape, use tensor factory functions such as torch.empty; for matching another tensor's size and similar types, use *_like factories.

Follow-up: Why avoid torch.Tensor(data)? It is a legacy constructor with behavior that depends on the input and is specifically discouraged by the documentation.

QHow does a tensor participate in automatic differentiation?

Set requires_grad=True when creating a tensor if operations on it should be recorded for automatic differentiation. The resulting computation can then participate in gradient propagation, as shown by calling backward() on a scalar result.

Follow-up: What determines whether an operation is tracked? Autograd tracking depends on the tensors involved and their gradient requirements, not merely on the tensor's numeric dtype.

  • torch.tensor
  • torch.dtype
  • torch.device
  • torch.Storage
  • torch.empty
  • torch.Tensor.new_*
  • torch.Tensor.*_like

Source: torch.Tensor — PyTorch 2.8 documentation. Checked 2026-10-15.