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 Losses Test

From Leeroopedia


Knowledge Sources
Domains Testing, Layers_API
Last Updated 2026-02-10 06:00 GMT

Overview

This test suite validates all loss function implementations in the TensorFlow.js Layers API. Loss functions measure the discrepancy between predicted and true values during model training. The tested functions include regression losses (MSE, MAE, MAPE, MSLE), hinge losses (squared hinge, hinge, categorical hinge), cross-entropy losses (categorical, sparse categorical, binary, with and without logits), and other losses (logcosh, Kullback-Leibler divergence, Poisson, cosine proximity). The suite also tests the loss function lookup utility and L2 normalization.

Code Reference

Source Location: tfjs-layers/src/losses_test.ts (443 lines)

Repository: GitHub

Test Describe Blocks

  • meanSquaredError - MSE for 1D and 2D inputs
  • meanAbsoluteError - MAE for 1D and 2D inputs
  • meanAbsolutePercentageError - MAPE for 1D and 2D inputs
  • meanSquaredLogarithmicError - MSLE for 2D inputs
  • squaredHinge - Squared hinge loss
  • hinge - Hinge loss
  • categoricalHinge - Categorical hinge for multi-class
  • logcosh - Log-cosh loss (smooth L1 approximation)
  • categoricalCrossentropy - Multi-class cross-entropy (two blocks: function and loss wrapper)
  • sparseCategoricalCrossentropy - Sparse categorical cross-entropy (two blocks)
  • sigmoidCrossEntropyWithLogits - Binary cross-entropy from logits
  • binaryCrossentropy - Binary cross-entropy
  • kullbackLeiblerDivergence - KL divergence
  • poisson - Poisson loss
  • cosineProximity - Cosine similarity loss
  • losses get - Loss function lookup by string name
  • l2Normalize - L2 normalization utility

I/O Contract

Inputs to tests:

  • True label tensors (yTrue) and predicted tensors (yPred) of matching shapes
  • 1D tensors for per-sample loss, 2D tensors for batched loss
  • Various numeric ranges including zeros, negatives, and boundary values

Expected outputs/assertions:

  • Loss values match hand-computed formulas (e.g., MSE: (1^2 + 2^2 + 3^2) / 3)
  • 2D inputs produce per-sample loss vectors
  • Cross-entropy losses handle edge cases (predictions near 0 or 1)
  • Loss lookup returns correct function for string identifiers
  • L2 normalization produces unit-length vectors along specified axis

Usage Example

describeMathCPUAndGPU('meanSquaredError', () => {
  it('1D', () => {
    const yTrue = tfc.zeros([3]);
    const yPred = tensor1d([1, 2, 3]);
    const expectedVal = scalar((1 * 1 + 2 * 2 + 3 * 3) / 3);
    const result = losses.meanSquaredError(yTrue, yPred);
    expectTensorsClose(result, expectedVal);
  });
  it('2D', () => {
    const yTrue = tfc.zeros([2, 2]);
    const yPred = tensor2d([[1, 2], [3, 4]], [2, 2]);
    const expectedVal = tensor1d([(1 + 4) / 2, (9 + 16) / 2]);
    const result = losses.meanSquaredError(yTrue, yPred);
    expectTensorsClose(result, expectedVal);
  });
});

Test Coverage Summary

Category Count Details
Regression Losses 8 MSE, MAE, MAPE, MSLE (1D and 2D)
Hinge Losses 3 Hinge, squared hinge, categorical hinge
Cross-Entropy 8 Categorical, sparse categorical, binary, sigmoid with logits
Other Losses 4 Logcosh, KL divergence, Poisson, cosine proximity
Utilities 2 losses.get lookup, l2Normalize
Test Environment Mixed CPU, GPU, WebGL2

Related Pages

Page Connections

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