"Function" (Net Encoder)


represents an encoder that uses a custom function f to encode an input producing an output tensor of dimensions d1×d2××dn.


  • NetEncoder[][input] applies the encoder to an input to produce an output.
  • NetEncoder[][{input1,input2,}] applies the encoder to a list of inputs to produce a list of outputs.
  • The input to the encoder inputi is of any type such that f[inputi] produces an array of dimensions d1×d2××dn. The array can be a NumericArray expression or a multidimensional list of numbers.
  • An encoder can be attached to an input port of a net by specifying "port"->NetEncoder[] when constructing the net.
  • Parameters
  • The following parameters are supported:
  • "Pattern"Noneexpected pattern for inputs to the function
    "Batched"Falsewhether the function should be provided a batch of inputs
  • The parameter "Pattern"patt specifies that inputs to the encoder must match the pattern patt. "Pattern" None indicates that no check should be performed.
  • With the parameter "Batched" True the function is applied once to a batch of examples rather than being applied to each of the examples individually.


open allclose all

Basic Examples  (1)

Create a function encoder that takes a scalar and produces a length-2 list of these scalars:

Apply the encoder to a scalar:

Apply the encoder to a list of scalars:

Scope  (2)

Create a custom encoder that encodes radian inputs as their Cartesian coordinates:

Apply the encoder to a list of angles:

Create an encoder that returns a sequence of positive bounded integers:

Use this encoder with an EmbeddingLayer:

Parameters  (2)

"Pattern"  (1)

Create a function encoder that checks that the input consists of real numbers:

"Batched"  (1)

Create an encoder that applies the function to a batch of inputs at once:

Compare this to an encoder that applies a function to each input separately:

Possible Issues  (1)

With the parameter "Pattern"None, whether or not the input is a single example or a list of examples is ambiguous. As a result, in cases in which a list of examples is provided, the function is called an extra time: