1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
//! A factorization machine model implemented using stochastic gradient descent.
//!
//! A [factorization machine](http://www.csie.ntu.edu.tw/~b97053/paper/Rendle2010FM.pdf) (Rendle 2008)
//! model combines the advantages of linear and factorization models. In this implementation, it approximates
//! second-order feature interactions (as in a quadratic SVM) via reduced-rank matrix factorization.
//! This allows it to estimate feature interactions even in sparse datasets (like recommender systems) where
//! traditional polynomial SVMs fail.
//!
//! The complexity of the model is controlled by the dimensionality of the factorization matrix:
//! a higher setting will make the model more expressive at the expense of training time and
//! risk of overfitting.
//!
//! # Parallelism
//!
//! The model supports multithreaded model fitting via asynchronous stochastic
//! gradient descent (Hogwild).
//!
//! # Examples
//!
//! ```
//! use rustlearn::prelude::*;
//! use rustlearn::factorization::factorization_machines::Hyperparameters;
//! use rustlearn::datasets::iris;
//!
//! let (X, y) = iris::load_data();
//!
//! let mut model = Hyperparameters::new(4, 10)
//!                                 .one_vs_rest();
//!
//! model.fit(&X, &y).unwrap();
//!
//! let prediction = model.predict(&X).unwrap();
//! ```
#![allow(non_snake_case)]

use std::cmp;

use prelude::*;

use multiclass::OneVsRestWrapper;
use utils::{check_data_dimensionality, check_matched_dimensions, check_valid_labels, EncodableRng};

use rand;
use rand::distributions::IndependentSample;

use crossbeam;


fn sigmoid(x: f32) -> f32 {
    1.0 / (1.0 + (-x).exp())
}


fn logistic_loss(y: f32, y_hat: f32) -> f32 {
    y_hat - y
}


macro_rules! max {
    ($x:expr, $y:expr) => {{
        match $x > $y {
            true => $x,
            false => $y,
        }
    }}
}


macro_rules! min {
    ($x:expr, $y:expr) => {{
        match $x < $y {
            true => $x,
            false => $y,
        }
    }}
}


/// Hyperparameters for a FactorizationMachine
#[derive(RustcEncodable, RustcDecodable)]
pub struct Hyperparameters {
    dim: usize,
    num_components: usize,

    learning_rate: f32,
    l2_penalty: f32,
    l1_penalty: f32,
    rng: EncodableRng,
}


impl Hyperparameters {
    /// Creates new Hyperparameters.
    ///
    /// The complexity of the model is controlled by the dimensionality of the factorization matrix:
    /// a higher `num_components` setting will make the model more expressive
    /// at the expense of training time and risk of overfitting.
    pub fn new(dim: usize, num_components: usize) -> Hyperparameters {
        Hyperparameters {
            dim: dim,
            num_components: num_components,
            learning_rate: 0.05,
            l2_penalty: 0.0,
            l1_penalty: 0.0,
            rng: EncodableRng::new(),
        }
    }
    /// Set the initial learning rate.
    ///
    /// During fitting, the learning rate decreases more for parameters which have
    /// have received larger gradient updates. This maintains more stable estimates
    /// for common features while allowing fast learning for rare features.
    pub fn learning_rate(&mut self, learning_rate: f32) -> &mut Hyperparameters {
        self.learning_rate = learning_rate;
        self
    }

    /// Set the L2 penalty.
    pub fn l2_penalty(&mut self, l2_penalty: f32) -> &mut Hyperparameters {
        self.l2_penalty = l2_penalty;
        self
    }

    /// Set the L1 penalty.
    pub fn l1_penalty(&mut self, l1_penalty: f32) -> &mut Hyperparameters {
        self.l1_penalty = l1_penalty;
        self
    }

    pub fn rng(&mut self, rng: rand::StdRng) -> &mut Hyperparameters {
        self.rng.rng = rng;
        self
    }

    /// Build a two-class model.
    pub fn build(&self) -> FactorizationMachine {

        let mut rng = self.rng.clone();

        FactorizationMachine {
            dim: self.dim,
            num_components: self.num_components,

            learning_rate: self.learning_rate,
            l2_penalty: self.l2_penalty,
            l1_penalty: self.l1_penalty,

            coefficients: Array::zeros(self.dim, 1),
            latent_factors: self.init_latent_factors_array(&mut rng),
            gradsq: Array::ones(self.dim, 1),
            latent_gradsq: Array::ones(self.dim, self.num_components),
            applied_l2: Array::ones(self.dim, 1),
            applied_l1: Array::zeros(self.dim, 1),
            latent_applied_l2: Array::ones(self.dim, self.num_components),
            latent_applied_l1: Array::zeros(self.dim, self.num_components),
            accumulated_l2: 1.0,
            accumulated_l1: 0.0,

            rng: rng,
        }
    }

    /// Initialize the latent factors.
    fn init_latent_factors_array(&self, rng: &mut EncodableRng) -> Array {

        let mut data = Vec::with_capacity(self.dim * self.num_components);
        // let normal = rand::distributions::normal::Normal::new(0.0, 0.1 / ((self.dim * self.num_components) as f64).sqrt());
        let normal = rand::distributions::normal::Normal::new(0.0,
                                                              1.0 / self.num_components as f64);

        for _ in 0..(self.dim * self.num_components) {
            data.push(normal.ind_sample(&mut rng.rng) as f32)
        }

        let mut array = Array::from(data);
        array.reshape(self.dim, self.num_components);
        array
    }

    /// Build a one-vs-rest multiclass model.
    #[allow(dead_code)]
    pub fn one_vs_rest(&self) -> OneVsRestWrapper<FactorizationMachine> {
        let base_model = self.build();

        OneVsRestWrapper::new(base_model)
    }
}


/// A two-class factorization machine implemented using stochastic gradient descent.
#[derive(Clone, RustcEncodable, RustcDecodable)]
pub struct FactorizationMachine {
    dim: usize,
    num_components: usize,

    learning_rate: f32,
    l2_penalty: f32,
    l1_penalty: f32,

    coefficients: Array,
    latent_factors: Array,
    gradsq: Array,
    latent_gradsq: Array,
    applied_l2: Array,
    applied_l1: Array,
    latent_applied_l2: Array,
    latent_applied_l1: Array,
    accumulated_l2: f32,
    accumulated_l1: f32,

    rng: EncodableRng,
}


impl FactorizationMachine {
    fn compute_prediction<T: NonzeroIterable>(&self, row: &T, component_sum: &mut [f32]) -> f32 {

        let mut result = 0.0;

        for (feature_idx, feature_value) in row.iter_nonzero() {
            result += feature_value * self.coefficients.get(feature_idx, 0);
        }

        for component_idx in 0..self.num_components {

            let mut component_sum_elem = 0.0;
            let mut component_sum_sq_elem = 0.0;

            for (feature_idx, feature_value) in row.iter_nonzero() {
                let val = self.latent_factors.get(feature_idx, component_idx) * feature_value;
                component_sum_elem += val;
                component_sum_sq_elem += val.powi(2);
            }

            component_sum[component_idx] = component_sum_elem;

            result += 0.5 * (component_sum_elem.powi(2) - component_sum_sq_elem);
        }

        result
    }

