.t()

Anonymous contributor's avatar
Anonymous contributor
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 tensor
ten = torch.tensor([[1, 2, 3],
[4, 3, 8]])
# Calculate the transpose of the tensor
out = 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:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn PyTorch on Codecademy