torch.Tensor — PyTorch 2.12 documentation
A torch.Tensor is a multi-dimensional matrix containing elements of a single data type. Please see torch.dtype for more details about dtype support. A tensor can be constructed from a Python list or sequence using the torch.tensor() constructor: Warning torch.tensor() always copies data. If you have a Tensor data and just want to change its requires_grad flag, use requires_grad_() or detach() to avoid a copy. If you have a numpy array and want to avoid a copy, use torch.as_tensor(). A tensor of specific data type can be constructed by passing a torch.dtype and/or a torch.device to a constructor or tensor creation op: For more information about building Tensors, see Creation Ops The contents of a tensor can be accessed and modified using Python’s indexing and slicing notation: Use torch.Tensor.item() to get a Python number from a tensor containing a single value: For more information about indexing, see Indexing, Slicing, Joining, Mutating Ops A tensor can be created with requires_grad=
torch.Tensor # Created On: Dec 23, 2016 | Last Updated On: Jun 27, 2025 A torch.Tensor is a multi-dimensional matrix containing elements of a single data type. Please see torch.dtype for more details about dtype support. Initializing and basic operations # A tensor can be constructed from a Python list or sequence using the torch.tensor() constructor: >>> torch . tensor ([[ 1. , - 1. ], [ 1. , - 1. ]]) tensor([[ 1.0000, -1.0000], [ 1.0000, -1.0000]]) >>> torch . tensor ( np . array ([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]])) tensor([[ 1, 2, 3], [ 4, 5, 6]]) Warning torch.tensor() always copies data . If
Explore this link on the map →related reading
- PyTorch internals : ezyang's blogblog.ezyang.com
- Tensors — PyTorch Tutorials 2.12.0+cu130 documentationpytorch.org
- Tensors — PyTorch Tutorials 2.13.0+cu130 documentationdocs.pytorch.org
- PyTorch (@PyTorch) / Xx.com
- Learning PyTorch with Examples — PyTorch Tutorials 2.12.0+cu130 documentationpytorch.org
- the bug that taught me more about PyTorch than years of using it | Elana Simonelanapearl.github.io
- torch.reshape — PyTorch 2.12 documentationdocs.pytorch.org
- Redirecting…docs.pytorch.org
- torch.empty — PyTorch 2.12 documentationdocs.pytorch.org
- Tensor Considered Harmfulnlp.seas.harvard.edu
- torch.ones_like — PyTorch 2.12 documentationdocs.pytorch.org
- torch.zeros_like — PyTorch 2.12 documentationdocs.pytorch.org