represents a net layer that transforms integers between 1 and n into n-dimensional unit vectors.
leaves the n to be inferred from context.
UnitVectorLayer
represents a net layer that transforms integers between 1 and n into n-dimensional unit vectors.
leaves the n to be inferred from context.
Details and Options
- UnitVectorLayer operates either on integers or arrays of integers, producing a vector or array, respectively.
- Specifying UnitVectorLayer[n] will produce a net that infers the n when a NetEncoder[…] that produces integers is connected to its input.
- UnitVectorLayer[…][input] explicitly computes the output from applying the layer to input.
- UnitVectorLayer[…][{input1,input2,…}] explicitly computes an output for each of the inputi.
- When given a NumericArray as input, the output will be a NumericArray.
- UnitVectorLayer is typically used inside NetChain, NetGraph, etc.
- UnitVectorLayer exposes the following ports for use in NetGraph etc.:
-
"Input" an integer or array of integers "Output" an n-dimensional numerical vector or array of vectors - UnitVectorLayer[size,"Input"->shape] allows the shape of the input to be specified. Possible forms for shape are:
-
NetEncoder[…] encoder producing an integer or array of integers "Integer" a single integer d a vector of integers of length d {d1,d2,…} an array of integers of dimensions d1×d2×… "Varying" a vector of integers whose length is variable {"Varying",d2,d3,…} an array of integers whose first dimension is variable and remaining dimensions are d2×d3×… - Options[UnitVectorLayer] gives the list of default options to construct the layer. Options[UnitVectorLayer[…]] gives the list of default options to evaluate the layer on some data.
- Information[UnitVectorLayer[…]] gives a report about the layer.
- Information[UnitVectorLayer[…],prop] gives the value of the property prop of UnitVectorLayer[…]. Possible properties are the same as for NetGraph.
Examples
open all close allBasic Examples (2)
Create a UnitVectorLayer that will accept an array containing 1 or 2 as input and produce an array of "one-hot" vectors:
UnitVectorLayer[2]Create a 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]Scope (3)
Ports (3)
Create a UnitVectorLayer with the input specified to be an integer between 1 and 3:
unit = UnitVectorLayer[3, "Input" -> "Integer"]unit[2]The layer threads across a batch of inputs:
unit[{1, 2, 3}]//MatrixFormCreate a 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}}]//MatrixFormDimensions[%]Applications (1)
Use a NetEncoder in conjunction with a UnitVectorLayer:
unit = UnitVectorLayer["Input" -> NetEncoder["Characters"]]unit["hello world, each letter corresponds to a row"] //ArrayPlotProperties & Relations (1)
UnitVectorLayer is related to the n-dimensional form of UnitVector:
layer = UnitVectorLayer[5]
unit = UnitVector[5, #]&layer[4]unit[4]Tech Notes
Related Guides
History
Text
Wolfram Research (2017), UnitVectorLayer, Wolfram Language function, https://reference.wolfram.com/language/ref/UnitVectorLayer.html.
CMS
Wolfram Language. 2017. "UnitVectorLayer." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/UnitVectorLayer.html.
APA
Wolfram Language. (2017). UnitVectorLayer. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/UnitVectorLayer.html
BibTeX
@misc{reference.wolfram_2026_unitvectorlayer, author="Wolfram Research", title="{UnitVectorLayer}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/UnitVectorLayer.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_unitvectorlayer, organization={Wolfram Research}, title={UnitVectorLayer}, year={2017}, url={https://reference.wolfram.com/language/ref/UnitVectorLayer.html}, note=[Accessed: 13-June-2026]}