class

Logarithm::SVM

Inherits Logarithm::AbstractModel < Reference < Object

Support Vector Machine for anomaly detection.

This class implements a simple linear SVM for unsupervised anomaly detection. It uses the One-Class SVM approach where training data is considered normal, and anomalies are detected based on distance to the decision boundary.

The SVM finds a hyperplane that maximizes the margin between the origin and the support vectors. During inference, the distance to this hyperplane indicates anomaly likelihood (negative distances are anomalies).

Constructors

new(vocab_size : Int32, nu : Float32 = 0.1_f32)
Source

Instance methods

load(path : String)

Loads a trained model from a file.

Parameters:

  • path: File path to load the model from
Source
load_from_string(data : String)

Deserializes the model from a string.

Parameters:

  • data: String representation of the model
Source
predict(input : Tensor) : Tensor

Performs inference on a single input vector.

Given an input vector, the model attempts to reconstruct it based on learned patterns. The reconstruction quality indicates how "normal" the input appears to the model.

Parameters:

  • input: Input vector to reconstruct

Returns: Reconstructed vector (same dimensions as input)

The reconstruction error (input - output) is used for anomaly scoring.

Source
save(path : String)

Saves the trained model to a file.

Parameters:

  • path: File path to save the model to
Source
save_to_string

Serializes the model to a string for storage/encryption.

Returns: String representation of the trained model

Source
train(data : Array(Tensor), epochs : Int32 = 100, early_stopping : Bool = false, patience : Int32 = 10, validation_data : Array(Tensor) | Nil = nil)

Trains the model on vectorized log data.

This method implements the learning algorithm that allows the model to learn patterns in normal log data. The training process optimizes the model's parameters to minimize reconstruction error for normal data.

Parameters:

  • data: Array of training vectors (from vectorizer)
  • epochs: Number of training iterations over the dataset

Training may take significant time depending on data size and epochs.

Source