represents a net layer that flattens any input array into a vector.
FlattenLayer[n]
represents a net layer that flattens its input to level n.
FlattenLayer[{{s11,s12,…},{s21,s22,…},…}]
represents a net layer that flattens its input by combining all levels sij to make each level i in the result.
FlattenLayer
represents a net layer that flattens any input array into a vector.
FlattenLayer[n]
represents a net layer that flattens its input to level n.
FlattenLayer[{{s11,s12,…},{s21,s22,…},…}]
represents a net layer that flattens its input by combining all levels sij to make each level i in the result.
Details and Options
- FlattenLayer is typically used inside NetChain, NetGraph, etc. to transform higher-rank arrays into vectors.
- In FlattenLayer[n], n can be one of the following:
-
i flatten the first i+1 dimensions together -i flatten the last i+1 dimensions together Infinity flatten all dimensions - FlattenLayer[] is equivalent to FlattenLayer[Infinity].
- FlattenLayer exposes the following ports for use in NetGraph etc.:
-
"Input" an array of arbitrary rank "Output" an array - FlattenLayer normally infers the dimensions of its input from its context in NetChain etc. To specify the dimensions explicitly as {n1,n2,…}, use FlattenLayer["Input"->{n1,n2,…}].
- FlattenLayer[…][input] explicitly computes the output from applying the layer to input.
- FlattenLayer[…][{input1,input2,…}] explicitly computes outputs for each of the inputi.
- When given a NumericArray as input, the output will be a NumericArray.
- Options[FlattenLayer] gives the list of default options to construct the layer. Options[FlattenLayer[…]] gives the list of default options to evaluate the layer on some data.
- Information[FlattenLayer[…]] gives a report about the layer.
- Information[FlattenLayer[…],prop] gives the value of the property prop of FlattenLayer[…]. Possible properties are the same as for NetGraph.
Examples
open all close allBasic Examples (2)
Create a FlattenLayer:
FlattenLayer[]Create a FlattenLayer with specified input dimensions:
flat = FlattenLayer["Input" -> {3, 2}]flat[{{1, 2}, {3, 4}, {5, 6}}]Scope (4)
Create a FlattenLayer that flattens the first two dimensions of the input, preserving the other dimensions:
flat = FlattenLayer[1]flat[{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}]Create a FlattenLayer that flattens the first three dimensions of its input together, and apply it to a 2×3×4×5 array:
input = RandomReal[1, {2, 3, 4, 5}];FlattenLayer[2][input]//DimensionsCreate and apply a FlattenLayer that flattens the last three dimensions together:
FlattenLayer[-2][input]//DimensionsCreate and apply a layer that flattens all the dimensions together:
FlattenLayer[Infinity][input]//DimensionsCreate a FlattenLayer that flattens levels 2 and 3 into the first level of the resulting array:
input = RandomReal[1, {3, 5, 7}];FlattenLayer[{{2, 3}, {1}}][input]//DimensionsCreate a FlattenLayer with specified input dimensions:
flat = FlattenLayer["Input" -> {1, 2}]Thread the layer across a batch of inputs:
flat[{{{1, 2}}, {{3, 4}}, {{5, 6}}}]Properties & Relations (3)
FlattenLayer[] behaves like Flatten:
data = {{1, 2, 3}, {4, 5, 6}};FlattenLayer[][data]Flatten[data]FlattenLayer[n] computes:
flatten[n_ ? Positive][input_] := Flatten[N[input], n];
flatten[n_ ? Negative][input_] := Block[{dims1, dims2},
{dims1, dims2} = TakeDrop[Dimensions[input], ArrayDepth[input] + n - 1];
ArrayReshape[N[input], Append[dims1, Times@@dims2]]
];data = {{{1}, {2}}, {{3}, {4}}, {{5}, {6}}};
Dimensions[data]Flatten the first two dimensions:
FlattenLayer[1][data]flatten[1][data]Dimensions[%]Flatten the last two dimensions:
FlattenLayer[-1][data]flatten[-1][data]Dimensions[%]FlattenLayer[n] is equivalent to a ReshapeLayer[dims], where the dims depend on n and the dimensions of the input to FlattenLayer. Create a rank-3 array:
data = {{{1}, {2}}, {{3}, {4}}, {{5}, {6}}};
Dimensions[data]Apply a particular FlattenLayer:
FlattenLayer[Infinity][data]Apply an equivalent ReshapeLayer:
ReshapeLayer[{6}][data]Possible Issues (1)
FlattenLayer cannot accept symbolic inputs:
flat = FlattenLayer[];
flat[{1, 2}]flat[{1, z}]Tech Notes
Related Guides
Text
Wolfram Research (2016), FlattenLayer, Wolfram Language function, https://reference.wolfram.com/language/ref/FlattenLayer.html (updated 2021).
CMS
Wolfram Language. 2016. "FlattenLayer." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/FlattenLayer.html.
APA
Wolfram Language. (2016). FlattenLayer. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FlattenLayer.html
BibTeX
@misc{reference.wolfram_2026_flattenlayer, author="Wolfram Research", title="{FlattenLayer}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/FlattenLayer.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_flattenlayer, organization={Wolfram Research}, title={FlattenLayer}, year={2021}, url={https://reference.wolfram.com/language/ref/FlattenLayer.html}, note=[Accessed: 12-June-2026]}