Implementation:Tensorflow Tfjs Convolutional Test
| Knowledge Sources | |
|---|---|
| Domains | Testing, Layers_API |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
This large test suite (1758 lines) validates all convolutional layer implementations in the TensorFlow.js Layers API. It covers 1D, 2D, and 3D convolutions including standard convolutions, transposed (deconvolution) layers, separable convolutions, cropping, and upsampling. Tests are organized by both low-level convolution functions (conv1dWithBias, conv2d, conv2dWithBiasActivation, conv3d, conv3dWithBias) and high-level Keras-style layer APIs. Each layer type is tested at both symbolic (shape inference) and tensor (numerical correctness) levels across data formats, padding modes, strides, and dilations.
Code Reference
Source Location: tfjs-layers/src/layers/convolutional_test.ts (1758 lines)
Repository: GitHub
Test Describe Blocks
conv1dWithBias- Low-level 1D convolution with bias across output channels, data formats, padding modesconv1d- Low-level 1D convolution with strides and dilationsconv2d- Low-level 2D convolution across formats and paddingconv2dWithBiasActivation- 2D convolution with fused bias and activationConv2D Layers: Symbolic- 2D conv layer shape inferenceConv2D Layer: Tensor- 2D conv layer numerical correctnessconv3d- Low-level 3D convolutionConv3D Layers: Symbolic- 3D conv layer shape inferenceConv3D Layer: Tensor- 3D conv layer numerical correctnessConv2DTranspose: Symbolic- Transposed 2D conv shape inferenceConv2DTranspose: Tensor- Transposed 2D conv numerical correctnessConv1D Layers: Symbolic- 1D conv layer shape inferenceConv1D Layer: Tensor- 1D conv layer numerical correctnessSeparableConv2D Layers: Symbolic- Depthwise separable 2D conv shape inferenceSeparableConv2D Layer: Tensor- Depthwise separable 2D conv numerical correctnessCropping2D Layer- Spatial cropping of 2D feature mapsUpSampling2D Layer: Symbolic- Upsampling shape inference (nearest, bilinear)UpSampling2D Layer- Upsampling numerical correctnessConv3DTranspose: Symbolic- Transposed 3D conv shape inferenceConv3DTranspose: Tensor- Transposed 3D conv numerical correctness
I/O Contract
Inputs to tests:
- 3D tensors for Conv1D:
[batch, width, channels] - 4D tensors for Conv2D:
[batch, height, width, channels]or[batch, channels, height, width] - 5D tensors for Conv3D:
[batch, depth, height, width, channels] - Kernel tensors, bias tensors, and layer configurations
- Parameters: outChannels, stride, dilations, padding (valid/same), dataFormat (channelsFirst/channelsLast)
Expected outputs/assertions:
- Output tensor shapes match expected spatial dimensions after convolution
- Numerical output values match hand-computed expected values (e.g., convolution of
[10, 20, 40, 80]with kernel[1, -1]plus bias) - Symbolic shape inference produces correct output shapes
- Transposed convolution reverses spatial dimension reduction
- Separable convolution correctly decomposes into depthwise and pointwise stages
Usage Example
describeMathCPUAndGPU('conv1dWithBias', () => {
it('outChannels=1, stride=1, valid, channelsLast', () => {
let x: Tensor = tensor3d([10, 20, 40, 80], [1, 4, 1]);
const kernel = tfc.transpose(
tensor3d([1, -1], [1, 1, 2]), [2, 0, 1]);
const bias = tensor1d([2.2]);
const y = conv1dWithBias(x, kernel, bias, 1, 'valid', 'channelsLast');
expectTensorsClose(y, tensor3d([-7.8, -17.8, -37.8], [1, 3, 1]));
});
});
Test Coverage Summary
| Category | Count | Details |
|---|---|---|
| Conv1D | 30+ | Low-level functions and layer API (symbolic + tensor) |
| Conv2D | 40+ | Standard, transposed, separable, with bias/activation |
| Conv3D | 30+ | Standard and transposed (symbolic + tensor) |
| Cropping2D | 5+ | Spatial cropping with various configurations |
| UpSampling2D | 10+ | Nearest and bilinear interpolation |
| Test Environment | Mixed | CPU, GPU, WebGL2 |