libsvm 0.1.0

High level Rust bindings for libsvm
Documentation
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! SVM model types.

use crate::{
    consts::{
        DEFAULT_CACHE_SIZE, DEFAULT_COEF0, DEFAULT_COST, DEFAULT_DEGREE, DEFAULT_MODEL_EPSILON,
        DEFAULT_NU, DEFAULT_PROBABILITY_ESTIMATES, DEFAULT_SHRINKING, DEFAULT_TERMINATION_EPSILON,
    },
    data::SvmNodes,
    error::Error,
    init::{KernelInit, ModelInit, SvmInit},
    state::{Trained, Untrained},
};
use num_derive::FromPrimitive;
use std::{
    collections::HashMap,
    convert::{TryFrom, TryInto},
    ffi::{CStr, CString},
    num::NonZeroUsize,
    os::raw::c_int,
    ptr::NonNull,
    str::FromStr,
};

/// The SVM model.
#[derive(Debug)]
pub struct Svm<State> {
    pub(crate) state: State,
}

impl Svm<Untrained> {
    pub fn kind(&self) -> SvmKind {
        num::FromPrimitive::from_usize(self.state.params.svm_type as usize).unwrap()
    }

    /// Trains the model on given dataset.
    pub fn fit<X, Y>(&self, x: X, y: Y) -> Result<Svm<Trained>, Error>
    where
        X: TryInto<SvmNodes, Error = Error>,
        Y: AsRef<[f64]>,
    {
        let Svm {
            state:
                Untrained {
                    gamma_opt,
                    mut params,
                    weight_labels,
                    weights,
                },
        } = self;

        // transoform x
        let x_nodes = x.try_into()?;
        let x_slice = x_nodes
            .end_indexes
            .iter()
            .cloned()
            .scan(0, |from, to| {
                let prev_from = *from;
                *from = to;
                Some((prev_from, to))
            })
            .map(|(from, to)| {
                x_nodes.nodes.get(from..to).unwrap().as_ptr() as *mut libsvm_sys::svm_node
            })
            .collect::<Vec<_>>();
        let x_ptr = x_slice.as_ptr() as *mut *mut libsvm_sys::svm_node;

        // transoform y
        let y_slice = y.as_ref();
        let y_ptr = y_slice.as_ptr() as *mut f64;

        if x_nodes.end_indexes.len() != y_slice.len() {
            return Err(Error::InvalidData {
                reason: "the size of data and label does not match".into(),
            });
        }

        // costruct problem
        let problem = libsvm_sys::svm_problem {
            l: x_nodes.end_indexes.len() as c_int,
            x: x_ptr,
            y: y_ptr,
        };

        // compute gamma if necessary
        let gamma = gamma_opt.unwrap_or_else(|| (x_nodes.n_features as f64).recip());

        // update raw params
        // we store pointers late to avoid move after saving pointers
        params.gamma = gamma;
        params.weight_label = weight_labels.as_ptr() as *mut _;
        params.weight = weights.as_ptr() as *mut _;

        unsafe {
            let ptr = libsvm_sys::svm_check_parameter(&problem, &params);

            if ptr != std::ptr::null() {
                let err_msg = CStr::from_ptr(ptr)
                    .to_str()
                    .map_err(|err| Error::InternalError {
                        reason: format!("failed to decode error mesage: {:?}", err),
                    })?;
                return Err(Error::InvalidHyperparameter {
                    reason: format!("svm_check_parameter() failed: {}", err_msg),
                });
            }
        }

        // train model
        let model_ptr = unsafe {
            NonNull::new(libsvm_sys::svm_train(&problem, &params)).ok_or_else(|| {
                Error::InternalError {
                    reason: "svm_train() returns null pointer".into(),
                }
            })?
        };

        Ok(Svm {
            state: Trained {
                model_ptr,
                nodes_opt: Some(x_nodes),
            },
        })
    }

