HyperTan

Struct HyperTan 

Source
pub struct HyperTan {
    pub alpha: f64,
    pub c: f64,
}
Expand description

The Hyperbolic Tangent Kernel.

ker(x,y) = tanh(αxTy + c)

Fields§

§alpha: f64

The scaling of the inner product.

§c: f64

The constant to add to the inner product.

Implementations§

Source§

impl HyperTan

Source

pub fn new(alpha: f64, c: f64) -> HyperTan

Constructs a new Hyperbolic Tangent Kernel.

§Examples
use rusty_machine::learning::toolkit::kernel;
use rusty_machine::learning::toolkit::kernel::Kernel;

// Construct a kernel with alpha = 1, c = 2.
let ker = kernel::HyperTan::new(1.0, 2.0);

println!("{0}", ker.kernel(&[1.,2.,3.], &[3.,4.,5.]));
Examples found in repository?
examples/svm-sign_learner.rs (line 32)
17fn main() {
18    println!("Sign learner sample:");
19
20    println!("Training...");
21    // Training data
22    let inputs = Matrix::new(11, 1, vec![
23                             -0.1, -2., -9., -101., -666.7,
24                             0., 0.1, 1., 11., 99., 456.7
25                             ]);
26    let targets = Vector::new(vec![
27                              -1., -1., -1., -1., -1.,
28                              1., 1., 1., 1., 1., 1.
29                              ]);
30
31    // Trainee
32    let mut svm_mod = SVM::new(HyperTan::new(100., 0.), 0.3);
33    // Our train function returns a Result<(), E>
34    svm_mod.train(&inputs, &targets).unwrap();
35
36    println!("Evaluation...");
37    let mut hits = 0;
38    let mut misses = 0;
39    // Evaluation
40    //   Note: We could pass all input values at once to the `predict` method!
41    //         Here, we use a loop just to count and print logs.
42    for n in (-1000..1000).filter(|&x| x % 100 == 0) {
43        let nf = n as f64;
44        let input = Matrix::new(1, 1, vec![nf]);
45        let out = svm_mod.predict(&input).unwrap();
46        let res = if out[0] * nf > 0. {
47            hits += 1;
48            true
49        } else if nf == 0. {
50            hits += 1;
51            true
52        } else {
53            misses += 1;
54            false
55        };
56
57        println!("{} -> {}: {}", Matrix::data(&input)[0], out[0], res);
58    }
59
60    println!("Performance report:");
61    println!("Hits: {}, Misses: {}", hits, misses);
62    let hits_f = hits as f64;
63    let total = (hits + misses) as f64;
64    println!("Accuracy: {}", (hits_f / total) * 100.);
65}

Trait Implementations§

Source§

impl Clone for HyperTan

Source§

fn clone(&self) -> HyperTan

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HyperTan

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for HyperTan

Constructs a default Hyperbolic Tangent Kernel.

The defaults are:

  • alpha = 1
  • c = 0
Source§

fn default() -> HyperTan

Returns the “default value” for a type. Read more
Source§

impl Kernel for HyperTan

Source§

fn kernel(&self, x1: &[f64], x2: &[f64]) -> f64

The kernel function. Read more
Source§

impl Copy for HyperTan

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.