Skip to main content

only_brain/
perceptron.rs

1use crate::{
2    activation_functions::{get_activation_function, ActivationFunction},
3    BVector,
4};
5use std::fmt;
6
7/// Perceptron
8///
9/// This is a single perceptron implementation. It contains weights, bias and an activation function.
10/// You can use this struct and its methods to create, manipulate and even implement your ways to
11/// train a perceptron (find the weights).
12/// # Example
13/// ```
14/// use only_brain::Perceptron;
15/// use only_brain::ActivationFunction;
16/// use only_brain::bvector;
17///
18/// fn main() {
19///     let mut perceptron = Perceptron::<2>::new(ActivationFunction::Sigmoid);
20///
21///     perceptron.set_weights(bvector![0.5, -0.2]);
22///     perceptron.set_bias(0.3);
23///     println!("{}", perceptron);
24///
25///     let inputs = bvector![0.6, 0.4];
26///     let output = perceptron.feed_forward(&inputs);
27///     println!("Output: {}", output);
28/// }
29/// ```
30#[derive(Debug, Clone, PartialEq)]
31pub struct Perceptron<const N: usize> {
32    weights: BVector<f64, N>,
33    bias: f64,
34    activation_function: ActivationFunction,
35}
36
37impl<const N: usize> Perceptron<N> {
38    pub fn new(activation_function: ActivationFunction) -> Self {
39        let weights = BVector::<f64, N>::from_element(0.0);
40        let bias = 0.0;
41
42        Self {
43            weights,
44            bias,
45            activation_function,
46        }
47    }
48
49    pub fn set_weights(&mut self, weights: BVector<f64, N>) {
50        self.weights = weights;
51    }
52
53    pub fn set_bias(&mut self, bias: f64) {
54        self.bias = bias;
55    }
56
57    pub fn feed_forward(&self, inputs: &BVector<f64, N>) -> f64 {
58        let weighted_sum = self.weights.dot(inputs) + self.bias;
59        get_activation_function(self.activation_function)(weighted_sum)
60    }
61}
62
63impl<const N: usize> fmt::Display for Perceptron<N> {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        writeln!(f, "Perceptron:")?;
66        writeln!(f, "Activation Function: {:?}", self.activation_function)?;
67        writeln!(f)?;
68        writeln!(f, "Inputs Size: {}", N)?;
69        writeln!(f, "Weights: {:?}", self.weights)?;
70        writeln!(f, "Bias: {}", self.bias)?;
71        writeln!(f)?;
72
73        Ok(())
74    }
75}