.permute()
Anonymous contributor
Published Dec 23, 2024
Contribute to Docs
In PyTorch, the .permute()
function returns a view of a given tensor with its dimensions permuted or rearranged according to a specific order.
Syntax
torch.permute(input, dims)
input
: The tensor whose dimensions are to be permuted.dims
: The order in which the dimensions are to be permuted.
Example
The following example demonstrates the usage of the .permute()
function:
import torch# Create a tensor of size (2, 3, 4)ten = torch.randn(2, 3, 4)# Permute the dimensions of the tensor in the order (2, 0, 1)res = torch.permute(ten, (2, 0, 1))# Print the size of the resultant tensorprint(res.size())
In the above example, the order (2, 0, 1)
:
- Moves the dimension located at index
2
to index0
- Moves the dimension located at index
0
to index1
- Moves the dimension located at index
1
to index2
The above code produces the following output:
torch.Size([4, 2, 3])
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Free course
Intro to PyTorch and Neural Networks
Learn how to use PyTorch to build, train, and test artificial neural networks in this course.Intermediate3 hours