WOLFRAM

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
    na 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×
    Automatican 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 allclose all

Basic Examples  (1)Summary of the most common use cases

Obtain a trained model:

Out[1]=1

Obtain a new model in which the input NetEncoder has been removed:

Out[2]=2

Obtain a new model that can be trained on a different set of classes:

Out[3]=3

Obtain a new model in which the first activation layer has been replaced:

Out[4]=4

Obtain a new model in which the first convolution biases have been randomized:

Out[5]=5

Scope  (10)Survey of the scope of standard use cases

Create an existing model that contains a DropoutLayer with dropout probability 0.5:

Out[1]=1

Update the dropout probability:

Out[2]=2

Create a linear layer with no weight matrix specified:

Out[1]=1

Insert specific weights and biases:

Out[2]=2

Evaluate the layer on an input:

Out[3]=3

Create a layer without an input encoder:

Out[1]=1
Out[2]=2

Attach a "Class" encoder to the input of the layer, which embeds the classes as {1,0} and {0,1}:

Out[3]=3

The resulting layer can now take the values True and False as inputs:

Out[4]=4
Out[5]=5

Add type information to an existing net:

Out[1]=1
Out[2]=2
Out[3]=3
Out[4]=4
Out[5]=5

Remove a decoder from a net:

Out[1]=1
Out[2]=2
Out[3]=3
Out[4]=4

Reshape an existing layer to have different input and output dimensions. Create a layer with a specific NetEncoder:

Out[1]=1

Apply the layer to an input:

Out[2]=2

Replace the input NetEncoder and output NetDecoder:

Out[3]=3

Apply the resized layer to an input:

Out[4]=4

Replace the second layer within a NetChain:

Out[1]=1
Out[2]=2

Replace a property of an existing NetEncoder:

Out[1]=1
Out[2]=2

Replace the value of a shared array in a network:

Out[1]=1
Out[2]=2

The weights have been replaced by the new value and are still shared:

Out[3]=3
Out[4]=4

Turn a network that processes fixed-length sequences into an equivalent network that handles variable-length sequences:

Out[1]=1
Out[2]=2

Turn a network that processes strings in a fixed-length fashion into an equivalent network that handles variable-length strings without padding nor clipping:

Out[3]=3
Out[4]=4
Out[5]=5
Out[6]=6

Properties & Relations  (1)Properties of the function, and connections to other functions

The part specifications supported by NetReplacePart are identical to those used by NetExtract.

Wolfram Research (2016), NetReplacePart, Wolfram Language function, https://reference.wolfram.com/language/ref/NetReplacePart.html (updated 2020).
Wolfram Research (2016), NetReplacePart, Wolfram Language function, https://reference.wolfram.com/language/ref/NetReplacePart.html (updated 2020).

Text

Wolfram Research (2016), NetReplacePart, Wolfram Language function, https://reference.wolfram.com/language/ref/NetReplacePart.html (updated 2020).

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.

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

Wolfram Language. (2016). NetReplacePart. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NetReplacePart.html

BibTeX

@misc{reference.wolfram_2024_netreplacepart, author="Wolfram Research", title="{NetReplacePart}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/NetReplacePart.html}", note=[Accessed: 08-January-2025 ]}

@misc{reference.wolfram_2024_netreplacepart, author="Wolfram Research", title="{NetReplacePart}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/NetReplacePart.html}", note=[Accessed: 08-January-2025 ]}

BibLaTeX

@online{reference.wolfram_2024_netreplacepart, organization={Wolfram Research}, title={NetReplacePart}, year={2020}, url={https://reference.wolfram.com/language/ref/NetReplacePart.html}, note=[Accessed: 08-January-2025 ]}

@online{reference.wolfram_2024_netreplacepart, organization={Wolfram Research}, title={NetReplacePart}, year={2020}, url={https://reference.wolfram.com/language/ref/NetReplacePart.html}, note=[Accessed: 08-January-2025 ]}