Implementation:Tensorflow Tfjs Pooling Test
| Knowledge Sources | |
|---|---|
| Domains | Testing, Layers_API |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
This test suite validates all pooling layer implementations in TensorFlow.js Layers. It covers low-level pooling functions (pool2d, pool3d) and high-level Keras-style pooling layers for 1D, 2D, and 3D inputs, as well as global pooling layers. Tests enumerate combinations of pool modes (max, avg), data formats (channelsFirst, channelsLast), strides, padding modes (same, valid), and pool sizes. Both symbolic (shape inference) and tensor (numerical correctness) tests are included.
Code Reference
Source Location: tfjs-layers/src/layers/pooling_test.ts (927 lines)
Repository: GitHub
Test Describe Blocks
pool2d- Low-level 2D pooling function (4x4, 5x5 inputs; max/avg; stride 1/2; same/valid)pool3d- Low-level 3D pooling functionPooling Layers 1D: Symbolic- MaxPooling1D and AveragePooling1D shape inferencePooling Layers 1D: Tensor- 1D pooling numerical correctnessPooling Layers 2D: Symbolic- MaxPooling2D and AveragePooling2D shape inferencePooling Layers 2D: Tensor- 2D pooling numerical correctnessPooling Layers 3D: Symbolic- MaxPooling3D and AveragePooling3D shape inferencePooling Layers 3D: Tensor- 3D pooling numerical correctness1D Global pooling Layers: Symbolic- GlobalMaxPooling1D and GlobalAveragePooling1D shape inference1D Global Pooling Layers: Tensor- 1D global pooling numerical correctness2D Global pooling Layers: Symbolic- GlobalMaxPooling2D and GlobalAveragePooling2D shape inference2D Global Pooling Layers: Tensor- 2D global pooling numerical correctness
I/O Contract
Inputs to tests:
- 3D tensors for 1D pooling:
[batch, steps, features] - 4D tensors for 2D pooling:
[batch, height, width, channels]or NCHW format - 5D tensors for 3D pooling:
[batch, depth, height, width, channels] - Pool configuration: poolSize, strides, padding, dataFormat, poolMode (max/avg)
Expected outputs/assertions:
- Max pooling selects maximum value in each pool window
- Average pooling computes mean of values in each pool window
- Output spatial dimensions follow convolution output length formula
- Padding mode 'same' preserves spatial dimensions, 'valid' reduces them
- Global pooling reduces spatial dimensions to 1
- Data format correctly handles channelsFirst vs channelsLast
Usage Example
describeMathCPUAndGPU('pool2d', () => {
const x4by4Data = [[[
[10, 30, 50, 70], [20, 40, 60, 80],
[-10, -30, -50, -70], [-20, -40, -60, -80]
]]];
it('4x4, stride=2, same, channelsFirst, max', () => {
const x = tensor4d(x4by4Data, [1, 1, 4, 4]);
const yExpected = tensor4d([[[[40, 80], [-10, -50]]]], [1, 1, 2, 2]);
const y = pool2d(x, [2, 2], [2, 2], 'same', 'channelsFirst', 'max');
expectTensorsClose(y, yExpected);
});
});
Test Coverage Summary
| Category | Count | Details |
|---|---|---|
| pool2d function | 20+ | Max/avg, stride 1/2, same/valid, data formats |
| pool3d function | 10+ | 3D pooling across configurations |
| 1D Pooling Layers | 10+ | Symbolic and tensor tests |
| 2D Pooling Layers | 15+ | Symbolic and tensor tests |
| 3D Pooling Layers | 10+ | Symbolic and tensor tests |
| Global Pooling | 10+ | 1D and 2D global max/avg pooling |
| Test Environment | CPU and GPU | describeMathCPUAndGPU
|