Implementation:Tensorflow Tfjs Backend Operations Test
| Knowledge Sources | |
|---|---|
| Domains | Testing, Layers_API |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
This test suite validates the TensorFlow.js core backend operations used by the Layers API. It covers fundamental tensor manipulation operations including type casting, reshaping, slicing, concatenation, mathematical operations (dot product, sign, square, pow), dropout, bias addition, and activation functions at the backend level. The tests verify correct behavior for backend configuration, dtype handling, parameter counting, and tensor memory management.
Code Reference
Source Location: tfjs-layers/src/backend/tfjs_backend_test.ts (895 lines)
Repository: GitHub
Test Describe Blocks
TensorMath- Backend setting and gettingdtype- Data type resolution for Tensor and SymbolicTensorcountParams- Parameter counting across tensor dimensionscast- Type casting between float32, int32, and boolexpandDims- Dimension expansion operationsRepeat- Tensor repetition along axesFlatten- Tensor flattening operationsbatchFlatten- Batch-aware flatteningsliceAlongFirstAxis- Slicing along first dimensionsliceAlongLastAxis- Slicing along last dimensionsliceAlongAxis- General axis slicingconcatenate- Tensor concatenation along axesconcatAlongFirstAxis- Concatenation along first axistile- Tensor tiling operationsrandomNormal- Random normal tensor generationdot- Dot product operationssign- Element-wise sign functionOneHot- One-hot encodingGather- Tensor gathering by indicesSquare- Element-wise squaringPow- Element-wise power operationssoftsign- Softsign activation at backend leveldropout- Dropout implementation with rate and noise shapebiasAdd- Bias addition with data format supportelu- ELU activation at backend levelSigmoid- Sigmoid at backend levelhardSigmoid- Hard sigmoid at backend levelinTrainPhase- Training phase branching
I/O Contract
Inputs to tests:
- Tensors of various ranks (scalar through 6D) with known numeric values
- Backend identifiers (e.g.,
'cpu','webgl') - Data types (
float32,int32,bool) - Configuration parameters for dropout rates, noise shapes, data formats
Expected outputs/assertions:
- Correct output dtype and shape after operations
- Numerically accurate results matching hand-computed expected values
- Proper parameter counts for tensors of different ranks
- Memory safety verified through
expectNoLeakedTensors
Usage Example
describeMathCPU('countParams', () => {
it('Tensor2D', () => {
const x = zeros([3, 2]);
expect(K.countParams(x)).toEqual(6);
expect(K.countParams(new LayerVariable(x).read())).toEqual(6);
});
});
describeMathCPUAndGPU('cast', () => {
it('float32 to int32', () => {
const x = tensor2d([[-1.1, -1.6], [1.1, 2.2], [3.6, 4.7]], [3, 2], 'float32');
const y = K.cast(x, 'int32');
expect(y.dtype).toEqual('int32');
expect(Array.from(y.dataSync())).toEqual([-1, -1, 1, 2, 3, 4]);
});
});
Test Coverage Summary
| Category | Count | Details |
|---|---|---|
| Backend Configuration | 2 | TensorMath, dtype |
| Tensor Manipulation | 10 | expand, repeat, flatten, slice, concat, tile, gather |
| Math Operations | 6 | dot, sign, square, pow, oneHot, randomNormal |
| Activations | 5 | softsign, elu, sigmoid, hardSigmoid, dropout |
| Utility | 2 | biasAdd, inTrainPhase |
| Test Environment | CPU and GPU | Mix of describeMathCPU and describeMathCPUAndGPU
|