Skip to content

Commit

Permalink
added feedforward
Browse files Browse the repository at this point in the history
  • Loading branch information
Dpbm committed Feb 3, 2024
1 parent ccceecf commit dc1521d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
10 changes: 9 additions & 1 deletion machine/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,13 @@ namespace Layers {

double (*Layer::get_activation_function())(double){
return this->activation;
}
}

void Layer::set_values(Matrix* values){
if(values->get_width() != this->size || values->get_height() != 1)
throw invalid_argument("The new values matrix must have the size dimensions as defined!");

delete this->values;
this->values = values;
}
}
1 change: 1 addition & 0 deletions machine/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Layers{
void activate_neurons();
bool is_input();
double (*get_activation_function())(double);
void set_values(Matrix* values);
private:
unsigned int size;
bool input = false;
Expand Down
17 changes: 17 additions & 0 deletions machine/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,21 @@ namespace Machine {
delete weight;
}

void NN::feedforward(){
for(unsigned int layer = 0; layer < this->total_layers-1; layer++){
Layer* actual_layer = this->get_layer(layer);
Layer* next_layer = this->get_layer(layer+1);

Matrix* layer_values = actual_layer->get_values();
Matrix* layer_weights = this->get_weight(layer)->get_weights();


//TODO: remove the & and .transpose() when (dot product returns Matrix*)
Matrix dot_product_result = (*layer_values) * (*layer_weights);
dot_product_result.transpose();
next_layer->set_values(&dot_product_result);

next_layer->activate_neurons();
}
}
}
1 change: 1 addition & 0 deletions machine/machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Machine {
unsigned int get_total_weights();
void save_weights(string filename);
~NN();
void feedforward();

private:
vector<Layer*>* layers = new vector<Layer*>;
Expand Down
3 changes: 2 additions & 1 deletion matrix/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ namespace Matrices{
Matrices::Matrix Matrix::operator *(const Matrices::Matrix& another_matrix){
if(this->width != another_matrix.get_height())
throw std::invalid_argument("The first matrix's width must be equal to the second's height!");


// here's also a better idea to instanciate in the heap
unsigned int second_matrix_width = another_matrix.get_width();
Matrices::Matrix resulting_matrix(second_matrix_width, this->height);

Expand Down

0 comments on commit dc1521d

Please sign in to comment.