Skip to content

Commit

Permalink
Pushed file
Browse files Browse the repository at this point in the history
  • Loading branch information
avdhoottt committed Jan 23, 2025
1 parent 55c9f8c commit 1d4c4c5
Showing 1 changed file with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
Title: '.transpose()'
Description: 'Reorders dimensions of a tensor by swapping two specified dimensions.'
Subjects:
- 'AI'
- 'Data Science'
Tags:
- 'AI'
- 'Arrays'
- 'Data Structures'
- 'Deep Learning'
CatalogContent:
- 'intro-to-py-torch-and-neural-networks'
- 'paths/computer-science'
---

In PyTorch, the `.transpose()` operation creates a view of the input tensor with the specified dimensions swapped, commonly used for matrix operations and reshaping data for neural network inputs. Unlike `permute()`, `transpose()` only swaps two dimensions at a time.

## Syntax

```pseudo
tensor.transpose(dim0, dim1)
```

- `dim0`: First dimension to be transposed
- `dim1`: Second dimension to be transposed

## Example

The following code creates a 2x3 tensor with 2 rows and 3 columns, then uses `transpose()` to swap row and column dimensions, resulting in a 3x2 tensor where original rows become columns and vice versa:

```py
import torch

# Create a 2x3 tensor and transpose its dimensions
x = torch.tensor([[1, 2, 3],
[4, 5, 6]])
y = x.transpose(0, 1)
print(y)
```

The above code will result in the following output:

```shell
tensor([[1, 4],
[2, 5],
[3, 6]])
```

0 comments on commit 1d4c4c5

Please sign in to comment.