Implementation:Tensorflow Tfjs Core Layers Test
| Knowledge Sources | |
|---|---|
| Domains | Testing, Layers_API |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
This test suite validates the core layer types in TensorFlow.js Layers: Dropout, SpatialDropout1D, Dense, Flatten, Activation, RepeatVector, Reshape, Permute, and Masking. These are the fundamental building blocks of most neural networks. Each layer is tested at both symbolic (shape inference, configuration) and tensor (numerical correctness) levels. The suite covers dropout behavior during training vs inference, Dense layer with various activations and initializers, Flatten for reshaping multi-dimensional tensors, and utility layers like Reshape and Permute.
Code Reference
Source Location: tfjs-layers/src/layers/core_test.ts (871 lines)
Repository: GitHub
Test Describe Blocks
Dropout Layer: Symbolic- Shape inference with various dropout rates and input shapesDropout Layer- Tensor-level dropout behavior (training vs inference, noise shapes, scaling)SpatialDropout1D Layer- Spatial dropout for 1D feature mapsDense Layer: Symbolic- Shape inference, activations, regularizers, constraints, initializersDense Layer: Tensor- Numerical output verification with known kernels and biasesFlatten Layer: Symbolic- Shape inference for various input shapes and data formatsFlatten Layer: Tensor- Tensor reshaping correctnessActivation Layer: Tensor- Standalone activation layer with various activation functionsRepeatVector Layer: Symbolic- Shape inference for vector repetitionRepeatVector Layer: Tensor- Numerical repetition correctnessReshape Layer: Symbolic- Shape inference for reshape operationsReshape Layer: Tensor- Tensor reshaping correctnessPermute Layer: Symbolic- Shape inference for dimension permutationMasking Layer: Symbolic- Shape inference for masking layersPermute Layer: Tensor- Dimension permutation correctnessMasking Layer: Tensor- Masking behavior with custom mask values
I/O Contract
Inputs to tests:
- SymbolicTensors with known shapes for symbolic tests
- Concrete tensors (ones, zeros, random) for tensor tests
- Layer configurations: units, activation, dropout rate, noise shape, target shape, dims
- Training flag to control dropout behavior
Expected outputs/assertions:
- Dropout outputs equal inputs during inference (training=false)
- During training, dropout zeros out elements and scales remaining by
1/(1-rate) - Dense layer produces correct matrix multiplication results
- Flatten correctly reshapes to 2D preserving batch dimension
- Reshape and Permute produce correct output shapes and values
- Masking correctly masks timesteps matching the mask value
Usage Example
describe('Dropout Layer: Symbolic', () => {
for (const rate of [0, 0.5]) {
for (const symbolicInput of symbolicInputs) {
it(`dropoutRate=${rate}; input shape=${JSON.stringify(symbolicInput.shape)}`, () => {
const dropoutLayer = tfl.layers.dropout({rate});
const output = dropoutLayer.apply(symbolicInput) as tfl.SymbolicTensor;
expect(output.dtype).toEqual(symbolicInput.dtype);
expect(output.shape).toEqual(symbolicInput.shape);
});
}
}
});
Test Coverage Summary
| Category | Count | Details |
|---|---|---|
| Dropout | 15+ | Symbolic and tensor tests, noise shapes, training/inference |
| Dense | 20+ | Activations, initializers, regularizers, constraints, serialization |
| Flatten | 10+ | Various input shapes, data formats |
| Activation | 5+ | Standalone activation layer |
| Reshape/Permute | 10+ | Shape transformation layers |
| Masking | 5+ | Mask value and output masking |
| Test Environment | Mixed | CPU, GPU, WebGL2 |