EmbeddingConcatenationLayer - Embedding concatenation layer - MATLAB (original) (raw)
Embedding concatenation layer
Since R2023b
Description
An embedding concatenation layer combines its input and an embedding vector by concatenation.
Creation
Syntax
Description
`layer` = embeddingConcatenationLayer
creates an embedding concatenation layer.
Properties
Parameters and Initialization
Function to initialize the weights, specified as one of these values:
"narrow-normal"
— Initialize the weights by independently sampling from a normal distribution with zero mean and a standard deviation of 0.01."glorot"
— Initialize the weights with the Glorot initializer [1] (also known as Xavier initializer). The Glorot initializer independently samples from a uniform distribution with zero mean and a variance of2/(numIn + numOut)
, wherenumIn
andnumOut
are the number of channels in the layer input, respectively."he"
— Initialize the weights with the He initializer[2]. The He initializer samples from a normal distribution with zero mean and a variance of2/numIn
, wherenumIn
is the number of channels in the layer input."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 must have the form
weights = func(sz)
, wheresz
is the size of the weights.
The layer initializes the weights only when the Weights
property is empty.
Learnable weights, specified as a numeric column vector of length numChannels
or []
.
The layer weights are learnable parameters. You can specify the initial value of the weights directly using the Weights
property of the layer. When you train a network, if the Weights
property of the layer is nonempty, then the trainnet function uses the Weights
property as the initial value. If the Weights
property is empty, then the software uses the initializer specified by the WeightsInitializer
property of the layer.
Data Types: single
| double
Layer
Data Types: char
| string
This property is read-only.
Number of inputs to the layer, stored as 1
. This layer accepts a single input only.
Data Types: double
This property is read-only.
Input names, stored as {'in'}
. This layer accepts a single input only.
Data Types: cell
This property is read-only.
Number of outputs from the layer, stored as 1
. This layer has a single output only.
Data Types: double
This property is read-only.
Output names, stored as {'out'}
. This layer has a single output only.
Data Types: cell
Examples
Create an embedding concatenation layer.
layer = embeddingConcatenationLayer
layer = EmbeddingConcatenationLayer with properties:
Name: ''
InputSize: 'auto'
WeightsInitializer: 'narrow-normal'
WeightLearnRateFactor: 1
WeightL2Factor: 1
Learnable Parameters Weights: []
State Parameters No properties.
Show all properties
Include an embedding concatenation layer in a neural network.
net = dlnetwork;
numChannels = 1;
embeddingOutputSize = 64; numWords = 128;
maxSequenceLength = 100; maxPosition = maxSequenceLength+1;
numHeads = 4; numKeyChannels = 4*embeddingOutputSize;
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") 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
An embedding concatenation layer combines its input and an embedding vector by concatenation.
The output of the layer has the same number of dimensions as the input. In the output, each vector in the first position over the channel dimension is the learnable embedding weights vector Weights
.
For example:
- For sequence data
X
represented by anumChannels
-by-numObservations
-by-numTimeSteps
array, wherenumChannels
,numObservations
, andnumTimeSteps
are the numbers of channels, observations, and time steps of the input, respectively, the output is anOutputSize
-by-numObservations
-by-(numTimeSteps+1)
arrayY
, whereY(:,:,1)
isWeights
andY(:,:,2:end)
isX
. - For 1-D image data
X
represented by aheight
-by-numChannels
-by-numObservations
array, whereheight
,numChannels
, andnumObservations
are the height, number of channels, and the number of observations of the input images, respectively, the output is a(height+1)
-by-OutputSize
-by-numObservations
arrayY
, whereY(1,:,:)
isWeights
andY(2:end,:,:)
isX
.
Layers in a layer array or layer graph pass data to subsequent layers as formatted dlarray objects. The format of a dlarray
object is a string of characters in which each character describes the corresponding dimension of the data. The format consists of one or more of these characters:
"S"
— Spatial"C"
— Channel"B"
— Batch"T"
— Time"U"
— Unspecified
For example, you can describe 2-D image data that is represented as a 4-D array, where the first two dimensions correspond to the spatial dimensions of the images, the third dimension corresponds to the channels of the images, and the fourth dimension corresponds to the batch dimension, as having the format "SSCB"
(spatial, spatial, channel, batch).
You can interact with these dlarray
objects in automatic differentiation workflows, such as those for developing a custom layer, using a functionLayer object, or using the forward and predict functions withdlnetwork
objects.
This table shows the supported input formats of EmbeddingConcatenationLayer
objects and the corresponding output format. If the software passes the output of the layer to a custom layer that does not inherit from the nnet.layer.Formattable
class, or aFunctionLayer
object with the Formattable
property set to 0
(false
), then the layer receives an unformatted dlarray
object with dimensions ordered according to the formats in this table. The formats listed here are only a subset. The layer may support additional formats such as formats with additional "S"
(spatial) or"U"
(unspecified) dimensions.
Input Format | Output Format |
---|---|
"SCB" (spatial, channel, batch) | "SCB" (spatial, channel, batch) |
"CBT" (channel, batch, time) | "CBT" (channel, batch, time) |
"SC" (spatial, channel) | "SC" (spatial, channel) |
In dlnetwork
objects, EmbeddingConcatenationLayer
objects also support these input and output format combinations.
Input Format | Output Format |
---|---|
"CT" (channel, time) | "CT" (channel, time) |
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
Extended Capabilities
Usage notes and limitations:
You can generate generic C/C++ code that does not depend on third-party libraries and deploy the generated code to hardware platforms.
Usage notes and limitations:
You can generate CUDA code that is independent of deep learning libraries and deploy the generated code to platforms that use NVIDIA® GPU processors.
Version History
Introduced in R2023b