Skip to content

Commit 83d28de

Browse files
Volodymyr OrlovVolodymyr Orlov
authored andcommitted
fix: svr, post-review changes
1 parent 20e58a8 commit 83d28de

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/svm/svr.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@
4141
//!
4242
//! let y_hat = svr.predict(&x).unwrap();
4343
//! ```
44+
//!
45+
//! ## References:
46+
//!
47+
//! * ["Support Vector Machines" Kowalczyk A., 2017](https://www.svm-tutorial.com/2017/10/support-vector-machines-succinctly-released/)
48+
//! * ["A Fast Algorithm for Training Support Vector Machines", Platt J.C., 1998](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/tr-98-14.pdf)
49+
//! * ["Working Set Selection Using Second Order Information for Training Support Vector Machines", Rong-En Fan et al., 2005](https://www.jmlr.org/papers/volume6/fan05a/fan05a.pdf)
50+
//! * ["A tutorial on support vector regression", SMOLA A.J., Scholkopf B., 2003](https://alex.smola.org/papers/2004/SmoSch04.pdf)
51+
4452
use std::cell::{Ref, RefCell};
4553
use std::fmt::Debug;
4654

@@ -87,6 +95,7 @@ struct SupportVector<T: RealNumber, V: BaseVector<T>> {
8795
k: T,
8896
}
8997

98+
/// Sequential Minimal Optimization algorithm
9099
struct Optimizer<'a, T: RealNumber, M: Matrix<T>, K: Kernel<T, M::RowVector>> {
91100
tol: T,
92101
c: T,
@@ -135,7 +144,7 @@ impl<T: RealNumber, M: Matrix<T>, K: Kernel<T, M::RowVector>> SVR<T, M, K> {
135144
)));
136145
}
137146

138-
let optimizer = Optimizer::optimize(x, y, &kernel, &parameters);
147+
let optimizer = Optimizer::new(x, y, &kernel, &parameters);
139148

140149
let (support_vectors, weight, b) = optimizer.smo();
141150

@@ -209,7 +218,7 @@ impl<T: RealNumber, V: BaseVector<T>> SupportVector<T, V> {
209218
}
210219

211220
impl<'a, T: RealNumber, M: Matrix<T>, K: Kernel<T, M::RowVector>> Optimizer<'a, T, M, K> {
212-
fn optimize(
221+
fn new(
213222
x: &M,
214223
y: &M::RowVector,
215224
kernel: &'a K,
@@ -244,7 +253,7 @@ impl<'a, T: RealNumber, M: Matrix<T>, K: Kernel<T, M::RowVector>> Optimizer<'a,
244253
}
245254
}
246255

247-
fn minmax(&mut self) {
256+
fn find_min_max_gradient(&mut self) {
248257
self.gmin = T::max_value();
249258
self.gmax = T::min_value();
250259

@@ -278,10 +287,14 @@ impl<'a, T: RealNumber, M: Matrix<T>, K: Kernel<T, M::RowVector>> Optimizer<'a,
278287
}
279288
}
280289

290+
/// Solvs the quadratic programming (QP) problem that arises during the training of support-vector machines (SVM) algorithm.
291+
/// Returns:
292+
/// * support vectors
293+
/// * hyperplane parameters: w and b
281294
fn smo(mut self) -> (Vec<M::RowVector>, Vec<T>, T) {
282295
let cache: Cache<T> = Cache::new(self.sv.len());
283296

284-
self.minmax();
297+
self.find_min_max_gradient();
285298

286299
while self.gmax - self.gmin > self.tol {
287300
let v1 = self.svmax;
@@ -417,22 +430,22 @@ impl<'a, T: RealNumber, M: Matrix<T>, K: Kernel<T, M::RowVector>> Optimizer<'a,
417430
v.grad[1] += si * k1[v.index] * delta_alpha_i + sj * k2[v.index] * delta_alpha_j;
418431
}
419432

420-
self.minmax();
433+
self.find_min_max_gradient();
421434
}
422435

423436
let b = -(self.gmax + self.gmin) / T::two();
424437

425-
let mut result: Vec<M::RowVector> = Vec::new();
426-
let mut alpha: Vec<T> = Vec::new();
438+
let mut support_vectors: Vec<M::RowVector> = Vec::new();
439+
let mut w: Vec<T> = Vec::new();
427440

428441
for v in self.sv {
429442
if v.alpha[0] != v.alpha[1] {
430-
result.push(v.x);
431-
alpha.push(v.alpha[1] - v.alpha[0]);
443+
support_vectors.push(v.x);
444+
w.push(v.alpha[1] - v.alpha[0]);
432445
}
433446
}
434447

435-
(result, alpha, b)
448+
(support_vectors, w, b)
436449
}
437450
}
438451

@@ -497,8 +510,6 @@ mod tests {
497510
.and_then(|lr| lr.predict(&x))
498511
.unwrap();
499512

500-
println!("{:?}", y_hat);
501-
502513
assert!(mean_squared_error(&y_hat, &y) < 2.5);
503514
}
504515

0 commit comments

Comments
 (0)