Logarithm::AbstractModel
Abstract base class for machine learning models.
Models are the core anomaly detection component in Logarithm. They learn patterns from normal log data during training and detect anomalies during inference by measuring how well they can reconstruct input data.
Anomaly Detection Principle
The unsupervised anomaly detection works as follows:
- Training: Model learns to reconstruct normal log patterns
- Inference: Model tries to reconstruct new logs
- Detection: Reconstruction error indicates anomaly likelihood
- Thresholding: Errors above threshold trigger alerts
Role in Pipeline
Models are the third stage in the anomaly detection pipeline:
- Log sources provide raw text
- Vectorizers convert text to vectors
- Models learn patterns and detect anomalies
- Pipeline applies thresholds and generates alerts
Interface
All models must implement:
train(data, epochs): Learn patterns from training vectorspredict(input): Reconstruct input vector (inference)- Save/load methods for model persistence
Example Implementation
class SimpleModel < AbstractModel
@weights = [] of Array(Float32)
def train(data : Array(Tensor), epochs : Int32)
# Implement training algorithm
epochs.times do
data.each do |sample|
# Update weights based on reconstruction error
prediction = predict(sample)
error = sample - prediction
# Gradient descent update...
end
end
end
def predict(input : Tensor) : Tensor
# Reconstruct input using learned weights
# Return best approximation of input
Tensor.new([0.0_f32]) # Placeholder
end
# Implement save/load methods...
end
Built-in Implementations
Autoencoder: Neural network autoencoder for pattern learning
Instance methods
Loads a trained model from a file.
Parameters:
- path: File path to load the model from
Deserializes the model from a string.
Parameters:
- data: String representation of the model
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.
Saves the trained model to a file.
Parameters:
- path: File path to save the model to
Serializes the model to a string for storage/encryption.
Returns: String representation of the trained model
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.