April 2, 2024, 10:19 p.m. | Super Kai (Kazuya Ito)

DEV Community dev.to

flatten() can remove zero or more dimensions from a 0D or more tensor as shown below:



import torch

my_tensor = torch.tensor(2)

torch.flatten(my_tensor)
torch.flatten(my_tensor, 0, 0)
torch.flatten(my_tensor, 0, -1)
torch.flatten(my_tensor, -1, 0)
torch.flatten(my_tensor, -1, -1)
# tensor([2])

my_tensor= torch.tensor([2, 7, 4]) # 1D tensor

torch.flatten(my_tensor)
torch.flatten(my_tensor, 0, 0)
torch.flatten(my_tensor, 0, -1)
torch.flatten(my_tensor, -1, 0)
torch.flatten(my_tensor, -1, -1)
# tensor([2, 7, 4])

my_tensor = torch.tensor([[2, 7, 4], [8, 3, 2]]) # 2D tensor

torch.flatten(my_tensor)
torch.flatten(my_tensor, 0, 0)
torch.flatten(my_tensor, 0, 1)
torch.flatten(my_tensor, 0, …

dimensions flat flatten import pytorch tensor torch

Data Scientist (m/f/x/d)

@ Symanto Research GmbH & Co. KG | Spain, Germany

Data Scientist 3

@ Wyetech | Annapolis Junction, Maryland

Technical Program Manager, Robotics

@ DeepMind | Mountain View, California, US

Machine Learning Engineer

@ Issuu | Braga

Business Intelligence Manager

@ Intuitive | Bengaluru, India

Expert Data Engineer (m/w/d)

@ REWE International Dienstleistungsgesellschaft m.b.H | Wien, Austria