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
SafeTensorsfile into a module. - load_
state_ dict - Load a state dictionary from a
SafeTensorsfile. - 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
SafeTensorsfile. - state_
dict - Extract state dictionary from a module.
Type Aliases§
- State
Dict - State dictionary: mapping from parameter names to tensor data and shapes.