PyTorch .t()
Published Sep 26, 2024
Contribute to Docs
In PyTorch, the .t() method returns the transpose of a given 2D tensor. If the tensor is a 0D or 1D tensor, the method returns it as it is.
Syntax
torch.t(ten)
ten: The tensor to be transposed.
Example
The following example demonstrates the usage of the .t() method:
import torch# Define a tensorten = torch.tensor([[1, 2, 3],[4, 3, 8]])# Calculate the transpose of the tensorout = torch.t(ten)print(out)
The above code produces the following output:
tensor([[1, 4],[2, 3],[3, 8]])
Codebyte Example
The following codebyte example shows the use of the .t() method:
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
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
- Intermediate.3 hours