Nasty.Statistics.Model behaviour (Nasty v0.3.0)

View Source

Behaviour 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 data
  • predict/2 - Make predictions on new data
  • save/2 - Serialize model to disk
  • load/1 - Deserialize model from disk
  • metadata/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

input_data()

@type input_data() :: term()

metadata()

@type metadata() :: %{
  :version => String.t(),
  :trained_at => DateTime.t(),
  optional(:accuracy) => float(),
  optional(:training_size) => pos_integer(),
  optional(atom()) => term()
}

model()

@type model() :: struct()

options()

@type options() :: keyword()

predictions()

@type predictions() :: term()

training_data()

@type training_data() :: list()

Callbacks

load(t)

@callback load(Path.t()) :: {:ok, model()} | {:error, term()}

Load a serialized model from disk.

Parameters

  • path - File path to load from

Returns

  • {:ok, model} - Successfully loaded model
  • {:error, reason} - Load failed

metadata(model)

@callback metadata(model()) :: metadata()

Get model metadata (version, training info, etc.).

Parameters

  • model - The model

Returns

  • Metadata map with version, accuracy, training time, etc.

predict(model, input_data, options)

@callback predict(model(), input_data(), options()) ::
  {:ok, predictions()} | {:error, term()}

Make predictions on new input data.

Parameters

  • model - Trained model
  • input_data - Data to predict on
  • opts - Prediction options

Returns

  • {:ok, predictions} - Predicted labels/structures
  • {:error, reason} - Prediction failed

save(model, t)

@callback save(model(), Path.t()) :: :ok | {:error, term()}

Serialize and save the model to disk.

Parameters

  • model - Model to save
  • path - File path for saving

Returns

  • :ok - Successfully saved
  • {:error, reason} - Save failed

train(model, training_data, options)

@callback train(model(), training_data(), options()) :: {:ok, model()} | {:error, term()}

Train the model on annotated training data.

Parameters

  • model - The model struct to train
  • training_data - Annotated training examples
  • opts - Training options (learning rate, iterations, etc.)

Returns

  • {:ok, trained_model} - Successfully trained model
  • {:error, reason} - Training failed

Functions

deserialize(binary)

@spec deserialize(binary()) :: {:ok, model(), metadata()} | {:error, term()}

Helper function to deserialize a model from binary format.

Validates version compatibility and extracts model data.

serialize(model, metadata)

@spec serialize(model(), metadata()) :: binary()

Helper function to serialize a model to binary format.

Uses Erlang's term_to_binary for efficient serialization. Includes versioning and compression.