class

Logarithm::Autoencoder

Inherits Logarithm::AbstractModel < Reference < Object

Autoencoder neural network for unsupervised anomaly detection.

This class implements a simple autoencoder with an encoder-decoder architecture designed for anomaly detection in log data. The network compresses log vectors into a low-dimensional bottleneck representation and then reconstructs them.

Architecture:

  • Encoder: vocab_size -> 32 -> 8 (bottleneck)
  • Decoder: 8 -> 32 -> vocab_size

Training uses mean squared error loss to minimize reconstruction error. Anomalies are detected when reconstruction error exceeds a threshold.

The model uses simple gradient descent with a fixed learning rate. For production use, consider more sophisticated optimizers and architectures.

Constructors

new(vocab_size : Int32)
Source

Instance methods

forward(x : Tensor) : Tensor
Source
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, learning_rate : Float32 = 0.001_f32, 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