*Memos:
- My post explains Flatten().
- My post explains unflatten().
- My post explains Unflatten().
flatten() can remove zero or more dimensions by selecting dimensions from the 0D or more D tensor of zero or more elements, getting the 1D or more D tensor of zero or more elements as shown below:
*Memos:
-
flatten()can be used with torch or a tensor. - The 1st argument(
input) withtorchor using a tensor(Required-Type:tensorofint,float,complexorbool). - The 2nd argument with
torchor the 1st argument with a tensor isstart_dim(Optional-Default:0-Type:int). - The 3rd argument with
torchor the 2nd argument with a tensor isend_dim(Optional-Default:-1-Type:int). -
flatten()can change a 0D tensor to a 1D tensor. -
flatten()does nothing for a 1D tensor. - The difference between Flatten() and flatten() is:
- The default value of
start_dimforFlatten()is1while the default value ofstart_dimforflatten()is0. - Basically,
Flatten()is used to define a model whileflatten()is not used to define a model.
- The default value of
import torch
my_tensor = torch.tensor(7)
torch.flatten(input=my_tensor)
my_tensor.flatten()
torch.flatten(input=my_tensor, start_dim=0, end_dim=0)
torch.flatten(input=my_tensor, start_dim=0, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=0)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=-1)
# tensor([7])
my_tensor = torch.tensor([7, 1, -8, 3, -6, 0])
torch.flatten(input=my_tensor)
torch.flatten(input=my_tensor, start_dim=0, end_dim=0)
torch.flatten(input=my_tensor, start_dim=0, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=0)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=-1)
# tensor([7, 1, -8, 3, -6, 0])
my_tensor = torch.tensor([[7, 1, -8], [3, -6, 0]])
torch.flatten(input=my_tensor)
torch.flatten(input=my_tensor, start_dim=0, end_dim=1)
torch.flatten(input=my_tensor, start_dim=0, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=1)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=-1)
# tensor([7, 1, -8, 3, -6, 0])
torch.flatten(input=my_tensor, start_dim=0, end_dim=0)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=0, end_dim=-2)
torch.flatten(input=my_tensor, start_dim=1, end_dim=1)
torch.flatten(input=my_tensor, start_dim=1, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=1)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=0)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=-2)
# tensor([[7, 1, -8], [3, -6, 0]])
my_tensor = torch.tensor([[[7], [1], [-8]], [[3], [-6], [0]]])
torch.flatten(input=my_tensor)
torch.flatten(input=my_tensor, start_dim=0, end_dim=2)
torch.flatten(input=my_tensor, start_dim=0, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-3, end_dim=2)
torch.flatten(input=my_tensor, start_dim=-3, end_dim=-1)
# tensor([7, 1, -8, 3, -6, 0])
torch.flatten(input=my_tensor, start_dim=0, end_dim=0)
torch.flatten(input=my_tensor, start_dim=0, end_dim=-3)
torch.flatten(input=my_tensor, start_dim=1, end_dim=1)
torch.flatten(input=my_tensor, start_dim=1, end_dim=-2)
torch.flatten(input=my_tensor, start_dim=2, end_dim=2)
torch.flatten(input=my_tensor, start_dim=2, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=2)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=1)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=-2)
torch.flatten(input=my_tensor, start_dim=-3, end_dim=0)
torch.flatten(input=my_tensor, start_dim=-3, end_dim=-3)
# tensor([[[7], [1], [-8]], [[3], [-6], [0]]])
torch.flatten(input=my_tensor, start_dim=0, end_dim=1)
torch.flatten(input=my_tensor, start_dim=0, end_dim=-2)
torch.flatten(input=my_tensor, start_dim=-3, end_dim=1)
torch.flatten(input=my_tensor, start_dim=-3, end_dim=-2)
# tensor([[7], [1], [-8], [3], [-6], [0]])
torch.flatten(input=my_tensor, start_dim=1, end_dim=2)
torch.flatten(input=my_tensor, start_dim=1, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=2)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=-1)
# tensor([[7, 1, -8], [3, -6, 0]])
my_tensor = torch.tensor([[[7.], [1.], [-8.]], [[3.], [-6.], [0.]]])
torch.flatten(input=my_tensor)
# tensor([7., 1., -8., 3., -6., 0.])
my_tensor = torch.tensor([[[7.+0.j], [1.+0.j], [-8.+0.j]],
[[3.+0.j], [-6.+0.j], [0.+0.j]]])
torch.flatten(input=my_tensor)
# tensor([7.+0.j, 1.+0.j, -8.+0.j, 3.+0.j, -6.+0.j, 0.+0.j])
my_tensor = torch.tensor([[[True], [False], [True]],
[[False], [True], [False]]])
torch.flatten(input=my_tensor)
# tensor([True, False, True, False, True, False])
ravel() can remove zero or more dimensions as much as possible from the 0D or more D tensor of zero or more elements, getting the 1D tensor of zero or more elements as shown below:
*Memos:
-
ravel()can be used withtorchor a tensor. - The 1st argument(
input) withtorchor using a tensor(Required-Type:tensorofint,float,complexorbool). -
ravel()can change a 0D tensor to a 1D tensor. -
ravel()does nothing for a 1D tensor.
import torch
my_tensor = torch.tensor(7)
torch.ravel(input=my_tensor)
my_tensor.ravel()
# tensor([7])
my_tensor = torch.tensor([7, 1, -8, 3, -6, 0])
torch.ravel(input=my_tensor)
# tensor([7, 1, -8, 3, -6, 0])
my_tensor = torch.tensor([[7, 1, -8], [3, -6, 0]])
torch.ravel(input=my_tensor)
# tensor([7, 1, -8, 3, -6, 0])
my_tensor = torch.tensor([[[7], [1], [-8]], [[3], [-6], [0]]])
torch.ravel(input=my_tensor)
# tensor([7, 1, -8, 3, -6, 0])
my_tensor = torch.tensor([[[7.], [1.], [-8.]], [[3.], [-6.], [0.]]])
torch.ravel(input=my_tensor)
# tensor([7., 1., -8., 3., -6., 0.])
my_tensor = torch.tensor([[[7.+0.j], [1.+0.j], [-8.+0.j]],
[[3.+0.j], [-6.+0.j], [0.+0.j]]])
torch.ravel(input=my_tensor)
# tensor([7.+0.j, 1.+0.j, -8.+0.j, 3.+0.j, -6.+0.j, 0.+0.j])
my_tensor = torch.tensor([[[True], [False], [True]],
[[False], [True], [False]]])
torch.ravel(input=my_tensor)
# tensor([True, False, True, False, True, False])
Top comments (0)