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

Software Engineer for AI Training Data (School Specific)

@ G2i Inc | Remote

Software Engineer for AI Training Data (Python)

@ G2i Inc | Remote

Software Engineer for AI Training Data (Tier 2)

@ G2i Inc | Remote

Data Engineer

@ Lemon.io | Remote: Europe, LATAM, Canada, UK, Asia, Oceania

Artificial Intelligence – Bioinformatic Expert

@ University of Texas Medical Branch | Galveston, TX

Lead Developer (AI)

@ Cere Network | San Francisco, US