reshape() and view() in PyTorch
Tags: pytorch, reshape, view, function
Published: 2024-07-12 04:28:30

*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/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/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).

[reshape()](https://pytorch.org/docs/stable/generated/torch.reshape.html) or [view()](https://pytorch.org/docs/stable/generated/torch.Tensor.view.html) can get the 0D or more D reshaped 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:
- `reshape()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) or a tensor while `view()` can be used with a tensor but not with `torch`.
- For `reshape()`, the 1st argument(`input`) with `torch` or using a tensor(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).
- For `reshape()`, the 2nd argument with `torch`(Type:`tuple` of `int` or `list` of `int`) or the 1st or more arguments with a tensor(Type:`int`, `tuple` of `int` or `list` of `int` are `shape`(Required).
- For `view()`, using a tensor(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).
- For `view()`, the 1st or more arguments with a tensor are `*shape`(Required-Type:`int`, `tuple` of `int` or `list` of `int`). *`*shape=` or `shape=` cannot be used.
- For `view()`, the 1st argument with a tensor is `dtype`(Required-Type:[dtype](https://pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)):
  *Memos:
  - If `dtype` is not given, `dtype` is inferred from a tensor.
  - [My post](https://dev.to/hyperkai/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch-13h2) explains `dtype` argument.
- For `reshape()` and `view()`, setting `-1` to `shape` as the 1st number can adjust the size automatically so you don't need to set `24`, `4`, `2` or `3` as the 1st number. *`-1` is available only as the 1st number.
- `reshape()` may create a copy taking more memory while `view()` doesn't create a copy so `view()` can be ligher and faster than `reshape()`.

```python
import torch

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

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

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

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

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

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

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

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

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

my_tensor = torch.tensor([[0., 1., 2.], [3., 4., 5.],
                          [6., 7., 8.], [9., 10., 11.]])
torch.reshape(input=my_tensor, shape=(12,))
torch.reshape(input=my_tensor, shape=(-1,))
my_tensor.view((12,))
my_tensor.view((-1,))
# tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 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.reshape(input=my_tensor, shape=(12,))
torch.reshape(input=my_tensor, shape=(-1,))
my_tensor.view((12,))
my_tensor.view((-1,))
# 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])

my_tensor = torch.tensor([[True, False, True], [True, False, True],
                          [False, True, False], [False, True, False]])
torch.reshape(input=my_tensor, shape=(12,))
torch.reshape(input=my_tensor, shape=(-1,))
my_tensor.view((12,))
my_tensor.view((-1,))
# tensor([True, False, True, True, False, True,
#         False, True, False, False, True, False])
```