class
Flatten
extends
ModuleFlatten(start_dim: int = 1, end_dim: int = -1)Flatten a contiguous range of dimensions into a single dimension.
Given an input tensor of shape ,
Flatten collapses dimensions start_dim through end_dim
(inclusive) into one dimension whose size is the product of the
collapsed sizes:
All dimensions outside the range are left unchanged.
Parameters
start_dimint= 1First dimension to flatten (default
1). Negative indices are
supported and follow the standard Python convention
(-1 is the last dimension).end_dimint= -1Last dimension to flatten, inclusive (default
-1, the last
dimension). Negative indices are supported.Attributes
start_dimintStored value of the
start_dim constructor argument.end_dimintStored value of the
end_dim constructor argument.Notes
- Input: — any number of dimensions.
- Output (defaults
start_dim=1,end_dim=-1): .
- The most common use case is between convolutional feature extraction
and a
Linearclassifier: spatial dimensions are merged into a flat vector of length . - Setting
start_dim=0flattens the batch dimension as well — use with caution. - The operation is backed by the C++ engine's
flattenkernel and is fully differentiable through autograd.
Examples
**Typical CNN → Linear transition (default behaviour):**
>>> import lucid
>>> import lucid.nn as nn
>>>
>>> flat = nn.Flatten() # start_dim=1, end_dim=-1
>>> x = lucid.zeros(8, 3, 32, 32)
>>> flat(x).shape
(8, 3072) # 3*32*32 = 3072
**Flatten only the spatial dimensions, keeping channels separate:**
>>> flat_hw = nn.Flatten(start_dim=2, end_dim=3)
>>> x = lucid.zeros(8, 16, 14, 14)
>>> flat_hw(x).shape
(8, 16, 196) # 14*14 = 196
**Inside a Sequential pipeline:**
>>> model = nn.Sequential(
... nn.Conv2d(3, 64, kernel_size=3, padding=1),
... nn.ReLU(),
... nn.AdaptiveAvgPool2d((1, 1)),
... nn.Flatten(), # (N, 64, 1, 1) -> (N, 64)
... nn.Linear(64, 10),
... )Methods (3)
dunder
__init__
→None__init__(start_dim: int = 1, end_dim: int = -1)Initialise the Flatten module. See the class docstring for parameter semantics.
fn
forward
→Tensorforward(x: Tensor)Flatten (or unflatten) the specified dimensions of the input.
Parameters
inputTensorInput tensor.
Returns
TensorTensor with the configured dimensions flattened or unflattened.
fn
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.