Skip to content
fn

accuracy

→Tensor
accuracy(logits: Tensor, target: Tensor, dim: int = -1)

Compute the mean classification accuracy in [0, 1].

Equivalent to (logits.argmax(dim=dim) == target).float().mean() — written as a fused expression so the result is a single Tensor (one Python wrap) rather than a four-op chain. Returns a 0-d Tensor; accuracy(...).item() materialises it as a Python float.

Parameters

logitsTensor
Predicted class scores of shape (*, C). The class dimension defaults to the last axis; override with dim for layouts that keep the class axis elsewhere (e.g. dense-prediction outputs).
targetTensor
Ground-truth class indices of shape (*,) — i.e. logits.shape with the class axis removed. Integer dtype expected.
dimint= -1
Axis along which to take the argmax. Negative indexing is resolved against logits.ndim.

Returns

Tensor

0-d scalar in [0, 1]; the float dtype matches the upcast used by .mean() (typically float32).

Notes

For accumulating correct-count across mini-batches without dividing by batch size every step, use correct_count instead — it skips the .float() cast + .mean() reduction and returns an int64 0-d Tensor that can be added cheaply across batches.

Examples

>>> import lucid
>>> import lucid.nn.functional as F
>>> logits = lucid.tensor([[2.0, 1.0, 0.0], [0.0, 1.0, 2.0]])
>>> target = lucid.tensor([0, 2])
>>> F.accuracy(logits, target).item()
1.0

Used by 1