torch.flatten — PyTorch 2.1 documentation (2024)

Table of Contents
torch.flatten Functionality Example

Shortcuts

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

Flattens input by reshaping it into a one-dimensional tensor. If start_dim or end_dimare passed, only dimensions starting with start_dim and ending with end_dim are flattened.The order of elements in input 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. See torch.Tensor.view() for details on when a view will be returned.

Note

Flattening a zero-dimensional tensor will return a one-dimensional view.

Parameters
  • input (Tensor) – the input tensor.

  • start_dim (int) – the first dim to flatten

  • end_dim (int) – the last dim to flatten

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]])
' document.getElementById("pytorch-article").insertAdjacentHTML('afterBegin', div) }

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 or end_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.

torch.flatten — PyTorch 2.1 documentation (2024)
Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 6331

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.