    fn apply_regularization(parameter_value: &mut f32,
                            applied_l2: &mut f32,
                            applied_l1: &mut f32,
                            local_learning_rate: f32,
                            accumulated_l2: f32,
                            accumulated_l1: f32) {
        let l2_update = accumulated_l2 / *applied_l2;

        *parameter_value *= 1.0 - (1.0 - l2_update) * local_learning_rate;
        *applied_l2 *= l2_update;

        let l1_potential_update = accumulated_l1 - *applied_l1;
        let pre_update_coeff = parameter_value.clone();

        if *parameter_value > 0.0 {
            *parameter_value = max!(0.0,
                                    *parameter_value - l1_potential_update * local_learning_rate);
        } else {
            *parameter_value = min!(0.0,
                                    *parameter_value + l1_potential_update * local_learning_rate);
        }

        let l1_actual_update = (pre_update_coeff - *parameter_value).abs();
        *applied_l1 += l1_actual_update;
    }

    fn update<T: NonzeroIterable>(&mut self, row: T, loss: f32, component_sum: &[f32]) {

        for (feature_idx, feature_value) in (&row).iter_nonzero() {

            // Update coefficients
            let gradsq = self.gradsq.get_mut(feature_idx, 0);
            let local_learning_rate = self.learning_rate / gradsq.sqrt();
            let coefficient_value = self.coefficients.get_mut(feature_idx, 0);

            let applied_l2 = self.applied_l2.get_mut(feature_idx, 0);
            let applied_l1 = self.applied_l1.get_mut(feature_idx, 0);

            let gradient = loss * feature_value;

            *coefficient_value -= local_learning_rate * gradient;
            *gradsq += gradient.powi(2);

            FactorizationMachine::apply_regularization(coefficient_value,
                                                       applied_l2,
                                                       applied_l1,
                                                       local_learning_rate,
                                                       self.accumulated_l2,
                                                       self.accumulated_l1);

            // Update latent factors
            let slice_start = feature_idx * self.num_components;
            let slice_stop = slice_start + self.num_components;

            let mut component_row = &mut self.latent_factors
                .as_mut_slice()[slice_start..slice_stop];
            let mut gradsq_row = &mut self.latent_gradsq.as_mut_slice()[slice_start..slice_stop];
            let mut applied_l2_row = &mut self.latent_applied_l2
                .as_mut_slice()[slice_start..slice_stop];
            let mut applied_l1_row = &mut self.latent_applied_l1
                .as_mut_slice()[slice_start..slice_stop];

            for (component_value, (gradsq, (applied_l2, (applied_l1, component_sum_value)))) in
                component_row.iter_mut().zip(gradsq_row.iter_mut().zip(applied_l2_row.iter_mut()
                .zip(applied_l1_row.iter_mut().zip(component_sum.iter())))) {

                let local_learning_rate = self.learning_rate / gradsq.sqrt();
                let update = loss *
                             ((component_sum_value * feature_value) -
                              (*component_value * feature_value.powi(2)));

                *component_value -= local_learning_rate * update;
                *gradsq += update.powi(2);

                FactorizationMachine::apply_regularization(component_value,
                                                           applied_l2,
                                                           applied_l1,
                                                           local_learning_rate,
                                                           self.accumulated_l2,
                                                           self.accumulated_l1);
            }
        }
    }

    fn accumulate_regularization(&mut self) {
        self.accumulated_l2 *= 1.0 - self.l2_penalty;
        self.accumulated_l1 += self.l1_penalty;
    }

    fn fit_sigmoid<'a, T>(&mut self, X: &'a T, y: &Array) -> Result<(), &'static str>
        where T: IndexableMatrix,
              &'a T: RowIterable
    {

        let mut component_sum = &mut vec![0.0; self.num_components][..];

        for (row, &true_y) in X.iter_rows().zip(y.data().iter()) {
            let y_hat = sigmoid(self.compute_prediction(&row, component_sum));

            let loss = logistic_loss(true_y, y_hat);

            self.update(row, loss, component_sum);

            self.accumulate_regularization();
        }

        self.regularize_all();

        Ok(())
    }

    /// Perform a dummy update pass over all features to force regularization to be applied.
    fn regularize_all(&mut self) {

        if self.l1_penalty == 0.0 && self.l2_penalty == 0.0 {
            return;
        }

        let array = Array::ones(1, self.dim);
        let num_components = self.num_components;

        self.update(&array.view_row(0),
                    0.0,
                    &vec![0.0; num_components.clone()][..]);

        self.accumulated_l2 = 1.0;
        self.accumulated_l1 = 0.0;
    }

    pub fn get_coefficients(&self) -> &Array {
        &self.coefficients
    }

    pub fn get_latent_factors(&self) -> &Array {
        &self.latent_factors
    }
}


