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 whileview()
can be used with a tensor but not withtorch
. - For
reshape()
, the 1st argument withtorch
or using a tensor isinput
(Required-Type:tensor
ofint
,float
,complex
orbool
). - For
reshape()
, the 2nd argument withtorch
(Type:tuple
ofint
orlist
ofint
) or the 1st or more arguments with a tensor(Type:int
,tuple
ofint
orlist
ofint
areshape
(Required). - For
view()
, the 1st or more arguments with a tensor are*shape
(Required-Type:int
,tuple
ofint
orlist
ofint
). **shape=
orshape=
cannot be used. - For
reshape()
andview()
, setting-1
toshape
as the 1st number can adjust the size automatically so you don't need to set24
,4
,2
or3
as the 1st number. *-1
is available only as the 1st number. -
reshape()
may create a copy taking more memory whileview()
doesn't create a copy soview()
can be ligher and faster thanreshape()
.
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)
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.