Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Class

nn.Flatten

torch.nn.Flatten is a module that combines a contiguous range of dimensions into one. By default, it preserves dimension 0 and flattens dimensions 1 through the last dimension.

How it works

nn.Flatten combines one contiguous range of tensor dimensions into a single dimension. By default it starts at dimension 1 and continues through the last dimension, so a batch dimension remains separate; custom start_dim and end_dim select a different inclusive range.

Syntax

class torch.nn.modules.flatten.Flatten(start_dim=1, end_dim=-1)

Parameters

NameTypeDescription
start_dimintFirst dimension to flatten; defaults to 1.
end_dimintLast dimension to flatten; defaults to -1.

Returns

A Tensor with the selected contiguous dimensions replaced by one dimension.

Key points

  • The module merges one contiguous, inclusive range of dimensions into a single dimension.
  • It can be inserted into an nn.Sequential model when a layer sequence needs a flattening step.
  • start_dim selects the first dimension included in the flattened range.
  • end_dim selects the final dimension included in the flattened range.

Common mistakes

  • The default leaves dimension 0 unchanged; use start_dim=0 when the leading dimension should also be flattened.
  • The default end dimension is the final dimension; specify a smaller end_dim to preserve trailing dimensions.

Example: Flatten non-batch dimensions

import torch
from torch import nn

x = torch.randn(32, 1, 5, 5)
layer = nn.Flatten()
y = layer(x)
print(y.shape)  # torch.Size([32, 25])

Flattens dimensions 1 through the final dimension while preserving the batch dimension.

When to use it

Bridge convolutional and linear layers

Convert image or feature-map tensors into vectors before fully connected layers.

Sequential model preprocessing

Add a reshape operation inside a layer pipeline without writing a custom module.

Partial tensor flattening

Flatten only selected middle dimensions while retaining leading or trailing structure.

Interview questions

QWhat does nn.Flatten do by default?

nn.Flatten is a module that combines a contiguous range of dimensions into one dimension. Its defaults are start_dim=1 and end_dim=-1, which preserve the leading dimension and flatten the rest.

Follow-up: How does the output shape change? The selected dimensions are replaced by one dimension whose size is the product of their sizes.

QWhat do start_dim and end_dim control?

start_dim identifies the first dimension included, and end_dim identifies the last one included. Both boundaries are part of the flattened range.

Follow-up: How would you flatten dimensions 0 through 2? Construct nn.Flatten(0, 2).

QGive an example of the output shape with default and non-default settings.

For an input shaped (32, 1, 5, 5), the default module produces shape (32, 25). With nn.Flatten(0, 2), it instead produces (160, 5).

Follow-up: Why is the second result 160? The dimensions 0, 1, and 2 are multiplied: 32 × 1 × 5 = 160.

  • torch.flatten
  • torch.nn.Unflatten
  • torch.nn.Sequential

Source: Flatten — PyTorch 2.11 documentation. Checked 2026-10-16.