klyn.math.nn.losses.MeanSquaredError.compute
method
public override compute(outputs as NDArray<T>, targets as NDArray<T>, gradientScale as Double = 1.0) as LossResult<T>
Description

Computes MSE and a same-shaped, independent output derivative. Targets must have the output shape; a [samples] target vector is also accepted for single-coordinate outputs [samples, 1]. Non-finite inputs are rejected. A mathematically overflowing loss may return infinity, and a scaled gradient may overflow its storage; fit() rejects non-finite losses and applies its existing FP16 scaling policy.

Parameters
ParameterDescription
outputsFloating tensor with a batch axis and output dimensions.
targetsSame-precision tensor of matching shape, or a scalar vector.
gradientScaleFinite positive multiplier applied only to derivatives.
Returns

Unscaled mean error and scaled mean-reduced output derivative.

Throws
  • ValueException for invalid shapes, scale or non-finite input.
Example

result = MeanSquaredError<Double>().compute(NDArray<Double>([[2.0], [4.0]]), NDArray<Double>([1.0, 2.0]))