dotnet/machinelearning · error · ArithmeticException

Not finite unit value

Error message

Not finite unit value

What it means

During SrCnnEntireAnomalyDetector training, the per-unit sensitivity bound is computed as averageTrendPart + |trend| * trendFraction; if the result overflows to positive infinity, an ArithmeticException('Not finite unit value') is thrown. This guards the detector against numerically degenerate inputs where trend magnitudes explode.

Solutions

  1. Sanitize the input series: remove or cap extreme outliers and sentinel magnitudes before training.
  2. Normalize/rescale the time series (e.g. z-score or min-max) so trend values stay within finite ranges.
  3. Verify data quality: check for double.MaxValue/Infinity-adjacent values with a quick pre-scan of the column.
  4. If this occurs on legitimately large-but-valid data, reduce the sensitivity/trendFraction or open an issue with a repro for a scale-aware fix.

Example fix

// before
var pipeline = mlContext.Transforms.Conversion...
    .Append(mlContext.AnomalyDetection.Trainers.SrCnnEntireAnomalyDetector(...));
// after: cap outliers first
for (int i = 0; i < values.Length; i++)
    values[i] = Math.Min(values[i], 1e6); // or drop/interpolate outliers
Defensive patterns

Strategy: validation

Validate before calling

bool finite = data.All(v => !double.IsNaN(v) && !double.IsInfinity(v) && Math.Abs(v) < 1e300);

Try / catch

try { model = pipeline.Fit(trainData); }
catch (ArithmeticException ex) when (ex.Message == "Not finite unit value")
{ /* sanitize/rescale inputs and retry */ }

Prevention

When it happens

Trigger: Fitting SrCnnEntireAnomalyDetector (SrCnnEntireModeler) on series whose computed trend values are huge — e.g. data containing extreme outliers or non-NaN-but-enormous magnitudes — causing _units[i] = averageTrendPart + Math.Abs(_trends[i]) * trendFraction to be double.PositiveInfinity.

Common situations: Training on raw data containing sentinel values like 1e308 or sensor glitches; forgetting to clean/scale extreme outliers before anomaly detection; very small datasets where trend estimation amplifies noise.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/1e186838ad101654. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.ML.TimeSeries/SrCnnEntireAnomalyDetector.cs:1016

                }
                else
                {
                    trendFraction = 1.0;
                }

                Array.Resize(ref _units, _trends.Length);
                for (int i = 0; i < _units.Length; ++i)
                {
                    if (closeToZero)
                    {
                        _units[i] = _unitForZero;
                    }
                    else
                    {
                        _units[i] = averageTrendPart + Math.Abs(_trends[i]) * trendFraction;
                        if (double.IsInfinity(_units[i]))
                        {
                            throw new ArithmeticException("Not finite unit value");
                        }
                    }
                }
            }

            private void MedianFilter(double[] data, int window, bool needTwoEnd = false)
            {
                int wLen = window / 2 * 2 + 1;
                int tLen = data.Length;
                Array.Resize(ref _val, tLen);
                Array.Copy(data, _val, tLen);
                Array.Resize(ref _trends, tLen);
                Array.Copy(data, _trends, tLen);
                Array.Resize(ref _curWindow, wLen);

                if (tLen < wLen)
                    return;

View on GitHub (pinned to 7b76e69cf9)