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
use mathru::algebra::linear::{Vector, Matrix};
use mathru::algebra::abstr::Sign;
use crate::SupervisedLearn;
use rand::Rng;
#[derive(Debug)]
pub struct SVM
{
alpha: Option<Vector<f64>>,
lambda: f64,
iter: usize
}
impl SVM
{
pub fn new(lambda: f64, iter: usize) -> SVM
{
SVM
{
alpha: None,
lambda: lambda,
iter: iter,
}
}
pub fn get_parameter(self: &Self) -> Vector<f64>
{
if self.alpha == None
{
panic!("Model is not trained");
}
return self.alpha.clone().unwrap();
}
}
impl SupervisedLearn<Matrix<f64>, Vector<f64>> for SVM
{
fn predict(self: &Self, input: &Matrix<f64>) -> Vector<f64>
{
let (m, n) : (usize, usize) = input.dim();
if self.alpha == None
{
panic!("Model is not trained");
}
let ones: Matrix<f64> = Matrix::<f64>::ones(m, n + 1);
let full_input: Matrix<f64> = ones.set_slice(input, 0, 0);
return (full_input * self.alpha.clone().unwrap()).sgn();
}
fn train(self: &mut Self, input: &Matrix<f64>, target: &Vector<f64>)
{
let mut rng = rand::thread_rng();
let (m, n): (usize, usize) = input.dim();
let ones: Matrix<f64> = Matrix::<f64>::ones(m, n + 1);
let full_input: Matrix<f64> = ones.set_slice( input, 0, 0);
let mut alpha: Vector<f64> = Vector::zero(n + 1);
for t in 0..self.iter
{
let i_t: usize = rng.gen_range(0, m);
let n_t: f64 = 1.0f64 / (self.lambda * (t as f64 + 1.0f64));
let x_i_t: Vector<f64> = full_input.get_row(&i_t).transpose();
let y_i_t: f64 = *target.get(&i_t);
if y_i_t * alpha.dotp(&x_i_t) < 1.0
{
alpha = alpha * (1.0f64 - n_t * self.lambda) + x_i_t * y_i_t * n_t;
}
else
{
if y_i_t * alpha.dotp(&x_i_t) >= 1.0
{
alpha = alpha * (1.0 - n_t * self.lambda);
}
}
}
self.alpha = Some(alpha);
}
}
impl SVM
{
}