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
    da 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×


open all close all

Basic Examples  (2)

Create a UnitVectorLayer that will accept an array containing 1 or 2 as input and produce an array of "one-hot" vectors:

Click for copyable input

Create a UnitVectorLayer that will accept 1, 2 or 3 as input and produce a "one-hot" vector:

Click for copyable input

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

Click for copyable input

Scope  (3)

Applications  (1)

Properties & Relations  (1)

Possible Issues  (1)

Introduced in 2017