"Function" (Net Decoder)


represents a decoder that applies f to the output of a net to obtain a decoded result.


  • NetDecoder[{"Function",}] is typically used to define a custom post-processing for a neural network.
  • NetDecoder[][input] applies the decoder to an input to produce an output.
  • NetDecoder[][{input1,input2,}] applies the decoder to a list of inputs to produce a list of outputs.
  • A decoder can be attached to an output port of a net by specifying "port"->NetDecoder[] when constructing the net.
  • Parameters
  • The following parameters are supported:
  • "Properties"{}available properties
    SaveDefinitionsTruewhether current definitions relevant for the evaluation of the encoder should automatically be saved
    InitializationNoneexpression to be evaluated when the encoder is first applied
    ExcludedContextsAutomaticcontexts whose symbols will not have their definitions recursively saved
    IncludedContextsAllcontexts whose symbols will have their definitions recursively saved
  • NetDecoder[{"Function",f,"Properties"{"prop1","prop2",}}] can be used to specify that the decoder supports obtaining any of the propi. The function f must then take the property to decode as its second argument. In this case, the default property that will be supplied to f if no specific property is requested is prop1.
  • By default, NetDecoder[{"Function",f,}] saves the definitions of all the symbols needed to evaluate f, and restores these definitions at the first evaluation of the decoder. The selection of definitions to be saved can be changed using options SaveDefinitions, Initialization, ExcludedContexts and IncludedContexts.


open allclose all

Basic Examples  (1)

Create a property that interprets its input as an RGB color:

Note that the function is passed a NumericArray and that Normal is used to convert it to a standard array that RGBColor can take.

Decode a 3-vector as an RGB color:

Attach the decoder to a randomly generated net:

The output of the net is automatically decoded as a color:

Parameters  (2)

"Properties"  (1)

Define a decoder with two possible properties "x2" and "+1":

By default, the decoder returns the first property:

Compute the other property:

Attach the decoder to a net:

Compute several output properties of the net applied to an input:

SaveDefinitions  (1)

By default, definitions needed to evaluate the decoder are saved:

Clear definitions:

When applying the encoder for the first time, definitions are restored:

This behavior can be disabled using SaveDefinitionsFalse:

Clear definitions:

Definitions cannot be restored:

Possible Issues  (1)

Many Wolfram Language functions do not support NumericArray inputs. This issue can be solved by using Normal to convert a NumericArray input to a standard array: