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

From Leeroopedia


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

Overview

This test suite validates all metric function implementations in the TensorFlow.js Layers API. Metrics are used to evaluate model performance during training and evaluation. The tested metrics include accuracy metrics (binary accuracy, sparse categorical accuracy, categorical accuracy), cross-entropy metrics (binary crossentropy, categorical crossentropy), precision, recall, R2 score, and the metric lookup utility. Tests cover exact and thresholded comparisons, 1D and 2D inputs, and various dtype combinations.

Code Reference

Source Location: tfjs-layers/src/metrics_test.ts (349 lines)

Repository: GitHub

Test Describe Blocks

  • binaryAccuracy - Binary accuracy with exact and thresholded predictions (1D and 2D)
  • sparseCategoricalAccuracy - Sparse categorical accuracy with float32 and int32 yTrue, 1D and 2D labels
  • binaryCrossentropy - Binary cross-entropy metric with reference Python values
  • categoricalAccuracy - Categorical accuracy for multi-class classification
  • categoricalCrossentropy metric - Categorical cross-entropy as a metric
  • precision metric - Precision computation with true/false positives
  • recall metric - Recall computation with true/false negatives
  • r2Score - R-squared (coefficient of determination)
  • metrics.get - Metric function lookup by string name
  • getLossOrMetricName - Extracting metric name from function reference

I/O Contract

Inputs to tests:

  • True label tensors (1D or 2D) and predicted probability/logit tensors
  • Binary labels: [1, 1, 1, 1, 0, 0, 0, 0]
  • Predicted probabilities: [0.2, 0.4, 0.6, 0.8, 0.2, 0.3, 0.4, 0.7]
  • Multi-class one-hot labels and prediction distributions
  • Sparse integer labels with probability distributions

Expected outputs/assertions:

  • Binary accuracy with threshold 0.5: correctly counts predictions above/below threshold
  • Sparse categorical accuracy: argmax of predictions matches integer label
  • Precision: TP / (TP + FP)
  • Recall: TP / (TP + FN)
  • R2 score: 1 - SS_res / SS_tot
  • Metric lookup returns correct function for string identifiers

Usage Example

describeMathCPUAndGPU('binaryAccuracy', () => {
  it('1D exact', () => {
    const x = tensor1d([1, 1, 1, 1, 0, 0, 0, 0]);
    const y = tensor1d([1, 0, 1, 0, 0, 1, 0, 1]);
    const accuracy = tfl.metrics.binaryAccuracy(x, y);
    expectTensorsClose(accuracy, scalar(0.5));
  });
  it('2D thresholded', () => {
    const x = tensor1d([1, 1, 1, 1, 0, 0, 0, 0]);
    const y = tensor1d([0.2, 0.4, 0.6, 0.8, 0.2, 0.3, 0.4, 0.7]);
    const accuracy = tfl.metrics.binaryAccuracy(x, y);
    expectTensorsClose(accuracy, scalar(5 / 8));
  });
});

Test Coverage Summary

Category Count Details
Accuracy Metrics 10+ Binary, sparse categorical, categorical (exact and thresholded)
Cross-Entropy Metrics 4+ Binary and categorical crossentropy
Precision/Recall 6+ Various TP/FP/FN configurations
R2 Score 2+ Coefficient of determination
Utilities 3+ metrics.get, getLossOrMetricName
Test Environment Mixed CPU, GPU, WebGL2

Related Pages

Page Connections

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