linfa-ftrl
provides a pure Rust implementations of follow the regularized leader, proximal, model.
linfa-ftrl
is a crate in the linfa
ecosystem, an effort to create a toolkit for classical Machine Learning implemented in pure Rust, akin to Python's scikit-learn
.
The linfa-ftrl
crate provides Follow The Regularized Leader - Proximal model with L1 and L2 regularization from Logistic Regression, and primarily used for CTR prediction. It actively stores z and n values, needed to calculate weights.
Without L1 and L2 regularization, it is identical to online gradient descent.
See also: * Paper about Ftrl
There is a usage example in the examples/
directory. To run, use:
bash
$ cargo run --example winequality
Show source code
```rust use linfa::prelude::*; use linfa::dataset::{AsSingleTargets, Records}; use linfa_ftrl::{Ftrl, Result}; use rand::{rngs::SmallRng, SeedableRng};
// load Winequality dataset let (train, valid) = linfadatasets::winequality() .maptargets(|v| if *v > 6 { true } else { false }) .splitwithratio(0.9);
let params = Ftrl::params() .alpha(0.005) .beta(1.0) .l1ratio(0.005) .l2ratio(1.0);
let validparams = params.clone().checkunwrap(); let mut model = Ftrl::new(valid_params, train.nfeatures());
// Bootstrap each row from the train dataset to imitate online nature of the data flow let mut rng = SmallRng::seedfromu64(42); let mut rowiter = train.bootstrapsamples(1, &mut rng); for _ in 0..train.nsamples() { let bdataset = rowiter.next().unwrap(); model = params.fitwith(Some(model), &bdataset)?; } let valpredictions = model.predict(&valid); println!("valid log loss {:?}", valpredictions.logloss(&valid.assingletargets().tovec())?);
```