Comments (6)
The models implement the Serde traits, so you should be able to use whatever serde
serialization/deserialization implementation you want. see #290
from linfa.
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.
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.
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.
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.
@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)
- Let linfa_linear::LinearRegression support weights from a dataset
- MultiLogisticRegression panics on normalized data.
- umap for dimension reduction HOT 1
- DBSCAN returns core samples HOT 1
- How to do clustering grid search with multiple CPUs / GPUs? HOT 2
- how to save svm model data and resuse it ?
- linfa_bayes show error: records not in row-major layout HOT 1
- get the score from logistic regression prediction
- the order of classification HOT 1
- .
- what is the difference between these libarary and pure rust? HOT 1
- gradient tolerance and alpha
- get loss for each iteration
- linfa_bayes::MultinomialNb,when use predict(), program paniced!
- MeanShift: potential contribution HOT 2
- Support streaming/incremental fitting
- Fix ci HOT 1
- linfa-reduction-0.7.0 Pca<f64> without the method of 'components' and 'mean'
- Intel MKL BLAS backend download error: `linfa` release required
- rust-analyzer does not recognize linfa types
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from linfa.