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
use matrix::Matrix;
use matrix::MatrixTrait;
use activation::Activation;

/// Represents a neural layer with its weights
pub struct NeuralLayer {
    pub activation: Box<Activation>,
    inputs: usize,
    neurons: usize,
    weights: Matrix,
    biases: Matrix,
}

impl NeuralLayer {
    pub fn new<T: 'static>(neurons: usize, inputs: usize, activation: T) -> NeuralLayer
    where
        T: Activation,
    {
        NeuralLayer {
            activation: Box::new(activation),
            inputs: inputs,
            neurons: neurons,
            weights: Matrix::random(neurons, inputs),
            biases: Matrix::random(neurons, 1),
        }
    }

    pub fn neurons(&self) -> usize {
        self.neurons
    }

    pub fn inputs(&self) -> usize {
        self.inputs
    }

    pub fn biases(&self) -> &Matrix {
        &self.biases
    }

    // weights without bias node
    pub fn weights(&self) -> &Matrix {
        &self.weights
    }

    // weights with bias node
    pub fn weights_with_bias(&self) -> &Matrix {
        &self.weights
    }

    pub fn set_weights(&mut self, weights: Matrix) {
        // because no one can change the dimension of the matrix
        assert!(weights.rows() == self.weights.rows());
        assert!(weights.cols() == self.weights.cols());

        self.weights = weights;
    }

    pub fn set_biases(&mut self, weights: Matrix) {
        // because no one can change the dimension of the matrix
        assert!(weights.rows() == self.biases.rows());
        assert!(weights.cols() == self.biases.cols());

        self.biases = weights;
    }
}

#[cfg(test)]
mod tests {
    use nl::NeuralLayer;
    use matrix::MatrixTrait;
    use activation::Sigmoid;

    #[test]
    fn new_neural_layer() {
        let test = NeuralLayer::new(4, 3, Sigmoid::new());
        assert_eq!(3usize, test.inputs());
        assert_eq!(4usize, test.neurons());

        assert_eq!(4usize, test.weights().rows());
        assert_eq!(3usize, test.weights().cols());
    }

    /*
    #[test]
    fn neural_layer_bias() {
        let test = NeuralLayer::new(4, 3, Sigmoid::new());

        assert_eq!(4usize, test.weights_with_bias().rows());
        // 4 because of bias node
        assert_eq!(4usize, test.weights_with_bias().cols());
    }
    */
}