Skip to content
This repository was archived by the owner on Jul 16, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions benches/examples/cross_validation.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use rusty_machine::linalg::{Matrix, BaseMatrix};
use rusty_machine::learning::{LearningResult, SupModel};
use rusty_machine::analysis::score::row_accuracy;
use rusty_machine::analysis::cross_validation::k_fold_validate;
use rand::{thread_rng, Rng};
use test::{Bencher, black_box};
use rusty_machine::analysis::cross_validation::k_fold_validate;
use rusty_machine::analysis::score::row_accuracy;
use rusty_machine::learning::{LearningResult, SupModel};
use rusty_machine::linalg::{BaseMatrix, Matrix};
use test::{black_box, Bencher};

fn generate_data(rows: usize, cols: usize) -> Matrix<f64> {
let mut rng = thread_rng();
Expand All @@ -22,14 +22,14 @@ fn generate_data(rows: usize, cols: usize) -> Matrix<f64> {
/// matrices when trained. Its prediction for each row is the
/// sum of the row's elements plus the precalculated training sum.
struct DummyModel {
sum: f64
sum: f64,
}

impl SupModel<Matrix<f64>, Matrix<f64>> for DummyModel {
fn predict(&self, inputs: &Matrix<f64>) -> LearningResult<Matrix<f64>> {
let predictions: Vec<f64> = inputs
.row_iter()
.map(|row| { self.sum + sum(row.iter()) })
.map(|row| self.sum + sum(row.iter()))
.collect();
Ok(Matrix::new(inputs.rows(), 1, predictions))
}
Expand All @@ -40,12 +40,12 @@ impl SupModel<Matrix<f64>, Matrix<f64>> for DummyModel {
}
}

fn sum<'a, I: Iterator<Item=&'a f64>>(x: I) -> f64 {
fn sum<'a, I: Iterator<Item = &'a f64>>(x: I) -> f64 {
x.fold(0f64, |acc, x| acc + x)
}

macro_rules! bench {
($name:ident: $params:expr) => {
($name:ident : $params:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
let (rows, cols, k) = $params;
Expand All @@ -54,12 +54,16 @@ macro_rules! bench {

b.iter(|| {
let mut model = DummyModel { sum: 0f64 };
let _ = black_box(
k_fold_validate(&mut model, &inputs, &targets, k, row_accuracy)
);
let _ = black_box(k_fold_validate(
&mut model,
&inputs,
&targets,
k,
row_accuracy,
));
});
}
}
};
}

bench!(bench_10_10_3: (10, 10, 3));
Expand Down
22 changes: 11 additions & 11 deletions benches/examples/k_means.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use rusty_machine::linalg::{Matrix, BaseMatrix};
use rusty_machine::learning::k_means::KMeansClassifier;
use rusty_machine::learning::UnSupModel;
use rusty_machine::linalg::{BaseMatrix, Matrix};

use rand::thread_rng;
use rand::distributions::IndependentSample;
use rand::distributions::normal::Normal;
use rand::distributions::IndependentSample;
use rand::thread_rng;

use test::{Bencher, black_box};
use test::{black_box, Bencher};

fn generate_data(centroids: &Matrix<f64>, points_per_centroid: usize, noise: f64) -> Matrix<f64> {
assert!(centroids.cols() > 0, "Centroids cannot be empty.");
assert!(centroids.rows() > 0, "Centroids cannot be empty.");
assert!(noise >= 0f64, "Noise must be non-negative.");
let mut raw_cluster_data = Vec::with_capacity(centroids.rows() * points_per_centroid *
centroids.cols());
let mut raw_cluster_data =
Vec::with_capacity(centroids.rows() * points_per_centroid * centroids.cols());

let mut rng = thread_rng();
let normal_rv = Normal::new(0f64, noise);
Expand All @@ -32,14 +32,15 @@ fn generate_data(centroids: &Matrix<f64>, points_per_centroid: usize, noise: f64
}
}

Matrix::new(centroids.rows() * points_per_centroid,
centroids.cols(),
raw_cluster_data)
Matrix::new(
centroids.rows() * points_per_centroid,
centroids.cols(),
raw_cluster_data,
)
}

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

const SAMPLES_PER_CENTROID: usize = 2000;
// Choose two cluster centers, at (-0.5, -0.5) and (0, 0.5).
let centroids = Matrix::new(2, 2, vec![-0.5, -0.5, 0.0, 0.5]);
Expand All @@ -55,7 +56,6 @@ fn k_means_train(b: &mut Bencher) {

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

const SAMPLES_PER_CENTROID: usize = 2000;
// Choose two cluster centers, at (-0.5, -0.5) and (0, 0.5).
let centroids = Matrix::new(2, 2, vec![-0.5, -0.5, 0.0, 0.5]);
Expand Down
24 changes: 12 additions & 12 deletions benches/examples/nnet.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use test::{Bencher, black_box};
use test::{black_box, Bencher};

use rand::{random, Closed01};
use std::vec::Vec;

use rusty_machine::learning::nnet::{NeuralNet, BCECriterion};
use rusty_machine::learning::toolkit::regularization::Regularization;
use rusty_machine::learning::toolkit::activ_fn::Sigmoid;
use rusty_machine::learning::nnet::{BCECriterion, NeuralNet};
use rusty_machine::learning::optim::grad_desc::StochasticGD;
use rusty_machine::learning::toolkit::activ_fn::Sigmoid;
use rusty_machine::learning::toolkit::regularization::Regularization;

use rusty_machine::linalg::Matrix;
use rusty_machine::learning::SupModel;
use rusty_machine::linalg::Matrix;

fn generate_data() -> (Matrix<f64>, Matrix<f64>, Matrix<f64>) {
const THRESHOLD: f64 = 0.7;
Expand All @@ -34,12 +34,7 @@ fn generate_data() -> (Matrix<f64>, Matrix<f64>, Matrix<f64>) {
let inputs = Matrix::new(SAMPLES, 2, input_data);
let targets = Matrix::new(SAMPLES, 1, label_data);

let test_cases = vec![
0.0, 0.0,
0.0, 1.0,
1.0, 1.0,
1.0, 0.0,
];
let test_cases = vec![0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0];
let test_inputs = Matrix::new(test_cases.len() / 2, 2, test_cases);

(inputs, targets, test_inputs)
Expand All @@ -52,7 +47,12 @@ fn nnet_and_gate_train(b: &mut Bencher) {
let criterion = BCECriterion::new(Regularization::L2(0.));

b.iter(|| {
let mut model = black_box(NeuralNet::mlp(layers, criterion, StochasticGD::default(), Sigmoid));
let mut model = black_box(NeuralNet::mlp(
layers,
criterion,
StochasticGD::default(),
Sigmoid,
));
let _ = black_box(model.train(&inputs, &targets).unwrap());
})
}
Expand Down
23 changes: 12 additions & 11 deletions benches/examples/svm.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
use rusty_machine::learning::svm::SVM;
// Necessary for the training trait.
use rusty_machine::learning::SupModel;
use rusty_machine::learning::toolkit::kernel::HyperTan;
use rusty_machine::learning::SupModel;

use rusty_machine::linalg::Matrix;
use rusty_machine::linalg::Vector;

use test::{Bencher, black_box};
use test::{black_box, Bencher};

fn generate_data() -> (Matrix<f64>, Vector<f64>) {
// Training data
let inputs = Matrix::new(11, 1, vec![
-0.1, -2., -9., -101., -666.7,
0., 0.1, 1., 11., 99., 456.7
]);
let targets = Vector::new(vec![
-1., -1., -1., -1., -1.,
1., 1., 1., 1., 1., 1.
]);
let inputs = Matrix::new(
11,
1,
vec![-0.1, -2., -9., -101., -666.7, 0., 0.1, 1., 11., 99., 456.7],
);
let targets = Vector::new(vec![-1., -1., -1., -1., -1., 1., 1., 1., 1., 1., 1.]);

(inputs, targets)
}
Expand All @@ -43,7 +41,10 @@ fn svm_sign_learner_train(b: &mut Bencher) {
fn svm_sign_learner_predict(b: &mut Bencher) {
let (inputs, targets) = generate_data();

let test_data = (-1000..1000).filter(|&x| x % 100 == 0).map(|x| x as f64).collect::<Vec<_>>();
let test_data = (-1000..1000)
.filter(|&x| x % 100 == 0)
.map(|x| x as f64)
.collect::<Vec<_>>();
let test_inputs = Matrix::new(test_data.len(), 1, test_data);
let mut svm_mod = SVM::new(HyperTan::new(100., 0.), 0.3);
let _ = svm_mod.train(&inputs, &targets);
Expand Down
2 changes: 1 addition & 1 deletion benches/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![feature(test)]

extern crate rand;
extern crate rusty_machine;
extern crate test;
extern crate rand;

mod examples {
mod cross_validation;
Expand Down
31 changes: 16 additions & 15 deletions examples/k-means_generating_cluster.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
extern crate rusty_machine;
extern crate rand;
extern crate rusty_machine;

use rusty_machine::linalg::{Matrix, BaseMatrix};
use rusty_machine::learning::k_means::KMeansClassifier;
use rusty_machine::learning::UnSupModel;
use rusty_machine::linalg::{BaseMatrix, Matrix};

use rand::thread_rng;
use rand::distributions::IndependentSample;
use rand::distributions::normal::Normal;
use rand::distributions::IndependentSample;
use rand::thread_rng;

fn generate_data(centroids: &Matrix<f64>,
points_per_centroid: usize,
noise: f64)
-> Matrix<f64> {
fn generate_data(centroids: &Matrix<f64>, points_per_centroid: usize, noise: f64) -> Matrix<f64> {
assert!(centroids.cols() > 0, "Centroids cannot be empty.");
assert!(centroids.rows() > 0, "Centroids cannot be empty.");
assert!(noise >= 0f64, "Noise must be non-negative.");
let mut raw_cluster_data = Vec::with_capacity(centroids.rows() * points_per_centroid *
centroids.cols());
let mut raw_cluster_data =
Vec::with_capacity(centroids.rows() * points_per_centroid * centroids.cols());

let mut rng = thread_rng();
let normal_rv = Normal::new(0f64, noise);
Expand All @@ -36,18 +33,22 @@ fn generate_data(centroids: &Matrix<f64>,
}
}

Matrix::new(centroids.rows() * points_per_centroid,
centroids.cols(),
raw_cluster_data)
Matrix::new(
centroids.rows() * points_per_centroid,
centroids.cols(),
raw_cluster_data,
)
}

fn main() {
println!("K-Means clustering example:");

const SAMPLES_PER_CENTROID: usize = 2000;

println!("Generating {0} samples from each centroids:",
SAMPLES_PER_CENTROID);
println!(
"Generating {0} samples from each centroids:",
SAMPLES_PER_CENTROID
);
// Choose two cluster centers, at (-0.5, -0.5) and (0, 0.5).
let centroids = Matrix::new(2, 2, vec![-0.5, -0.5, 0.0, 0.5]);
println!("{}", centroids);
Expand Down
Loading