python-pytorchHow do I determine the shape of a tensor in Python using PyTorch?
To determine the shape of a tensor in Python using PyTorch, we can use the shape
attribute of the tensor. For example, given a tensor x
:
import torch
x = torch.randn(2, 3)
We can use the shape
attribute to determine its shape:
print(x.shape)
Output example
torch.Size([2, 3])
The shape
attribute returns a torch.Size
object, which is a tuple of the dimensions of the tensor. In this example, x
has two dimensions, with the first dimension having a size of 2 and the second dimension having a size of 3.
Code explanation
import torch
: imports the PyTorch libraryx = torch.randn(2, 3)
: creates a tensorx
with two dimensions of size 2 and 3 respectivelyprint(x.shape)
: prints the shape of the tensorx
Helpful links
More of Python Pytorch
- How can I use Yolov5 with PyTorch?
- How can I use Python, PyTorch, and YOLOv5 to build an object detection model?
- How do I use Pytorch with Python 3.11 on Windows?
- How can I use Python and PyTorch to parse XML files?
- How can I enable CUDA support in Python PyTorch?
- How do I install a Python PyTorch .whl file?
- How do I use PyTorch with Python version 3.11?
- How can I use PyTorch with Python 3.11?
- How do I update PyTorch using Python?
- How can I use Python and PyTorch to create an XOR gate?
See more codes...