NetReplacePart[layer,"array"value]
replaces an array within a layer, returning a new layer.
NetReplacePart[net,"port"type]
returns a new layer or network in which an input or output port has the specified type.
NetReplacePart[net,"input"encoder]
attaches a NetEncoder[…] to a specified input port.
NetReplacePart[net,"output"decoder]
attaches a NetDecoder[…] to a specified output port.
NetReplacePart[net,lspeclayer]
returns a new NetChain or NetGraph in which the layer identified by lspec has been replaced.
NetReplacePart[net,NetArray[name]value]
sets a shared array within a network or layer to a specified value.
NetReplacePart[coder,"param"value]
returns a new NetEncoder[…] or NetDecoder[…] in which a parameter has been replaced.
NetReplacePart[net,{lspec,pspec}value]
makes a replacement of a part pspec of a layer or coder lspec within a NetGraph or NetChain.
NetReplacePart[…,{spec1val1,spec2val2,…}]
makes multiple simultaneous replacements.
NetReplacePart
NetReplacePart[layer,"array"value]
replaces an array within a layer, returning a new layer.
NetReplacePart[net,"port"type]
returns a new layer or network in which an input or output port has the specified type.
NetReplacePart[net,"input"encoder]
attaches a NetEncoder[…] to a specified input port.
NetReplacePart[net,"output"decoder]
attaches a NetDecoder[…] to a specified output port.
NetReplacePart[net,lspeclayer]
returns a new NetChain or NetGraph in which the layer identified by lspec has been replaced.
NetReplacePart[net,NetArray[name]value]
sets a shared array within a network or layer to a specified value.
NetReplacePart[coder,"param"value]
returns a new NetEncoder[…] or NetDecoder[…] in which a parameter has been replaced.
NetReplacePart[net,{lspec,pspec}value]
makes a replacement of a part pspec of a layer or coder lspec within a NetGraph or NetChain.
NetReplacePart[…,{spec1val1,spec2val2,…}]
makes multiple simultaneous replacements.
Details
- NetReplacePart can replace layer parameters, layer arrays, layers, encoders, decoders, encoder parameters, decoder parameters, input array sizes and output array sizes.
- The part specifications supported by NetReplacePart are identical to those used by NetExtract.
- When replacing an array within a layer, the new value must have the same dimensions as the original array.
- When replacing an input or an output in order to fully specify a partially specified network, any of the following values can be used to specify the type of the port:
-
"Real" a single real number "Integer" a single integer n a vector of length n {n1,n2,…} an array of dimensions n1×n2×… "Varying" a variable-length vector {"Varying",n2,n3,…} an array whose first dimension is variable and remaining dimensions are n2×n3×… Automatic an array with a shape that should be inferred NetEncoder[…] an encoder (for input ports) NetDecoder[…] a decoder (for output ports) - An encoder or decoder can be removed from a port by specifying the value None.
- Changing the output dimension of a NetChain or NetGraph is in general possible, but might require changing intermediate layers, using syntax like NetReplacePart[net,{layernamenewlayer,"Output"newoutput}].
- NetReplacePart will fail if the replacement specifications lead to a net with incompatible dimensions.
Examples
open all close allBasic Examples (1)
net = NetModel["LeNet Trained on MNIST Data"]Obtain a new model in which the input NetEncoder has been removed:
NetReplacePart[net, "Input" -> None]Obtain a new model in which the first convolution biases have been randomized:
NetReplacePart[net, {1, "Biases"} -> RandomReal[1, {20}]]Scope (11)
net = NetModel["LeNet Trained on MNIST Data"]Obtain a new model in which the first activation layer has been replaced:
NetReplacePart[net, 2 -> ElementwiseLayer[Tanh]]Create an existing model that contains a DropoutLayer with dropout probability 0.5:
chain = NetChain[{10, Ramp, DropoutLayer[0.5], 5}]Update the dropout probability:
NetReplacePart[chain, {3, "DropoutProbability"} -> 0.1]Create a linear layer with no weight matrix specified:
layer = LinearLayer[2]Insert specific weights and biases:
layer2 = NetReplacePart[layer, {"Weights" -> {{1, 2}, {3, 4}}, "Biases" -> None}]Evaluate the layer on an input:
layer2[{0, 1}]Create a layer without an input encoder:
layer = NetInitialize@LinearLayer[2, "Input" -> 2]layer[{0, 1}]Attach a "Class" encoder to the input of the layer, which embeds the classes as {1,0} and {0,1}:
layer2 = NetReplacePart[layer, "Input" -> NetEncoder[{"Class", {True, False}, "UnitVector"}]]The resulting layer can now take the values True and False as inputs:
layer2[True]layer2[False]Add type information to an existing net:
net = NetChain[{5, Ramp, 5}]NetInitialize[net]net2 = NetReplacePart[net, "Input" -> 2]net2 = NetInitialize[net2]net2[{4, 5}]net = NetInitialize@NetChain[{LinearLayer[3], SoftmaxLayer[]}, "Input" -> "Scalar", "Output" -> NetDecoder[{"Class", {a, b, c}}]]net[3]net2 = NetReplacePart[net, "Output" -> None]net2[3]Reshape an existing layer to have different input and output dimensions. Create a layer with a specific NetEncoder:
pool = PoolingLayer[3, "Input" -> NetEncoder[{"Image", 16}], "Output" -> NetDecoder["Image"]]pool[[image]]Replace the input NetEncoder and output NetDecoder:
pool2 = NetReplacePart[pool, {"Input" -> NetEncoder[{"Image", 32}], "Output" -> NetDecoder["Image"]}]Apply the resized layer to an input:
pool2[[image]]Replace the second layer within a NetChain:
chain = NetChain[{LinearLayer[2], ElementwiseLayer[Ramp], LinearLayer[3], ElementwiseLayer[Ramp]}]NetReplacePart[chain, 2 -> ElementwiseLayer[Tanh]]Replace a property of an existing NetEncoder:
enc = NetEncoder["Image"]NetReplacePart[enc, "Interleaving" -> True]Replace the value of a shared array in a network:
net = NetInitialize[NetChain[{NetInsertSharedArrays[LinearLayer[2]], NetInsertSharedArrays[LinearLayer[2]]}, "Input" -> 2, "Output" -> 2]]net2 = NetReplacePart[net, NetArray["Weights"] -> NumericArray[{{1, 2}, {3, 4}}]]The weights have been replaced by the new value and are still shared:
NetExtract[net, {All, "Weights"}]NetExtract[net2, {All, "Weights"}]Turn a network that processes fixed-length sequences into an equivalent network that handles variable-length sequences:
net = NetExtract[NetModel["Wolfram English Character-Level Language Model V1", "TrainingNet"], "predict"]reshaped = NetReplacePart[net, "Input" -> {"Varying", Restricted["Integer", 97]}]Turn a network that processes strings in a fixed-length fashion into an equivalent network that handles variable-length strings without padding nor clipping:
netstring = NetInitialize[NetTake[NetModel["Wolfram English Character-Level Language Model V1", "TrainingNet"], {All, "predict"}]]reshapedstring = NetReplacePart[netstring, {"Input", "TargetLength"} -> All]Dimensions[netstring["abc"]]Dimensions[reshapedstring["abc"]]Properties & Relations (1)
The part specifications supported by NetReplacePart are identical to those used by NetExtract. Replace the weights of the third layer of a NetChain:
partSpec = {3, "Weights"};net = NetInitialize@NetChain[{LinearLayer[2], Ramp, LinearLayer[3], Ramp}, "Input" -> {3}];net = NetReplacePart[net, partSpec -> {{1, 2}, {3, 4}, {5, 6}}]Use the same specification to extract the weights:
NetExtract[net, partSpec]Tech Notes
Related Guides
Text
Wolfram Research (2016), NetReplacePart, Wolfram Language function, https://reference.wolfram.com/language/ref/NetReplacePart.html (updated 2020).
CMS
Wolfram Language. 2016. "NetReplacePart." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/NetReplacePart.html.
APA
Wolfram Language. (2016). NetReplacePart. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NetReplacePart.html
BibTeX
@misc{reference.wolfram_2026_netreplacepart, author="Wolfram Research", title="{NetReplacePart}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/NetReplacePart.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_netreplacepart, organization={Wolfram Research}, title={NetReplacePart}, year={2020}, url={https://reference.wolfram.com/language/ref/NetReplacePart.html}, note=[Accessed: 13-June-2026]}