Create tensors from existing values
Convert nested Python data or a scalar into a tensor for model inputs or numerical operations.
PyTorch · Function
Creates a new tensor by copying array-like data. It can configure dtype, device, autograd tracking, and pinned CPU memory.
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.
torch.tensor(data, *, dtype=None, device=None, requires_grad=False, pin_memory=False) → Tensor| Name | Type | Description |
|---|---|---|
data | array_like | Initial data for the tensor; accepts a list, tuple, NumPy ndarray, scalar, and other types. |
dtype | torch.dtype, optional | Desired data type; if None, inferred from data. |
device | torch.device, optional | Device for the constructed tensor; if None, uses the input tensor's device or the current device. |
requires_grad | bool, optional | Whether autograd records operations on the returned tensor. Default: False. |
pin_memory | bool, optional | Whether to allocate returned CPU storage in pinned memory. Default: False. |
Tensor
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.
Convert nested Python data or a scalar into a tensor for model inputs or numerical operations.
Create an independent tensor from another tensor without retaining its gradient history.
Prepare a gradient-tracking leaf from copied input data.
Allocate CPU data in pinned memory for suitable host-to-device workflows.
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.
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.
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_tensortorch.Tensor.clonetorch.Tensor.detachtorch.Tensor.requires_grad_torch.tensor_like