({{, (RefLink[UnitVectorLayer, paclet:ref/UnitVectorLayer])[n] 
represents a net layer that transforms integers between 1 and n into n-dimensional unit vectors. }, {, (RefLink[UnitVectorLayer, paclet:ref/UnitVectorLayer])[]
leaves the n to be inferred from context.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[UnitVectorLayer, paclet:ref/UnitVectorLayer]) operates either on integers or arrays of integers, producing a vector or array, respectively.

Specifying (RefLink[UnitVectorLayer, paclet:ref/UnitVectorLayer])[n] will produce a net that infers the n when a (RefLink[NetEncoder, paclet:ref/NetEncoder])[…] that produces integers is connected to its input.

(RefLink[UnitVectorLayer, paclet:ref/UnitVectorLayer])[…][input] explicitly computes the output from applying the layer to input.

(RefLink[UnitVectorLayer, paclet:ref/UnitVectorLayer])[…][{(Subscript[input, 1]),(Subscript[input, 2]),…}] explicitly computes an output for each of the (Subscript[input, i]).

When given a (RefLink[NumericArray, paclet:ref/NumericArray]) as input, the output will be a (RefLink[NumericArray, paclet:ref/NumericArray]).

(RefLink[UnitVectorLayer, paclet:ref/UnitVectorLayer]) is typically used inside (RefLink[NetChain, paclet:ref/NetChain]), (RefLink[NetGraph, paclet:ref/NetGraph]), etc.

(RefLink[UnitVectorLayer, paclet:ref/UnitVectorLayer]) exposes the following ports for use in (RefLink[NetGraph, paclet:ref/NetGraph]) etc.:

(RefLink[UnitVectorLayer, paclet:ref/UnitVectorLayer])[size,"Input"->shape] allows the shape of the input to be specified. Possible forms for shape are:

OpenersPrimaryExamplesSection(Spacer1)Examples  (8)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Create a (RefLink[UnitVectorLayer, paclet:ref/UnitVectorLayer]) that will accept an array containing 1 or 2 as input and produce an array of "one-hot" vectors:

UnitVectorLayer[2]

Create a (RefLink[UnitVectorLayer, paclet:ref/UnitVectorLayer]) that will accept 1, 2 or 3 as input and produce a "one-hot" vector:

unit=UnitVectorLayer[3,"Input"->"Integer"]

Apply the layer to an integer to produce an output vector:

unit[3]

OpenersExampleSection1(Spacer1)Scope  (3)

Create a (RefLink[UnitVectorLayer, paclet:ref/UnitVectorLayer]) with the input specified to be an integer between 1 and 3:

unit=UnitVectorLayer[3,"Input"->"Integer"]

Apply the layer to an input:

unit[2]

The layer threads across a batch of inputs:

unit[{1,2,3}]//MatrixForm

Create a (RefLink[UnitVectorLayer, paclet:ref/UnitVectorLayer]) with the input specified to be a sequence of integers between 1 and 3:

unit=UnitVectorLayer[3,"Input"->"Varying"]

Apply the layer to a sequence:

unit[{3,2,1}]

Apply the layer to a batch of sequences of varying lengths:

unit[{{1,2},{3,2,1},{3}}]

Map[MatrixForm,%]

Explicitly specify that the input is a matrix of integers between 1 and 3:

unit=UnitVectorLayer[3,"Input"->{2,2}]

Apply the input to a matrix to obtain a three-dimensional array:

unit[{{1,2},{3,2}}]//MatrixForm

Dimensions[%]

OpenersExampleSection3(Spacer1)Applications  (1)

Use a (RefLink[NetEncoder, paclet:ref/NetEncoder]) in conjunction with a (RefLink[UnitVectorLayer, paclet:ref/UnitVectorLayer]):

unit=UnitVectorLayer["Input"->NetEncoder["Characters"]]

unit["hello world, each letter corresponds to a row"] //ArrayPlot

OpenersExampleSection4(Spacer1)Properties & Relations  (1)

(RefLink[UnitVectorLayer, paclet:ref/UnitVectorLayer]) is related to the n-dimensional form of (RefLink[UnitVector, paclet:ref/UnitVector]):

layer=UnitVectorLayer[5]
unit=UnitVector[5,#]&

Apply both to an integer:

layer[4]

unit[4]

OpenersExampleSection5(Spacer1)Possible Issues  (1)

Out-of-bounds integers will trigger a failure:

UnitVectorLayer[3][4]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[EmbeddingLayer, paclet:ref/EmbeddingLayer])  ▪  (RefLink[NetChain, paclet:ref/NetChain])  ▪  (RefLink[NetGraph, paclet:ref/NetGraph])  ▪  (RefLink[NetInitialize, paclet:ref/NetInitialize])  ▪  (RefLink[NetTrain, paclet:ref/NetTrain])  ▪  (RefLink[NetExtract, paclet:ref/NetExtract])  ▪  (RefLink[UnitVector, paclet:ref/UnitVector])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Neural Networks in the Wolfram Language, paclet:tutorial/NeuralNetworksOverview])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Neural Networks, paclet:guide/NeuralNetworks])}})}})