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
use super::activations::Activations;
use super::layer::layers::Layer;
use super::input::Input;
use super::serialize::ser_layer::SerializedLayer;
pub struct Network {
pub layers: Vec<Box<dyn Layer>>,
}
impl Network{
pub fn new() -> Network {
Network { layers: vec![] }
}
pub fn predict(&mut self, input: &dyn Input) -> Vec<f32>{
let in_box: Box<dyn Input> = input.to_box();
self.feed_forward(&in_box)
}
///Travels through a neural network's abstracted Layers and returns the resultant vector at the
///end
///
///# Arguments
///* `input_obj` - Any structure that implements the Input trait to act as an input to the data
///# Returns
///A vector at the end of the feed forward
///
///# Examples
///
///```
///let new_net = Network::New();
///new_new.add_layer(LayerTypes::Dense(2, Activations::SIGMOID, 0.01));
///new_new.add_layer(LayerTypes::Dense(3, Activations::SIGMOID, 0.01));
///new_new.add_layer(LayerTypes::Dense(4, Activations::SIGMOID, 0.01));
///new_new.add_layer(LayerTypes::Dense(2, Activations::TANH, 0.01));
///new_new.add_layer(LayerTypes::Dense(1, Activations::SIGMOID, 0.01));
///
///new_net.compile()
///
///let res = new_net.feed_forward(vec![1.0, 0.54]);
///```
fn feed_forward(&mut self, input_obj: &Box<dyn Input>) -> Vec<f32> {
let mut data_at: Box<dyn Input> = Box::new(input_obj.to_param());
for i in 0..self.layers.len(){
data_at = self.layers[i].forward(&data_at);
}
data_at.to_param().to_owned()
}
pub fn deserialize_unda_fmt_string(format_string: String, activation: Activations) -> Network {
let mut net: Network = Network::new();
let parse_triton = format_string.split("#");
for layer in parse_triton {
let new_layer: Box<dyn Layer> = SerializedLayer::from_string(layer.to_string()).from(activation);
net.layers.push(new_layer);
}
net
}
}