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

PyTorch · Function

torch.as_tensor

Converts array-like data to a tensor, sharing storage and preserving autograd history when possible. Matching tensors can be returned directly; dtype or device differences cause conversion.

How it works

torch.as_tensor converts array-like input into a tensor while avoiding a copy when the input already has the requested representation. For tensors it can return the original object, preserving autograd history; changing dtype or device causes a conversion. NumPy arrays can share storage when compatible, and CuPy arrays retain their device unless overridden.

Syntax

torch.as_tensor(data: Any, *, dtype: torch.dtype | None = None, device: torch.device | None = None) -> torch.Tensor

Parameters

NameTypeDescription
dataarray_likeInitial data for the tensor; it may be a list, tuple, NumPy ndarray, scalar, or other supported type.
dtypetorch.dtype, optionalDesired data type of the returned tensor. If None, it is inferred from data.
devicetorch.device, optionalDevice for the constructed tensor. If None, tensor input keeps its device; other input uses the current device.

Returns

A torch.Tensor containing the converted data.

Key points

  • It converts supported array-like input while preferring storage sharing and retention of autograd history.
  • A tensor already matching both requested attributes can be returned without creating another tensor object.
  • A mismatch in dtype or device triggers a conversion equivalent to calling the tensor's to method.
  • The data argument accepts common Python containers, scalars, NumPy arrays, and other supported array-like values.
  • Leaving dtype unset makes PyTorch infer the result type from the supplied data.
  • With tensor input and no explicit device, placement follows the input tensor.

Common mistakes

  • Passing a tensor may return the very same object, so mutations through either reference affect the same tensor.
  • Compatible NumPy input can share storage; modifying the array can therefore modify the tensor.
  • CuPy input normally determines the output device, but an explicit device or default device can override it.
  • Use torch.tensor when an independent copy and a fresh leaf tensor are required; as_tensor is designed to preserve history and avoid copies.

Example: Create a tensor with a chosen dtype

import torch

values = [1, 2, 3]
tensor = torch.as_tensor(values, dtype=torch.float32)
values[0] = 9

print(tensor)  # tensor([1., 2., 3.])

Creates a tensor from a Python list with the requested dtype. The list is copied because the documented sharing behavior applies to tensors and supported external array types.

When to use it

Efficient tensor normalization

Convert existing tensors without unnecessary allocation when their dtype and device already match.

Interop with NumPy

Wrap compatible NumPy arrays while retaining shared storage.

Control representation and placement

Convert input data while selecting an explicit dtype or device.

Preserve computation history

Keep autograd behavior when adapting existing tensors.

Interview questions

QWhen can torch.as_tensor return the original tensor?

It converts array-like data to a tensor and shares data when possible. If a tensor already has the requested dtype and device, the same tensor is returned; otherwise conversion occurs.

Follow-up: How does this differ from torch.tensor? torch.tensor always copies its input and creates a new leaf tensor, whereas as_tensor can preserve autograd history and avoid copies.

QHow does torch.as_tensor interact with NumPy arrays?

For a NumPy ndarray with a compatible dtype and device, PyTorch constructs the result through torch.from_numpy(), allowing storage sharing. A dtype or device mismatch prevents that exact compatible path.

Follow-up: What practical consequence follows from sharing? Changes made through the compatible NumPy array can be visible through the tensor, so ownership and mutation must be managed carefully.

QWhat do dtype and device control?

dtype requests the output data type; if omitted, PyTorch infers it from the input. device selects placement; for tensor input, an omitted device uses the input device, while non-tensor input uses the current device.

Follow-up: What happens when a tensor's requested dtype or device differs? PyTorch copies it as though data.to(dtype=dtype, device=device) had been called.

  • torch.tensor()
  • torch.from_numpy()
  • Tensor.to()
  • Autograd mechanics

Source: torch.as_tensor — PyTorch main documentation. Checked 2026-10-16.