"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.
- The following parameters are supported:
-
"Pattern" None expected pattern for inputs to the function "Batched" False whether the function should be provided a batch of inputs SaveDefinitions True whether current definitions relevant for the evaluation of the encoder should automatically be saved Initialization None expression to be evaluated when the encoder is first applied ExcludedContexts Automatic contexts whose symbols will not have their definitions recursively saved IncludedContexts All contexts 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.
Parameters
Examples
open allclose allBasic Examples (1)
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)
"Batched" (1)
SaveDefinitions (1)
By default, definitions needed to evaluate the encoder are saved:
When applying the encoder for the first time, definitions are restored:
This behavior can be disabled using SaveDefinitionsFalse:
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: