metrax.SparseCategoricalAccuracy#

class metrax.SparseCategoricalAccuracy(total: Array, count: Array)#

Bases: Average

Computes accuracy for sparse categorical classification models.

This metric calculates the frequency with which the predicted class matches the integer target class by comparing argmax(predictions, axis=-1) and labels. If no sample_weights are provided, weights default to 1 for each element.

__init__(total: Array, count: Array) None#

Methods

__init__(total, count)

compute()

Computes final metrics from intermediate values.

compute_value()

Wraps compute() and returns a values.Value.

empty()

Returns an empty instance (i.e. .merge(Metric.empty()) is a no-op).

from_fun(fun)

Calls cls.from_model_output with the return value from fun.

from_model_output(predictions, labels[, ...])

Updates the metric state with new predictions and labels.

from_output(name)

Calls cls.from_model_output with model output named name.

merge(other)

Returns Metric that is the accumulation of self and other.

reduce()

Reduces the metric along it first axis by calling _reduce_merge().

replace(**updates)

Returns a new object replacing the specified fields with new values.

Attributes

total

count

classmethod from_model_output(predictions: Array, labels: Array, sample_weights: Array | None = None) SparseCategoricalAccuracy#

Updates the metric state with new predictions and labels.

Parameters:
  • predictions – JAX array of predicted values (typically logits or probabilities). Expected to have one additional class dimension at the end compared to labels (e.g., predictions: (batch_size, num_classes) and labels: (batch_size,)).

  • labels – JAX array of true integer values. Expected to have a shape compatible with predictions for element-wise comparison (e.g., (batch_size,), (batch_size, 1), (batch_size, seq_len)).

  • sample_weights – Optional JAX array of weights. If provided, it must be broadcastable to the shape of labels (which should also be compatible with predictions’ shape).

Returns:

An updated instance of SparseCategoricalAccuracy metric.

__init__(total: Array, count: Array) None#
replace(**updates)#

Returns a new object replacing the specified fields with new values.