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

PyTorch · Function

torch.flatten

Flattens a tensor by combining a selected inclusive range of dimensions into one. By default, all dimensions are flattened while element order is preserved.

How it works

torch.flatten reshapes a contiguous range of dimensions into one dimension, preserving the input element order. With the defaults, every dimension is flattened; specifying start_dim and end_dim limits the range. Depending on the input layout, the result can be the original tensor, a view, or a copied tensor.

Syntax

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

Parameters

NameTypeDescription
inputTensorInput tensor to reshape by flattening the selected dimensions.
start_dimintFirst dimension included in the flattened range; default is 0.
end_dimintLast dimension included in the flattened range; default is -1.

Returns

A Tensor with the selected dimensions combined into one dimension; the result may be the original object, a view, or a copy.

Key points

  • With default arguments, the function reshapes the entire input into one dimension.
  • Passing dimension bounds restricts flattening to that inclusive range.
  • Flattening preserves the original traversal order of tensor elements.
  • When the selected range requires no flattening, the exact input object is returned.
  • A view is preferred when the input layout supports the requested reshaping.
  • The input parameter is a Tensor whose selected dimensions are flattened.

Common mistakes

  • Do not assume the result always owns copied data; it may alias the input through a view.
  • A copy is made only when the requested flattened shape cannot be represented as a view.
  • A scalar becomes a one-dimensional tensor rather than remaining zero-dimensional.

Example: Flatten tensor dimensions

import torch

x = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(torch.flatten(x).shape)          # torch.Size([8])
print(torch.flatten(x, start_dim=1).shape)  # torch.Size([2, 4])

Flattens all dimensions, or only the selected dimension range while preserving element order.

When to use it

Connect convolutional output to linear layers

Prepare a multidimensional activation tensor for a fully connected layer while retaining the batch dimension.

Create a one-dimensional representation

Convert an entire tensor into a single vector for serialization, inspection, or downstream processing.

Flatten a selected dimension range

Collapse only feature or spatial dimensions while preserving surrounding dimensions.

Interview questions

QWhat does torch.flatten do by default?

The default range is start_dim=0 through end_dim=-1, so all dimensions are combined into one dimension. The output is one-dimensional and retains the input element order.

Follow-up: What happens for a non-contiguous tensor? If the requested shape cannot be represented as a view, torch.flatten copies the data.

QHow do start_dim and end_dim affect the output shape?

It combines dimensions from start_dim through end_dim into a single dimension, leaving dimensions outside that range unchanged. For example, flattening a shape (2, 3, 4) from dimension 1 produces (2, 12).

Follow-up: What does end_dim=-1 mean? It selects the last dimension, using Python-style negative indexing.

QDoes torch.flatten always copy the input?

The result can be the original object when no dimensions are flattened, a view when the flattened shape is view-compatible, or a copy otherwise. Thus, code should not depend on a copy being created.

Follow-up: How can view compatibility be investigated? The documentation points to torch.Tensor.view() for the conditions governing when a view is returned.

  • torch.Tensor.view
  • torch.Tensor.flatten
  • torch.unflatten

Source: torch.flatten — PyTorch 2.14 documentation. Checked 2026-10-16.