Connect convolutional output to linear layers
Prepare a multidimensional activation tensor for a fully connected layer while retaining the batch dimension.
PyTorch · Function
Flattens a tensor by combining a selected inclusive range of dimensions into one. By default, all dimensions are flattened while element order is preserved.
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.
torch.flatten(input, start_dim=0, end_dim=-1) → Tensor| Name | Type | Description |
|---|---|---|
input | Tensor | Input tensor to reshape by flattening the selected dimensions. |
start_dim | int | First dimension included in the flattened range; default is 0. |
end_dim | int | Last dimension included in the flattened range; default is -1. |
A Tensor with the selected dimensions combined into one dimension; the result may be the original object, a view, or a copy.
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.
Prepare a multidimensional activation tensor for a fully connected layer while retaining the batch dimension.
Convert an entire tensor into a single vector for serialization, inspection, or downstream processing.
Collapse only feature or spatial dimensions while preserving surrounding dimensions.
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.
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.
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.viewtorch.Tensor.flattentorch.unflatten