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.
Example
ae := AutoencoderDetector->New([8, 3, 8], 97.5, 50, 42);
flags := ae->FitPredict(X);
errors := ae->Score(X_test);Operations
- New
- DecisionFunction
- Fit
- FitPredict
- GetError
- GetNetwork
- GetSensitivity
- GetSensitivityName
- GetThreshold
- GetTrainingLoss
- GetTrainingScores
- IsFitted
- Predict
- Reconstruct
- Score
- SetBatchSize
- SetLearningRate
- SetSensitivity
- SetStandardize
DecisionFunction #
Score minus the threshold; positive means outlier.
method : public : DecisionFunction(X:Float[,]) ~ Float[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Float | values, or Nil when unfitted or the input is refused |
Fit #
Standardizes, trains the network and sets the threshold.
method : public : Fit(X:Float[,]) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Bool | true on success |
FitPredict #
Fits, then labels the training rows.
method : public : FitPredict(X:Float[,]) ~ Bool[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Bool | labels (true = outlier), or Nil when the fit failed |
GetError #
Why the last call failed.
method : public : GetError() ~ StringReturn
| Type | Description |
|---|---|
| String | the reason, or an empty string |
GetNetwork #
The trained network.
method : public : GetNetwork() ~ DenseNetworkReturn
| Type | Description |
|---|---|
| DenseNetwork | the network, or Nil when unfitted |
GetSensitivity #
The percentile.
method : public : GetSensitivity() ~ FloatReturn
| Type | Description |
|---|---|
| Float | percentile |
GetSensitivityName #
The setting's name.
method : public : GetSensitivityName() ~ StringReturn
| Type | Description |
|---|---|
| String | "percentile" |
GetThreshold #
The reconstruction-error threshold: the percentile of the training errors.
method : public : GetThreshold() ~ FloatReturn
| Type | Description |
|---|---|
| Float | threshold |
GetTrainingLoss #
The last training epoch's mean squared error.
method : public : GetTrainingLoss() ~ FloatReturn
| Type | Description |
|---|---|
| Float | loss, or 0.0 when unfitted |
GetTrainingScores #
The training rows' reconstruction errors.
method : public : GetTrainingScores() ~ Float[]Return
| Type | Description |
|---|---|
| Float | errors, or Nil when unfitted |
IsFitted #
Whether the detector is fitted.
method : public : IsFitted() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true 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
| Name | Type | Description |
|---|---|---|
| hidden | Int | hidden layer sizes, e.g. [8, 3, 8]; Nil for [ceil(p/2), ceil(p/4), ceil(p/2)] |
| percentile | Float | training-error percentile used as the threshold, in (0, 100] |
| epochs | Int | training passes (>= 1) |
| seed | Int | seeds the weights and the minibatch order |
Predict #
Labels rows: true where the reconstruction error exceeds the threshold.
method : public : Predict(X:Float[,]) ~ Bool[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Bool | labels, 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
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Float | reconstructions, 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
| Name | Type | Description |
|---|---|---|
| X | Float | rows x features |
Return
| Type | Description |
|---|---|
| Float | errors, 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) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| size | Int | rows per batch (>= 1) |
Return
| Type | Description |
|---|---|
| Bool | false when out of range |
SetLearningRate #
Sets the Adam learning rate for the next Fit (default 0.001).
method : public : SetLearningRate(rate:Float) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| rate | Float | learning rate (> 0) |
Return
| Type | Description |
|---|---|
| Bool | false when out of range |