Table of Contents

Class AdaptiveAveragePoolingLayer<T>

Namespace
AiDotNet.NeuralNetworks.Layers
Assembly
AiDotNet.dll

Implements adaptive average pooling that outputs a fixed spatial size regardless of input dimensions.

public class AdaptiveAveragePoolingLayer<T> : LayerBase<T>, ILayer<T>, IJitCompilable<T>, IDiagnosticsProvider, IWeightLoadable<T>, IDisposable

Type Parameters

T

The numeric type used for calculations, typically float or double.

Inheritance
AdaptiveAveragePoolingLayer<T>
Implements
Inherited Members

Remarks

Adaptive average pooling automatically calculates the required kernel size and stride to produce an output of the specified dimensions. This is particularly useful when you want to handle variable input sizes but need a fixed output size (e.g., before a fully connected layer).

For Beginners: Regular pooling uses a fixed window size (like 2x2) and reduces the image. Adaptive pooling works in reverse: you specify the output size you want (like 1x1), and it automatically figures out how to pool the entire input to get that size.

For example:

  • Input: 14x14, Output: 1x1 → Pools each entire channel to a single value
  • Input: 7x7, Output: 1x1 → Same result: each channel becomes one value
  • Input: 56x56, Output: 7x7 → Divides into 7x7 regions and averages each

This is commonly used in ResNet and other architectures for "global average pooling" where the final feature maps are reduced to a single value per channel before classification.

Constructors

AdaptiveAveragePoolingLayer(int, int, int, int, int)

Initializes a new instance of the AdaptiveAveragePoolingLayer<T> class.

public AdaptiveAveragePoolingLayer(int inputChannels, int inputHeight, int inputWidth, int outputHeight = 1, int outputWidth = 1)

Parameters

inputChannels int

The number of input channels.

inputHeight int

The expected input height (can vary at runtime).

inputWidth int

The expected input width (can vary at runtime).

outputHeight int

The desired output height (default: 1 for global pooling).

outputWidth int

The desired output width (default: 1 for global pooling).

Remarks

For Beginners: The default output size of 1x1 creates "global average pooling", which averages all spatial positions in each channel into a single value. This is commonly used before the final classification layer in modern CNNs.

Properties

SupportsGpuExecution

Gets a value indicating whether this layer supports GPU execution.

protected override bool SupportsGpuExecution { get; }

Property Value

bool

SupportsJitCompilation

Gets whether this layer supports JIT compilation.

public override bool SupportsJitCompilation { get; }

Property Value

bool

Remarks

AdaptiveAveragePoolingLayer supports JIT compilation by computing the appropriate pool size and stride to achieve the desired output dimensions, then using AvgPool2D.

SupportsTraining

Gets a value indicating whether this layer supports training.

public override bool SupportsTraining { get; }

Property Value

bool

Remarks

Pooling layers don't have trainable parameters, but they support backpropagation.

Methods

Backward(Tensor<T>)

Performs the backward pass of adaptive average pooling.

public override Tensor<T> Backward(Tensor<T> outputGradient)

Parameters

outputGradient Tensor<T>

The gradient of the loss with respect to the output.

Returns

Tensor<T>

The gradient of the loss with respect to the input.

BackwardGpu(IGpuTensor<T>)

Performs the GPU-resident backward pass of adaptive average pooling.

public override IGpuTensor<T> BackwardGpu(IGpuTensor<T> outputGradient)

Parameters

outputGradient IGpuTensor<T>

The GPU tensor containing the gradient of the loss with respect to the output.

Returns

IGpuTensor<T>

The gradient of the loss with respect to the input.

ExportComputationGraph(List<ComputationNode<T>>)

Exports the computation graph for JIT compilation.

public override ComputationNode<T> ExportComputationGraph(List<ComputationNode<T>> inputNodes)

Parameters

inputNodes List<ComputationNode<T>>

List to populate with input computation nodes.

Returns

ComputationNode<T>

The output computation node representing the AdaptiveAveragePoolingLayer.

Remarks

Adaptive average pooling is implemented by calculating the appropriate pool size and stride to achieve the target output dimensions. For global average pooling (1x1), the pool size equals the entire input spatial dimensions.

For a given input size H_in and target output H_out: - Pool size H = ceiling(H_in / H_out) - Stride H = floor(H_in / H_out)

Forward(Tensor<T>)

Performs the forward pass of adaptive average pooling.

public override Tensor<T> Forward(Tensor<T> input)

Parameters

input Tensor<T>

The input tensor of any rank >= 3. Last 3 dims are [C, H, W].

Returns

Tensor<T>

The pooled output tensor with same leading dims, [C, outH, outW].

ForwardGpu(params IGpuTensor<T>[])

Performs the forward pass of adaptive average pooling on GPU tensors.

public override IGpuTensor<T> ForwardGpu(params IGpuTensor<T>[] inputs)

Parameters

inputs IGpuTensor<T>[]

GPU tensor inputs.

Returns

IGpuTensor<T>

GPU tensor output after pooling.

Remarks

This method uses the native GPU AdaptiveAvgPool2D operation for efficient pooling to any target output size.

GetParameters()

Gets all trainable parameters. Returns empty for pooling layers.

public override Vector<T> GetParameters()

Returns

Vector<T>

An empty vector.

GlobalPool(int, int, int)

Creates a global average pooling layer that pools to 1x1.

public static AdaptiveAveragePoolingLayer<T> GlobalPool(int inputChannels, int inputHeight, int inputWidth)

Parameters

inputChannels int

The number of input channels.

inputHeight int

The expected input height.

inputWidth int

The expected input width.

Returns

AdaptiveAveragePoolingLayer<T>

An adaptive pooling layer that performs global average pooling.

ResetState()

Resets the internal state.

public override void ResetState()

UpdateParameters(T)

Updates the parameters. Pooling layers have no trainable parameters.

public override void UpdateParameters(T learningRate)

Parameters

learningRate T

The learning rate (unused).