We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Metrics & Evaluation step 1 of 7
Accuracy – Multiclass
Implement multiclass classification accuracy.
What is accuracy?
Accuracy is the fraction of examples whose predicted class matches the
true class label. For a multiclass model that outputs a score vector of
shape (C,) per example, the predicted class is the index with the
highest score — the argmax.
accuracy = (number of correct predictions) / (total examples)
Step by step
-
Take
argmaxover the class dimension (dim=-1in PyTorch /axis=-1in JAX) to get a predicted class index for each example. -
Compare element-wise to
labels— this yields a boolean tensor. - Cast to float and take the mean. Return as a Python scalar.
When to use accuracy vs F1
Accuracy is the right default for balanced class distributions: if
each class appears roughly equally often, a random classifier scores
1/C and gains from being right. When classes are imbalanced —
e.g. 99 % negatives — accuracy is misleading (a model that always
predicts the majority class is 99 % accurate but useless). In that case
prefer macro-F1 or per-class precision/recall.
Reference: sklearn.metrics.accuracy_score computes the same
quantity.
Inputs
-
predictions: shape(N, C)— class scores (logits or probabilities).Nexamples,Cclasses. -
labels: shape(N,)— integer class indices, delivered as floats by the test harness.
Output
Scalar float — fraction of examples whose argmax equals the label.
Stuck?
PyTorch reference solution
Sign in to attempt this problem and reveal the reference solution.