Implementation:Tensorflow Tfjs Merge Test
| Knowledge Sources | |
|---|---|
| Domains | Testing, Layers_API |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
This test suite validates the merge layers in TensorFlow.js Layers, which combine multiple input tensors into a single output. The tested layers include Add, Average, Multiply, Maximum, Minimum, Concatenate, and Dot. Tests cover symbolic shape inference (verifying output shapes from input shapes), functional API usage (calling layers directly with tensors), tensor-level numerical correctness, error handling for invalid inputs (single input, mismatched batch sizes), and serialization/deserialization. The Dot layer tests include various axes configurations and normalization options.
Code Reference
Source Location: tfjs-layers/src/layers/merge_test.ts (1011 lines)
Repository: GitHub
Test Describe Blocks
Merge Layers Except Concatenate: Symbolic- Shape inference for Add, Average, Multiply, Maximum, MinimumAdd-Functional- Functional API: symbolic and tensor usage, model predictionMultiply-Functional- Element-wise multiplication functional APIAverage-Functional- Element-wise averaging functional APIMaximum-Functional- Element-wise maximum functional APIMinimum-Functional- Element-wise minimum functional APIConcatenate-Functional- Concatenation along axes functional APIConcatenate Layer: Symbolic- Concatenation shape inference with axis configurationAdd Layer: Tensor- Numerical addition correctness (2D, 3D, broadcasting)Multiply Layer: Tensor- Numerical multiplication correctnessAverage Layer: Tensor- Numerical averaging correctnessMaximum Layer: Tensor- Numerical maximum correctnessMinimum Layer: Tensor- Numerical minimum correctnessConcatenate Layer: Tensor- Numerical concatenation correctness, masking supportDeserialize Merge Layers- Serialization round-trips for all merge layer typesDot-Layer: Symbolic- Dot product shape inference with various axes and normalizationDot-Layer: Tensor- Dot product numerical correctness
I/O Contract
Inputs to tests:
- Multiple SymbolicTensors or concrete tensors (2-4 inputs per merge operation)
- Layer configurations: axis for concatenation, axes and normalize for Dot
- Various input shapes (2D, 3D) and number of inputs (2, 4)
Expected outputs/assertions:
- Output shape matches input shape for element-wise operations (Add, Multiply, etc.)
- Concatenate output shape grows along the specified axis
- Numerical results match expected element-wise operations
- Single input throws error ("at least 2 inputs")
- Mismatched batch sizes throw error
- Serialization preserves layer configuration
Usage Example
describeMathCPUAndGPU('Add-Functional', () => {
it('Calling with tensors returns tensor', () => {
const input1 = tensor2d([1, 2, 3, 4], [2, 2]);
const input2 = tensor2d([10, 20, 30, 40], [2, 2]);
const output = tfl.layers.add().apply([input1, input2]) as Tensor;
expectTensorsClose(output, tensor2d([11, 22, 33, 44], [2, 2]));
});
});
Test Coverage Summary
| Category | Count | Details |
|---|---|---|
| Element-wise Merge | 25+ | Add, Multiply, Average, Maximum, Minimum |
| Concatenate | 15+ | Symbolic, tensor, masking, axis configuration |
| Dot | 15+ | Various axes, normalization, symbolic and tensor |
| Functional API | 10+ | Layer creation, apply with symbolic/concrete tensors |
| Serialization | 10+ | Round-trip for all merge layer types |
| Error Handling | 3+ | Single input, batch size mismatch |
| Test Environment | Mixed | CPU, GPU, WebGL2 |