Repository navigation
feat(metrics): add Precision metric and factory functions (#1227) - #1460
Merged
michalharakal merged 1 commit intoOct 30, 2026
Merged
michalharakal merged 1 commit into
michalharakal merged 1 commit into
Conversation
michalharakal
approved these changes
Oct 30, 2026
Contributor
|
Thank you @EdinEd |
michalharakal
added a commit
to EdinEd/SKaiNET
that referenced
this pull request
Oct 30, 2026
The branch was cut before the Precision metric (SKaiNET-developers#1460) and F1Score (SKaiNET-developers#1462) merged into develop, so the checked-in JVM API dump was missing the Precision/PrecisionKt entries, failing jvmApiCheck on the merge commit. Merge develop and regenerate the dump via :skainet-lang:skainet-lang-core:apiDump. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This was referenced Oct 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements Lane 2 (Kotlin core) sub-issue #1227 for the Precision, Recall, and F1Score metric feature.
This PR introduces the public
Precisionmetric class and its companion factory functions (precision()andbinaryPrecision()) inskainet-lang-core. The implementation delegates batch traversal and accumulator state entirely to the internalConfusionMatrixAccumulatorandreduce()engine from #1226, contributing only the precision formulatp / (tp + fp).Part of DARC tracking issue #1222.
Closes #1227.
Depends on #1226.
Note
Unit tests only; ground-truth wiring tracked in SKEEP-005 ([SKEEP-005]: Ground-truth validation for stateful metrics #1223).
Changes
Precisionclass (sk.ainet.lang.nn.metrics.Precision):Metricwithoverride val name: String = "precision".ConfusionMatrixAccumulator(dim, threshold).reduce(acc.counts(), averaging) { tp, fp, _ -> if (tp + fp == 0L) 0.0 else tp.toDouble() / (tp + fp) }.averaging,dim,threshold, and zero-division behavior.Factory functions (
sk.ainet.lang.nn.metrics.PrecisionKt):public fun precision(averaging: Averaging = Averaging.MACRO, dim: Int = -1): Metric: Factory for macro/micro multi-class precision.public fun binaryPrecision(threshold: Float = 0.5f): Metric: Explicitly wired withAveraging.BINARYto report positive class1precision ($TP_1 / (TP_1 + FP_1)$) per [Lane 1 · numerics] Precision/Recall/F1 — averaging-mode and zero-division conventions #1224.API Dump (
skainet-lang-core.api):./gradlew :skainet-lang:skainet-lang-core:apiDump.PrecisionandPrecisionKtwith zero leakage of internal accumulator types.Lane 1 Contract Alignment (#1224)
0.0(neverNaN) on a fresh accumulator, empty batch, or whenevertargets ∪ predictions), ensuring unobserved classes in mini-batches do not artificially dilute macro precision.0or1); evaluates class1, returning0.0if class1was never observed. ThrowsIllegalArgumentExceptionon multiclass inputs.Testing Strategy
Tests are placed in
skainet-backends/skainet-backend-cpu/src/commonTest/kotlin/sk/ainet/exec/metrics/PrecisionTest.ktusingDirectCpuExecutionContext(phase = Phase.EVAL), mirroringAccuracyTest.kt.Test Coverage Highlights (13 tests):
preds = [2, 0, 1, 2],targets = [2, 1, 1, 0]yields Macro Precision0.5.0.5, confirms equality withAccuracyon the reference fixture, and confirms divergence fromMacroon imbalanced batches (2/3vs0.5).Int32index targets andFP32one-hot targets.0.7f(2/3) vs default0.5f(0.5).(N, 1)sigmoid tensors with custom threshold0.6fand default0.5f(including exact tie at0.5fmapping to class0).argmax.update()calls match a single concatenated batch;reset()restores compute to0.0.0.0.0.0(neverNaN).0.0.0.0.Shape(0, 3)) evaluates to0.0without errors.assertFailsWith<IllegalArgumentException>whenAveraging.BINARYreceives multiclass labels (> 1).dim = 1.Acceptance Checklist
Precision,precision(), andbinaryPrecision()are public and in the API dump.skainet-backends/skainet-backend-cpu:jvmTest(13/13 tests pass).skainet-backends/skainet-backend-cpu:linuxX64Test(Linux Native multiplatform verification passes).ConfusionMatrixAccumulatorandreduce().:skainet-lang:skainet-lang-core:apiCheckand:skainet-backends:skainet-backend-cpu:apiCheckpassed../gradlew checkUnmarkedStubspassed.