Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tensorflow Tfjs Activations Test

From Leeroopedia


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 unchanged
  • elu activation - Exponential Linear Unit
  • selu activation - Scaled Exponential Linear Unit
  • relu activation - Rectified Linear Unit
  • relu6 activation - ReLU capped at 6
  • sigmoid activation - Logistic sigmoid function
  • hardSigmoid activation - Piecewise linear approximation of sigmoid
  • softplus activation - Smooth approximation of ReLU
  • softsign activation - Softsign activation
  • tanh activation - Hyperbolic tangent
  • softmax activation - Normalized exponential
  • logsoftmax activation - Log of softmax
  • swish activation - Self-gated activation
  • mish activation - Mish self-regularized activation
  • gelu activation - Gaussian Error Linear Unit
  • gelu_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

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment