Class MaxPool3DLayer<T>
- Namespace
- AiDotNet.NeuralNetworks.Layers
- Assembly
- AiDotNet.dll
Represents a 3D max pooling layer for downsampling volumetric data.
public class MaxPool3DLayer<T> : LayerBase<T>, ILayer<T>, IJitCompilable<T>, IDiagnosticsProvider, IWeightLoadable<T>, IDisposable
Type Parameters
TThe numeric type used for calculations, typically float or double.
- Inheritance
-
LayerBase<T>MaxPool3DLayer<T>
- Implements
-
ILayer<T>
- Inherited Members
Remarks
A 3D max pooling layer reduces the spatial dimensions (depth, height, width) of volumetric data while preserving the most prominent features. This helps reduce computational cost and provides translation invariance.
For Beginners: Max pooling works like summarizing a 3D region by keeping only the largest value.
Think of it like this:
- You have a 3D grid of numbers
- You divide it into small cubes (e.g., 2x2x2)
- For each cube, you keep only the largest number
- This makes your data smaller while keeping the important features
This is useful because:
- It reduces the amount of computation needed
- It helps the network focus on the most important features
- It makes the network more robust to small position changes
Constructors
MaxPool3DLayer(int[], int, int)
Initializes a new instance of the MaxPool3DLayer<T> class.
public MaxPool3DLayer(int[] inputShape, int poolSize, int stride = 0)
Parameters
inputShapeint[]The shape of the input tensor [channels, depth, height, width].
poolSizeintThe size of the pooling window (applied to all three dimensions).
strideintThe stride for moving the pooling window. Defaults to poolSize if 0.
Remarks
For Beginners: This creates a max pooling layer that shrinks 3D data.
If stride equals pool size, the output will have dimensions reduced by the pool size factor. For example, with pool size 2 and stride 2, a 32x32x32 input becomes 16x16x16.
Exceptions
- ArgumentException
Thrown when inputShape has invalid length.
- ArgumentOutOfRangeException
Thrown when poolSize or stride is non-positive.
Properties
PoolSize
Gets the size of the pooling window (same for depth, height, width).
public int PoolSize { get; }
Property Value
Remarks
The pool size determines the size of the region from which to extract the maximum value. Common values are 2 (halves each spatial dimension) or 3.
Stride
Gets the stride (step size) for moving the pooling window.
public int Stride { get; }
Property Value
Remarks
Stride controls how much the pooling window moves between positions. A stride equal to pool size produces non-overlapping regions. A stride of 1 with pool size > 1 produces overlapping regions.
SupportsGpuExecution
Gets a value indicating whether this layer supports GPU execution.
protected override bool SupportsGpuExecution { get; }
Property Value
Remarks
MaxPool3D supports GPU execution via CUDA, OpenCL, and HIP backends.
SupportsJitCompilation
Gets a value indicating whether this layer supports JIT compilation.
public override bool SupportsJitCompilation { get; }
Property Value
- bool
trueif the input shape is configured.
SupportsTraining
Gets a value indicating whether this layer supports training (backpropagation).
public override bool SupportsTraining { get; }
Property Value
- bool
Always
trueas MaxPool3D supports gradient flow through max indices.
Methods
Backward(Tensor<T>)
Performs the backward pass to route gradients through max pooling.
public override Tensor<T> Backward(Tensor<T> outputGradient)
Parameters
outputGradientTensor<T>The gradient of the loss with respect to this layer's output.
Returns
- Tensor<T>
The gradient of the loss with respect to this layer's input.
Remarks
During backpropagation, gradients are routed only to the positions that had the maximum values in the forward pass. All other positions receive zero gradient.
Exceptions
- InvalidOperationException
Thrown when Forward has not been called.
BackwardGpu(IGpuTensor<T>)
Performs GPU-resident backward pass of 3D max pooling.
public override IGpuTensor<T> BackwardGpu(IGpuTensor<T> outputGradient)
Parameters
outputGradientIGpuTensor<T>The gradient of the output on GPU.
Returns
- IGpuTensor<T>
The gradient with respect to input as a GPU-resident tensor.
Deserialize(BinaryReader)
Deserializes the layer from a binary stream.
public override void Deserialize(BinaryReader reader)
Parameters
readerBinaryReaderThe binary reader to deserialize from.
ExportComputationGraph(List<ComputationNode<T>>)
Exports the layer as a computation graph for JIT compilation.
public override ComputationNode<T> ExportComputationGraph(List<ComputationNode<T>> inputNodes)
Parameters
inputNodesList<ComputationNode<T>>List to populate with input nodes.
Returns
- ComputationNode<T>
The output computation node.
Exceptions
- ArgumentNullException
Thrown when inputNodes is null.
- InvalidOperationException
Thrown when layer is not properly initialized.
Forward(Tensor<T>)
Performs the forward pass of 3D max pooling.
public override Tensor<T> Forward(Tensor<T> input)
Parameters
inputTensor<T>The input tensor with shape [batch, channels, depth, height, width] or [channels, depth, height, width].
Returns
- Tensor<T>
The pooled output tensor with reduced spatial dimensions.
Remarks
This method uses the vectorized Engine.MaxPool3DWithIndices operation for CPU/GPU acceleration. The max indices are cached for use in the backward pass.
Exceptions
- ArgumentException
Thrown when input tensor has invalid rank.
ForwardGpu(params IGpuTensor<T>[])
Performs GPU-resident forward pass of 3D max pooling, keeping all data on GPU.
public override IGpuTensor<T> ForwardGpu(params IGpuTensor<T>[] inputs)
Parameters
inputsIGpuTensor<T>[]The input tensors on GPU (uses first input).
Returns
- IGpuTensor<T>
The pooled output as a GPU-resident tensor.
GetActivationTypes()
Gets the activation function types used by this layer.
public override IEnumerable<ActivationFunction> GetActivationTypes()
Returns
- IEnumerable<ActivationFunction>
An empty enumerable as max pooling has no activation function.
GetParameters()
Gets all trainable parameters. Max pooling has none.
public override Vector<T> GetParameters()
Returns
- Vector<T>
An empty vector.
ResetState()
Resets the cached state from forward/backward passes.
public override void ResetState()
Remarks
Call this method to free memory after inference is complete or when switching between different inputs.
Serialize(BinaryWriter)
Serializes the layer to a binary stream.
public override void Serialize(BinaryWriter writer)
Parameters
writerBinaryWriterThe binary writer to serialize to.
UpdateParameters(T)
Updates parameters. Max pooling has no trainable parameters.
public override void UpdateParameters(T learningRate)
Parameters
learningRateTThe learning rate (unused).