    /// Runs cross validation on given data.
    pub fn cross_validate<X, Y>(&self, x: X, y: Y, n_folds: NonZeroUsize) -> Result<Vec<f64>, Error>
    where
        X: TryInto<SvmNodes, Error = Error>,
        Y: AsRef<[f64]>,
    {
        let Svm {
            state:
                Untrained {
                    gamma_opt,
                    mut params,
                    weight_labels,
                    weights,
                },
        } = self;

        // transoform x
        let x_nodes = x.try_into()?;
        let x_slice = x_nodes
            .end_indexes
            .iter()
            .cloned()
            .scan(0, |from, to| {
                let prev_from = *from;
                *from = to;
                Some((prev_from, to))
            })
            .map(|(from, to)| {
                x_nodes.nodes.get(from..to).unwrap().as_ptr() as *mut libsvm_sys::svm_node
            })
            .collect::<Vec<_>>();
        let x_ptr = x_slice.as_ptr() as *mut *mut libsvm_sys::svm_node;

        // transoform y
        let y_slice = y.as_ref();
        let y_ptr = y_slice.as_ptr() as *mut f64;

        let n_records = {
            if x_nodes.end_indexes.len() != y_slice.len() {
                return Err(Error::InvalidData {
                    reason: "the size of data and label does not match".into(),
                });
            }
            y_slice.len()
        };

        // costruct problem
        let problem = libsvm_sys::svm_problem {
            l: x_nodes.end_indexes.len() as c_int,
            x: x_ptr,
            y: y_ptr,
        };

        // compute gamma if necessary
        let gamma = gamma_opt.unwrap_or_else(|| (x_nodes.n_features as f64).recip());

        // update raw params
        // we store pointers late to avoid move after saving pointers
        params.gamma = gamma;
        params.weight_label = weight_labels.as_ptr() as *mut _;
        params.weight = weights.as_ptr() as *mut _;

        // run cross validation
        let target = unsafe {
            let mut target = std::iter::repeat(0.0).take(n_records).collect::<Vec<_>>();
            libsvm_sys::svm_cross_validation(
                &problem,
                &params,
                n_folds.get() as c_int,
                target.as_mut_ptr(),
            );
            target
        };

        Ok(target)
    }
}

impl Svm<Trained> {
    /// Gets the type of the model.
    pub fn kind(&self) -> SvmKind {
        num::FromPrimitive::from_usize(unsafe {
            libsvm_sys::svm_get_svm_type(self.state.model_ptr.as_ptr()) as usize
        })
        .unwrap()
    }

    /// Gets the number of output classes.
    pub fn nr_classes(&self) -> usize {
        unsafe { libsvm_sys::svm_get_nr_class(self.state.model_ptr.as_ptr()) as usize }
    }

    /// Gets the label indexes.
    pub fn labels(&self) -> Vec<usize> {
        unsafe {
            let mut labels = std::iter::repeat(0)
                .take(self.nr_classes())
                .collect::<Vec<_>>();
            libsvm_sys::svm_get_labels(self.state.model_ptr.as_ptr(), labels.as_mut_ptr());
            labels
                .into_iter()
                .map(|label| label as usize)
                .collect::<Vec<_>>()
        }
    }

    /// Gets the support vector indexes in training data.
    pub fn get_sv_indexes(&self) -> Vec<usize> {
        let n_sv = unsafe { libsvm_sys::svm_get_nr_sv(self.state.model_ptr.as_ptr()) as usize };
        let indexes = unsafe {
            let mut indexes = std::iter::repeat(0).take(n_sv).collect::<Vec<_>>();
            libsvm_sys::svm_get_sv_indices(self.state.model_ptr.as_ptr(), indexes.as_mut_ptr());
            indexes
                .into_iter()
                .map(|index| index as usize)
                .collect::<Vec<_>>()
        };
        indexes
    }

    /// Predicts the output for given data.
    pub fn predict<X>(&self, x: X) -> Result<Vec<f64>, Error>
    where
        X: TryInto<SvmNodes, Error = Error>,
    {
        let x_nodes = x.try_into()?;
        let n_features = self.nr_classes();
        if x_nodes.n_features > n_features {
            return Err(Error::InvalidData {
                reason: format!(
                    "too many features in input data, expect {} features but get {}",
                    n_features, x_nodes.n_features
                ),
            });
        }

        let predictions = {
            x_nodes
                .end_indexes
                .iter()
                .cloned()
                .scan(0, |from, to| {
                    let prev_from = *from;
                    *from = to;
                    Some((prev_from, to))
                })
                .map(|(from, to)| {
                    x_nodes.nodes.get(from..to).unwrap().as_ptr() as *mut libsvm_sys::svm_node
                })
                .map(|node_ptr| unsafe {
                    libsvm_sys::svm_predict(self.state.model_ptr.as_ptr(), node_ptr)
                })
                .collect::<Vec<_>>()
        };

        Ok(predictions)
    }
}

impl FromStr for Svm<Trained> {
    type Err = Error;

    fn from_str(desc: &str) -> Result<Self, Self::Err> {
        let model_ptr = unsafe {
            let cstring = CString::new(desc.bytes().chain(vec![0]).collect::<Vec<_>>()).unwrap();
            let raw = cstring.into_raw();
            let model_ptr = libsvm_sys::svm_load_model(raw);
            CString::from_raw(raw);
            NonNull::new(model_ptr).ok_or_else(|| Error::InternalError {
                reason: "svm_load_model() returns null pointer".into(),
            })?
        };

        Ok(Svm {
            state: Trained {
                model_ptr,
                nodes_opt: None,
            },
        })
    }
}

