Logarithm::AbstractVectorizer
Abstract base class for text vectorization algorithms.
Vectorizers convert raw text log messages into numerical vectors that can be processed by machine learning models. They learn patterns in the text during training and apply the same transformation during inference.
Role in Pipeline
Vectorizers are the second stage in the anomaly detection pipeline:
- Log sources provide raw text
- Vectorizers convert text to vectors
- ML models learn patterns in vectors
- Anomalies are detected based on reconstruction error
Interface
All vectorizers must implement:
fit(logs): Learn vocabulary and patterns from training datatransform(log): Convert a single log to vector representationvocab_size: Property indicating learned vocabulary size- Save/load methods for model persistence
Example Implementation
class SimpleVectorizer < AbstractVectorizer
@word_to_index = {} of String => Int32
def fit(logs : Array(String))
# Build vocabulary from training logs
words = logs.flat_map(&.split).uniq
@word_to_index = words.each_with_index.to_h
@vocab_size = words.size
end
def transform(log : String) : Tensor
# Convert log to bag-of-words vector
vector = Array(Float32).new(@vocab_size, 0.0_f32)
log.split.each do |word|
if index = @word_to_index[word]?
vector[index] = 1.0_f32
end
end
Tensor.new(vector)
end
# Implement save/load methods...
end
Built-in Implementations
TfidfVectorizer: TF-IDF weighted term vectors with configurable vocabulary
Instance methods
Learns vocabulary and transformation parameters from training logs.
This method analyzes the training data to build internal data structures
needed for vectorization. It should set the vocab_size property.
Parameters:
- logs: Array of training log messages
This method is called once during training.
Loads the vectorizer state from a file.
Parameters:
- path: File path to load from
Deserializes the vectorizer from a string.
Parameters:
- data: String representation of vectorizer state
Saves the vectorizer state to a file.
Parameters:
- path: File path to save to
Serializes the vectorizer to a string for storage/encryption.
Returns: String representation of the vectorizer state
Converts a single log message to its vector representation.
The output vector should have vocab_size dimensions and be
compatible with the ML model's input requirements.
Parameters:
- log: Single log message to vectorize
Returns: Numerical vector representation as Tensor
Size of the learned vocabulary (number of features).
This property is set during fit() and indicates how many
dimensions the output vectors will have.