Contenuto principale

embeddingLayer

Embedding layer

Since R2026a

    Description

    An embedding layer converts numeric indices to numeric vectors, where the indices correspond to discrete data.

    Use embeddings to map discrete data such as integer representations of categorical values or words to numeric vectors.

    Creation

    Description

    layer = embeddingLayer(outputSize,maxIndex) creates an embedding layer and sets the OutputSize and MaxIndex properties.

    example

    layer = embeddingLayer(outputSize,maxIndex,Name=Value) also specifies options using one or more name-value arguments. For example, Name="emb" sets the Name property to "emb".

    Input Arguments

    expand all

    Output size, specified as a positive integer.

    The layer maps discrete inputs to numeric vectors with length of the specified size.

    This argument sets the OutputSize property.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Number of numeric vectors that the layer can map discrete inputs to, specified as a positive integer.

    This argument sets the MaxIndex property.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Name-Value Arguments

    expand all

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: embeddingLayer(outputSize,maxIndex,Name="emb") creates an embedding layer and sets the Name property to "emb".

    Mask index, specified as one of these values:

    • [] — The embedding does not contain a vector that corresponds to a mask value.

    • nonnegative integer — The embedding vector with the specified index corresponds to a mask value. The mask index must be less than or equal to MaxIndex. If the mask index is 0, then ZeroIndexMapping must be "first-vector".

    During training, the layer does not update the embedding vector that corresponds to the mask value.

    This argument sets the MaskIndex property.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Mapping of zero index values, specified as one of these values:

    • "zeros" — Map zero-valued indices to vectors of zeros.

    • "first-vector" — Map zero-valued indices to the first vector of the embedding weights.

    • "error" — Throw an error when the layer receives zero-valued indices.

    This argument sets the ZeroIndexMapping property.

    Learnable parameters from other layers, specified as one of these values:

    • "weights" — Receive weights from another layer.

    • {} — Use the weights in the Weights property.

    If InputLearnables specifies that the layer receives weights from another layer, then the layer has two inputs with the names 'in' and 'weights', which correspond to the output data and learnable weights, respectively. In this case, the corresponding properties that contain the initializer, learnable parameters, learning rate factor, and L2 regularization factor must be [].

    If the InputLearnables value is empty, then the layer has one input with the name 'in', which corresponds to the input data. In this case, the layer uses the Weights property for the layer operation.

    For more information, see Neural Network Weight Tying.

    This argument sets the InputLearnables property.

    Learnable parameters to output to other layers, specified as one of these values:

    • "weights" — Output weights to another layer.

    • {} — Do not output weights.

    If OutputLearnables specifies that the layer outputs learnables to another layer, then the layer has two outputs with the names 'out' and 'weights', which correspond to the output data and learnable weights, respectively. In this case, the layer also outputs the weights that it uses in the layer operation.

    If the OutputLearnables value is empty, then the layer has one output with the name 'out', which corresponds to the output data.

    For more information, see Neural Network Weight Tying.

    This argument sets the OutputLearnables property.

    Function to initialize the weights, specified as one these values:

    • "narrow-normal" — Initialize the weights by independently sampling from a normal distribution with zero mean and standard deviation 0.01.

    • "glorot" — Initialize the weights with the Glorot initializer [1] (also known as the Xavier initializer). The Glorot initializer independently samples from a normal distribution with zero mean and variance 2/(1 + OutputSize).

    • "he" — Initialize the weights with the He initializer [2]. The He initializer samples from a normal distribution with zero mean and variance 2.

    • "orthogonal" — Initialize the input weights with Q, the orthogonal matrix given by the QR decomposition of Z = QR for a random matrix Z sampled from a unit normal distribution. [3]

    • "zeros" — Initialize the weights with zeros.

    • "ones" — Initialize the weights with ones.

    • Function handle — Initialize the weights with a custom function. If you specify a function handle, then the function syntax must be of the form weights = func(sz), where sz is the size of the weights. For an example, see Specify Custom Weight Initialization Function.

    • [] — Do not initialize the weights. Use the weights from the 'weights' input instead.

    The layer initializes the weights only when the Weights and InputLearnables properties are empty.

    If InputLearnables is "weights", then the Weights, WeightsInitializer, WeightLearnRateFactor, and WeightL2Factor properties must be empty.

    This argument sets the WeightsInitializer property.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | string | function_handle

    Embedding weights, specified as an OutputSize-by-MaxIndex matrix.

    The vector Weights(:,i) is the embedding vector for the input value i.

    If InputLearnables specifies that the layer receives weights from another layer, then the layer has two inputs with the names 'in' and 'weights', which correspond to the output data and learnable weights, respectively. In this case, the corresponding properties that contain the initializer, learnable parameters, learning rate factor, and L2 regularization factor must be [].

    If the InputLearnables value is empty, then the layer has one input with the name 'in', which corresponds to the input data. In this case, the layer uses the Weights property for the layer operation.

    If InputLearnables is "weights", then the Weights, WeightsInitializer, WeightLearnRateFactor, and WeightL2Factor properties must be empty.

    This argument sets the Weights property.

    Data Types: single | double

    Learning rate factor for the weights, specified as one of these values:

    • Nonnegative scalar — Use the specified learning rate factor.

    • [] — Use learning rate factor from another layer.

    The software multiplies this factor by the global learning rate to determine the learning rate for the weights in this layer. For example, if WeightLearnRateFactor is 2, then the learning rate for the weights in this layer is twice the current global learning rate. The software determines the global learning rate based on the settings you specify using the trainingOptions function.

    If InputLearnables is "weights", then the Weights, WeightsInitializer, WeightLearnRateFactor, and WeightL2Factor properties must be empty.

    This argument sets the WeightLearnRateFactor property.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    L2 regularization factor for the weights, specified as one of these values:

    • Nonnegative scalar — Use the specified regularization factor.

    • [] — Use regularization factor from another layer.

    The software multiplies this factor by the global L2 regularization factor to determine the L2 regularization for the weights in this layer. For example, if WeightL2Factor is 2, then the L2 regularization for the weights in this layer is twice the global L2 regularization factor. You can specify the global L2 regularization factor using the trainingOptions function.

    If InputLearnables is "weights", then the Weights, WeightsInitializer, WeightLearnRateFactor, and WeightL2Factor properties must be empty.

    This argument sets the WeightL2Factor property.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Layer name, specified as a character vector or a string scalar. For Layer array input, the trainnet and dlnetwork functions automatically assign names to unnamed layers.

    This argument sets the Name property.

    Data Types: char | string

    Properties

    expand all

    Embedding

    This property is read-only after object creation. To set this property, use the corresponding name-value argument when you create the EmbeddingLayer object.

    Output size, specified as a positive integer.

    The layer maps discrete inputs to numeric vectors with length of the specified size.

    Data Types: double

    This property is read-only after object creation. To set this property, use the corresponding name-value argument when you create the EmbeddingLayer object.

    Number of numeric vectors that the layer can map discrete inputs to, specified as a positive integer.

    Data Types: double

    This property is read-only after object creation. To set this property, use the corresponding name-value argument when you create the EmbeddingLayer object.

    Mask index, specified as one of these values:

    • [] — The embedding does not contain a vector that corresponds to a mask value.

    • nonnegative integer — The embedding vector with the specified index corresponds to a mask value. The mask index must be less than or equal to MaxIndex. If the mask index is 0, then ZeroIndexMapping must be "first-vector".

    During training, the layer does not update the embedding vector that corresponds to the mask value.

    Data Types: double

    This property is read-only after object creation. To set this property, use the corresponding name-value argument when you create the EmbeddingLayer object.

    Mapping of zero index values, specified as one of these values:

    • 'zeros' — Map zero-valued indices to vectors of zeros.

    • 'first-vector' — Map zero-valued indices to the first vector of the embedding weights.

    • 'error' — Throw an error when the layer receives zero-valued indices.

    Weight Tying

    This property is read-only after object creation. To set this property, use the corresponding name-value argument when you create the EmbeddingLayer object.

    Learnable parameters from other layers, specified as one of these values:

    • {'Weights'} — Receive weights from another layer.

    • {} — Use the weights in the Weights property.

    If InputLearnables specifies that the layer receives weights from another layer, then the layer has two inputs with the names 'in' and 'weights', which correspond to the output data and learnable weights, respectively. In this case, the corresponding properties that contain the initializer, learnable parameters, learning rate factor, and L2 regularization factor must be [].

    If the InputLearnables value is empty, then the layer has one input with the name 'in', which corresponds to the input data. In this case, the layer uses the Weights property for the layer operation.

    For more information, see Neural Network Weight Tying.

    This property is read-only after object creation. To set this property, use the corresponding name-value argument when you create the EmbeddingLayer object.

    Learnable parameters to output to other layers, specified as one of these values:

    • {'Weights'} — Output weights to another layer.

    • {} — Do not output weights.

    If OutputLearnables specifies that the layer outputs learnables to another layer, then the layer has two outputs with the names 'out' and 'weights', which correspond to the output data and learnable weights, respectively. In this case, the layer also outputs the weights that it uses in the layer operation.

    If the OutputLearnables value is empty, then the layer has one output with the name 'out', which corresponds to the output data.

    For more information, see Neural Network Weight Tying.

    Parameters and Initialization

    Function to initialize the weights, specified as one these values:

    • 'narrow-normal' — Initialize the weights by independently sampling from a normal distribution with zero mean and standard deviation 0.01.

    • 'glorot' — Initialize the weights with the Glorot initializer [1] (also known as the Xavier initializer). The Glorot initializer independently samples from a normal distribution with zero mean and variance 2/(1 + OutputSize).

    • 'he' — Initialize the weights with the He initializer [2]. The He initializer samples from a normal distribution with zero mean and variance 2.

    • 'orthogonal' — Initialize the input weights with Q, the orthogonal matrix given by the QR decomposition of Z = QR for a random matrix Z sampled from a unit normal distribution. [3]

    • 'zeros' — Initialize the weights with zeros.

    • 'ones' — Initialize the weights with ones.

    • Function handle — Initialize the weights with a custom function. If you specify a function handle, then the function syntax must be of the form weights = func(sz), where sz is the size of the weights. For an example, see Specify Custom Weight Initialization Function.

    • [] — Do not initialize the weights. Use the weights from the 'weights' input instead.

    The layer initializes the weights only when the Weights and InputLearnables properties are empty.

    Data Types: double | char | function_handle

    Embedding weights, specified as an OutputSize-by-MaxIndex matrix.

    The vector Weights(:,i) is the embedding vector for the input value i.

    If InputLearnables specifies that the layer receives weights from another layer, then the layer has two inputs with the names 'in' and 'weights', which correspond to the output data and learnable weights, respectively. In this case, the corresponding properties that contain the initializer, learnable parameters, learning rate factor, and L2 regularization factor must be [].

    If the InputLearnables value is empty, then the layer has one input with the name 'in', which corresponds to the input data. In this case, the layer uses the Weights property for the layer operation.

    If InputLearnables is "weights", then the Weights, WeightsInitializer, WeightLearnRateFactor, and WeightL2Factor properties must be empty.

    Data Types: single | double

    Learning Rate and Regularization

    Learning rate factor for the weights, specified as one of these values:

    • Nonnegative scalar — Use the specified learning rate factor.

    • [] (since R2026a) — Use learning rate factor from another layer.

    The software multiplies this factor by the global learning rate to determine the learning rate for the weights in this layer. For example, if WeightLearnRateFactor is 2, then the learning rate for the weights in this layer is twice the current global learning rate. The software determines the global learning rate based on the settings you specify using the trainingOptions function.

    If InputLearnables is "weights", then the Weights, WeightsInitializer, WeightLearnRateFactor, and WeightL2Factor properties must be empty.

    Data Types: double

    L2 regularization factor for the weights, specified as one of these values:

    • Nonnegative scalar — Use the specified regularization factor.

    • [] — Use regularization factor from another layer.

    The software multiplies this factor by the global L2 regularization factor to determine the L2 regularization for the weights in this layer. For example, if WeightL2Factor is 2, then the L2 regularization for the weights in this layer is twice the global L2 regularization factor. You can specify the global L2 regularization factor using the trainingOptions function.

    If InputLearnables is "weights", then the Weights, WeightsInitializer, WeightLearnRateFactor, and WeightL2Factor properties must be empty.

    Data Types: double

    Layer

    Layer name, specified as a character vector. For Layer array input, the trainnet and dlnetwork functions automatically assign names to unnamed layers.

    Data Types: char

    This property is read-only.

    Number of inputs to the layer.

    If InputLearnables specifies that the layer receives weights from another layer, then the layer has two inputs with the names 'in' and 'weights', which correspond to the output data and learnable weights, respectively. In this case, the corresponding properties that contain the initializer, learnable parameters, learning rate factor, and L2 regularization factor must be [].

    If the InputLearnables value is empty, then the layer has one input with the name 'in', which corresponds to the input data. In this case, the layer uses the Weights property for the layer operation.

    Data Types: double

    This property is read-only.

    Input names of the layer.

    If InputLearnables specifies that the layer receives weights from another layer, then the layer has two inputs with the names 'in' and 'weights', which correspond to the output data and learnable weights, respectively. In this case, the corresponding properties that contain the initializer, learnable parameters, learning rate factor, and L2 regularization factor must be [].

    If the InputLearnables value is empty, then the layer has one input with the name 'in', which corresponds to the input data. In this case, the layer uses the Weights property for the layer operation.

    This property is read-only.

    Number of outputs to the layer.

    If OutputLearnables specifies that the layer outputs learnables to another layer, then the layer has two outputs with the names 'out' and 'weights', which correspond to the output data and learnable weights, respectively. In this case, the layer also outputs the weights that it uses in the layer operation.

    If the OutputLearnables value is empty, then the layer has one output with the name 'out', which corresponds to the output data.

    Data Types: double

    This property is read-only.

    Output names of the layer.

    If OutputLearnables specifies that the layer outputs learnables to another layer, then the layer has two outputs with the names 'out' and 'weights', which correspond to the output data and learnable weights, respectively. In this case, the layer also outputs the weights that it uses in the layer operation.

    If the OutputLearnables value is empty, then the layer has one output with the name 'out', which corresponds to the output data.

    Examples

    collapse all

    Create an embedding layer with an output size of 100 and a maximum index of 512.

    layer = embeddingLayer(100,512)
    layer = 
      EmbeddingLayer with properties:
    
                    Name: ''
        ZeroIndexMapping: 'zeros'
               MaskIndex: []
    
       Hyperparameters
              OutputSize: 100
                MaxIndex: 512
    
       Learnable Parameters
                 Weights: []
    
      Show all properties
    
    

    Include an embedding layer in a layer array.

    layers = [
        sequenceInputLayer(1)
        embeddingLayer(300,512)
        lstmLayer(100)
        fullyConnectedLayer(10)
        softmaxLayer];

    Algorithms

    expand all

    References

    [1] Glorot, Xavier, and Yoshua Bengio. "Understanding the Difficulty of Training Deep Feedforward Neural Networks." In Proceedings of the Thirteenth International Conference on Artificial Intelligence and Statistics, 249–356. Sardinia, Italy: AISTATS, 2010. https://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf

    [2] He, Kaiming, Xiangyu Zhang, Shaoqing Ren, and Jian Sun. "Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification." In 2015 IEEE International Conference on Computer Vision (ICCV), 1026–34. Santiago, Chile: IEEE, 2015. https://doi.org/10.1109/ICCV.2015.123

    [3] Saxe, Andrew M., James L. McClelland, and Surya Ganguli. "Exact Solutions to the Nonlinear Dynamics of Learning in Deep Linear Neural Networks.” Preprint, submitted February 19, 2014. https://arxiv.org/abs/1312.6120.

    Extended Capabilities

    expand all

    Version History

    Introduced in R2026a