NetPrepend[net,layer]
prepends a layer or a net onto a NetChain, a layer or a NetGraph with one input port.
NetPrepend[net,"name"layer]
appends a layer with a given name.
NetPrepend[net,{layer1,layer2,…}]
prepends several layers or nets.
NetPrepend
NetPrepend[net,layer]
prepends a layer or a net onto a NetChain, a layer or a NetGraph with one input port.
NetPrepend[net,"name"layer]
appends a layer with a given name.
NetPrepend[net,{layer1,layer2,…}]
prepends several layers or nets.
Details and Options
- The output of the prepended layer must be compatible with the input of the first layer in the chain.
- Any NetEncoder attached to the end of the chain will be removed.
- NetPrepend takes the following options:
-
"Input" Automatic type for the input of the resulting chain "Output" Automatic type for the output of the resulting chain - Options "Input" and "Output" support the normal type specifications allowed by NetChain, including NetEncoder and NetDecoder.
Examples
open all close allBasic Examples (3)
Prepend a layer to the start of a chain:
NetPrepend[NetChain[{4, SoftmaxLayer[]}], ElementwiseLayer[Tanh]]Prepend a named layer to the start of a chain:
NetPrepend[NetChain[<|"Linear" -> 4, "Softmax" -> SoftmaxLayer[]|>], "Tanh" -> ElementwiseLayer[Tanh]]Prepend several layers to the start of a chain:
NetPrepend[NetChain[{Tanh, 4, SoftmaxLayer[]}], {UnitVectorLayer[], LinearLayer[4]}]Scope (3)
Prepend a layer and attach a NetEncoder:
NetPrepend[NetChain[{4, SoftmaxLayer[]}], UnitVectorLayer[], "Input" -> NetEncoder[{"Class", {"a", "b"}}]]Create a NetChain with NetPrepend:
NetPrepend[LinearLayer[], Ramp]Prepend a layer to a NetGraph with one input:
ng = NetGraph[{1, 2, 3}, {1 -> {2, 3}}]NetPrepend[ng, LinearLayer[]]Prepend a layer with a given name:
NetPrepend[ng, "Foo" -> LinearLayer[]]Options (6)
"Input" (4)
Set the input shape of the net:
NetPrepend[NetChain[{ConvolutionLayer[3, 3], Tanh}], ElementwiseLayer[Tanh], "Input" -> {3, 256, 256}]Set a NetEncoder for the net:
NetPrepend[NetChain[{ConvolutionLayer[3, 3], Tanh}], ElementwiseLayer[Tanh], "Input" -> NetEncoder[{"Image", {256, 256}}]]Using this option is equivalent to using NetReplacePart on the returned net:
net = NetPrepend[NetChain[{ConvolutionLayer[3, 3], Tanh}], ElementwiseLayer[Tanh]]NetReplacePart[net, "Input" -> NetEncoder[{"Image", {256, 256}}]]An existing NetEncoder can be added back to the returned net using this option, provided it is compatible with the new initial layer:
enc = NetEncoder["Image"]net = NetChain[{ConvolutionLayer[3, 3], Tanh}, "Input" -> enc]NetPrepend[net, ElementwiseLayer[Tanh]]NetPrepend[net, ElementwiseLayer[Tanh], "Input" -> enc]"Output" (2)
Set a NetDecoder for the net:
NetPrepend[NetChain[{ConvolutionLayer[3, 3], Tanh}], ElementwiseLayer[Tanh], "Output" -> NetDecoder["Image"]]Using this option is equivalent to using NetReplacePart on the returned net:
net = NetPrepend[NetChain[{ConvolutionLayer[3, 3], Tanh}], ElementwiseLayer[Tanh]]NetReplacePart[net, "Output" -> NetDecoder["Image"]]Possible Issues (3)
The output of the prepended layer must be compatible with the input of the first layer in the chain:
NetPrepend[NetChain[{PoolingLayer[{3}], Ramp}], ConvolutionLayer[3, {2, 2}]]A network with several outputs cannot be prepended:
NetPrepend[NetChain[{1, 2, 3}], NetGraph[{1, 2}, {}]]A layer cannot be prepended to a network with more than one input:
ng = NetGraph[{1, 2, Plus}, {{NetPort["Input1"] -> 1, NetPort["Input2"] -> 2} -> 3}]NetPrepend[ng, LinearLayer[]]Tech Notes
Related Guides
Text
Wolfram Research (2018), NetPrepend, Wolfram Language function, https://reference.wolfram.com/language/ref/NetPrepend.html (updated 2021).
CMS
Wolfram Language. 2018. "NetPrepend." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/NetPrepend.html.
APA
Wolfram Language. (2018). NetPrepend. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NetPrepend.html
BibTeX
@misc{reference.wolfram_2026_netprepend, author="Wolfram Research", title="{NetPrepend}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/NetPrepend.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_netprepend, organization={Wolfram Research}, title={NetPrepend}, year={2021}, url={https://reference.wolfram.com/language/ref/NetPrepend.html}, note=[Accessed: 12-June-2026]}