Implementation:Tensorflow Tfjs Activations Test
| Knowledge Sources | |
|---|---|
| Domains | Testing, Layers_API |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
This test suite validates the correctness of all activation functions implemented in the TensorFlow.js Layers API. Each activation function is tested across multiple tensor dimensionalities (1D, 2D, 3D) to ensure consistent behavior regardless of input shape. The suite also includes memory leak checks to verify that activations do not leak tensors during execution. The activation functions tested include linear, elu, selu, relu, relu6, sigmoid, hardSigmoid, softplus, softsign, tanh, softmax, logSoftmax, swish, mish, gelu, and geluNew.
Code Reference
Source Location: tfjs-layers/src/activations_test.ts (463 lines)
Repository: GitHub
Test Describe Blocks
linear activation- Identity activation returning input unchangedelu activation- Exponential Linear Unitselu activation- Scaled Exponential Linear Unitrelu activation- Rectified Linear Unitrelu6 activation- ReLU capped at 6sigmoid activation- Logistic sigmoid functionhardSigmoid activation- Piecewise linear approximation of sigmoidsoftplus activation- Smooth approximation of ReLUsoftsign activation- Softsign activationtanh activation- Hyperbolic tangentsoftmax activation- Normalized exponentiallogsoftmax activation- Log of softmaxswish activation- Self-gated activationmish activation- Mish self-regularized activationgelu activation- Gaussian Error Linear Unitgelu_new activation- Approximate GELU variant
I/O Contract
Inputs to tests:
- Float32Array or number array values including negative, zero, and positive numbers (e.g.,
[-1, 2, 0, 4, -5, 6]) - Tensors of varying dimensions: 1D (
tensor1d), 2D (tensor2d), 3D (tensor3d)
Expected outputs/assertions:
- Each activation produces mathematically correct output values matching the activation function formula
- Output tensor shape matches the input tensor shape
- Memory leak tests verify zero (or minimal expected) leaked tensors via
expectNoLeakedTensors
Usage Example
describeMathCPUAndGPU('elu activation', () => {
const initVals = [-1, 2, 0, 4, -5, 6];
const expectedVals = initVals.map(x => x < 0 ? Math.exp(x) - 1 : x);
const elu = new Elu().apply;
it('1D', () => {
const initX = tensor1d(initVals);
expectTensorsClose(elu(initX), tensor1d(expectedVals));
});
it('Does not leak', () => {
const initX = tensor1d(initVals);
expectNoLeakedTensors(() => elu(initX), 1);
});
});
Test Coverage Summary
| Category | Count | Details |
|---|---|---|
| Activation Functions | 16 | linear, elu, selu, relu, relu6, sigmoid, hardSigmoid, softplus, softsign, tanh, softmax, logSoftmax, swish, mish, gelu, geluNew |
| Dimensionality Tests | 3 per activation | 1D, 2D, 3D tensor inputs |
| Memory Leak Tests | 1 per activation | Validates no tensor leaks |
| Test Environment | CPU and GPU | All tests run via describeMathCPUAndGPU
|