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,NetSharedArray[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)

Obtain a trained model:

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

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

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

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

Scope  (10)

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

Update the dropout probability:

Create a linear layer with no weight matrix specified:

Insert specific weights and biases:

Evaluate the layer on an input:

Create a layer without an input encoder:

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

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

Add type information to an existing net:

Remove a decoder from a net:

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

Apply the layer to an input:

Replace the input NetEncoder and output NetDecoder:

Apply the resized layer to an input:

Replace the second layer within a NetChain:

Replace a property of an existing NetEncoder:

Replace the value of a shared array in a network:

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

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

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

Properties & Relations  (1)

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

Introduced in 2016
 (11.0)
 |
Updated in 2020
 (12.1)