From db0438731474cee98f39add3ecb6c1fc2d15b5af Mon Sep 17 00:00:00 2001 From: LeLeLeLeto Date: Tue, 31 Dec 2024 02:08:13 +0100 Subject: [PATCH] Fix wrong matrix sizes in layer constructor --- layer.h | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/layer.h b/layer.h index f0c99e7..529cc71 100644 --- a/layer.h +++ b/layer.h @@ -1,3 +1,6 @@ +#ifndef LAYER_H_ +#define LAYER_H_ + #include "matrices.h" #include #include @@ -15,8 +18,6 @@ class Layer { static inline float Sigmoid(float); static inline float SigmoidPrime(float); - inline Layer(int); // Number of neurons - inline void Forward(); // Forward Pass with sigmoid inline void Forward(float (*activation)(float)); // Forward Pass with custom activation function @@ -29,21 +30,22 @@ class Layer { }; Layer::Layer(){ - + } Layer::Layer(int input_size, int size){ this->input = Matrix(input_size, 1); - this->weights = Matrix(input_size, size); - this->weights.Randomize(0.0F, 1.0F); + // Every neuron has a weight for every input + this->weights = Matrix(size, input_size); + this->weights.Randomize(-1.0F, 1.0F); - // Z, A and B are the same size as A - this->raw_output = this->input; - this->activated_output = this->input; + this->raw_output = Matrix(size, 1); + this->activated_output = this->raw_output; - this->biases = this->input; - this->biases.Randomize(0.0F, 1.0F); + // One bias per neuron + this->biases = Matrix(size, 1); + this->biases.Randomize(-1.0F, 1.0F); } void Layer::Feed(Matrix a){ @@ -60,9 +62,9 @@ float Layer::SigmoidPrime(float x){ } void Layer::Forward(float (*activation)(float)){ - // Multiply inputs by weights + // Multiply weight matrix by input matrix // W x I + B = Z - this->raw_output = this->input.Multiply(&this->weights).Add(&this->biases); + this->raw_output = this->weights.Multiply(&this->input).Add(&this->biases); // Now through activation function // A = F(Z) @@ -71,4 +73,6 @@ void Layer::Forward(float (*activation)(float)){ void Layer::Forward(){ this->Forward(&Layer::Sigmoid); -} \ No newline at end of file +} + +#endif \ No newline at end of file