transpose() and t() in PyTorch
Tags: pytorch, transpose, t, function
Published: 2024-07-12 14:47:35

*Memos:
- [My post](https://dev.to/hyperkai/adjoint-mh-and-mt-in-pytorch-5c04) explains [adjoint()](https://pytorch.org/docs/stable/generated/torch.adjoint.html), [mH](https://pytorch.org/docs/stable/tensors.html#torch.Tensor.mH) and [mT](https://pytorch.org/docs/stable/tensors.html#torch.Tensor.mT).
- [My post](https://dev.to/hyperkai/reshape-and-view-in-pytorch-40n5) explains [reshape()](https://pytorch.org/docs/stable/generated/torch.reshape.html) and [view()](https://pytorch.org/docs/stable/generated/torch.Tensor.view.html).
- [My post](https://dev.to/hyperkai/permute-in-pytorch-5d49) explains [permute()](https://pytorch.org/docs/stable/generated/torch.permute.html).
- [My post](https://dev.to/hyperkai/movedim-in-pytorch-5gla) explains [movedim()](https://pytorch.org/docs/stable/generated/torch.movedim.html).

[transpose()](https://pytorch.org/docs/stable/generated/torch.transpose.html) can get the 0D or more D transposed tensor of zero or more elements without losing data from the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `transpose()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) or a tensor.
- The 1st argument(`input`) with `torch` or using a tensor(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).
- The 2nd argument with `torch` or the 1st argument with a tensor is `dim0`(Required-Type:`int`).
- The 3rd argument with `torch` or the 2nd argument with a tensor is `dim1`(Required-Type:`int`).
- [swapaxes()](https://pytorch.org/docs/stable/generated/torch.swapaxes.html) and [swapdims()](https://pytorch.org/docs/stable/generated/torch.swapdims.html) are the aliases of `transpose()`.

```python
import torch

my_tensor = torch.tensor([[[0, 1, 2], [3, 4, 5]],
                          [[6, 7, 8], [9, 10, 11]],
                          [[12, 13, 14], [15, 16, 17]],
                          [[18, 19, 20], [21, 22, 23]]])
torch.transpose(input=my_tensor, dim0=0, dim1=0)
my_tensor.transpose(dim0=0, dim1=0)
torch.transpose(input=my_tensor, dim0=1, dim1=1)
torch.transpose(input=my_tensor, dim0=2, dim1=2)
torch.transpose(input=my_tensor, dim0=1, dim1=-2)
torch.transpose(input=my_tensor, dim0=2, dim1=-1)
torch.transpose(input=my_tensor, dim0=2, dim1=-2)
torch.transpose(input=my_tensor, dim0=-1, dim1=2)
torch.transpose(input=my_tensor, dim0=-2, dim1=1)
torch.transpose(input=my_tensor, dim0=-1, dim1=-1)
torch.transpose(input=my_tensor, dim0=-2, dim1=-2)
# tensor([[[0, 1, 2], [3, 4, 5]],
#         [[6, 7, 8], [9, 10, 11]],
#         [[12, 13, 14], [15, 16, 17]],
#         [[18, 19, 20], [21, 22, 23]]])

torch.transpose(input=my_tensor, dim0=0, dim1=1)
torch.transpose(input=my_tensor, dim0=1, dim1=0)
torch.transpose(input=my_tensor, dim0=0, dim1=-2)
torch.transpose(input=my_tensor, dim0=-2, dim1=0)
# tensor([[[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]],
#         [[3, 4, 5], [ 9, 10, 11], [15, 16, 17], [21, 22, 23]]])

torch.transpose(input=my_tensor, dim0=0, dim1=2)
torch.transpose(input=my_tensor, dim0=2, dim1=0)
torch.transpose(input=my_tensor, dim0=0, dim1=-1)
torch.transpose(input=my_tensor, dim0=-1, dim1=0)
# tensor([[[0, 6, 12, 18], [3, 9, 15, 21]],
#         [[1, 7, 13, 19], [4, 10, 16, 22]],
#         [[2, 8, 14, 20], [5, 11, 17, 23]]])

torch.transpose(input=my_tensor, dim0=1, dim1=2)
torch.transpose(input=my_tensor, dim0=2, dim1=1)
torch.transpose(input=my_tensor, dim0=1, dim1=-1)
torch.transpose(input=my_tensor, dim0=-1, dim1=1)
torch.transpose(input=my_tensor, dim0=-1, dim1=-2)
torch.transpose(input=my_tensor, dim0=-2, dim1=-1)
torch.transpose(input=my_tensor, dim0=-2, dim1=2)
# tensor([[[0, 3], [1, 4], [2, 5]],
#         [[6, 9], [7, 10], [8, 11]],
#         [[12, 15], [13, 16], [14, 17]],
#         [[18, 21], [19, 22], [20, 23]]])

my_tensor = torch.tensor([[[0., 1., 2.], [3., 4., 5.]],
                          [[6., 7., 8.], [9., 10., 11.]],
                          [[12., 13., 14.], [15., 16., 17.]],
                          [[18., 19., 20.], [21., 22., 23.]]])
torch.transpose(input=my_tensor, dim0=0, dim1=0)
# tensor([[[0., 1., 2.], [3., 4., 5.]],
#         [[6., 7., 8.], [9., 10., 11.]],
#         [[12., 13., 14.], [15., 16., 17.]],
#         [[18., 19., 20.], [21., 22., 23.]]])

my_tensor = torch.tensor([[[0.+0.j, 1.+0.j, 2.+0.j],
                           [3.+0.j, 4.+0.j, 5.+0.j]],
                          [[6.+0.j, 7.+0.j, 8.+0.j],
                           [9.+0.j, 10.+0.j, 11.+0.j]],
                          [[12.+0.j, 13.+0.j, 14.+0.j],
                           [15.+0.j, 16.+0.j, 17.+0.j]],
                          [[18.+0.j, 19.+0.j, 20.+0.j],
                           [21.+0.j, 22.+0.j, 23.+0.j]]])
torch.transpose(input=my_tensor, dim0=0, dim1=0)
# tensor([[[0.+0.j, 1.+0.j, 2.+0.j],
#          [3.+0.j, 4.+0.j, 5.+0.j]],
#         [[6.+0.j, 7.+0.j, 8.+0.j],
#          [9.+0.j, 10.+0.j, 11.+0.j]],
#         [[12.+0.j, 13.+0.j, 14.+0.j],
#          [15.+0.j, 16.+0.j, 17.+0.j]],
#         [[18.+0.j, 19.+0.j, 20.+0.j],
#          [21.+0.j, 22.+0.j, 23.+0.j]]])

my_tensor = torch.tensor([[[True, False, True], [True, False, True]],
                          [[False, True, False], [False, True, False]],
                          [[True, False, True], [True, False, True]],
                          [[False, True, False], [False, True, False]]])
torch.transpose(input=my_tensor, dim0=0, dim1=0)
# tensor([[[True, False, True], [True, False, True]],
#         [[False, True, False], [False, True, False]],
#         [[True, False, True], [True, False, True]],
#         [[False, True, False], [False, True, False]]])
```

[t()](https://pytorch.org/docs/stable/generated/torch.t.html) can get the 0D, 1D or 2D transposed tensor of zero or more elements without losing data from the 0D, 1D or 2D tensor of zero or more elements as shown below:

*Memos:
- `t()` can be used with `torch` or a tensor.
- The 1st argument(`input`) with `torch` or using a tensor(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).
- `t()` is equivalent to `transpose(input=my_tensor, dim0=0, dim1=1)`.

```python
import torch

my_tensor = torch.tensor(0)

torch.t(input=my_tensor)
my_tensor.t()
# tensor(0)

my_tensor = torch.tensor([0, 1, 2])

torch.t(input=my_tensor)
# tensor([0, 1, 2])

my_tensor = torch.tensor([[0, 1, 2], [3, 4, 5],
                          [6, 7, 8], [9, 10, 11]])
torch.t(input=my_tensor)
# tensor([[0, 3, 6, 9],
#         [1, 4, 7, 10],
#         [2, 5, 8, 11]])

my_tensor = torch.tensor([[0., 1., 2.], [3., 4., 5.],
                          [6., 7., 8.], [9., 10., 11.]])
torch.t(input=my_tensor)
# tensor([[0., 3., 6., 9.],
#         [1., 4., 7., 10.],
#         [2., 5., 8., 11.]])

my_tensor = torch.tensor([[0.+0.j, 1.+0.j, 2.+0.j],
                          [3.+0.j, 4.+0.j, 5.+0.j],
                          [6.+0.j, 7.+0.j, 8.+0.j],
                          [9.+0.j, 10.+0.j, 11.+0.j]])
torch.t(input=my_tensor)
# tensor([[0.+0.j, 3.+0.j, 6.+0.j, 9.+0.j],
#         [1.+0.j, 4.+0.j, 7.+0.j, 10.+0.j],
#         [2.+0.j, 5.+0.j, 8.+0.j, 11.+0.j]])

my_tensor = torch.tensor([[True, False, True], [True, False, True],
                          [False, True, False], [False, True, False]])
torch.t(input=my_tensor)
# tensor([[True, True, False, False],
#         [False, False, True, True],
#         [True, True, False, False]])
```