reshape() and view() in PyTorch

reshape() or view() can reshape the 0D or more D 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 or a tensor while view() can be used…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

reshape() or view() can reshape the 0D or more D 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 or a tensor while view() can be used with a tensor but not with torch.
  • For reshape(), the 1st argument with torch or using a tensor is input(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(), 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 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().
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.reshape(input=my_tensor, shape=(24,))
torch.reshape(input=my_tensor, shape=(-1,))
my_tensor.reshape(shape=(24,))
my_tensor.reshape(shape=(-1,))
my_tensor.reshape(24)
my_tensor.reshape(-1)
my_tensor.view((24,))
my_tensor.view((-1,))
my_tensor.view(24)
my_tensor.view(-1)
# 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.reshape(input=my_tensor, shape=(1, 24))
torch.reshape(input=my_tensor, shape=(-1, 24))
my_tensor.reshape(shape=(1, 24))
my_tensor.reshape(shape=(-1, 24))
my_tensor.reshape(1, 24)
my_tensor.reshape(-1, 24)
my_tensor.view((1, 24))
my_tensor.view((-1, 24))
my_tensor.view(1, 24)
my_tensor.view(-1, 24)
# 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.reshape(input=my_tensor, shape=(2, 12))
torch.reshape(input=my_tensor, shape=(-1, 12))
my_tensor.reshape(shape=(2, 12))
my_tensor.reshape(shape=(-1, 12))
my_tensor.reshape(2, 12)
my_tensor.reshape(-1, 12)
my_tensor.view((2, 12))
my_tensor.view((-1, 12))
my_tensor.view(2, 12)
my_tensor.view(-1, 12)
# 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.reshape(input=my_tensor, shape=(3, 8))
torch.reshape(input=my_tensor, shape=(-1, 8))
my_tensor.reshape(shape=(3, 8))
my_tensor.reshape(shape=(-1, 8))
my_tensor.reshape(3, 8)
my_tensor.reshape(-1, 8)
my_tensor.view((3, 8))
my_tensor.view((-1, 8))
my_tensor.view(3, 8)
my_tensor.view(-1, 8)
# 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.reshape(input=my_tensor, shape=(4, 6))
torch.reshape(input=my_tensor, shape=(-1, 6))
my_tensor.reshape(shape=(4, 6))
my_tensor.reshape(shape=(-1, 6))
my_tensor.reshape(4, 6)
my_tensor.reshape(-1, 6)
my_tensor.view((4, 6))
my_tensor.view((-1, 6))
my_tensor.view(4, 6)
my_tensor.view(-1, 6)
# 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.reshape(input=my_tensor, shape=(6, 4))
torch.reshape(input=my_tensor, shape=(-1, 4))
my_tensor.reshape(shape=(6, 4))
my_tensor.reshape(shape=(-1, 4))
my_tensor.reshape(6, 4)
my_tensor.reshape(-1, 4)
my_tensor.view((6, 4))
my_tensor.view((-1, 4))
my_tensor.view(6, 4)
my_tensor.view(-1, 4)
# 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.reshape(input=my_tensor, shape=(8, 3))
torch.reshape(input=my_tensor, shape=(-1, 3))
my_tensor.reshape(shape=(8, 3))
my_tensor.reshape(shape=(-1, 3))
my_tensor.reshape(8, 3)
my_tensor.reshape(-1, 3)
my_tensor.view((8, 3))
my_tensor.view((-1, 3))
my_tensor.view(8, 3)
my_tensor.view(-1, 3)
# 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.reshape(input=my_tensor, shape=(12, 2))
torch.reshape(input=my_tensor, shape=(-1, 2))
my_tensor.reshape(shape=(12, 2))
my_tensor.reshape(shape=(-1, 2))
my_tensor.reshape(12, 2)
my_tensor.reshape(-1, 2)
my_tensor.view((12, 2))
my_tensor.view((-1, 2))
my_tensor.view(12, 2)
my_tensor.view(-1, 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.reshape(input=my_tensor, shape=(24, 1))
torch.reshape(input=my_tensor, shape=(-1, 1))
my_tensor.reshape(shape=(24, 1))
my_tensor.reshape(shape=(-1, 1))
my_tensor.reshape(24, 1)
my_tensor.reshape(-1, 1)
my_tensor.view((24, 1))
my_tensor.view((-1, 1))
my_tensor.view(24, 1)
my_tensor.view(-1, 1)
# 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]])
etc.

torch.reshape(input=my_tensor, shape=(2, 3, 4))
torch.reshape(input=my_tensor, shape=(-1, 3, 4))
my_tensor.reshape(shape=(2, 3, 4))
my_tensor.reshape(shape=(-1, 3, 4))
my_tensor.reshape(2, 3, 4)
my_tensor.reshape(-1, 3, 4)
my_tensor.view((2, 3, 4))
my_tensor.view((-1, 3, 4))
my_tensor.view(2, 3, 4)
my_tensor.view(-1, 3, 4)
# 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]]])
etc.

torch.reshape(input=my_tensor, shape=(3, 2, 2, 2))
torch.reshape(input=my_tensor, shape=(-1, 2, 2, 2))
my_tensor.reshape(shape=(3, 2, 2, 2))
my_tensor.reshape(shape=(-1, 2, 2, 2))
my_tensor.reshape(3, 2, 2, 2)
my_tensor.reshape(-1, 2, 2, 2)
my_tensor.view((3, 2, 2, 2))
my_tensor.view((-1, 2, 2, 2))
my_tensor.view(3, 2, 2, 2)
my_tensor.view(-1, 2, 2, 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]]]])
etc.


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2024-07-12T04:28:30+00:00) reshape() and view() in PyTorch. Retrieved from https://www.scien.cx/2024/07/12/reshape-and-view-in-pytorch/

MLA
" » reshape() and view() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Friday July 12, 2024, https://www.scien.cx/2024/07/12/reshape-and-view-in-pytorch/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Friday July 12, 2024 » reshape() and view() in PyTorch., viewed ,<https://www.scien.cx/2024/07/12/reshape-and-view-in-pytorch/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » reshape() and view() in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/12/reshape-and-view-in-pytorch/
CHICAGO
" » reshape() and view() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2024/07/12/reshape-and-view-in-pytorch/
IEEE
" » reshape() and view() in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2024/07/12/reshape-and-view-in-pytorch/. [Accessed: ]
rf:citation
» reshape() and view() in PyTorch | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2024/07/12/reshape-and-view-in-pytorch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.