Skip to content

Commit

Permalink
added activation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Dpbm committed Feb 3, 2024
1 parent 15ad431 commit 0b0e4ee
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
15 changes: 15 additions & 0 deletions machine/activation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <cmath>
#include "activation.h"

using std::exp;
using std::pow;

namespace Activations {
double relu(double x){
return x <= 0 ? 0 : x;
}

double sigmoid(double x){
return pow((1 + exp(-x)), -1);
}
}
6 changes: 6 additions & 0 deletions machine/activation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

namespace Activations {
double relu(double x);
double sigmoid(double x);
}
27 changes: 25 additions & 2 deletions machine/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@

using Matrices::Matrix;
using std::invalid_argument;
using std::invalid_argument;

namespace Layers {
bool input;
unsigned int size;
Matrix *values;
double (*activation)(double);

Layer::Layer(unsigned int size){
Layer::Layer(unsigned int size, bool input){
this->size = size;
this->values = new Matrix(size, 1);
this->values->random(-1, 1);
this->input = input;
}

Layer::Layer(Matrix *values){
Layer::Layer(Matrix *values, bool input){
if(values->get_height() != 1)
throw invalid_argument("Invalid values dimensions!");

this->size = values->get_width();
this->values = values;
this->input = input;
}

Matrix* Layer::get_values(){
Expand All @@ -33,4 +38,22 @@ namespace Layers {
unsigned int Layer::get_size() const {
return this->size;
}

void Layer::set_activation_function(double (*activation)(double)){
if(this->input)
invalid_argument("Input layer must not have a activation function!");

this->activation = activation;
}

void Layer::activate_neurons(){
double *neurons = this->values->get_row(0);

for(unsigned int i = 0; i < this->size; i++){
double neuron_value = neurons[i];
this->values->update_value(i, 0, this->activation(neuron_value));
}

delete neurons;
}
}
8 changes: 6 additions & 2 deletions machine/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ using Matrices::Matrix;
namespace Layers{
class Layer{
public:
Layer(unsigned int size);
Layer(Matrix *values);
Layer(unsigned int size, bool input=false);
Layer(Matrix *values, bool input=false);
Matrix* get_values();
unsigned int get_size() const;
~Layer();
void set_activation_function(double (*activation)(double));
void activate_neurons();
private:
unsigned int size;
bool input = false;
Matrix *values;
double (*activation)(double);
};

};
1 change: 1 addition & 0 deletions machine/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ namespace Machine {
for(Weights *weight: (*this->weights))
delete weight;
}

}

0 comments on commit 0b0e4ee

Please sign in to comment.