metrax.CategoricalAccuracy#
- class metrax.CategoricalAccuracy(total: Array, count: Array)#
Bases:
AverageComputes accuracy for one-hot categorical classification models.
This metric calculates the frequency with which the predicted class matches the true class by comparing argmax(predictions, axis=-1) and argmax(labels, axis=-1). If no sample_weights are provided, weights default to 1 for each element.
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
totalcount- classmethod from_model_output(predictions: Array, labels: Array, sample_weights: Array | None = None) CategoricalAccuracy#
Updates the metric state with new predictions and labels.
- Parameters:
predictions – JAX array of predicted values (typically logits or probabilities). Expected to have a shape compatible with labels (e.g., (batch_size, num_classes), (batch_size, seq_len, num_features)).
labels – JAX array of true one-hot values. Expected to have the same shape as predictions.
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 CategoricalAccuracy metric.
- replace(**updates)#
Returns a new object replacing the specified fields with new values.