klyn.math.nn.losses.SparseCategoricalCrossentropy.compute
method
public override compute(logits as NDArray<T>, labels as NDArray<Int>, gradientScale as Double = 1.0) as LossResult<T>
Description

Computes the mean loss and its mean-reduced input gradient. In logit mode Softmax is integrated into the loss and must not be applied first.

Parameters
ParameterDescription
logitsMatrix [samples, classes] of finite logits or probabilities.
gradientScalePositive finite multiplier for the derivative; loss remains unscaled.
labelsOne-dimensional Int array; values are in [0, classes).
Returns

Loss and derivative with respect to the supplied inputs (logits or probabilities).

Throws
  • ValueException for invalid shapes, labels or non-finite logits.
Example

result = SparseCategoricalCrossentropy<Float>().compute(logits, labels)