# embeddingConcatenationLayer

## Description

An embedding concatenation layer combines its input and an embedding vector by concatenation.

## Creation

### Description

creates
an embedding concatenation layer.`layer`

= embeddingConcatenationLayer

creates an embedding concatenation layer and sets the Parameters and Initialization and `layer`

= embeddingConcatenationLayer(`Name=Value`

)`Name`

properties using one or more name-value arguments.

## Properties

### Parameters and Initialization

`WeightsInitializer`

— Function to initialize weights

`"narrow-normal"`

(default) | `"glorot"`

| `"he"`

| `"zeros"`

| `"ones"`

| function handle

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 of`2/(numIn + numOut)`

, where`numIn`

and`numOut`

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 of`2/numIn`

, where`numIn`

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)`

, where`sz`

is the size of the weights.

The layer initializes the weights only when the `Weights`

property is empty.

`Weights`

— Learnable weights

`[]`

(default) | column vector

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

`Name`

— Layer name

`""`

(default) | character vector | string scalar

`NumInputs`

— Number of inputs

`1`

(default)

This property is read-only.

Number of inputs to the layer, returned as `1`

. This layer accepts a
single input only.

**Data Types: **`double`

`InputNames`

— Input names

`{'in'}`

(default)

This property is read-only.

Input names, returned as `{'in'}`

. This layer accepts a single input
only.

**Data Types: **`cell`

`NumOutputs`

— Number of outputs

`1`

(default)

This property is read-only.

Number of outputs from the layer, returned as `1`

. This layer has a
single output only.

**Data Types: **`double`

`OutputNames`

— Output names

`{'out'}`

(default)

This property is read-only.

Output names, returned as `{'out'}`

. This layer has a single output
only.

**Data Types: **`cell`

## Examples

### Create Embedding Concatenation Layer

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. Use properties method to see a list of 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

### Embedding Concatenation Layer

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 a`numChannels`

-by-`numObservations`

-by-`numTimeSteps`

array, where`numChannels`

,`numObservations`

, and`numTimeSteps`

are the numbers of channels, observations, and time steps of the input, respectively, the output is an`OutputSize`

-by-`numObservations`

-`by-(numTimeSteps+1)`

array`Y`

, where`Y(:,:,1)`

is`Weights`

and`Y(:,:,2:end)`

is`X`

.For 1-D image data

`X`

represented by a`height`

-by-`numChannels`

-by-`numObservations`

array, where`height`

,`numChannels`

, and`numObservations`

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`

array`Y`

, where`Y(1,:,:)`

is`Weights`

and`Y(2:end,:,:)`

is`X`

.

### Layer Input and Output Formats

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 formats consist 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 with
`dlnetwork`

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 a
`FunctionLayer`

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

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

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.

### GPU Code Generation

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

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**

## See Also

`selfAttentionLayer`

| `attentionLayer`

| `positionEmbeddingLayer`

| `indexing1dLayer`

| `trainnet`

| `trainingOptions`

| `dlnetwork`

## MATLAB 명령

다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.

명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)