Main Content

selfAttentionLayer

Self-attention layer

Since R2023a

    Description

    A self-attention layer computes single-head or multihead self-attention of its input.

    The layer:

    1. Computes the queries, keys, and values from the input

    2. Computes the scaled dot-product attention across heads using the queries, keys, and values

    3. Merges the results from the heads

    4. Performs a linear transformation on the merged result

    Creation

    Description

    layer = selfAttentionLayer(numHeads,numQueryChannels) creates a self-attention layer with the specified number of heads and query channels.

    example

    layer = selfAttentionLayer(numHeads,numQueryChannels,Name=Value) sets writable properties using one or more name-value arguments. For example, selfAttentionLayer(3,12,DropoutProbability=0.1) creates a self attention layer with 3 heads, 12 query channels, and sets the DropoutProbability property to 0.1.

    Input Arguments

    expand all

    This property is read-only.

    Number of attention heads, specified as a positive integer.

    Each head performs a separate linear transformation of the input and computes attention weights independently. The layer uses these attention weights to compute a weighted sum of the input representations, generating a context vector. Increasing the number of heads lets the model capture different types of dependencies and attend to different parts of the input simultaneously. Reducing the number of heads can lower the computational cost of the layer.

    The value of numHeads must divide numQueryChannels evenly.

    This arguments sets the NumHeads property.

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

    Number of channels for the queries, specified as a positive integer.

    The value of numQueryChannels must be divisible by numHeads.

    This arguments sets the NumQueryChannels property (since R2026a).

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

    Properties

    expand all

    Self-Attention

    This property is read-only after object creation. To set this property, use the corresponding positional input argument when you create the SelfAttentionLayer object.

    Number of attention heads, specified as a positive integer.

    Each head performs a separate linear transformation of the input and computes attention weights independently. The layer uses these attention weights to compute a weighted sum of the input representations, generating a context vector. Increasing the number of heads lets the model capture different types of dependencies and attend to different parts of the input simultaneously. Reducing the number of heads can lower the computational cost of the layer.

    Data Types: double

    Since R2026a

    This property is read-only after object creation. To set this property, use the corresponding positional input argument when you create the SelfAttentionLayer object.

    Number of channels for the queries, specified as a positive integer.

    Data Types: double

    Since R2026a

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

    Number of query groups (equivalent to the number of key-value heads), specified as a positive integer. The value of NumQueryGroups must divide NumQueryChannels.

    The value of NumQueryGroups specifies the type of attention operation:

    • For multihead attention, set NumQueryGroups to numHeads.

    • For multiquery attention (MQA), set NumQueryGroups to 1.

    • For grouped-query attention (GQA), set NumQueryGroups to a positive integer between 1 and numHeads.

    When the number of query groups is greater than 1, the operation creates groups of query channels-per-head, and applies the attention operation within each group.

    For example, for six heads with three query groups, the operation splits the query channels into the heads (h1, …, h6) and then creates the groups of heads g1=(h1,h2), g2=(h3,h4), and g3=(h5,h6). The operation also splits the key and value channels into the heads g1, g2, and g3.

    When the number of query groups matches the number of heads, the groups have one head each and is equivalent to multihead attention. When the number of query groups is 1, then all the heads are in the same group and is equivalent to multiquery attention.

    The default value is the NumHeads property value. To programmatically set the number of query groups to the number of heads when you create the layer, you can set the NumQueryGroups name-value argument to "num-heads".

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

    This property is read-only.

    Number of channels for the keys, specified as a positive integer.

    The layer uses NumQueryChannels*NumQueryGroups/NumHeads as the number of channels for the keys (since R2026a).

    Before R2026a: The layer uses numQueryChannels as the number of the channels for the keys. This is equivalent when the number of query groups matches the number of heads.

    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 SelfAttentionLayer object.

    Number of channels for the values, specified as one of these values:

    • "auto" — Use NumKeyChannels.

    • Positive integer — Use the specified number of channels. This value must be divisible by NumHeads.

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

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

    Number of channels of the layer output, specified as one of these values:

    • "auto" — Use the number of channels in the layer input.

    • Positive integer — Use the specified number of channels.

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

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

    Flag indicating whether the layer has an input that represents the padding mask, specified as 0 (false) or 1 (true).

    If the HasPaddingMaskInput property is 0 (false), then the layer has one input with the name "in", which corresponds to the input data. In this case, the layer treats all elements as data.

    If the HasPaddingMaskInput property is 1 (true), then the layer has two inputs with the names "in" and "mask", which correspond to the input data and the mask, respectively. In this case, the padding mask is an array of ones and zeros. The layer uses and ignores elements of the input when the corresponding element in the mask is one or zero, respectively.

    The dimension labels of the padding mask must match the dimension labels of the input data, ignoring any "C" (channel) and "U" (unspecified) dimensions. (since R2026a).

    Before R2026a: The format of the padding mask must match that of the input data.

    The size of the "S" (spatial) or "T" (time) dimension of the padding mask must match the sum of the size of the corresponding dimension in the input and the size of the second dimension of the KeyState and ValueState properties. The size of the "B" (batch) dimension of the padding mask must match the size of the corresponding dimension in the input.

    The padding mask can have any number of channels. The software uses the values in the first channel only to indicate padding values.

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

    Mask preventing attention to elements in key-value pairs, specified as one of these values:

    • "none" — Do not prevent attention to elements based on their positions. If HasPaddingMaskInput is 1 (true), then the layer prevents attention to padding elements only.

    • "causal" — Prevent elements in position M from attending to elements in position N, where N is greater than M. Use this option for autoregressive models.

    Probability of dropping out attention scores, specified as a scalar in the range [0, 1).

    During training, the software randomly sets values in the attention scores to zero using the specified probability. These dropouts can encourage the model to learn more robust and generalizable representations by preventing it from relying too heavily on specific dependencies.

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

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

    Flag indicating whether the layer has an output that represents the scores (also known as the attention weights), specified as 0 (false) or 1 (true).

    If the HasScoresOutput property is 0 (false), then the layer has one output with the name "out", which corresponds to the output data.

    If the HasScoresOutput property is 1 (true), then the layer has two inputs with the names "out" and "scores", which correspond to the output data and the attention scores, respectively.

    This property is read-only.

    Number of input channels, specified as one of these values:

    • "auto" — Automatically determine the number of input channels when you initialize the network

    • Positive integer — Configure the layer for the specified number of input channels. InputSize and the number of channels in the layer input data must match.

    Data Types: double | char

    Parameters and Initialization

    Function to initialize the query, key, value, and output weights, specified as one of these values:

    • "glorot" — Initialize the weights with the Glorot initializer (also known as Xavier initializer) [2]. The Glorot initializer independently samples from a uniform distribution with zero mean and a variance of 2/(numIn + numOut). The values of numIn and numOut depend on the weight matrix:

      WeightnumInnumOut
      QueryInputSizeNumQueryChannels
      KeyInputSizeNumKeyChannels
      ValueInputSizeNumValueChannels
      OutputNumValueChannelsOutputSize

    • "he" — Initialize the weights with the He initializer [3]. The He initializer samples from a normal distribution with zero mean and a variance of 2/numIn. The values of numIn and numOut depend on the weight matrix:

      WeightnumInnumOut
      QueryInputSizeNumQueryChannels
      KeyInputSizeNumKeyChannels
      ValueInputSizeNumValueChannels
      OutputNumValueChannelsOutputSize

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

    • "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.

    The layer only initializes the weights when the corresponding weights property is empty.

    The SelfAttentionLayer object stores this property as a character vector or a function handle.

    Data Types: char | string | function_handle

    Function to initialize the query, key, value, and output biases, specified as one of these values:

    • "zeros" — Initialize the biases with zeros.

    • "ones" — Initialize the biases with ones.

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

    • Function handle — Initialize the biases with a custom function. If you specify a function handle, then the function must have the form bias = func(sz), where sz is the size of the biases.

    The layer only initializes the biases when the corresponding bias property is empty.

    The SelfAttentionLayer object stores this property as a character vector or a function handle.

    Data Types: char | string | function_handle

    Query weights, specified as a NumQueryChannels-by-numInputChannels matrix or [], where numInputChannels is the number of channels in the layer input.

    Data Types: single | double

    Key weights, specified as a NumKeyChannels-by-numInputChannels matrix or [], where numInputChannels is the number of channels in the layer input.

    Data Types: single | double

    Value weights, specified as a NumValueChannels-by-numInputChannels matrix or [], where numInputChannels is the number of channels in the layer input.

    Data Types: single | double

    Output weights, specified as an OutputSize-by-NumValueChannels matrix or [].

    Data Types: single | double

    Query biases, specified as a NumQueryChannels-by-1 vector or [].

    Data Types: single | double

    Key biases, specified as a NumKeyChannels-by-1 vector or [].

    Data Types: single | double

    Value biases, specified as a NumValueChannels-by-1 vector or [].

    Data Types: single | double

    Output biases, specified as an OutputSize-by-1 vector or [].

    Data Types: single | double

    State

    Since R2026a

    Key state, specified as a NumKeyChannels-by-x-by-b numeric array or [], where b is the size of the "B" (batch) dimension of the input and x can be any value.

    When you update the state of a network containing a SelfAttentionLayer object with the AttentionMask property set to "causal", the KeyState property contains keys used in previous forward passes of the network. If AttentionMask is "none", then the KeyState property must be empty.

    After you set this property manually, calls to the resetState function set the key state to this value.

    Data Types: single | double

    Since R2026a

    Value state, specified as a NumValueChannels-by-x-by-b numeric array or [], where b is the size of the "B" (batch) dimension of the input and x can be any value.

    When you update the state of a network containing a SelfAttentionLayer with the AttentionMask property set to "causal", this property contains values used in previous forward passes of the network. If AttentionMask is "none", then the ValueState property must be empty.

    After you set this property manually, calls to the resetState function set the value state to this value.

    Data Types: single | double

    Learning Rate and Regularization

    Learning rate factor for the query, key, value, and output weights, specified as a nonnegative scalar.

    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.

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

    Learning rate factor for the query, key, value, and output biases, specified as a nonnegative scalar.

    The software multiplies this factor by the global learning rate to determine the learning rate for the biases in this layer. For example, if BiasLearnRateFactor is 2, then the learning rate for the biases in the 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.

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

    L2 regularization factor for the query, key, value, and output weights, specified as a nonnegative scalar.

    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.

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

    L2 regularization factor for the query, key, value, and output biases, specified as a nonnegative scalar.

    The software multiplies this factor by the global L2 regularization factor to determine the L2 regularization for the biases in this layer. For example, if BiasL2Factor is 2, then the L2 regularization for the biases in this layer is twice the global L2 regularization factor. The software determines the global L2 regularization factor based on the settings you specify using the trainingOptions function.

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

    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, returned as 1 or 2.

    If the HasPaddingMaskInput property is 0 (false), then the layer has one input with the name "in", which corresponds to the input data. In this case, the layer treats all elements as data.

    If the HasPaddingMaskInput property is 1 (true), then the layer has two inputs with the names "in" and "mask", which correspond to the input data and the mask, respectively. In this case, the padding mask is an array of ones and zeros. The layer uses and ignores elements of the input when the corresponding element in the mask is one or zero, respectively.

    Data Types: double

    This property is read-only.

    Input names of the layer, returned as a cell array of character vectors.

    If the HasPaddingMaskInput property is 0 (false), then the layer has one input with the name "in", which corresponds to the input data. In this case, the layer treats all elements as data.

    If the HasPaddingMaskInput property is 1 (true), then the layer has two inputs with the names "in" and "mask", which correspond to the input data and the mask, respectively. In this case, the padding mask is an array of ones and zeros. The layer uses and ignores elements of the input when the corresponding element in the mask is one or zero, respectively.

    The SelfAttentionLayer object stores this property as a cell array of character vectors.

    This property is read-only.

    Number of outputs of the layer.

    If the HasScoresOutput property is 0 (false), then the layer has one output with the name "out", which corresponds to the output data.

    If the HasScoresOutput property is 1 (true), then the layer has two inputs with the names "out" and "scores", which correspond to the output data and the attention scores, respectively.

    Data Types: double

    This property is read-only.

    Output names of the layer.

    If the HasScoresOutput property is 0 (false), then the layer has one output with the name "out", which corresponds to the output data.

    If the HasScoresOutput property is 1 (true), then the layer has two inputs with the names "out" and "scores", which correspond to the output data and the attention scores, respectively.

    The SelfAttentionLayer object stores this property as a cell array of character vectors.

    Examples

    collapse all

    Create a self-attention layer with eight heads and 256 key and query channels.

    layer = selfAttentionLayer(8,256)
    layer = 
      SelfAttentionLayer with properties:
    
                       Name: ''
              AttentionMask: 'none'
        HasPaddingMaskInput: 0
            HasScoresOutput: 0
    
       Hyperparameters
                  InputSize: 'auto'
                   NumHeads: 8
             NumQueryGroups: 8
           NumQueryChannels: 256
             NumKeyChannels: 256
           NumValueChannels: 'auto'
                 OutputSize: 'auto'
         DropoutProbability: 0
    
       Learnable Parameters
               QueryWeights: []
                 KeyWeights: []
               ValueWeights: []
              OutputWeights: []
                  QueryBias: []
                    KeyBias: []
                  ValueBias: []
                 OutputBias: []
    
       State Parameters
                   KeyState: []
                 ValueState: []
    
      Show all properties
    
    

    Include a self-attention layer in a layer array.

    layers = [
        sequenceInputLayer(12)
        selfAttentionLayer(4,12)
        layerNormalizationLayer
        fullyConnectedLayer(9)
        softmaxLayer];

    Algorithms

    expand all

    References

    [1] Vaswani, Ashish, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, and Illia Polosukhin. "Attention is all you need." In Advances in Neural Information Processing Systems, Vol. 30. Curran Associates, Inc., 2017. https://papers.nips.cc/paper_files/paper/2017/hash/3f5ee243547dee91fbd053c1c4a845aa-Abstract.html.

    [2] 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

    [3] 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

    Extended Capabilities

    expand all

    Version History

    Introduced in R2023a

    expand all