"Function" (Net Encoder)

NetEncoder[{"Function",f,{d1,d2,,dn}]

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

Details

  • NetEncoder[{"Function",}] is typically used to define a custom pre-processing for a neural network.
  • 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
    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
  • 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.
  • By default, NetEncoder[{"Function",f,}] saves the definitions of all the symbols needed to evaluate f, and restores these definitions at the first evaluation of the encoder. The selection of definitions to be saved can be changed using options SaveDefinitions, Initialization, ExcludedContexts and IncludedContexts.

Examples

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  (3)

"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:

SaveDefinitions  (1)

By default, definitions needed to evaluate the encoder 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)

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: