Skip to content

Commit

Permalink
Add forward to layer
Browse files Browse the repository at this point in the history
  • Loading branch information
opixelum committed Mar 11, 2024
1 parent 80beac8 commit c35fe5a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
23 changes: 11 additions & 12 deletions rust/src/ai/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ impl Layer {
}
}

// 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()]);
// }
pub fn forward(&mut self, inputs: Vec<f64>) -> Vec<f64> {
let mut outputs = vec![];

// neuron.forward(inputs.clone())
// })
// .collect()
//}
for neuron in self.neurons.iter_mut() {
if neuron.weights.is_none() {
neuron.weights = Some(vec![1.0; inputs.len()])
}
outputs.push((self.activation)(neuron.forward(inputs.clone())))
}

outputs
}
}
12 changes: 9 additions & 3 deletions rust/tests/ai/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ use opixelib::ai::neuron::Neuron;

#[test]
fn test_new() {
let perceptron = Layer::new(1, binary_step);
assert_eq!(
perceptron,
Layer::new(1, binary_step),
Layer {
neurons: vec![Neuron {
weights: None,
bias: 0.0
}],
activation: binary_step
}
)
);
}

#[test]
fn test_forward() {
let mut perceptron = Layer::new(1, binary_step);
assert_eq!(perceptron.forward(vec![1.0, 2.0]), vec![1.0]);
assert_eq!(perceptron.forward(vec![-1.0, -2.0]), vec![0.0])
}

0 comments on commit c35fe5a

Please sign in to comment.