Skip to content

Commit

Permalink
Add new method to Layer struct
Browse files Browse the repository at this point in the history
  • Loading branch information
opixelum committed Mar 8, 2024
1 parent 38f600e commit 620f8f3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 32 deletions.
31 changes: 25 additions & 6 deletions rust/src/ai/layer.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
use crate::ai::activation::Activation;
use crate::ai::neuron::Neuron;

#[derive(Debug, PartialEq)]
pub struct Layer {
pub neurons: Vec<Neuron>,
pub activation: Activation,
}

impl Layer {
pub fn forward(&self) -> Vec<f64> {
let mut outputs = Vec::new();
pub fn new(number_of_neurons: u128, activation: Activation) -> Self {
let neurons: Vec<Neuron> = (0..number_of_neurons)
.map(|_| Neuron {
weights: None,
bias: 0.0,
})
.collect();

for neuron in self.neurons.iter() {
outputs.push((self.activation)(neuron.forward()))
Layer {
neurons,
activation,
}

outputs
}

// fn forward(&self, inputs: Vec<f64>) -> Vec<f64> {
// self.neurons
// .iter()
// .enumerate()
// .map(|(i, neuron)| {
// if neuron.weights.is_none() {
// self.neurons[i].weights = Some(vec![0.5; inputs.len()]);
// }

// neuron.forward(inputs.clone())
// })
// .collect()
//}
}
13 changes: 9 additions & 4 deletions rust/src/ai/neuron.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use crate::math::vector::dot;

#[derive(Debug, PartialEq)]
pub struct Neuron {
pub inputs: Vec<f64>,
pub weights: Vec<f64>,
pub weights: Option<Vec<f64>>,
pub bias: f64,
}

impl Neuron {
pub fn forward(&self) -> f64 {
dot(&self.inputs, &self.weights).unwrap() + self.bias
fn forward(&self, inputs: Vec<f64>) -> f64 {
dot(
inputs,
self.weights.clone().expect("Weights are not initialized."),
)
.unwrap()
+ self.bias
}
}
25 changes: 12 additions & 13 deletions rust/tests/ai/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ use opixelib::ai::layer::*;
use opixelib::ai::neuron::Neuron;

#[test]
fn test_layer_forward() {
let neuron: Neuron = Neuron {
inputs: vec![1.0, 2.0, 3.0],
weights: vec![4.0, 5.0, 6.0],
bias: 10.0,
};

let perceptron = Layer {
neurons: vec![neuron],
activation: binary_step,
};

assert_eq!(perceptron.forward(), vec![1.0])
fn test_new() {
let perceptron = Layer::new(1, binary_step);
assert_eq!(
perceptron,
Layer {
neurons: vec![Neuron {
weights: None,
bias: 0.0
}],
activation: binary_step
}
)
}
18 changes: 9 additions & 9 deletions rust/tests/ai/neuron.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use opixelib::ai::neuron::*;

#[test]
fn test_neuron_forward() {
let neuron: Neuron = Neuron {
inputs: vec![1.0, 2.0, 3.0],
weights: vec![4.0, 5.0, 6.0],
bias: 10.0,
};
assert_eq!(neuron.forward(), 42.0)
}
//#[test]
//fn test_neuron_forward() {
// let neuron: Neuron = Neuron {
// inputs: vec![1.0, 2.0, 3.0],
// weights: vec![4.0, 5.0, 6.0],
// bias: 10.0,
// };
// assert_eq!(neuron.forward(), 42.0)
//}

0 comments on commit 620f8f3

Please sign in to comment.