({{, (RefLink[ExtractLayer, paclet:ref/ExtractLayer])[]
represents a net layer that takes an array and a position specification as inputs and extracts the specified parts from the array.}})

OpenersNotesSection(Spacer1)Details and Options

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

(RefLink[ExtractLayer, paclet:ref/ExtractLayer])[…][<|"Input"->input,"Position"->position|>] explicitly computes the output from applying the layer to input and position.

(RefLink[ExtractLayer, paclet:ref/ExtractLayer])[…][<|"Input"->{(Subscript[input, 1]),(Subscript[input, 2]),…},"Input"->{(Subscript[pos, 1]),(Subscript[pos, 2]),…}|> explicitly computes output for each of the (Subscript[input, i]) and the (Subscript[pos, i]).

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

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

(RefLink[ExtractLayer, paclet:ref/ExtractLayer]) normally infers the dimensions of its input from its context in (RefLink[NetChain, paclet:ref/NetChain]) etc. To specify the dimensions explicitly as {(Subscript[n, 1]),(Subscript[n, 2]),…} and {(Subscript[m, 1]),(Subscript[m, 2]),…}, use (RefLink[ExtractLayer, paclet:ref/ExtractLayer])["Input"->{(Subscript[n, 1]),(Subscript[n, 2]),…},"Position"->{(Subscript[m, 1]),(Subscript[m, 2]),…}].

(RefLink[ExtractLayer, paclet:ref/ExtractLayer])["Port"->shape] allows the shape of the "Input" or "Position" port to be specified. Possible forms of shape are:

The shape of the "Position" port can also be set to "Integer" to specify a single integer.

The input to the port "Position" is an arbitrary array of part specifications. Given an input of dimensions {(Subscript[n, 1]),(Subscript[n, 2]),…,(Subscript[n, d]) } and a position specification of dimensions {(Subscript[m, 1]),(Subscript[m, 2]),…,(Subscript[m, p]),s}, the output will have dimensions {(Subscript[m, 1]),(Subscript[m, 2]),…,(Subscript[m, p]),(Subscript[n, s+1]),…,(Subscript[n, d])}.

The last dimension of the position specification s is used to specify elements of the input, and so it cannot be greater than the input rank d. In the case of s=d, the output dimensions will just be {(Subscript[m, 1]),(Subscript[m, 2]),…,(Subscript[m, p])}.

Negative integers in the position specification are counted from the last element progressing backward.

OpenersPrimaryExamplesSection(Spacer1)Examples  (9)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Create an (RefLink[ExtractLayer, paclet:ref/ExtractLayer]):

ExtractLayer[]

Create an (RefLink[ExtractLayer, paclet:ref/ExtractLayer]):

extr=ExtractLayer[]

Apply the layer to a vector and a part specification:

extr[<|"Input"->{74,85,12},"Position"->{2}|>]

OpenersExampleSection1(Spacer1)Scope  (2)

Create an (RefLink[ExtractLayer, paclet:ref/ExtractLayer]):

extr=ExtractLayer[]

Specify a list of parts:

out1=extr[<|"Input"->{74,85,12},"Position"->{{1},{2},{2},{3}}|>]

out2=extr[<|"Input"->{{74,85,12},{3,7,4}},"Position"->{{1},{2},{2},{3}}|>]

Create an (RefLink[ExtractLayer, paclet:ref/ExtractLayer]) and specify the shape of its "Input" and "Position" ports:

extr=ExtractLayer["Input"->{2,2},"Position"->{3,1}]

Apply the layer to an input-position pair:

extr[<|"Input"->{{87,54},{32,56}},"Position"->{{2},{2},{1}}|>]

Apply the layer to a batch of input-position pairs:

extr[<|"Input"->{{{87,54},{32,56}},{{4,5},{2,8}}},"Position"->{{{2},{2},{1}},{{1},{1},{2}}}|>]

OpenersExampleSection3(Spacer1)Properties & Relations  (2)

(RefLink[ExtractLayer, paclet:ref/ExtractLayer]) performs the same computation as (RefLink[Extract, paclet:ref/Extract]):

data=ArrayReshape[Range[5*5*5],{5,5,5}];

positions={{1,2,3},{2,3,4}};

ExtractLayer[]@<|"Input"->data,"Position"->positions|>

Extract[data,{{1,2,3},{2,3,4}}]

(RefLink[ExtractLayer, paclet:ref/ExtractLayer]) selects parts of an array based on the current specification, while (RefLink[PartLayer, paclet:ref/PartLayer]) has a fixed-part specification:

data={{0.45,0.3},{1.2,4.7}};

positions={2,1};

extr=ExtractLayer[]

extr[<|"Input"->data,"Position"->positions|>]

part=PartLayer[{2,1}]

part[data]

(RefLink[PartLayer, paclet:ref/PartLayer]) only supports a single-part specification and not an arbitrary array of those, but accepts (RefLink[Span, paclet:ref/Span]) to extract chunks of the input:

part=PartLayer[{3,2;;5}]

part[RandomReal[1,{6,6}]]

OpenersExampleSection4(Spacer1)Possible Issues  (3)

The last dimension of the position specification selects the desired parts of the input; hence its size must not exceed the input rank:

ExtractLayer["Input"->{3,7,5},"Position"->{2,5}]

If the "Position" specification has a rank of 1, its size cannot be variable:

ExtractLayer["Input"->{3,4,5},"Position"->"Varying"]

An unspecified (RefLink[ExtractLayer, paclet:ref/ExtractLayer]) will always assume it's being evaluated on a single example and not on a batch. In the following, the arrays data and pos will be interpreted as a single 2×3 input array and a 2×1 part specification, respectively:

data={{54,32,85},{12,36,94}};

pos={{2},{1}};

ExtractLayer[][<|"Input"->data,"Position"->pos|>]

In order to have them interpreted as a batch of two lists of length 3 and 1, respectively, explicit shapes must be specified prior to evaluation:

ExtractLayer["Input"->3,"Position"->1][<|"Input"->data,"Position"->pos|>]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Extract, paclet:ref/Extract])  ▪  (RefLink[PartLayer, paclet:ref/PartLayer])  ▪  (RefLink[OrderingLayer, paclet:ref/OrderingLayer])  ▪  (RefLink[ReshapeLayer, paclet:ref/ReshapeLayer])  ▪  (RefLink[FlattenLayer, paclet:ref/FlattenLayer])  ▪  (RefLink[ReplicateLayer, paclet:ref/ReplicateLayer])  ▪  (RefLink[TransposeLayer, paclet:ref/TransposeLayer])  ▪  (RefLink[NetChain, paclet:ref/NetChain])  ▪  (RefLink[NetGraph, paclet:ref/NetGraph])}})}})

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

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