NetFlatten[net]
NetFlatten[net,n]
collapses up to nesting level n.
NetFlatten
NetFlatten[net]
NetFlatten[net,n]
collapses up to nesting level n.
Details and Options
- NetFlatten[net] will flatten nested nets at any level, including within operators such as NetMapOperator and NetFoldOperator.
Examples
open all close allBasic Examples (2)
chain = NetChain[{2, NetChain[{3, Ramp}], 4}]NetFlatten[chain]graph = NetGraph[{4, Tanh, NetGraph[{2, Tanh, Ramp, CatenateLayer[]}, {1 -> {2, 3} -> 4}], ThreadingLayer[Times]}, {1 -> 2, {2, 3} -> 4}]NetFlatten[graph]Scope (3)
Turn a NetChain with nested NetGraph into a unique NetGraph:
graphinchain = NetChain[{2, NetGraph[{Tanh, Ramp, CatenateLayer[]}, {{1, 2} -> 3}], 8}]NetFlatten[graphinchain]Collapse nested NetChain and NetGraph up to level 1:
capsulenet = NetModel["CapsNet Trained on MNIST Data"]NetFlatten[capsulenet, 1]Turn a flat NetGraph into a NetChain:
flatgraph = NetGraph[{2, Tanh, NetGraph[{3, SoftmaxLayer[]}, {1 -> 2}]}, {1 -> 2 -> 3}]NetFlatten[flatgraph]Properties & Relations (3)
NetFlatten flattens nested nets, including within operators.
Construct an operator that contains nested chains:
map = NetMapOperator[NetChain[{1, NetChain[{Ramp, 2}]}]]The inner net of the NetMapOperator has been flattened:
flattened = NetFlatten[map]NetFlatten yields a NetChain for a NetGraph that is fully flat:
flatgraph = NetGraph[{2, Tanh, NetGraph[{3, SoftmaxLayer[]}, {1 -> 2}]}, {1 -> 2 -> 3}]NetFlatten[flatgraph]NetFlatten yields a NetGraph for a NetChain that contains a nested NetGraph:
graphinchain = NetChain[{2, NetGraph[{Tanh, Ramp, CatenateLayer[]}, {{1, 2} -> 3}], 8}]NetFlatten[graphinchain]Tech Notes
Related Guides
Text
Wolfram Research (2018), NetFlatten, Wolfram Language function, https://reference.wolfram.com/language/ref/NetFlatten.html (updated 2019).
CMS
Wolfram Language. 2018. "NetFlatten." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2019. https://reference.wolfram.com/language/ref/NetFlatten.html.
APA
Wolfram Language. (2018). NetFlatten. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NetFlatten.html
BibTeX
@misc{reference.wolfram_2026_netflatten, author="Wolfram Research", title="{NetFlatten}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/NetFlatten.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_netflatten, organization={Wolfram Research}, title={NetFlatten}, year={2019}, url={https://reference.wolfram.com/language/ref/NetFlatten.html}, note=[Accessed: 13-June-2026]}