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
use std::slice::Iter;
use activation::Activation;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Layer{
pub inputs: usize,
pub outputs: usize,
pub activation: Activation
}
impl Layer {
fn new(inputs: usize, outputs: usize, activation: Activation) -> Self {
Layer{
inputs: inputs,
outputs: outputs,
activation: activation
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TopologyBuilder {
last : usize,
layers: Vec<Layer>
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Topology {
layers: Vec<Layer>
}
impl Topology {
pub fn input(size: usize) -> TopologyBuilder {
assert!(size >= 1, "cannot define a zero-sized input layer");
TopologyBuilder{
last : size,
layers: vec![]
}
}
pub fn len_input(&self) -> usize {
self.layers
.first()
.expect("a finished disciple must have a valid first layer!")
.inputs
}
pub fn len_output(&self) -> usize {
self.layers
.last()
.expect("a finished disciple must have a valid last layer!")
.outputs
}
pub fn iter_layers(&self) -> Iter<Layer> {
self.layers.iter()
}
}
impl TopologyBuilder {
fn push_layer(&mut self, layer_size: usize, act: Activation) {
assert!(layer_size >= 1, "cannot define a zero-sized hidden layer");
self.layers.push(Layer::new(self.last, layer_size, act));
self.last = layer_size;
}
pub fn layer(mut self, layer_size: usize, act: Activation) -> TopologyBuilder {
self.push_layer(layer_size, act);
self
}
pub fn layers(mut self, layers: &[(usize, Activation)]) -> TopologyBuilder {
for &layer in layers {
self.push_layer(layer.0, layer.1);
}
self
}
pub fn output(mut self, layer_size: usize, act: Activation) -> Topology {
assert!(layer_size >= 1, "cannot define a zero-sized output layer");
self.push_layer(layer_size, act);
Topology {
layers: self.layers,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn construction() {
use self::Activation::{Logistic, Identity, ReLU, Tanh};
let dis = Topology::input(2)
.layer(5, Logistic)
.layers(&[
(10, Identity),
(10, ReLU)
])
.output(5, Tanh);
let mut it = dis.iter_layers()
.map(|&size| size);
assert_eq!(it.next(), Some(Layer::new(2, 5, Logistic)));
assert_eq!(it.next(), Some(Layer::new(5, 10, Identity)));
assert_eq!(it.next(), Some(Layer::new(10, 10, ReLU)));
assert_eq!(it.next(), Some(Layer::new(10, 5, Tanh)));
}
}