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

PyTorch · Function

torch.tensor

Creates a new tensor by copying array-like data. It can configure dtype, device, autograd tracking, and pinned CPU memory.

Since
1.0.0

How it works

torch.tensor creates a new tensor by copying array-like input, so the result starts without autograd history. Optional keywords select its data type, device, gradient tracking, and pinned CPU memory allocation.

Syntax

torch.tensor(data, *, dtype=None, device=None, requires_grad=False, pin_memory=False) → Tensor

Parameters

NameTypeDescription
dataarray_likeInitial data for the tensor; accepts a list, tuple, NumPy ndarray, scalar, and other types.
dtypetorch.dtype, optionalDesired data type; if None, inferred from data.
devicetorch.device, optionalDevice for the constructed tensor; if None, uses the input tensor's device or the current device.
requires_gradbool, optionalWhether autograd records operations on the returned tensor. Default: False.
pin_memorybool, optionalWhether to allocate returned CPU storage in pinned memory. Default: False.

Returns

Tensor

Key points

  • The function copies its input into a new leaf tensor without inheriting autograd history.
  • The data argument accepts common array-like inputs, including Python sequences, NumPy arrays, and scalars.
  • Use dtype to request the returned tensor's element type; otherwise it is inferred from the input.
  • Use device to control where the new tensor is allocated.
  • Set requires_grad=True when operations on the result should participate in automatic differentiation.
  • pin_memory requests pinned allocation for the returned tensor and defaults to disabled.

Common mistakes

  • For an existing tensor, prefer the explicit clone, detach, and requires_grad_ methods when their intent is clearer.
  • pin_memory=True is supported only when the returned tensor is a CPU tensor.

Example: Create a tensor from existing data

import torch

values = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32)
print(values)

Creates a new floating-point tensor by copying nested-list data.

When to use it

Create tensors from existing values

Convert nested Python data or a scalar into a tensor for model inputs or numerical operations.

Detach and copy tensor data

Create an independent tensor from another tensor without retaining its gradient history.

Start a differentiable computation

Prepare a gradient-tracking leaf from copied input data.

Use pinned CPU memory

Allocate CPU data in pinned memory for suitable host-to-device workflows.

Interview questions

QWhat does torch.tensor do with its input?

It copies the supplied array-like data and returns a new tensor with no autograd history, making it a leaf tensor. The input may be a list, tuple, NumPy array, scalar, or other supported array-like value.

Follow-up: How does tensor input differ from preserving history? torch.tensor(t) is equivalent to t.detach().clone(), so it does not retain t's autograd history.

QHow do dtype and device defaults work?

dtype controls the returned tensor's data type; when omitted, PyTorch infers it from data. device controls placement, using the input tensor's device when data is a tensor, or the current device otherwise.

Follow-up: What happens when requires_grad=True? Operations on the returned tensor are recorded by autograd, and the copied tensor becomes a gradient-tracking leaf.

QWhat do requires_grad and pin_memory control?

requires_grad defaults to False and determines whether autograd records operations on the result. pin_memory defaults to False and requests pinned allocation, but that option works only for CPU tensors.

Follow-up: Can pin_memory=True place a tensor on a GPU? No; it controls pinned CPU allocation rather than selecting a GPU device.

  • torch.as_tensor
  • torch.Tensor.clone
  • torch.Tensor.detach
  • torch.Tensor.requires_grad_
  • torch.tensor_like

Source: torch.tensor — PyTorch 2.12 documentation. Checked 2026-10-15.