Nasty.Statistics.Model behaviour (Nasty v0.3.0)
View SourceBehaviour for statistical models in Nasty.
All statistical models (HMM, PCFG, CRF, etc.) implement this behaviour, providing a consistent interface for training, prediction, and persistence.
Model Lifecycle
# Training
model = MyModel.new(opts)
model = MyModel.train(model, training_data)
:ok = MyModel.save(model, "path/to/model.bin")
# Loading and prediction
{:ok, model} = MyModel.load("path/to/model.bin")
predictions = MyModel.predict(model, input_data)Callbacks
train/2- Train the model on annotated datapredict/2- Make predictions on new datasave/2- Serialize model to diskload/1- Deserialize model from diskmetadata/1- Get model metadata (version, accuracy, etc.)
Summary
Callbacks
Load a serialized model from disk.
Get model metadata (version, training info, etc.).
Make predictions on new input data.
Serialize and save the model to disk.
Train the model on annotated training data.
Functions
Helper function to deserialize a model from binary format.
Helper function to serialize a model to binary format.
Types
@type input_data() :: term()
@type metadata() :: %{ :version => String.t(), :trained_at => DateTime.t(), optional(:accuracy) => float(), optional(:training_size) => pos_integer(), optional(atom()) => term() }
@type model() :: struct()
@type options() :: keyword()
@type predictions() :: term()
@type training_data() :: list()
Callbacks
Load a serialized model from disk.
Parameters
path- File path to load from
Returns
{:ok, model}- Successfully loaded model{:error, reason}- Load failed
Get model metadata (version, training info, etc.).
Parameters
model- The model
Returns
- Metadata map with version, accuracy, training time, etc.
@callback predict(model(), input_data(), options()) :: {:ok, predictions()} | {:error, term()}
Make predictions on new input data.
Parameters
model- Trained modelinput_data- Data to predict onopts- Prediction options
Returns
{:ok, predictions}- Predicted labels/structures{:error, reason}- Prediction failed
Serialize and save the model to disk.
Parameters
model- Model to savepath- File path for saving
Returns
:ok- Successfully saved{:error, reason}- Save failed
@callback train(model(), training_data(), options()) :: {:ok, model()} | {:error, term()}
Train the model on annotated training data.
Parameters
model- The model struct to traintraining_data- Annotated training examplesopts- Training options (learning rate, iterations, etc.)
Returns
{:ok, trained_model}- Successfully trained model{:error, reason}- Training failed
Functions
Helper function to deserialize a model from binary format.
Validates version compatibility and extracts model data.
Helper function to serialize a model to binary format.
Uses Erlang's term_to_binary for efficient serialization. Includes versioning and compression.