represents a trainable net layer that embeds integers between 1 and n into a continuous vector space of dimension size.


leaves the n to be inferred from context.

Details and Options

  • EmbeddingLayer operates on a single integer or rank-k array of integers, producing a vector or rank-k+1 array, respectively.
  • Specifying EmbeddingLayer[size] will produce a net that infers the n when a NetEncoder[] that produces integers is connected to its input.
  • The embedding performed by EmbeddingLayer is learned during training.
  • The following optional parameter can be included:
  • "Weights"Automaticinitial matrix of weights of dimensions n×size
  • With Automatic settings, the weights are added automatically when NetInitialize or NetTrain is used.
  • If weights have been added, EmbeddingLayer[][input] explicitly computes the output from applying the layer.
  • EmbeddingLayer[][{input1,input2,}] explicitly computes outputs for each of the inputi.
  • When given a NumericArray as input, the output will be a NumericArray.
  • NetExtract can be used to extract weights from an EmbeddingLayer object.
  • EmbeddingLayer is typically used inside NetChain, NetGraph, etc.
  • EmbeddingLayer exposes the following ports for use in NetGraph etc.:
  • "Input"an integer or rank-k array of integers
    "Output"a vector or rank-k+1 array
  • EmbeddingLayer[size,n,"Input"->shape] allows the shape of the input to be specified. Possible forms for shape are:
  • NetEncoder[]encoder producing an integer or array of integers
    "Integer"a single integer
    da vector of integers of length d
    {d1,d2,}a array of integers of dimensions d1×d2×
    "Varying"a variable-length vector of integers
    {"Varying",d2,d3,}an array whose first dimension is variable and whose remaining dimensions are d2×d3×


open all close all

Basic Examples  (2)

Create an EmbeddingLayer that will take integers 1, 2 and 3 and produces vectors of size 4:

Click for copyable input

Create a randomly initialized EmbeddingLayer that will take integers 1, 2 and 3 and produces vectors of size 2:

Click for copyable input

Apply the layer to an integer:

Click for copyable input

Apply the layer to a vector of integers:

Click for copyable input

Scope  (4)

Options  (1)

Applications  (2)

Properties & Relations  (1)

Introduced in 2016