impl<'a, T> SupervisedModel<&'a T> for FactorizationMachine
    where &'a T: RowIterable,
          T: IndexableMatrix
{
    fn fit(&mut self, X: &'a T, y: &Array) -> Result<(), &'static str> {

        try!(check_data_dimensionality(self.dim, X));
        try!(check_matched_dimensions(X, y));
        try!(check_valid_labels(y));

        self.fit_sigmoid(X, y)
    }

    fn decision_function(&self, X: &'a T) -> Result<Array, &'static str> {

        try!(check_data_dimensionality(self.dim, X));

        let mut data = Vec::with_capacity(X.rows());

        let mut component_sum = &mut vec![0.0; self.num_components][..];

        for row in X.iter_rows() {
            let prediction = self.compute_prediction(&row, component_sum);
            data.push(sigmoid(prediction));
        }

        Ok(Array::from(data))
    }
}


impl<'a, T> ParallelSupervisedModel<&'a T> for FactorizationMachine
    where &'a T: RowIterable,
          T: IndexableMatrix + Sync
{
    fn fit_parallel(&mut self,
                    X: &'a T,
                    y: &Array,
                    num_threads: usize)
                    -> Result<(), &'static str> {

        try!(check_data_dimensionality(self.dim, X));
        try!(check_matched_dimensions(X, y));
        try!(check_valid_labels(y));

        let rows_per_thread = X.rows() / num_threads + 1;
        let num_components = self.num_components;

        let model_ptr = unsafe { &*(self as *const FactorizationMachine) };

        crossbeam::scope(|scope| {
            for thread_num in 0..num_threads {
                scope.spawn(move || {

                    let start = thread_num * rows_per_thread;
                    let stop = cmp::min((thread_num + 1) * rows_per_thread, X.rows());

                    let mut component_sum = vec![0.0; num_components];

                    let model = unsafe {
                        &mut *(model_ptr as *const FactorizationMachine
                                                as *mut FactorizationMachine)
                    };

                    for (row, &true_y) in X.iter_rows_range(start..stop)
                        .zip(y.data()[start..stop].iter()) {
                        let y_hat = sigmoid(model.compute_prediction(&row, &mut component_sum[..]));
                        let loss = logistic_loss(true_y, y_hat);
                        model.update(row, loss, &mut component_sum[..]);
                        model.accumulate_regularization();
                    }
                });
            }
        });

        self.regularize_all();

        Ok(())
    }
}


#[cfg(test)]
mod tests {
    use rand::{StdRng, SeedableRng};

    use prelude::*;

    use cross_validation::cross_validation::CrossValidation;
    use datasets::iris::load_data;
    use metrics::accuracy_score;
    use multiclass::OneVsRest;

    #[cfg(feature = "all_tests")]
    use datasets::newsgroups;

    use super::*;

    #[test]
    fn basic_updating() {

        let mut model = Hyperparameters::new(2, 2)
            .learning_rate(0.01)
            .l2_penalty(0.0)
            .l1_penalty(0.0)
            .build();

        // Set to zero to allow coefficient update tests
        // to be straightforward
        for elem in model.latent_factors.as_mut_slice().iter_mut() {
            *elem = 0.0;
        }

        let y = Array::ones(1, 1);
        let X = Array::from(&vec![vec![1.0, -0.1]]);

        model.fit(&X, &y).unwrap();

        assert!(model.gradsq.data()[0] > 1.0);
        assert!(model.gradsq.data()[1] > 1.0);

        assert!(model.coefficients.data()[0] == 0.005);
        assert!(model.coefficients.data()[1] == -0.0005);

        model.fit(&X, &y).unwrap();
        println!("model coefficients {:?}", model.coefficients.data());

        assert!(model.coefficients.data()[0] == 0.009460844);
        assert!(model.coefficients.data()[1] == -0.0009981153);
    }

    #[test]
    fn test_basic_l1() {
        let mut model = Hyperparameters::new(2, 2)
            .learning_rate(0.01)
            .l2_penalty(0.0)
            .l1_penalty(100.0)
            .rng(StdRng::from_seed(&[100]))
            .build();

        let y = Array::ones(1, 1);
        let X = Array::from(&vec![vec![1.0, -0.1]]);

        for &elem in model.latent_factors.data() {
            assert!(elem != 0.0);
        }

        model.fit(&X, &y).unwrap();

        assert!(model.gradsq.data()[0] > 1.0);
        assert!(model.gradsq.data()[1] > 1.0);

        // All the coefficients/factors should
        // have been regularized away to zero.
        for &elem in model.coefficients.data() {
            assert!(elem == 0.0);
        }

        for &elem in model.latent_factors.data() {
            assert!(elem == 0.0);
        }
    }

    #[test]
    fn test_iris() {
        let (data, target) = load_data();

        let mut test_accuracy = 0.0;
        let mut train_accuracy = 0.0;

        let no_splits = 10;

        let mut cv = CrossValidation::new(data.rows(), no_splits);
        cv.set_rng(StdRng::from_seed(&[100]));

        for (train_idx, test_idx) in cv {

            let x_train = data.get_rows(&train_idx);
            let x_test = data.get_rows(&test_idx);

            let y_train = target.get_rows(&train_idx);

            let mut model = Hyperparameters::new(data.cols(), 5)
                .learning_rate(0.05)
                .l2_penalty(0.0)
                .rng(StdRng::from_seed(&[100]))
                .one_vs_rest();

            for _ in 0..20 {
                model.fit(&x_train, &y_train).unwrap();
            }

            let y_hat = model.predict(&x_test).unwrap();
            let y_hat_train = model.predict(&x_train).unwrap();

            test_accuracy += accuracy_score(&target.get_rows(&test_idx), &y_hat);
            train_accuracy += accuracy_score(&target.get_rows(&train_idx), &y_hat_train);
        }

        test_accuracy /= no_splits as f32;
        train_accuracy /= no_splits as f32;

        println!("Accuracy {}", test_accuracy);
        println!("Train accuracy {}", train_accuracy);

        assert!(test_accuracy > 0.94);
    }

    #[test]
    fn test_iris_parallel() {
        let (data, target) = load_data();

        // Get a binary target so that the parallelism
        // goes through the FM model and not through the
        // OvR wrapper.
        let (_, target) = OneVsRest::split(&target).next().unwrap();

        let mut test_accuracy = 0.0;
        let mut train_accuracy = 0.0;

        let no_splits = 10;

        let mut cv = CrossValidation::new(data.rows(), no_splits);
        cv.set_rng(StdRng::from_seed(&[100]));

        for (train_idx, test_idx) in cv {

            let x_train = data.get_rows(&train_idx);
            let x_test = data.get_rows(&test_idx);

            let y_train = target.get_rows(&train_idx);

            let mut model = Hyperparameters::new(data.cols(), 5)
                .learning_rate(0.05)
                .l2_penalty(0.0)
                .rng(StdRng::from_seed(&[100]))
                .build();

            for _ in 0..20 {
                model.fit_parallel(&x_train, &y_train, 4).unwrap();
            }

            let y_hat = model.predict(&x_test).unwrap();
            let y_hat_train = model.predict(&x_train).unwrap();

            test_accuracy += accuracy_score(&target.get_rows(&test_idx), &y_hat);
            train_accuracy += accuracy_score(&target.get_rows(&train_idx), &y_hat_train);
        }

        test_accuracy /= no_splits as f32;
        train_accuracy /= no_splits as f32;

        println!("Accuracy {}", test_accuracy);
        println!("Train accuracy {}", train_accuracy);

        assert!(test_accuracy > 0.94);
    }

    #[test]
    #[cfg(feature = "all_tests")]
    fn test_fm_newsgroups() {

        let (X, target) = newsgroups::load_data();

        let no_splits = 2;

        let mut test_accuracy = 0.0;
        let mut train_accuracy = 0.0;

        let mut cv = CrossValidation::new(X.rows(), no_splits);
        cv.set_rng(StdRng::from_seed(&[100]));

        for (train_idx, test_idx) in cv {

            let x_train = X.get_rows(&train_idx);

            let x_test = X.get_rows(&test_idx);
            let y_train = target.get_rows(&train_idx);

            let mut model = Hyperparameters::new(X.cols(), 10)
                .learning_rate(0.005)
                .rng(StdRng::from_seed(&[100]))
                .one_vs_rest();

            for _ in 0..5 {
                model.fit(&x_train, &y_train).unwrap();
                println!("fit");
            }

            let y_hat = model.predict(&x_test).unwrap();
            let y_hat_train = model.predict(&x_train).unwrap();

            test_accuracy += accuracy_score(&target.get_rows(&test_idx), &y_hat);
            train_accuracy += accuracy_score(&target.get_rows(&train_idx), &y_hat_train);
        }

        train_accuracy /= no_splits as f32;
        test_accuracy /= no_splits as f32;

        println!("Train accuracy {}", train_accuracy);
        println!("Test accuracy {}", test_accuracy);

        assert!(train_accuracy > 0.95);
        assert!(test_accuracy > 0.65);
    }
}


#[cfg(feature = "bench")]
#[allow(unused_imports)]
mod bench {
    use rand::{StdRng, SeedableRng};

    use test::Bencher;

    use prelude::*;

    use cross_validation::cross_validation::CrossValidation;
    use datasets::iris::load_data;
    use datasets::newsgroups;
    use metrics::{accuracy_score, roc_auc_score};
    use multiclass::OneVsRest;

    use super::*;

    #[bench]
    fn bench_iris_sparse(b: &mut Bencher) {

        let (data, target) = load_data();

        let sparse_data = SparseRowArray::from(&data);

        let mut model = Hyperparameters::new(data.cols(), 5)
            .learning_rate(0.05)
            .l2_penalty(0.0)
            .rng(StdRng::from_seed(&[100]))
            .one_vs_rest();

        b.iter(|| {
            model.fit(&sparse_data, &target).unwrap();
        });
    }

    #[bench]
    fn bench_fm_newsgroups(b: &mut Bencher) {

        let (X, target) = newsgroups::load_data();
        let (_, target) = OneVsRest::split(&target).next().unwrap();

        let X = X.get_rows(&(..500));
        let target = target.get_rows(&(..500));

        let mut model = Hyperparameters::new(X.cols(), 10)
            .rng(StdRng::from_seed(&[100]))
            .build();

        b.iter(|| {
            model.fit(&X, &target).unwrap();
        });
    }

    #[bench]
    fn bench_fm_newsgroups_parallel(b: &mut Bencher) {

        let (X, target) = newsgroups::load_data();
        let (_, target) = OneVsRest::split(&target).next().unwrap();

        let X = X.get_rows(&(..500));
        let target = target.get_rows(&(..500));

        let mut model = Hyperparameters::new(X.cols(), 10)
            .rng(StdRng::from_seed(&[100]))
            .build();

        b.iter(|| {
            model.fit_parallel(&X, &target, 2).unwrap();
        });
    }
}