Fix svr tests (#222)

This commit is contained in:
Lorenzo
2022-11-03 11:48:40 +00:00
committed by GitHub
parent deac31a2ab
commit 1964424589
6 changed files with 121 additions and 97 deletions
+2 -2
View File
@@ -22,10 +22,10 @@
//! //!
//! <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script> //! <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
//! <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script> //! <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
pub mod svc;
pub mod svr;
/// search parameters /// search parameters
pub mod search; pub mod search;
pub mod svc;
pub mod svr;
use core::fmt::Debug; use core::fmt::Debug;
use std::marker::PhantomData; use std::marker::PhantomData;
-1
View File
@@ -135,7 +135,6 @@
// } // }
// } // }
// #[cfg(test)] // #[cfg(test)]
// mod tests { // mod tests {
// use num::ToPrimitive; // use num::ToPrimitive;
-5
View File
@@ -100,22 +100,17 @@ pub struct SVCParameters<
X: Array2<TX>, X: Array2<TX>,
Y: Array1<TY>, Y: Array1<TY>,
> { > {
#[cfg_attr(feature = "serde", serde(default))]
/// Number of epochs. /// Number of epochs.
pub epoch: usize, pub epoch: usize,
#[cfg_attr(feature = "serde", serde(default))]
/// Regularization parameter. /// Regularization parameter.
pub c: TX, pub c: TX,
#[cfg_attr(feature = "serde", serde(default))]
/// Tolerance for stopping criterion. /// Tolerance for stopping criterion.
pub tol: TX, pub tol: TX,
#[cfg_attr(feature = "serde", serde(skip_deserializing))] #[cfg_attr(feature = "serde", serde(skip_deserializing))]
/// The kernel function. /// The kernel function.
pub kernel: Option<&'a dyn Kernel<'a>>, pub kernel: Option<&'a dyn Kernel<'a>>,
#[cfg_attr(feature = "serde", serde(default))]
/// Unused parameter. /// Unused parameter.
m: PhantomData<(X, Y, TY)>, m: PhantomData<(X, Y, TY)>,
#[cfg_attr(feature = "serde", serde(default))]
/// Controls the pseudo random number generation for shuffling the data for probability estimates /// Controls the pseudo random number generation for shuffling the data for probability estimates
seed: Option<u64>, seed: Option<u64>,
} }
+113 -83
View File
@@ -79,13 +79,13 @@ use crate::api::{PredictorBorrow, SupervisedEstimatorBorrow};
use crate::error::{Failed, FailedError}; use crate::error::{Failed, FailedError};
use crate::linalg::basic::arrays::{Array1, Array2, MutArray}; use crate::linalg::basic::arrays::{Array1, Array2, MutArray};
use crate::numbers::basenum::Number; use crate::numbers::basenum::Number;
use crate::numbers::realnum::RealNumber; use crate::numbers::floatnum::FloatNumber;
use crate::svm::Kernel; use crate::svm::Kernel;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
/// SVR Parameters /// SVR Parameters
pub struct SVRParameters<'a, T: Number + RealNumber> { pub struct SVRParameters<'a, T: Number + FloatNumber + PartialOrd> {
/// Epsilon in the epsilon-SVR model. /// Epsilon in the epsilon-SVR model.
pub eps: T, pub eps: T,
/// Regularization parameter. /// Regularization parameter.
@@ -97,9 +97,12 @@ pub struct SVRParameters<'a, T: Number + RealNumber> {
pub kernel: Option<&'a dyn Kernel<'a>>, pub kernel: Option<&'a dyn Kernel<'a>>,
} }
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
/// Epsilon-Support Vector Regression /// Epsilon-Support Vector Regression
pub struct SVR<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>> { pub struct SVR<'a, T: Number + FloatNumber + PartialOrd, X: Array2<T>, Y: Array1<T>> {
instances: Option<Vec<Vec<f64>>>, instances: Option<Vec<Vec<f64>>>,
#[cfg_attr(feature = "serde", serde(skip_deserializing))]
parameters: Option<&'a SVRParameters<'a, T>>, parameters: Option<&'a SVRParameters<'a, T>>,
w: Option<Vec<T>>, w: Option<Vec<T>>,
b: T, b: T,
@@ -117,7 +120,7 @@ struct SupportVector<T> {
} }
/// Sequential Minimal Optimization algorithm /// Sequential Minimal Optimization algorithm
struct Optimizer<'a, T: Number + RealNumber> { struct Optimizer<'a, T: Number + FloatNumber + PartialOrd> {
tol: T, tol: T,
c: T, c: T,
parameters: Option<&'a SVRParameters<'a, T>>, parameters: Option<&'a SVRParameters<'a, T>>,
@@ -129,13 +132,15 @@ struct Optimizer<'a, T: Number + RealNumber> {
gmaxindex: usize, gmaxindex: usize,
tau: T, tau: T,
sv: Vec<SupportVector<T>>, sv: Vec<SupportVector<T>>,
/// avoid infinite loop if SMO does not converge
max_iterations: usize,
} }
struct Cache<T: Clone> { struct Cache<T: Clone> {
data: Vec<RefCell<Option<Vec<T>>>>, data: Vec<RefCell<Option<Vec<T>>>>,
} }
impl<'a, T: Number + RealNumber> SVRParameters<'a, T> { impl<'a, T: Number + FloatNumber + PartialOrd> SVRParameters<'a, T> {
/// Epsilon in the epsilon-SVR model. /// Epsilon in the epsilon-SVR model.
pub fn with_eps(mut self, eps: T) -> Self { pub fn with_eps(mut self, eps: T) -> Self {
self.eps = eps; self.eps = eps;
@@ -158,7 +163,7 @@ impl<'a, T: Number + RealNumber> SVRParameters<'a, T> {
} }
} }
impl<'a, T: Number + RealNumber> Default for SVRParameters<'a, T> { impl<'a, T: Number + FloatNumber + PartialOrd> Default for SVRParameters<'a, T> {
fn default() -> Self { fn default() -> Self {
SVRParameters { SVRParameters {
eps: T::from_f64(0.1).unwrap(), eps: T::from_f64(0.1).unwrap(),
@@ -169,7 +174,7 @@ impl<'a, T: Number + RealNumber> Default for SVRParameters<'a, T> {
} }
} }
impl<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>> impl<'a, T: Number + FloatNumber + PartialOrd, X: Array2<T>, Y: Array1<T>>
SupervisedEstimatorBorrow<'a, X, Y, SVRParameters<'a, T>> for SVR<'a, T, X, Y> SupervisedEstimatorBorrow<'a, X, Y, SVRParameters<'a, T>> for SVR<'a, T, X, Y>
{ {
fn new() -> Self { fn new() -> Self {
@@ -186,7 +191,7 @@ impl<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>>
} }
} }
impl<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>> PredictorBorrow<'a, X, T> impl<'a, T: Number + FloatNumber + PartialOrd, X: Array2<T>, Y: Array1<T>> PredictorBorrow<'a, X, T>
for SVR<'a, T, X, Y> for SVR<'a, T, X, Y>
{ {
fn predict(&self, x: &'a X) -> Result<Vec<T>, Failed> { fn predict(&self, x: &'a X) -> Result<Vec<T>, Failed> {
@@ -194,7 +199,7 @@ impl<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>> PredictorBorrow<'a,
} }
} }
impl<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>> SVR<'a, T, X, Y> { impl<'a, T: Number + FloatNumber + PartialOrd, X: Array2<T>, Y: Array1<T>> SVR<'a, T, X, Y> {
/// Fits SVR to your data. /// Fits SVR to your data.
/// * `x` - _NxM_ matrix with _N_ observations and _M_ features in each observation. /// * `x` - _NxM_ matrix with _N_ observations and _M_ features in each observation.
/// * `y` - target values /// * `y` - target values
@@ -275,7 +280,9 @@ impl<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>> SVR<'a, T, X, Y> {
} }
} }
impl<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>> PartialEq for SVR<'a, T, X, Y> { impl<'a, T: Number + FloatNumber + PartialOrd, X: Array2<T>, Y: Array1<T>> PartialEq
for SVR<'a, T, X, Y>
{
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
if (self.b - other.b).abs() > T::epsilon() * T::two() if (self.b - other.b).abs() > T::epsilon() * T::two()
|| self.w.as_ref().unwrap().len() != other.w.as_ref().unwrap().len() || self.w.as_ref().unwrap().len() != other.w.as_ref().unwrap().len()
@@ -301,7 +308,7 @@ impl<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>> PartialEq for SVR<'
} }
} }
impl<T: Number + RealNumber> SupportVector<T> { impl<T: Number + FloatNumber + PartialOrd> SupportVector<T> {
fn new(i: usize, x: Vec<f64>, y: T, eps: T, k: f64) -> SupportVector<T> { fn new(i: usize, x: Vec<f64>, y: T, eps: T, k: f64) -> SupportVector<T> {
SupportVector { SupportVector {
index: i, index: i,
@@ -313,7 +320,7 @@ impl<T: Number + RealNumber> SupportVector<T> {
} }
} }
impl<'a, T: Number + RealNumber> Optimizer<'a, T> { impl<'a, T: Number + FloatNumber + PartialOrd> Optimizer<'a, T> {
fn new<X: Array2<T>, Y: Array1<T>>( fn new<X: Array2<T>, Y: Array1<T>>(
x: &'a X, x: &'a X,
y: &'a Y, y: &'a Y,
@@ -355,12 +362,13 @@ impl<'a, T: Number + RealNumber> Optimizer<'a, T> {
gmaxindex: 0, gmaxindex: 0,
tau: T::from_f64(1e-12).unwrap(), tau: T::from_f64(1e-12).unwrap(),
sv: support_vectors, sv: support_vectors,
max_iterations: 49999,
} }
} }
fn find_min_max_gradient(&mut self) { fn find_min_max_gradient(&mut self) {
// self.gmin = <T as Bounded>::max_value()(); self.gmin = <T as Bounded>::max_value();
// self.gmax = <T as Bounded>::min_value(); self.gmax = <T as Bounded>::min_value();
for i in 0..self.sv.len() { for i in 0..self.sv.len() {
let v = &self.sv[i]; let v = &self.sv[i];
@@ -398,10 +406,13 @@ impl<'a, T: Number + RealNumber> Optimizer<'a, T> {
/// * hyperplane parameters: w and b (computed with T) /// * hyperplane parameters: w and b (computed with T)
fn smo(mut self) -> (Vec<Vec<f64>>, Vec<T>, T) { fn smo(mut self) -> (Vec<Vec<f64>>, Vec<T>, T) {
let cache: Cache<f64> = Cache::new(self.sv.len()); let cache: Cache<f64> = Cache::new(self.sv.len());
let mut n_iteration = 0usize;
self.find_min_max_gradient(); self.find_min_max_gradient();
while self.gmax - self.gmin > self.tol { while self.gmax - self.gmin > self.tol {
if n_iteration > self.max_iterations {
break;
}
let v1 = self.svmax; let v1 = self.svmax;
let i = self.gmaxindex; let i = self.gmaxindex;
let old_alpha_i = self.sv[v1].alpha[i]; let old_alpha_i = self.sv[v1].alpha[i];
@@ -546,6 +557,7 @@ impl<'a, T: Number + RealNumber> Optimizer<'a, T> {
} }
self.find_min_max_gradient(); self.find_min_max_gradient();
n_iteration += 1;
} }
let b = -(self.gmax + self.gmin) / T::two(); let b = -(self.gmax + self.gmin) / T::two();
@@ -581,11 +593,11 @@ impl<T: Clone> Cache<T> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
// use super::*; use super::*;
// use crate::linalg::basic::matrix::DenseMatrix; use crate::linalg::basic::matrix::DenseMatrix;
// use crate::metrics::mean_squared_error; use crate::metrics::mean_squared_error;
// #[cfg(feature = "serde")] #[cfg(feature = "serde")]
// use crate::svm::*; use crate::svm::Kernels;
// #[test] // #[test]
// fn search_parameters() { // fn search_parameters() {
@@ -606,78 +618,96 @@ mod tests {
// } // }
//TODO: had to disable this test as it runs for too long //TODO: had to disable this test as it runs for too long
// #[cfg_attr(all(target_arch = "wasm32", not(target_os = "wasi")), wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
// #[test] all(target_arch = "wasm32", not(target_os = "wasi")),
// fn svr_fit_predict() { wasm_bindgen_test::wasm_bindgen_test
// let x = DenseMatrix::from_2d_array(&[ )]
// &[234.289, 235.6, 159.0, 107.608, 1947., 60.323], #[test]
// &[259.426, 232.5, 145.6, 108.632, 1948., 61.122], fn svr_fit_predict() {
// &[258.054, 368.2, 161.6, 109.773, 1949., 60.171], let x = DenseMatrix::from_2d_array(&[
// &[284.599, 335.1, 165.0, 110.929, 1950., 61.187], &[234.289, 235.6, 159.0, 107.608, 1947., 60.323],
// &[328.975, 209.9, 309.9, 112.075, 1951., 63.221], &[259.426, 232.5, 145.6, 108.632, 1948., 61.122],
// &[346.999, 193.2, 359.4, 113.270, 1952., 63.639], &[258.054, 368.2, 161.6, 109.773, 1949., 60.171],
// &[365.385, 187.0, 354.7, 115.094, 1953., 64.989], &[284.599, 335.1, 165.0, 110.929, 1950., 61.187],
// &[363.112, 357.8, 335.0, 116.219, 1954., 63.761], &[328.975, 209.9, 309.9, 112.075, 1951., 63.221],
// &[397.469, 290.4, 304.8, 117.388, 1955., 66.019], &[346.999, 193.2, 359.4, 113.270, 1952., 63.639],
// &[419.180, 282.2, 285.7, 118.734, 1956., 67.857], &[365.385, 187.0, 354.7, 115.094, 1953., 64.989],
// &[442.769, 293.6, 279.8, 120.445, 1957., 68.169], &[363.112, 357.8, 335.0, 116.219, 1954., 63.761],
// &[444.546, 468.1, 263.7, 121.950, 1958., 66.513], &[397.469, 290.4, 304.8, 117.388, 1955., 66.019],
// &[482.704, 381.3, 255.2, 123.366, 1959., 68.655], &[419.180, 282.2, 285.7, 118.734, 1956., 67.857],
// &[502.601, 393.1, 251.4, 125.368, 1960., 69.564], &[442.769, 293.6, 279.8, 120.445, 1957., 68.169],
// &[518.173, 480.6, 257.2, 127.852, 1961., 69.331], &[444.546, 468.1, 263.7, 121.950, 1958., 66.513],
// &[554.894, 400.7, 282.7, 130.081, 1962., 70.551], &[482.704, 381.3, 255.2, 123.366, 1959., 68.655],
// ]); &[502.601, 393.1, 251.4, 125.368, 1960., 69.564],
&[518.173, 480.6, 257.2, 127.852, 1961., 69.331],
&[554.894, 400.7, 282.7, 130.081, 1962., 70.551],
]);
// let y: Vec<f64> = vec![ let y: Vec<f64> = vec![
// 83.0, 88.5, 88.2, 89.5, 96.2, 98.1, 99.0, 100.0, 101.2, 104.6, 108.4, 110.8, 112.6, 83.0, 88.5, 88.2, 89.5, 96.2, 98.1, 99.0, 100.0, 101.2, 104.6, 108.4, 110.8, 112.6,
// 114.2, 115.7, 116.9, 114.2, 115.7, 116.9,
// ]; ];
// let knl = Kernels::linear(); let knl = Kernels::linear();
// let y_hat = SVR::fit(&x, &y, &SVRParameters::default() let y_hat = SVR::fit(
// .with_eps(2.0) &x,
// .with_c(10.0) &y,
// .with_kernel(&knl) &SVRParameters::default()
// ) .with_eps(2.0)
// .and_then(|lr| lr.predict(&x)) .with_c(10.0)
// .unwrap(); .with_kernel(&knl),
)
.and_then(|lr| lr.predict(&x))
.unwrap();
// assert!(mean_squared_error(&y_hat, &y) < 2.5); let t = mean_squared_error(&y_hat, &y);
// } println!("{:?}", t);
assert!(t < 2.5);
}
// #[cfg_attr(all(target_arch = "wasm32", not(target_os = "wasi")), wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
// #[test] all(target_arch = "wasm32", not(target_os = "wasi")),
// #[cfg(feature = "serde")] wasm_bindgen_test::wasm_bindgen_test
// fn svr_serde() { )]
// let x = DenseMatrix::from_2d_array(&[ #[test]
// &[234.289, 235.6, 159.0, 107.608, 1947., 60.323], #[cfg(feature = "serde")]
// &[259.426, 232.5, 145.6, 108.632, 1948., 61.122], fn svr_serde() {
// &[258.054, 368.2, 161.6, 109.773, 1949., 60.171], let x = DenseMatrix::from_2d_array(&[
// &[284.599, 335.1, 165.0, 110.929, 1950., 61.187], &[234.289, 235.6, 159.0, 107.608, 1947., 60.323],
// &[328.975, 209.9, 309.9, 112.075, 1951., 63.221], &[259.426, 232.5, 145.6, 108.632, 1948., 61.122],
// &[346.999, 193.2, 359.4, 113.270, 1952., 63.639], &[258.054, 368.2, 161.6, 109.773, 1949., 60.171],
// &[365.385, 187.0, 354.7, 115.094, 1953., 64.989], &[284.599, 335.1, 165.0, 110.929, 1950., 61.187],
// &[363.112, 357.8, 335.0, 116.219, 1954., 63.761], &[328.975, 209.9, 309.9, 112.075, 1951., 63.221],
// &[397.469, 290.4, 304.8, 117.388, 1955., 66.019], &[346.999, 193.2, 359.4, 113.270, 1952., 63.639],
// &[419.180, 282.2, 285.7, 118.734, 1956., 67.857], &[365.385, 187.0, 354.7, 115.094, 1953., 64.989],
// &[442.769, 293.6, 279.8, 120.445, 1957., 68.169], &[363.112, 357.8, 335.0, 116.219, 1954., 63.761],
// &[444.546, 468.1, 263.7, 121.950, 1958., 66.513], &[397.469, 290.4, 304.8, 117.388, 1955., 66.019],
// &[482.704, 381.3, 255.2, 123.366, 1959., 68.655], &[419.180, 282.2, 285.7, 118.734, 1956., 67.857],
// &[502.601, 393.1, 251.4, 125.368, 1960., 69.564], &[442.769, 293.6, 279.8, 120.445, 1957., 68.169],
// &[518.173, 480.6, 257.2, 127.852, 1961., 69.331], &[444.546, 468.1, 263.7, 121.950, 1958., 66.513],
// &[554.894, 400.7, 282.7, 130.081, 1962., 70.551], &[482.704, 381.3, 255.2, 123.366, 1959., 68.655],
// ]); &[502.601, 393.1, 251.4, 125.368, 1960., 69.564],
&[518.173, 480.6, 257.2, 127.852, 1961., 69.331],
&[554.894, 400.7, 282.7, 130.081, 1962., 70.551],
]);
// let y: Vec<f64> = vec![ let y: Vec<f64> = vec![
// 83.0, 88.5, 88.2, 89.5, 96.2, 98.1, 99.0, 100.0, 101.2, 104.6, 108.4, 110.8, 112.6, 83.0, 88.5, 88.2, 89.5, 96.2, 98.1, 99.0, 100.0, 101.2, 104.6, 108.4, 110.8, 112.6,
// 114.2, 115.7, 116.9, 114.2, 115.7, 116.9,
// ]; ];
// let svr = SVR::fit(&x, &y, Default::default()).unwrap(); let knl = Kernels::rbf().with_gamma(0.7);
let params = SVRParameters::default().with_kernel(&knl);
let svr = SVR::fit(&x, &y, &params).unwrap();
let serialized = &serde_json::to_string(&svr).unwrap();
println!("{}", &serialized);
// let deserialized_svr: SVR<f64, DenseMatrix<f64>, LinearKernel> = // let deserialized_svr: SVR<f64, DenseMatrix<f64>, LinearKernel> =
// serde_json::from_str(&serde_json::to_string(&svr).unwrap()).unwrap(); // serde_json::from_str(&serde_json::to_string(&svr).unwrap()).unwrap();
// assert_eq!(svr, deserialized_svr); // assert_eq!(svr, deserialized_svr);
// } }
} }