- torch.flatten(input, start_dim=0, end_dim=-1) → Tensor¶
Flattens
input
by reshaping it into a one-dimensional tensor. Ifstart_dim
orend_dim
are passed, only dimensions starting withstart_dim
and ending withend_dim
are flattened.The order of elements ininput
is unchanged.Unlike NumPy’s flatten, which always copies input’s data, this function may return the original object, a view,or copy. If no dimensions are flattened, then the original object
input
is returned. Otherwise, if input canbe viewed as the flattened shape, then that view is returned. Finally, only if the input cannot be viewed as theflattened shape is input’s data copied. Seetorch.Tensor.view()
for details on when a view will be returned.Note
Flattening a zero-dimensional tensor will return a one-dimensional view.
- Parameters
Example:
>>> t = torch.tensor([[[1, 2],... [3, 4]],... [[5, 6],... [7, 8]]])>>> torch.flatten(t)tensor([1, 2, 3, 4, 5, 6, 7, 8])>>> torch.flatten(t, start_dim=1)tensor([[1, 2, 3, 4], [5, 6, 7, 8]])
I'm an expert and enthusiast with a strong background in machine learning and deep learning frameworks, particularly PyTorch. My expertise is backed by a comprehensive understanding of the underlying concepts, algorithms, and practical applications in the field. I have hands-on experience with various PyTorch functions, including torch.flatten, and can provide detailed insights into its functionality.
Now, let's delve into the information related to the torch.flatten function:
torch.flatten
The torch.flatten
function is a PyTorch method used for reshaping a tensor into a one-dimensional tensor. It provides the flexibility to flatten only specific dimensions within the input tensor. The key parameters for this function are:
- input (Tensor): The input tensor that needs to be flattened.
- start_dim (int): The first dimension to be flattened. It is an optional parameter and defaults to 0.
- end_dim (int): The last dimension to be flattened. Another optional parameter, and it defaults to -1.
Functionality
- The function reshapes the input tensor into a one-dimensional tensor without changing the order of its elements.
- If
start_dim
orend_dim
is specified, only dimensions within that range are flattened. - Unlike NumPy's flatten, which always copies the data,
torch.flatten
may return the original object, a view, or a copy of the data. - If no dimensions are flattened, the original object is returned.
- If the input can be viewed as the flattened shape, a view is returned; otherwise, a copy of the data is returned.
- Flattening a zero-dimensional tensor results in a one-dimensional view.
Example
import torch
# Example tensor
t = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
# Flatten the entire tensor
result1 = torch.flatten(t)
print(result1)
# Output: tensor([1, 2, 3, 4, 5, 6, 7, 8])
# Flatten along the second dimension
result2 = torch.flatten(t, start_dim=1)
print(result2)
# Output: tensor([[1, 2, 3, 4], [5, 6, 7, 8]])
This function proves to be valuable when dealing with multidimensional data and is particularly useful in neural network architectures where reshaping tensors is a common operation. The provided examples demonstrate how to use torch.flatten
to achieve different flattening effects based on the specified dimensions.