Metrics hingeloss multiclass - CyrilB1531/lodestar GitHub Wiki

Development build. This page describes main, not a released package. The latest published Lodestar.Metrics is 0.3.0 — read its documentation.

HomeMetricsClassification metrics

HingeLoss.MultiClass

The multiclass hinge loss — sklearn.metrics.hinge_loss over one decision per class.

public static double MultiClass(ReadOnlySpan<int> yTrue, ReadOnlySpan<double> predDecision, int classCount, ReadOnlySpan<double> sampleWeight = default)

ParametersyTrue is the true class index of each sample, in [0, classCount). predDecision is one decision per class, row-major: sample 0's classes, then sample 1's. classCount is how many classes each row scores. sampleWeight is one weight per sample, or empty.

Returnsdouble, 0 or above. 0 when every sample's own class wins by at least 1.

ExceptionsArgumentException when predDecision is not yTrue.Length × classCount, or a label is not a class index below classCount. A decision holding NaN or an infinity is refused with the reference's "Input contains NaN." or its infinity counterpart, naming predDecision. Weights summing to zero are refused with numpy's "Weights sum to zero, can't be normalized."; hinge_loss never calls _check_sample_weight, so a non-finite weight is scored, NaN on both sides. ArgumentOutOfRangeException when classCount is below two.

Example — four samples over three classes.

using Lodestar.Metrics;

int[] truth = [0, 1, 2, 1];
double[] decisions =
[
    1.2, 0.3, -0.5,
    0.1, 0.9, 0.2,
    0.4, 0.2, 0.7,
    0.3, 0.1, 0.6,
];

double loss = HingeLoss.MultiClass(truth, decisions, classCount: 3);  // => 0.65

The last sample's own class scores 0.1 against a best rival of 0.6, a margin of -0.5, so it alone costs 1.5 of the 2.6 the four sum to.

Remarks — the margin is the true class's decision less the best of the others, not less all of them: Crammer and Singer's multiclass hinge, which is what the reference computes. Only that one rival matters, so improving a class the sample was never going to be confused with changes nothing.

Applies to — net10.0, netstandard2.0.

See alsoHingeLoss.Score, LogLoss.MultiClass, the Python equivalence table.

⚠️ **GitHub.com Fallback** ⚠️