v2026.11.2
All Bundles
Bundle

Core machine learning types: the seedable Random generator, Matrix2D linear algebra and activations, matrix reference holders, the NeuralNetwork and CSV-backed MatrixReader. Compile with -lib ml.

AutoencoderDetector

Autoencoder anomaly detection: a DenseNetwork trained to reproduce its input through a bottleneck, so rows unlike the training data reconstruct badly.

Features are standardized with the training mean and population standard deviation (a constant feature scales to 0), the network maps p features through the hidden sizes back to p (ReLU hidden layers, linear output), trained by Adam on mean squared error. A row's score is its mean squared reconstruction error in the standardized space. The threshold is the given percentile (default 95.0) of the TRAINING reconstruction errors, numpy's linear interpolation; 99.5 flags about 0.5% of the training rows. The percentile only moves the threshold, so SetSensitivity keeps the trained network.

Hidden sizes default to [ceil(p/2), ceil(p/4), ceil(p/2)]. Training: 50 epochs, batches of 32, learning rate 0.001, seed 42.

Implements: OutlierDetector

Example

ae := AutoencoderDetector->New([8, 3, 8], 97.5, 50, 42);
flags := ae->FitPredict(X);
errors := ae->Score(X_test);

Operations

DecisionFunction #

Score minus the threshold; positive means outlier.

method : public : DecisionFunction(X:Float[,]) ~ Float[]

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Floatvalues, or Nil when unfitted or the input is refused

Fit #

Standardizes, trains the network and sets the threshold.

method : public : Fit(X:Float[,]) ~ Bool

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Booltrue on success

FitPredict #

Fits, then labels the training rows.

method : public : FitPredict(X:Float[,]) ~ Bool[]

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Boollabels (true = outlier), or Nil when the fit failed

GetError #

Why the last call failed.

method : public : GetError() ~ String

Return

TypeDescription
Stringthe reason, or an empty string

GetNetwork #

The trained network.

method : public : GetNetwork() ~ DenseNetwork

Return

TypeDescription
DenseNetworkthe network, or Nil when unfitted

GetSensitivity #

The percentile.

method : public : GetSensitivity() ~ Float

Return

TypeDescription
Floatpercentile

GetSensitivityName #

The setting's name.

method : public : GetSensitivityName() ~ String

Return

TypeDescription
String"percentile"

GetThreshold #

The reconstruction-error threshold: the percentile of the training errors.

method : public : GetThreshold() ~ Float

Return

TypeDescription
Floatthreshold

GetTrainingLoss #

The last training epoch's mean squared error.

method : public : GetTrainingLoss() ~ Float

Return

TypeDescription
Floatloss, or 0.0 when unfitted

GetTrainingScores #

The training rows' reconstruction errors.

method : public : GetTrainingScores() ~ Float[]

Return

TypeDescription
Floaterrors, or Nil when unfitted

IsFitted #

Whether the detector is fitted.

method : public : IsFitted() ~ Bool

Return

TypeDescription
Booltrue when fitted

New # constructor

Constructor with the defaults: hidden sizes from the data, percentile 95.0, 50 epochs, seed 42.

New()

New # constructor

Constructor.

New(hidden:Int[], percentile:Float, epochs:Int, seed:Int)

Parameters

NameTypeDescription
hiddenInthidden layer sizes, e.g. [8, 3, 8]; Nil for [ceil(p/2), ceil(p/4), ceil(p/2)]
percentileFloattraining-error percentile used as the threshold, in (0, 100]
epochsInttraining passes (>= 1)
seedIntseeds the weights and the minibatch order

Predict #

Labels rows: true where the reconstruction error exceeds the threshold.

method : public : Predict(X:Float[,]) ~ Bool[]

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Boollabels, or Nil when unfitted or the input is refused

Reconstruct #

Reconstructions of each row, in the original feature units.

method : public : Reconstruct(X:Float[,]) ~ Float[,]

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Floatreconstructions, or Nil when unfitted or the input is refused

Score #

Per-row mean squared reconstruction error (standardized space).

method : public : Score(X:Float[,]) ~ Float[]

Parameters

NameTypeDescription
XFloatrows x features

Return

TypeDescription
Floaterrors, or Nil when unfitted or the input is refused

SetBatchSize #

Sets the minibatch size for the next Fit (default 32).

method : public : SetBatchSize(size:Int) ~ Bool

Parameters

NameTypeDescription
sizeIntrows per batch (>= 1)

Return

TypeDescription
Boolfalse when out of range

SetLearningRate #

Sets the Adam learning rate for the next Fit (default 0.001).

method : public : SetLearningRate(rate:Float) ~ Bool

Parameters

NameTypeDescription
rateFloatlearning rate (> 0)

Return

TypeDescription
Boolfalse when out of range

SetSensitivity #

Sets the percentile; a fitted detector keeps its network and re-derives the threshold from the training errors.

method : public : SetSensitivity(value:Float) ~ Bool

Parameters

NameTypeDescription
valueFloatpercentile in (0, 100]

Return

TypeDescription
Boolfalse when out of range

SetStandardize #

Whether the next Fit standardizes the features first (default true).

method : public : SetStandardize(standardize:Bool) ~ Nil

Parameters

NameTypeDescription
standardizeBoolfalse to feed the raw features to the network