adjoint(), mH and mT in PyTorch
Tags: pytorch, adjoint, mh, mt
Published: 2024-07-12 14:49:03

*Memos:
- [My post](https://dev.to/hyperkai/transpose-and-t-in-pytorch-1pp2) explains [transpose()](https://pytorch.org/docs/stable/generated/torch.transpose.html) and [t()](https://pytorch.org/docs/stable/generated/torch.t.html).
- [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).

[adjoint()](https://pytorch.org/docs/stable/generated/torch.adjoint.html) or [mH](https://pytorch.org/docs/stable/tensors.html#torch.Tensor.mH) can get the view of the 2D or more D transposed and conjugated tensor of zero or more elements without losing data from the 2D or more D tensor of zero or more elements as shown below:

- `adjoint()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) or a tensor while `mH` can be used with a tensor but not with `torch`.
- For `adjoint()`, the 1st argument(`input`) with `torch` or using a tensor(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).
- For `mH`, using a tensor(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).
- `adjoint()` is equivalent to `mH`, equivalent to `transpose(-2, -1)` for an `int`, `float` or `bool` tensor and equivalent to `transpose(-2, -1).conj()` for a `complex` tensor.
- `adjoint()` or `mH` can also get the 0D tensor of zero or more elements but it's deprecated.

```python
import torch

my_tensor = torch.tensor([[0, 1, 2], [3, 4, 5],
                          [6, 7, 8], [9, 10, 11]])
torch.adjoint(input=my_tensor)
my_tensor.adjoint()
my_tensor.mH
# 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]],
                          [[12, 13, 14], [15, 16, 17]],
                          [[18, 19, 20], [21, 22, 23]]])
torch.adjoint(input=my_tensor)
my_tensor.mH
# 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.adjoint(input=my_tensor)
my_tensor.mH
# 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.+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.adjoint(input=my_tensor)
my_tensor.mH
# tensor([[[0.-0.j, 3.-0.j], [1.-0.j, 4.-0.j], [2.-0.j, 5.-0.j]],
#         [[6.-0.j, 9.-0.j], [7.-0.j, 10.-0.j], [8.-0.j, 11.-0.j]],
#         [[12.-0.j, 15.-0.j], [13.-0.j, 16.-0.j], [14.-0.j, 17.-0.j]],
#         [[18.-0.j, 21.-0.j], [19.-0.j, 22.-0.j], [20.-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.adjoint(input=my_tensor)
my_tensor.mH
# tensor([[[True, True], [False, False], [True, True]],
#         [[False, False], [True, True], [False, False]],
#         [[True, True], [False, False], [True, True]],
#         [[False, False], [True, True], [False, False]]])
```

[mT](https://pytorch.org/docs/stable/tensors.html#torch.Tensor.mT) can get the view of the 2D or more D transposed tensor of zero or more elements without losing data from the 2D or more D tensor of zero or more elements as shown below:

- `mT` can be used with a tensor but not with `torch`.
- Using a tensor(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).
- `mT` can also get the 0D tensor of zero or more elements but it's deprecated.

```python
import torch

my_tensor = torch.tensor([[0, 1, 2], [3, 4, 5],
                          [6, 7, 8], [9, 10, 11]])
my_tensor.mT
# 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]],
                          [[12, 13, 14], [15, 16, 17]],
                          [[18, 19, 20], [21, 22, 23]]])
my_tensor.mT
# 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.]]])
my_tensor.mT
# 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.+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.mT
# tensor([[[0.+0.j, 3.+0.j], [ 1.+0.j, 4.+0.j], [2.+0.j, 5.+0.j]],
#         [[6.+0.j, 9.+0.j], [7.+0.j, 10.+0.j], [8.+0.j, 11.+0.j]],
#         [[12.+0.j, 15.+0.j], [13.+0.j, 16.+0.j], [14.+0.j, 17.+0.j]],
#         [[18.+0.j, 21.+0.j], [19.+0.j, 22.+0.j], [20.+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]]])
my_tensor.mT
# tensor([[[True, True], [False, False], [True, True]],
#         [[False, False], [True, True], [False, False]],
#         [[True, True], [False, False], [True, True]],
#         [[False, False], [True, True], [False, False]]])
```