rust-ml / linfa

A Rust machine learning framework.
Apache License 2.0
3.7k stars 243 forks source link

Serialize a trained model to disk? #316

Closed zachcp closed 11 months ago

zachcp commented 1 year ago

Hi Team-Linfa,

This is a wonderful effort and I am kicking the tires. One thing I would like to be able to do is to train a model and save the model to disk in order to reload/reuse it later. However, my initial attempts have all hit Serialization issues (see below). The source code seems to have serialization (and derive) enabled for DecisionTree so I am not sure how to resolve this and I would appreciate any pointers.

thanks! zach cp

// to https://github.com/rust-ml/linfa/blob/master/algorithms/linfa-trees/examples/decision_tree.rs
// add these deps
use bincode::{deserialize, serialize, Options};
use ::serde_json;

// try serde_json
println!("{}", serde_json::to_string(&entropy_model).unwrap());
//  ^^^^^^^^^^^^^^ the trait `serde::ser::Serialize` is not implemented for `DecisionTree<f64, usize>`

// try bincode
println!("{:?}",  bincode::serialize(&entropy_model).unwrap());
//^^^^^^^^^^^^^^ the trait `serde::ser::Serialize` is not implemented for `DecisionTree<f64, usize>`
zachcp commented 1 year ago

So for serde to work I needed to invoke cargo differently:

// to https://github.com/rust-ml/linfa/blob/master/algorithms/linfa-trees/examples/decision_tree.rs
// add these deps
use bincode;
use std::fs::File;

// updated lines
let _encoded: Vec<u8> = bincode::serialize(&entropy_model).unwrap();
let mut file = File::create("my_data.bin").unwrap();
bincode::serialize_into(&mut file, &_encoded).unwrap();
cargo add bincode
cargo run --release --example decision_tree --features serde
YuhanLiin commented 11 months ago

The key here is --features serde. You need to enable the serde feature on linfa-trees to enable the Serde support.