Struct SupportVectorMachine

Source
pub struct SupportVectorMachine { /* private fields */ }

Implementations§

Source§

impl SupportVectorMachine

Source

pub fn new( class_num: usize, feature_num: usize, lr: f32, bsz: Option<usize>, lambda: f32, ) -> Self

Support Vector Machine for binary classification

only supports linear kernel, using SGD to optimize hinge loss

update = lambda * (regularization(w) or marginal width or so called structure loss) + 1/batch * Hinge Loss

  • bsz: default will be len(dataset)
Examples found in repository?
examples/05_svm.rs (line 22)
6fn main() -> std::io::Result<()> {
7    let datas = vec![
8        vec![1.0, 3.0],
9        vec![2.0, 3.0],
10        vec![1.0, 2.0],
11        vec![4.0, 0.0],
12        vec![3.0, 0.0],
13        vec![3.0, -1.0],
14        vec![3.0, 0.5],
15    ];
16    let labels = vec![0usize, 0, 0, 1, 1, 1, 1];
17    let mut label_map = HashMap::new();
18    label_map.insert(0, "up left".to_string());
19    label_map.insert(1, "down right".to_string());
20    let dataset = Dataset::new(datas, labels, Some(label_map));
21
22    let mut model = SupportVectorMachine::new(2, dataset.feature_len(), 1e-1, None, 1.0);
23    model.train(dataset.clone(), 1000, SVMLoss::Hinge, true, true)?;
24
25    let (correct, acc) = evaluate(&dataset, &model);
26    println!("correct {correct}/{} acc {acc}", dataset.len());
27
28    Ok(())
29}
Source

pub fn train( &mut self, dataset: Dataset<usize>, epoch: usize, loss: SVMLoss, early_stop: bool, verbose: bool, ) -> Result<()>

train the SVM model

  • verbose: whether show the training info
Examples found in repository?
examples/05_svm.rs (line 23)
6fn main() -> std::io::Result<()> {
7    let datas = vec![
8        vec![1.0, 3.0],
9        vec![2.0, 3.0],
10        vec![1.0, 2.0],
11        vec![4.0, 0.0],
12        vec![3.0, 0.0],
13        vec![3.0, -1.0],
14        vec![3.0, 0.5],
15    ];
16    let labels = vec![0usize, 0, 0, 1, 1, 1, 1];
17    let mut label_map = HashMap::new();
18    label_map.insert(0, "up left".to_string());
19    label_map.insert(1, "down right".to_string());
20    let dataset = Dataset::new(datas, labels, Some(label_map));
21
22    let mut model = SupportVectorMachine::new(2, dataset.feature_len(), 1e-1, None, 1.0);
23    model.train(dataset.clone(), 1000, SVMLoss::Hinge, true, true)?;
24
25    let (correct, acc) = evaluate(&dataset, &model);
26    println!("correct {correct}/{} acc {acc}", dataset.len());
27
28    Ok(())
29}

Trait Implementations§

Source§

impl Model<usize> for SupportVectorMachine

Source§

fn predict(&self, feature: &Vec<f32>) -> usize

predict one sample Read more
Source§

fn predict_with_batch(&self, features: &NdArray) -> Vec<T>

predict a batch Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.