Skip to content
fn

correct_count

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

Count of correct top-1 predictions as an int64 0-d Tensor.

Equivalent to (logits.argmax(dim=dim) == target).long().sum() — written as a single function so the four-op chain in user code collapses to one Python wrap. The complementary primitive to accuracy: where accuracy divides by total count eagerly, correct_count keeps the count integer so the user can accumulate across batches and divide once at the end of the epoch.

Parameters

logitsTensor
Predicted class scores of shape (*, C).
targetTensor
Ground-truth class indices of shape (*,). Integer dtype expected.
dimint= -1
Axis along which to take the argmax.

Returns

Tensor

0-d int64 scalar — the number of positions where argmax(logits) == target.

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, 1])
>>> F.correct_count(logits, target).item()
1

Used by 1