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
use rand::StdRng;
use rustc_serialize::*;
use prelude::*;
#[derive(Clone)]
pub struct EncodableRng {
pub rng: StdRng,
}
impl EncodableRng {
pub fn new() -> EncodableRng {
EncodableRng { rng: StdRng::new().unwrap() }
}
}
impl Default for EncodableRng {
fn default() -> Self {
EncodableRng::new()
}
}
impl Encodable for EncodableRng {
fn encode<S: Encoder>(&self, _: &mut S) -> Result<(), S::Error> {
Ok(())
}
}
impl Decodable for EncodableRng {
fn decode<D: Decoder>(_: &mut D) -> Result<Self, D::Error> {
Ok((EncodableRng::new()))
}
}
pub fn check_valid_labels(y: &Array) -> Result<(), &'static str> {
if y.cols() != 1 {
return Err("Target array has more than one column.");
}
if y.data().iter().all(|&x| x == 0.0 || x == 1.0) {
Ok(())
} else {
Err("Invalid labels: target data is not either 0.0 or 1.0")
}
}
pub fn check_data_dimensionality<T: IndexableMatrix>(model_dim: usize,
X: &T)
-> Result<(), &'static str> {
if X.cols() == model_dim {
Ok(())
} else {
Err("Model input and model dimensionality differ.")
}
}
pub fn check_matched_dimensions<T: IndexableMatrix>(X: &T, y: &Array) -> Result<(), &'static str> {
if X.rows() == y.rows() {
Ok(())
} else {
Err("Data matrix and target array do not have the same number of rows")
}
}