represents a layer that aggregates an array of arbitrary rank into a vector, using the function f.
AggregationLayer[f,n]
aggregates an array at level n.
AggregationLayer[f,n1;;n2]
aggregates an array at levels n1 through n2.
AggregationLayer[f,{n1,n2,…}]
aggregates an array at levels n1,n2,….
AggregationLayer
represents a layer that aggregates an array of arbitrary rank into a vector, using the function f.
AggregationLayer[f,n]
aggregates an array at level n.
AggregationLayer[f,n1;;n2]
aggregates an array at levels n1 through n2.
AggregationLayer[f,{n1,n2,…}]
aggregates an array at levels n1,n2,….
Details and Options
- AggregationLayer is typically used inside NetChain, NetGraph, etc. as the final stage in a chain of convolutions, poolings, etc. to convert an array with spatial dimensions into a fixed-size vector representation.
- AggregationLayer[f] operates on an array of dimensions {d1,…,dn} to produce a vector of size d1, effectively mapping the function f over a list of flattened subarrays of size d2×…×dn.
- AggregationLayer[f,{n1,…,nk}] operates on a rank-m array of dimensions {d1,…,dm} to produce an array of rank m-k, with dimensions Complement[{d1,…,dm},{dn1,…,dnk}].
- Possible values for f are Mean, Min, Max, Total, Times, Median, Variance, StandardDeviation, MeanDeviation and InterquartileRange.
- AggregationLayer[f] is equivalent to AggregationLayer[f,2;;All].
- AggregationLayer exposes the following ports for use in NetGraph etc.:
-
"Input" an array "Output" an array - When it cannot be inferred from other layers in a larger net, the option "Input"{d1,…,dn} can be used to fix the input of AggregationLayer to be an array of dimensions {d1,…,dn}.
- AggregationLayer[…][input] explicitly computes the output from applying the layer.
- AggregationLayer[…][{input1,input2,…}] explicitly computes outputs for each of the inputi.
- When given a NumericArray as input, the output will be a NumericArray.
- Options[AggregationLayer] gives the list of default options to construct the layer. Options[AggregationLayer[…]] gives the list of default options to evaluate the layer on some data.
- Information[AggregationLayer[…]] gives a report about the layer.
- Information[AggregationLayer[…],prop] gives the value of the property prop of AggregationLayer[…]. Possible properties are the same as for NetGraph.
Examples
open all close allBasic Examples (2)
Create an AggregationLayer using Max as the aggregation function:
AggregationLayer[Max]Create an AggregationLayer that sums the elements of each row of a matrix:
aggregate = AggregationLayer[Total, 2, "Input" -> {4, 3}]aggregate[(| | | |
| - | - | - |
| 1 | 2 | 3 |
| 4 | 9 | 2 |
| 0 | 0 | 1 |
| 3 | 2 | 1 |)]Scope (5)
Create an AggregationLayer that finds the maximum value of an array:
aggregate = AggregationLayer[Max, All]data = RandomReal[1, {3, 4, 4, 2}];aggregate[data]aggregate2 = AggregationLayer[Max, 1 ;; ]aggregate2[data]Create an AggregationLayer that aggregates over the first, third and fourth dimensions of an input array:
aggregate = AggregationLayer[Mean, {1, 3, 4}]aggregate[RandomReal[1, {1, 2, 3, 4}]]Create an AggregationLayer with the input size specified:
aggregate = AggregationLayer[Total, "Input" -> {3, 4, 4}]aggregate[RandomReal[1, {3, 4, 4}]]Thread the layer across a batch of inputs:
aggregate[RandomReal[1, {5, 3, 4, 4}]]Apply an AggregationLayer to aggregate over all the pixels in an image, returning the mean values for each color channel:
aggregate = AggregationLayer[Mean, "Input" -> NetEncoder["Image"]]aggregate[[image]]Apply an AggregationLayer to aggregate across color channels, turning a color image into a grayscale one:
aggregate = AggregationLayer[Mean, 1, "Input" -> NetEncoder["Image"]]Image@aggregate[[image]]Apply an AggregationLayer to aggregate over a varying dimension:
enc = NetEncoder[{"Characters", "gatc", "OutputForm" -> "UnitVector"}];
aggregate = AggregationLayer[Mean, 1, "Input" -> enc]aggregate["gattaca"]Properties & Relations (1)
AggregationLayer[f,{n1,…,nk}] is equivalent to:
agg[f_, ind_List] := Map[f, Flatten[#, Append[Partition[Complement[Range[TensorRank[#]], ind], 1], ind]], {-2}]&data = RandomReal[1, {3, 4, 5, 3}];agg[Max, {1, 3, 4}][data]AggregationLayer[Max, {1, 3, 4}][data]Possible Issues (1)
AggregationLayer cannot accept symbolic inputs:
aggregate = AggregationLayer[Max];
aggregate[{{1, 2}}]aggregate[{{1, z}}]See Also
SummationLayer PoolingLayer ThreadingLayer TotalLayer NetChain NetGraph NetTrain
Function Repository: LogSumExpLayer
Tech Notes
Related Guides
History
Introduced in 2017 (11.1) | Updated in 2017 (11.2) ▪ 2018 (11.3) ▪ 2020 (12.1) ▪ 2020 (12.2)
Text
Wolfram Research (2017), AggregationLayer, Wolfram Language function, https://reference.wolfram.com/language/ref/AggregationLayer.html (updated 2020).
CMS
Wolfram Language. 2017. "AggregationLayer." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/AggregationLayer.html.
APA
Wolfram Language. (2017). AggregationLayer. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/AggregationLayer.html
BibTeX
@misc{reference.wolfram_2026_aggregationlayer, author="Wolfram Research", title="{AggregationLayer}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/AggregationLayer.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_aggregationlayer, organization={Wolfram Research}, title={AggregationLayer}, year={2020}, url={https://reference.wolfram.com/language/ref/AggregationLayer.html}, note=[Accessed: 12-June-2026]}