neural_network_rs/neural_network/layer/
mod.rs

1use ndarray::Array2;
2use ndarray_rand::{rand_distr::Normal, RandomExt};
3
4use super::activation_function::ActivationFunction;
5
6pub struct Layer<'a> {
7    pub weights: Array2<f64>,
8    pub biases: Array2<f64>,
9    pub activation: &'a ActivationFunction,
10}
11
12impl Layer<'_> {
13    pub fn new(input_size: usize, output_size: usize, activation: &ActivationFunction) -> Layer {
14        let weights = Array2::random((input_size, output_size), Normal::new(0.0, 1.0).unwrap())
15            / (input_size as f64).sqrt();
16        let biases = Array2::random((1, output_size), Normal::new(0.0, 0.1).unwrap());
17
18        Layer {
19            weights,
20            biases,
21            activation,
22        }
23    }
24
25    // Predicts the output of the layer given an input
26    pub fn predict(&self, input: &Array2<f64>) -> Array2<f64> {
27        self.activation.function(&self.forward(input))
28    }
29
30    // Calculates the weighted sum of the input
31    pub fn forward(&self, input: &Array2<f64>) -> Array2<f64> {
32        input.dot(&self.weights) + &self.biases
33    }
34}