Efficient tensor normalization
Convert existing tensors without unnecessary allocation when their dtype and device already match.
PyTorch · Function
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.
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.
torch.as_tensor(data: Any, *, dtype: torch.dtype | None = None, device: torch.device | None = None) -> torch.Tensor| Name | Type | Description |
|---|---|---|
data | array_like | Initial data for the tensor; it may be a list, tuple, NumPy ndarray, scalar, or other supported type. |
dtype | torch.dtype, optional | Desired data type of the returned tensor. If None, it is inferred from data. |
device | torch.device, optional | Device for the constructed tensor. If None, tensor input keeps its device; other input uses the current device. |
A torch.Tensor containing the converted data.
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.
Convert existing tensors without unnecessary allocation when their dtype and device already match.
Wrap compatible NumPy arrays while retaining shared storage.
Convert input data while selecting an explicit dtype or device.
Keep autograd behavior when adapting existing tensors.
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.
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.
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