NetEncoder

NetEncoder[type]

represents an encoder that takes a given type of input and encodes it as a tensor for use in a net.

Details

  • A NetEncoder object can be attached to an input port of a net by specifying "port"->NetEncoder[] when constructing the net. Specifying "port"->"type" will create an encoder using NetEncoder["type"] and attach it.
  • Possible types include:
  • "Scalar"a single numeric value
    "Image"any image
    {"Image",s}image encoded with width and height s
    {"Image",{w,h}}image encoded with width w and height h
    {"Image",opts}image with specified encoding options
    {"Class",{c1,c2,}}classes ci encoded as codes i
    "Boolean"True and False encoded as 1 and 0
    {"Function",f,dims}apply f to produce a tensor of given dimensions
  • Possible types for encoding sequences include:
  • "Characters"printable ASCII string encoded as integers
    {"Characters","c1c2"}characters ci of string encoded as codes i
    "Tokens"encode common English words in string as integers
    {"Tokens",{"t1","t2",}}tokens ti within input string encoded as codes i
  • For the types "Class", "Characters" and "Tokens", the form of output for the discrete codes can be specified by NetEncoder[{"type",spec,form}]. Settings for form are:
  • "Index"code i represented by integer i (default)
    "UnitVector"code i represented by the i^(th) unit vector
  • NetEncoder[][input] gives the specified encoding for input.
  • NetEncoder[][{input1,input2, }] explicitly computes outputs for each of the inputi.
  • In NetEncoder[{"Image",opts}], possible options to specify how an image should be converted before encoding include:
  • "ImageSize"Automaticimage size to use for encoding
    "ColorChannels"Automaticcolor channels to use for encoding
    "ColorSpace""RGB"color space to use for encoding
    "MeanImage"Nonemean image to subtract
  • The following settings can be used for "MeanImage":
  • Nonedo not subtract anything
    mvalue to subtract from each channel of each pixel
    {m1,m2,}values to be subtracted from different channels
    Image[]overall image to subtract
  • Pixels are normalized to the range 0, 1 before doing subtraction.
  • NetEncoder[{"Function",f,{d1,d2,,dn}] can be used to define a custom encoding, such that an arbitrary function f is applied to each input and produces a tensor of dimensions d1×d2××dn.
  • In NetEncoder[{"Characters",table}], the mapping from characters to codes is specified by table, which can have the following forms:
  • "c1c2"map each character ci to successive available codes
    "c1c2"nmap all characters ci to code n
    "c1c2"Automaticmap all characters ci to the next available code
    n;;mspecmap characters between n and m to spec
    {spec1,spec2,}assign codes in sequence from the speci
  • The following symbolic character groups can be used in the table:
  • Automaticall printable ASCII characters, plus space, tab and newline
    LetterCharacterthe letters a through z and A through Z
    DigitCharacterthe digits 0 through 9
    WordCharacterthe union of LetterCharacter and DigitCharacter
    PunctuationCharacterall visible ASCII punctuation characters
    WhitespaceCharacterspace, tab and newline
    StartOfStringvirtual character that occurs before the beginning of the string
    EndOfStringvirtual character that occurs after the end of the string
    _any otherwise unassigned character
  • NetEncoder[{"Characters",spec,IgnoreCase->c}] specifies whether uppercase and lowercase letters are considered equivalent. Default is IgnoreCaseFalse.
  • NetEncoder[{"Tokens",spec,IgnoreCase->c}] specifies whether uppercase and lowercase tokens are considered equivalent. Default is IgnoreCaseFalse.
  • NetEncoder["Characters"] is suitable for typical English prose and consists of all printable ASCII characters, as well as tab, space and newline.
  • NetEncoder[{"Tokens",{"t1","t2",}}] uses the string pattern WordBoundary to tokenize the input string and recognizes the tokens "ti" in the resulting list.
  • NetEncoder[{"Tokens", "language"}] effectively uses WordList["language"] to produce a list of common words for the given language.
  • NetEncoder["Tokens"] uses a list of common English words.
  • When NetTrain is allowed to automatically attach a loss layer and a NetDecoder is attached to the output of the net, a NetEncoder of the same type will be created for the "Target" input of the loss layer.
  • NetEncoder[NetDecoder[]] will create an encoder based on the parameters of an existing decoder.

Examples

open allclose all

Basic Examples  (6)

Create a class encoder:

In[1]:=
Click for copyable input
Out[1]=

Apply it to a batch of inputs:

In[2]:=
Click for copyable input
Out[2]=

Create a scalar encoder:

In[1]:=
Click for copyable input
Out[1]=

Encode a numeric value as a vector of length 1:

In[2]:=
Click for copyable input
Out[2]=

Encode a list of values at once:

In[3]:=
Click for copyable input
Out[3]=

Create an image encoder of size 8×8:

In[1]:=
Click for copyable input
Out[1]=

Encode an image:

In[2]:=
Click for copyable input
Out[2]=

The output has dimensions given by the number of channels and the height and width specified in the encoder:

In[3]:=
Click for copyable input
Out[3]=

Create a Boolean encoder:

In[1]:=
Click for copyable input
Out[1]=

Use it to encode inputs:

In[2]:=
Click for copyable input
Out[2]=

Create a character encoder:

In[1]:=
Click for copyable input
Out[1]=

Encode an ASCII sentence:

In[2]:=
Click for copyable input
Out[2]=

Create a token encoder for English text:

In[1]:=
Click for copyable input
Out[1]=

Encode an English sentence:

In[2]:=
Click for copyable input
Out[2]=

Out-of-vocabulary words are encoded as the maximum code:

In[3]:=
Click for copyable input
Out[3]=

Scope  (12)

Generalizations & Extensions  (1)

Properties & Relations  (2)

See Also

NetDecoder  NetChain  Boole  ConformImages

Introduced in 2016
(11.0)
| Updated in 2017
(11.1)