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

From Leeroopedia


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

Overview

The exports_metrics.ts module provides the public API for metric functions in TensorFlow.js, accessible under tf.metrics.*. These functions evaluate model performance during training and evaluation. Each function takes ground-truth (yTrue) and predicted (yPred) tensors and returns a scalar or per-sample metric tensor.

The module delegates to internal metrics and losses modules for the actual computation. It covers classification metrics (accuracy, precision, recall), crossentropy losses used as metrics, regression metrics (MAE, MAPE, MSE, R2), and similarity metrics (cosine proximity).

Code Reference

Source Location

tfjs-layers/src/exports_metrics.ts (View on GitHub)

Key Signatures

// Classification metrics
export function binaryAccuracy(yTrue: Tensor, yPred: Tensor): Tensor;
export function categoricalAccuracy(yTrue: Tensor, yPred: Tensor): Tensor;
export function sparseCategoricalAccuracy(yTrue: Tensor, yPred: Tensor): Tensor;
export function precision(yTrue: Tensor, yPred: Tensor): Tensor;
export function recall(yTrue: Tensor, yPred: Tensor): Tensor;

// Crossentropy metrics
export function binaryCrossentropy(yTrue: Tensor, yPred: Tensor): Tensor;
export function categoricalCrossentropy(yTrue: Tensor, yPred: Tensor): Tensor;

// Regression metrics
export function meanAbsoluteError(yTrue: Tensor, yPred: Tensor): Tensor;
export function meanAbsolutePercentageError(yTrue: Tensor, yPred: Tensor): Tensor;
export function MAPE(yTrue: Tensor, yPred: Tensor): Tensor;
export function mape(yTrue: Tensor, yPred: Tensor): Tensor;
export function meanSquaredError(yTrue: Tensor, yPred: Tensor): Tensor;
export function MSE(yTrue: Tensor, yPred: Tensor): Tensor;
export function mse(yTrue: Tensor, yPred: Tensor): Tensor;
export function r2Score(yTrue: Tensor, yPred: Tensor): Tensor;

// Similarity metrics
export function cosineProximity(yTrue: Tensor, yPred: Tensor): Tensor;

Import

import * as tf from '@tensorflow/tfjs';
// Access via tf.metrics namespace
const accuracy = tf.metrics.binaryAccuracy(yTrue, yPred);

I/O Contract

Input

All metric functions accept two tensors:

  • yTrue: Ground-truth tensor. For classification metrics, this is one-hot encoded or integer labels. For regression metrics, it contains continuous values.
  • yPred: Prediction tensor. For classification, these are probabilities or logits. For regression, they are continuous predicted values.

Output

Returns a Tensor containing the computed metric. For per-sample metrics (accuracy, crossentropy), the result has one value per sample. For aggregate metrics (precision, recall, r2Score), the result is a scalar.

Metric Details

Function Description Aliases
binaryAccuracy Thresholds predictions at 0.5, compares with truth -
categoricalAccuracy Compares argmax of one-hot truth vs predictions -
sparseCategoricalAccuracy Compares integer labels with argmax of predictions -
precision True positives / (true positives + false positives) -
recall True positives / (true positives + false negatives) -
meanAbsoluteError mean(abs(yPred - yTrue)) -
meanAbsolutePercentageError Percentage-based MAE MAPE, mape
meanSquaredError mean((yPred - yTrue)^2) MSE, mse
r2Score Coefficient of determination -
cosineProximity Negative cosine similarity -

Usage Examples

import * as tf from '@tensorflow/tfjs';

// Binary accuracy
const yTrue = tf.tensor2d([[1, 1, 1, 1], [0, 0, 0, 0]]);
const yPred = tf.tensor2d([[1, 0, 1, 0], [0, 0, 0, 1]]);
const accuracy = tf.metrics.binaryAccuracy(yTrue, yPred);
accuracy.print(); // [0.5, 0.75]

// Mean squared error
const trueVals = tf.tensor2d([[0, 1], [3, 4]]);
const predVals = tf.tensor2d([[0, 1], [-3, -4]]);
const mse = tf.metrics.meanSquaredError(trueVals, predVals);
mse.print(); // [0, 32]

// Cosine proximity
const a = tf.tensor2d([[1, 0], [1, 0]]);
const b = tf.tensor2d([[1, 0], [0, 1]]);
const proximity = tf.metrics.cosineProximity(a, b);
proximity.print(); // [-1, 0]

Related Pages

Page Connections

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