Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Term Entry] PyTorch Tensor Operations: .swapaxes() #5896

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
Title: '.swapaxes()'
Description: 'Swaps two specified axes (dimensions) of a tensor, effectively rearranging its shape.'
Subjects:
- 'Computer Science'
- 'Machine Learning'
- 'Data Science'
Tags:
- 'AI'
- 'Data Types'
- 'Deep Learning'
- 'Functions'
CatalogContent:
- 'intro-to-py-torch-and-neural-networks'
- 'paths/data-science'
---

In PyTorch, the **`.swapaxes()`** function swaps two specified axes (dimensions) of a tensor.

## Syntax

```pseudo
torch.swapaxes(input, axis0, axis1)
```

- `input`: The tensor input array.
- `axis0`: The first axis to be swapped.
- `axis1`: The second axis to be swapped.

## Example

The following example demonstrates the usage of the `.swapaxes()` function:

```py
import torch

# Create a tensor
x = torch.tensor([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])

# Swap the axes (0 and 1)
swapped1 = torch.swapaxes(x, 0, 1)
print("After swapping axes 0 and 1:")
print(swapped1)

# Swap the axes (0 and 2)
swapped2 = torch.swapaxes(x, 0, 2)
print("\nAfter swapping axes 0 and 2:")
print(swapped2)
```

The output of the above code is as follows:

```shell
After swapping axes 0 and 1:
tensor([[[0, 1],
[4, 5]],

[[2, 3],
[6, 7]]])

After swapping axes 0 and 2:
tensor([[[0, 4],
[2, 6]],

[[1, 5],
[3, 7]]])
```