represents a net layer that takes an array and a position specification as inputs and extracts the specified parts from the array.
ExtractLayer
represents a net layer that takes an array and a position specification as inputs and extracts the specified parts from the array.
Details and Options
- ExtractLayer exposes the following ports for use in NetGraph etc.:
-
"Input" input array "Position" integer array of part specifications "Output" output array - ExtractLayer is typically used inside NetChain, NetGraph, etc.
- ExtractLayer normally infers the dimensions of its input from its context in NetChain etc. To specify the dimensions explicitly as {n1,n2,…} and {m1,m2,…}, use ExtractLayer["Input"->{n1,n2,…},"Position"->{m1,m2,…}].
- ExtractLayer["Port"shape] allows the shape of the "Input" or "Position" port to be specified. Possible forms of shape are:
-
n a vector of length n {n1,n2,…} an array of dimensions n1×n2×… {"Varying",n2,n3,…} an array whose first dimension is variable and whose remaining dimensions are n2×n3×… NetEncoder[…] an encoder - 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 {n1,n2,…,nd } and a position specification of dimensions {m1,m2,…,mp,s}, the output will have dimensions {m1,m2,…,mp,ns+1,…,nd}.
- 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 {m1,m2,…,mp}.
- ExtractLayer[…][<|"Input"input,"Position"position|>] explicitly computes the output from applying the layer to input and position.
- ExtractLayer[…][<|"Input"{input1,input2,…},"Input"{pos1,pos2,…}|>] explicitly computes output for each of the inputi and the posi.
- Negative integers in the position specification are counted from the last element progressing backward.
- When given a NumericArray as input, the output will be a NumericArray.
- Options[ExtractLayer] gives the list of default options to construct the layer. Options[ExtractLayer[…]] gives the list of default options to evaluate the layer on some data.
- Information[ExtractLayer[…]] gives a report about the layer.
- Information[ExtractLayer[…],prop] gives the value of the property prop of ExtractLayer[…]. Possible properties are the same as for NetGraph.
Examples
open all close allBasic Examples (1)
Create an ExtractLayer:
extr = ExtractLayer[]Apply the layer to a vector and a part specification:
extr[<|"Input" -> {74, 85, 12}, "Position" -> {2}|>]Scope (2)
Arguments (1)
Create an ExtractLayer:
extr = ExtractLayer[]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}}|>]Ports (1)
Create an 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}}}|>]Properties & Relations (2)
ExtractLayer performs the same computation as 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}}]ExtractLayer selects parts of an array based on the current specification, while 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]PartLayer only supports a single-part specification and not an arbitrary array of those, but accepts Span to extract chunks of the input:
part = PartLayer[{3, 2 ;; 5}]part[RandomReal[1, {6, 6}]]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 ExtractLayer will always assume it is 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|>]Tech Notes
Related Guides
History
Text
Wolfram Research (2019), ExtractLayer, Wolfram Language function, https://reference.wolfram.com/language/ref/ExtractLayer.html.
CMS
Wolfram Language. 2019. "ExtractLayer." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ExtractLayer.html.
APA
Wolfram Language. (2019). ExtractLayer. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ExtractLayer.html
BibTeX
@misc{reference.wolfram_2026_extractlayer, author="Wolfram Research", title="{ExtractLayer}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/ExtractLayer.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_extractlayer, organization={Wolfram Research}, title={ExtractLayer}, year={2019}, url={https://reference.wolfram.com/language/ref/ExtractLayer.html}, note=[Accessed: 12-June-2026]}