Deep-learning data
Representing model inputs, parameters, intermediate activations, and outputs.
PyTorch · Class
torch.Tensor is PyTorch’s multidimensional container for values sharing one data type. Create tensors from existing data with torch.tensor() or use factory operations to control shape, dtype, device, and related properties.
A tensor stores values of one data type in a multidimensional structure. Create one from existing array-like data with torch.tensor(), or use factory functions for a chosen shape, dtype, device, or related tensor; indexing and slicing access or modify its contents, while requires_grad=True enables autograd tracking.
class torch.Tensor(*args, **kwargs)torch.tensor() to build a tensor from nested Python sequences or other existing array-like data.requires_grad makes operations on the tensor eligible for automatic-differentiation recording.torch.Tensor(...) as the general-purpose data constructor; prefer torch.tensor(...) or an appropriate tensor factory.import torch
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]], requires_grad=True)
y = (x * 2).sum()
y.backward()
print(x.shape)
print(x.grad)Creates a floating-point tensor, records operations for automatic differentiation, and computes gradients.
Representing model inputs, parameters, intermediate activations, and outputs.
Performing vector, matrix, and higher-dimensional numeric operations.
Building differentiable computations for training.
Selecting storage placement and numeric representation for computation.
torch.Tensor?torch.Tensor represents a multidimensional matrix whose elements share one data type. A tensor also provides indexing, slicing, numeric operations, and metadata such as dtype, device, and layout.
Follow-up: How would you preserve an existing tensor's properties while changing its shape? Use a suitable tensor operation or a *_like creation operation when creating a related tensor.
Use torch.tensor(data) when you already have array-like values. For a specified shape, use tensor factory functions such as torch.empty; for matching another tensor's size and similar types, use *_like factories.
Follow-up: Why avoid torch.Tensor(data)? It is a legacy constructor with behavior that depends on the input and is specifically discouraged by the documentation.
Set requires_grad=True when creating a tensor if operations on it should be recorded for automatic differentiation. The resulting computation can then participate in gradient propagation, as shown by calling backward() on a scalar result.
Follow-up: What determines whether an operation is tracked? Autograd tracking depends on the tensors involved and their gradient requirements, not merely on the tensor's numeric dtype.
torch.tensortorch.dtypetorch.devicetorch.Storagetorch.emptytorch.Tensor.new_*torch.Tensor.*_like