Module serialize

Module serialize 

Source
Expand description

Neural network model serialization.

Provides SafeTensors-compatible serialization for nn modules. This enables saving trained models and loading them for inference.

§Example

use aprender::nn::{Sequential, Linear, ReLU, Module};
use aprender::nn::serialize::{save_model, load_state_dict};

// Build and train a model
let model = Sequential::new()
    .add(Linear::new(784, 256))
    .add(ReLU::new())
    .add(Linear::new(256, 10));

// Save model weights
save_model(&model, "model.safetensors").unwrap();

// Load weights into a new model
let mut new_model = Sequential::new()
    .add(Linear::new(784, 256))
    .add(ReLU::new())
    .add(Linear::new(256, 10));

let state = load_state_dict("model.safetensors").unwrap();
load_into_model(&mut new_model, &state).unwrap();

Functions§

count_parameters
Get the number of parameters that would be saved.
load_model
Load parameters from a SafeTensors file into a module.
load_state_dict
Load a state dictionary from a SafeTensors file.
load_state_dict_into
Load state dictionary into a module.
model_size_bytes
Get the size in bytes that would be saved (F32 = 4 bytes per parameter).
save_model
Save a module’s parameters to a SafeTensors file.
state_dict
Extract state dictionary from a module.

Type Aliases§

StateDict
State dictionary: mapping from parameter names to tensor data and shapes.