Main Content

indexing1dLayer

1-D indexing layer

Since R2023b

    Description

    A 1-D indexing layer extracts the data from the specified index of the time or spatial dimensions of the input data.

    Creation

    Description

    example

    layer = indexing1dLayer creates a 1-D indexing layer that extracts the data from the first index of the time or spatial dimensions of the input data.

    layer = indexing1dLayer(index) creates a 1-D indexing layer object and sets the Index property.

    example

    layer = indexing1dLayer(___,Name=Value) sets the 1-D Indexing and Name properties using one or more name-value arguments.

    Properties

    expand all

    1-D Indexing

    Index of data to extract, specified as one of these values:

    • "first" — Extract data from the first index of the input.

    • "last" — Extract data from the last index of the input.

    If the layer has a padding mask input, then the layer ignores padding values when it extracts data.

    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.

    Layer

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

    The Indexing1DLayer object stores this property as a character vector.

    Data Types: char | string

    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 Indexing1DLayer object stores this property as a cell array of character vectors.

    This property is read-only.

    Number of outputs from the layer, returned as 1. This layer has a single output only.

    Data Types: double

    This property is read-only.

    Output names, returned as {'out'}. This layer has a single output only.

    Data Types: cell

    Examples

    collapse all

    Create a 1-D indexing layer.

    layer = indexing1dLayer
    layer = 
      Indexing1DLayer with properties:
    
                       Name: ''
                      Index: "first"
        HasPaddingMaskInput: 0
    
       Learnable Parameters
        No properties.
    
       State Parameters
        No properties.
    
    Use properties method to see a list of all properties.
    
    

    Include a 1-D indexing layer in a layer graph.

    numChannels = 1;
    
    embeddingOutputSize = 64;
    numWords = 128;
    
    maxSequenceLength = 100;
    maxPosition = maxSequenceLength+1;
    
    numHeads = 4;
    numKeyChannels = 4*embeddingOutputSize;
    
    net = dlnetwork;
    
    layers = [ 
        sequenceInputLayer(numChannels)
        wordEmbeddingLayer(embeddingOutputSize,numWords,Name="word-emb")
        embeddingConcatenationLayer(Name="emb-cat")
        positionEmbeddingLayer(embeddingOutputSize,maxPosition,Name="pos-emb");
        additionLayer(2,Name="add")
        selfAttentionLayer(numHeads,numKeyChannels,AttentionMask="causal")
        indexing1dLayer(Name="idx-first")
        fullyConnectedLayer(numWords)
        softmaxLayer];
    
    net = addLayers(net,layers);
    net = connectLayers(net,"emb-cat","add/in2");

    View the neural network architecture.

    plot(net)
    axis off
    box off

    Algorithms

    expand all

    Extended Capabilities

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    GPU Code Generation
    Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

    Version History

    Introduced in R2023b

    expand all