/// The model type.
#[derive(Clone, Copy, Debug, FromPrimitive)]
pub enum SvmKind {
    CSvc = libsvm_sys::C_SVC as isize,
    NuSvc = libsvm_sys::NU_SVC as isize,
    NuSvr = libsvm_sys::NU_SVR as isize,
    OneClass = libsvm_sys::ONE_CLASS as isize,
    EpsilonSvr = libsvm_sys::EPSILON_SVR as isize,
}

#[derive(Debug, Clone)]
pub(crate) struct SvmParams {
    pub model: ModelParams,
    pub kernel: KernelParams,
    pub cache_size: usize,
    pub probability_estimates: bool,
    pub shrinking: bool,
    pub termination_eps: f64,
    pub label_weights: HashMap<usize, f64>,
}

impl TryFrom<&SvmInit> for SvmParams {
    type Error = Error;

    fn try_from(init: &SvmInit) -> Result<Self, Self::Error> {
        let SvmInit {
            model,
            kernel,
            cache_size,
            probability_estimates,
            shrinking,
            termination_eps,
            label_weights,
        } = init;

        let svm = Self {
            model: model.as_ref().map(From::from).unwrap_or(Default::default()),
            kernel: kernel
                .as_ref()
                .map(From::from)
                .unwrap_or(Default::default()),
            cache_size: cache_size.unwrap_or(DEFAULT_CACHE_SIZE),
            probability_estimates: probability_estimates.unwrap_or(DEFAULT_PROBABILITY_ESTIMATES),
            shrinking: shrinking.unwrap_or(DEFAULT_SHRINKING),
            termination_eps: termination_eps.unwrap_or(DEFAULT_TERMINATION_EPSILON),
            label_weights: label_weights.clone().unwrap_or(HashMap::new()),
        };

        if svm.cache_size <= 0 {
            return Err(Error::InvalidHyperparameter {
                reason: "cache_size must be positive".into(),
            });
        }

        if svm.termination_eps <= 0.0 {
            return Err(Error::InvalidHyperparameter {
                reason: "termination_eps must be positive".into(),
            });
        }

        Ok(svm)
    }
}

#[derive(Clone, Debug)]
pub(crate) enum ModelParams {
    CSvc { cost: f64 },
    NuSvc { nu: f64 },
    NuSvr { nu: f64 },
    OneClass { nu: f64 },
    EpsilonSvr { epsilon: f64 },
}

impl Default for ModelParams {
    fn default() -> Self {
        ModelParams::CSvc { cost: DEFAULT_COST }
    }
}

impl From<&ModelInit> for ModelParams {
    fn from(init: &ModelInit) -> Self {
        match init {
            ModelInit::CSvc { cost } => Self::CSvc {
                cost: cost.unwrap_or(DEFAULT_COST),
            },
            ModelInit::NuSvc { nu } => ModelParams::NuSvc {
                nu: nu.unwrap_or(DEFAULT_NU),
            },
            ModelInit::NuSvr { nu } => ModelParams::NuSvr {
                nu: nu.unwrap_or(DEFAULT_NU),
            },
            ModelInit::OneClass { nu } => ModelParams::OneClass {
                nu: nu.unwrap_or(DEFAULT_NU),
            },
            ModelInit::EpsilonSvr { epsilon } => ModelParams::EpsilonSvr {
                epsilon: epsilon.unwrap_or(DEFAULT_MODEL_EPSILON),
            },
        }
    }
}

#[derive(Clone, Debug)]
pub(crate) enum KernelParams {
    Linear,
    Polynomial {
        gamma: Option<f64>,
        coef0: f64,
        degree: usize,
    },
    Rbf {
        gamma: Option<f64>,
    },
    Sigmoid {
        gamma: Option<f64>,
        coef0: f64,
    },
    // Precomputed,
}

impl Default for KernelParams {
    fn default() -> Self {
        KernelParams::Rbf { gamma: None }
    }
}

impl From<&KernelInit> for KernelParams {
    fn from(init: &KernelInit) -> Self {
        match *init {
            KernelInit::Linear => KernelParams::Linear,
            KernelInit::Polynomial {
                gamma,
                coef0,
                degree,
            } => KernelParams::Polynomial {
                gamma,
                coef0: coef0.unwrap_or(DEFAULT_COEF0),
                degree: degree.unwrap_or(DEFAULT_DEGREE),
            },
            KernelInit::Rbf { gamma } => KernelParams::Rbf { gamma },
            KernelInit::Sigmoid { gamma, coef0 } => KernelParams::Sigmoid {
                gamma,
                coef0: coef0.unwrap_or(DEFAULT_COEF0),
            },
        }
    }
}