Code Monkey home page Code Monkey logo

Comments (6)

YuhanLiin avatar YuhanLiin commented on July 30, 2024 1

The models should implement the Serde trait, so you can serialize them using something like ciborium

from linfa.

YuhanLiin avatar YuhanLiin commented on July 30, 2024 1

Seems like MultiClassModel and linfa-bayes have no support for Serde. Weird. We'll need to add that.

from linfa.

Bastian1110 avatar Bastian1110 commented on July 30, 2024 1

My bad, it turns out the example SVM model uses Svm<f64, bool> not Svm<_, bool>. I only changed the line to :

let model: Svm<f65, bool> = model_value.deserialized().unwrap();

And it works super cool!

from linfa.

Bastian1110 avatar Bastian1110 commented on July 30, 2024

I'm having a little problem hehe, I'm trying to serialize the model (I'm using the example of linfa_svm) but I don't know if I'm using the correct syntax since I get the error in the line where I use cbor:

the trait bound `MultiClassModel<ndarray::ArrayBase<ndarray::data_repr::OwnedRepr<f64>, ndarray::dimension::dim::Dim<[usize; 2]>>, usize>: serde::ser::Serialize` is not satisfied
the following other types implement trait `serde::ser::Serialize`:

This is the code I'm using (linfa_svm/examples/winequality_multi.rs) :

let model = train
        .one_vs_all()?
        .into_iter()
        .map(|(l, x)| (l, params.fit(&x).unwrap()))
        .collect::<MultiClassModel<_, _>>();
 let pred = model.predict(&valid);

//Trying to serialize model
 let save_model = cbor!(model).unwrap();

Could you give me a more detailed example? I would appreciate it too much!

from linfa.

Bastian1110 avatar Bastian1110 commented on July 30, 2024

Oh! I will try with normal SVM then, thank you!

from linfa.

Bastian1110 avatar Bastian1110 commented on July 30, 2024

One last question,
I already managed to serialize the SVM model without Multiclass to CBOR, using ciborium, I also managed to de-serialize it in another file and convert it to Value, the last step would be to convert it from Value to SVM, any idea how to do this?

Code for creating and exporting the model :

    let model = Svm::<_, bool>::params().pos_neg_weights(50000., 5000.).gaussian_kernel(80.0).fit(&train)?;

    //Serializing the trained model with ciborium
    let value_model : Value = cbor!(model).unwrap();
    let mut vec_model : Vec<u8> = Vec::new();
    let _cebor_writer = ciborium::ser::into_writer(&value_model, &mut vec_model);

    //Esporting it to a .cbor file
    let path: &Path = Path::new("./model.cbor");
    fs::write(path, vec_model).unwrap();

Attempt to use the trained model in other .rs program :

    let mut file = File::open("./model.cbor").unwrap();
    let mut data: Vec<u8> = Vec::new();
    file.read_to_end(&mut data).unwrap();

    let model_value : Value = ciborium::de::from_reader::<Value, _>(&data[..]).unwrap();
    let model: Svm<_, bool> = model_value.deserialized().unwrap(); // Error 

But I keep getting error when trying to converting form ciborium::Value to SVM, the rust-analyzer suggests : consider specifying the generic argument: ::<Svm<_, bool>>, I guess I have to pass the SVM serve-deserializer but I don't know how to do that.

I know this has nothing to do with linfa, but I really think that exporting and importing the models can be very useful.

Thank you!

from linfa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.