Code Monkey home page Code Monkey logo

Comments (6)

relf avatar relf commented on July 30, 2024

The models implement the Serde traits, so you should be able to use whatever serde serialization/deserialization implementation you want. see #290

from linfa.

DataPsycho avatar DataPsycho commented on July 30, 2024

Hi, I have followed the issue to preproduce a logistic. Thats how the code looks like:

fn train () {
    let dataset = get_dataset();
    info!("Step: Start Training the model.");
    let model = LogisticRegression::default()
    .max_iterations(500)
    .gradient_tolerance(0.0001)
    .fit(&dataset)
    .expect("Can not train the model");
    let value_model = cbor!(model).unwrap();
    let mut vec_model = Vec::new();
    let _result = ciborium::ser::into_writer(&value_model, &mut vec_model).unwrap();
    // let prediction = model.predict(&dataset.records);
    // println!("{:?}", prediction);
    let write_path = Path::new("model").join("model.cbor");
    fs::write(write_path.clone(), vec_model).unwrap();
    info!("Model saved at {:?}", write_path.as_path());
}

fn load_model() {
    let dataset = get_dataset();
    let mut data: Vec<u8> = Vec::new();
    let path = Path::new("model").join("model.cbor");
    let mut file = File::open(&path).unwrap();
    file.read_to_end(&mut data).unwrap();
    let model_value = ciborium::de::from_reader::<value::Value, _>(&data[..]).unwrap();
    let model: LogisticRegression<f64> = model_value.deserialized().unwrap();
    let result = model.predict(&dataset.records); //THis does not work
}

But I can not load the model. It says the predict method is not available for the model. Which is true the LogisticRegression has no predict method, there might be some function I need to call. Can anyone help me.

from linfa.

DataPsycho avatar DataPsycho commented on July 30, 2024

I just tried with FittedLogisticRegression type but could not load the model. I might be doing something really stupid. But this is frustrating to look everywhere on the internet to figure out the solution. Though there is not a single example I found which shows the whole process of building a Logistic Model. I hope when my solution works I will be able to create a complete document on the following processes with linfa.

  • Trainning a Model
  • Validation and Test of a model
  • Save a model
  • Load a model and make prediction out of it
fn load_model() {
    let dataset = get_dataset();
    let mut data: Vec<u8> = Vec::new();
    let path = Path::new("model").join("model.cbor");
    let mut file = File::open(&path).unwrap();
    file.read_to_end(&mut data).unwrap();
    let model_value = ciborium::de::from_reader::<value::Value, _>(&data[..]).unwrap();
    let model: FittedLogisticRegression<f32, bool> = model_value.deserialized().unwrap(); // THis does not work
    model.predict(dataset.records);
    info!("Model loading was also successful!")
}

Here is the error:
thread 'main' panicked at 'called Result::unwrap() on an Err value: Custom("invalid type: integer 1, expected bool")', src/bin/stages/train.rs:114:81
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

from linfa.

relf avatar relf commented on July 30, 2024

What is the type of your dataset targets? i32? Did you try

let model: FittedLogisticRegression<f32, i32> = model_value.deserialized().unwrap();

There is also an example of serialization/deserialization using rmp-serde in the tests here

from linfa.

DataPsycho avatar DataPsycho commented on July 30, 2024

Hi, It looks like it's working. Thanks a lot. I need to understand the type system of linfa . Thanks for the help. Closing the issue.

from linfa.

wangjiawen2013 avatar wangjiawen2013 commented on July 30, 2024

@DataPsycho
Hi, I tried to save a logistic model using your way but failed, here is the error:

let model = LogisticRegression::default()
    .max_iterations(1500)
    .gradient_tolerance(0.0001)
    .fit(&dataset_training)
    .expect("Cannot train model");
let value_model = cbor!(model).unwrap();

the trait bound `FittedLogisticRegression<f64, &str>: Serialize` is not satisfied

I have found out why this happened. I forget to activate serde feature of logisticregssion!

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.