Repository navigation
feat(metrics): add F1Score metric and factory functions (#1229) - #1462
Merged
michalharakal merged 1 commit intoOct 30, 2026
Merged
Conversation
michalharakal
approved these changes
Oct 30, 2026
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 #1229 for the Precision, Recall, and F1Score metric feature.
This PR introduces the public$F_1$ directly from raw confusion-matrix counts via the shared
F1Scoremetric class and its companion factory functions (f1Score()andbinaryF1Score()) inskainet-lang-core. The implementation computesConfusionMatrixAccumulatorandreduce()engine from #1226, walking batches once without allocating or delegating to separatePrecisionorRecallinstances.Part of DARC tracking issue #1222.
Closes #1229.
Depends on #1226.
Unblocks Lane 3 platform verification (#1230–#1233) and review (#1235).
Note
Unit tests only; ground-truth wiring tracked in SKEEP-005 ([SKEEP-005]: Ground-truth validation for stateful metrics #1223).
Changes
F1Scoreclass (sk.ainet.lang.nn.metrics.F1Score):Metricwithoverride val name: String = "f1".ConfusionMatrixAccumulator(dim, threshold). Does not allocate or delegate toPrecision/Recall, preventing duplicate tensor traversal.using
reduce(acc.counts(), averaging) { tp, fp, fn -> if (2 * tp + fp + fn == 0L) 0.0 else (2.0 * tp) / (2 * tp + fp + fn) }.Averaging.MACRO.Factory functions (
sk.ainet.lang.nn.metrics.F1ScoreKt):public fun f1Score(averaging: Averaging = Averaging.MACRO, dim: Int = -1): Metric: Factory for macro/micro multi-class F1 score.public fun binaryF1Score(threshold: Float = 0.5f): Metric: Explicitly wired withAveraging.BINARYto report positive class1F1 score 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.F1ScoreandF1ScoreKtin alphabetical order with 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 F1.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/F1ScoreTest.ktusingDirectCpuExecutionContext(phase = Phase.EVAL), mirroringAccuracyTest.kt,PrecisionTest.kt, andRecallTest.kt.Test Coverage Highlights:
preds = [2, 0, 1, 2],targets = [2, 1, 1, 0], per-class F1 scores areaccuracy()andprecision(Averaging.MICRO).Int32index targets andFP32one-hot targets.0.5f(0.7f(0.5f, verifying exact tie0.5fmaps strictly to negative class0.argmax.update()calls match a single concatenated batch;reset()restores compute to0.0.0.0.NaN).0.0.Shape(0, 3)) evaluates to0.0without errors.dim = 1.computeForBatchhelper reset round-trip.assertFailsWith<IllegalArgumentException>whenAveraging.BINARYreceives multiclass labels.Acceptance Checklist
F1Score,f1Score(), andbinaryF1Score()are public and registered inskainet-lang-core.api.:skainet-backends:skainet-backend-cpu:jvmTest).:skainet-backends:skainet-backend-cpu:linuxX64Test).update()via a singleConfusionMatrixAccumulator.:skainet-lang:skainet-lang-core:apiCheckand:skainet-backends:skainet-backend-cpu:apiCheckpassed../gradlew checkUnmarkedStubspassed.