PyTorch Flatten Function: Usage Explained

In PyTorch, the flatten function is used to flatten an input tensor into a one-dimensional tensor. Its usage is as follows:

torch.flatten(input, start_dim=0, end_dim=-1)

Explanation of parameters:

  1. tensor input.
  2. start_dim: the dimension in which flattening should start, with a default value of 0.
  3. The dimension for ending flattening, with the default value of -1 meaning flattening to the last dimension.

The flatten function flattens the input tensor along the specified dimension range into a one-dimensional tensor. The flattened tensor will contain all the elements from the original tensor, rearranged into a one-dimensional structure.

Original: 我明天要去接爸爸。
Paraphrased: I am going to pick up my dad tomorrow.

import torch

x = torch.randn(3, 4, 5)
flattened = torch.flatten(x)
print(flattened.shape)  # 输出: torch.Size([60])

flattened_dim1 = torch.flatten(x, start_dim=1)
print(flattened_dim1.shape)  # 输出: torch.Size([3, 20])

flattened_dim1_dim2 = torch.flatten(x, start_dim=1, end_dim=2)
print(flattened_dim1_dim2.shape)  # 输出: torch.Size([3, 20, 5])

In the example above, the flatten function first flattens the tensor x with shape (3, 4, 5) into a one-dimensional tensor with shape (60,). Then, by specifying start_dim=1, the second dimension of the tensor x is flattened, resulting in a tensor with shape (3, 20). Lastly, by specifying start_dim=1 and end_dim=2, the second and third dimensions of the tensor x are flattened, resulting in a tensor with shape (3, 20, 5).

bannerAds