diff --git a/rust/src/ai/layer.rs b/rust/src/ai/layer.rs index 0c0596d..5baed70 100644 --- a/rust/src/ai/layer.rs +++ b/rust/src/ai/layer.rs @@ -22,17 +22,16 @@ impl Layer { } } - // fn forward(&self, inputs: Vec) -> Vec { - // 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) -> Vec { + 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 + } } diff --git a/rust/tests/ai/layer.rs b/rust/tests/ai/layer.rs index f4f93c5..0004b66 100644 --- a/rust/tests/ai/layer.rs +++ b/rust/tests/ai/layer.rs @@ -4,9 +4,8 @@ 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, @@ -14,5 +13,12 @@ fn test_new() { }], 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]) }