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

PyTorch · Module

tensor.item

Converts a one-element tensor into its value as a standard Python number. The operation is not differentiable and is intended for scalar extraction.

How it works

Call item() on a tensor containing exactly one element to obtain its value as a regular Python number. The conversion is intended for scalar extraction and does not participate in automatic differentiation.

Syntax

torch.Tensor.item() → number

Returns

A standard Python number containing the value of the one-element tensor.

Key points

  • The method extracts the tensor’s scalar value as a native Python number.
  • The input must contain exactly one element, regardless of whether it is zero-dimensional or shaped as a single-value tensor.
  • Use tolist() when the tensor contains multiple values or a nested Python representation is needed.
  • Calling item() exits differentiable tensor computation for the extracted result.

Common mistakes

  • Calling item() on a tensor with multiple elements is invalid; select or reduce to one element first.
  • The returned Python number cannot be used to propagate gradients through the original tensor.

Example: Extract a scalar

import torch

x = torch.tensor([1.0])
value = x.item()
print(value)  # 1.0

Converts a one-element tensor into a standard Python number.

When to use it

Print or record a scalar metric as a Python value.

Logging

Pass a single tensor value to an API that expects a native numeric type.

Interop

Use one computed tensor value in ordinary Python logic when gradient tracking is not needed.

Control flow

Interview questions

QWhat does torch.Tensor.item() do?

Tensor.item() converts a one-element tensor into a standard Python number. It is useful when an ordinary Python scalar is required instead of a tensor.

Follow-up: What happens for a tensor with several elements? item() is not the appropriate operation; use indexing, reduction, or tolist() depending on the intended result.

QWhat shape or size constraint does item() have?

The tensor must contain exactly one element. A tensor with more than one element cannot be converted by item().

Follow-up: How can a collection of values be converted instead? Use tolist(), which is the alternative identified by the documentation for other cases.

QHow does item() interact with differentiation?

item() is not differentiable, so its result does not preserve an autograd path. Use it when leaving tensor-based computation is intentional.

Follow-up: Why might calling item() inside a training computation be undesirable? It extracts a Python value and therefore cannot support gradient propagation through that value.

  • torch.Tensor.tolist
  • torch.Tensor

Source: torch.Tensor.item — PyTorch 2.14 documentation. Checked 2026-10-16.