-
-
Notifications
You must be signed in to change notification settings - Fork 107
Serializing SVC #297
Hi there,
Wonderful library.
I'm trying to do some tests using the SVC.
But the training is slow.
So I'm trying to save the model to re-use it.,
But when deserializing it, I get a parameters unwraps to a None error.
I looked into the implementation and it seems deserialization is skipped for the params and the kernel on wasm32 targets.
I'm testing on non-wasm32 but need to run on wasm32.
Is it possible to save the SVC model?
Thanks!
I really appreciate the hard work you did. Cool project.
All reactions
Replies: 3 comments 1 reply
Thanks for using smartcore 👍
could you provide a use case and a code example of what you are trying to do?
All reactions
Yes. I'm following the User Guide under "Model persistence" at https://smartcorelib.org/user_guide/model_selection.html
In this code I fit the SVC and save it to file for later use. On loading the file, I get error:
thread 'main' panicked at C:\Users\nothi\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\smartcore-0.4.0\src\svm\svc.rs:349:26: called Option::unwrap()on aNonevalue
smartcore = { version = "0.4.0", features = ["datasets", "serde"] } serde = "1.0.115" bincode = "1.3.1"
` let matrix = from_dataframe_to_dense_matrix(df.clone(), false);
let matrix1 = from_dataframe_to_dense_matrix(df1.clone(), false);
let params = SVCParameters::default()
.with_c(1.0)
.with_kernel(Kernels::rbf().with_gamma(0.7));
let svc = SVC::fit(&matrix, &y_train_original, ¶ms).expect("oh no");
// File name for the model
let file_name = "svcpre2024.model";
// Save the model
{
let svc_bytes = bincode::serialize(&svc).expect("Can not serialize the model");
File::create(file_name)
.and_then(|mut f| f.write_all(&svc_bytes))
.expect("Can not persist model");
}
// Load the model
let mut svc: SVC<f64, i32, DenseMatrix<f64>, Vec<i32>> = {
let mut buf: Vec<u8> = Vec::new();
File::open(&file_name)
.and_then(|mut f| f.read_to_end(&mut buf))
.expect("Can not load model");
bincode::deserialize(&buf).expect("Can not deserialize the model")
};
let y_hat = svc.predict(&matrix1).unwrap();`
All reactions
thanks. please try the latest commit in the development branch by doing:
[dependencies]
crate_name = { git = "https://github.com/username/repo.git", branch = "branch_name" }
and let me know if the error persists. This is the 4.1 version WIP.
Also notice that the website documentation is stuck to version 3. For updated documentation check Rust docstrings or https://docs.rs/smartcore/latest/smartcore/
version 4 has a new interface and some examples may be outdated. Sorry for the inconvenience.
All reactions
It seems this error persists on this branch as well. :(
smartcore = { git = "https://github.com/smartcorelib/smartcore", branch = "development", features = ["datasets", "serde"] }
called Option::unwrap()on aNone value
This seems to be relevant:
#[cfg_attr(feature = "serde", serde(skip))] parameters: Option<&'a SVCParameters<TX, TY, X, Y>>,
`#[cfg_attr(
all(feature = "serde", target_arch = "wasm32"),
serde(skip_serializing, skip_deserializing)
)]
pub kernel: Option<Box<dyn Kernel>>,`