Skip to content

Commit

Permalink
added activation functions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dpbm committed Feb 3, 2024
1 parent 653a7c3 commit ccceecf
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 13 deletions.
14 changes: 7 additions & 7 deletions machine/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ namespace Layers {

void Layer::set_activation_function(double (*activation)(double)){
if(this->input)
invalid_argument("Input layer must not have a activation function!");
throw 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));
double neuron_value = this->values->get_position_value(0, i);
this->values->update_value(0, i, this->activation(neuron_value));
}

delete neurons;
}

bool Layer::is_input(){
return this->input;
}

double (*Layer::get_activation_function())(double){
return this->activation;
}
}
1 change: 1 addition & 0 deletions machine/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Layers{
void set_activation_function(double (*activation)(double));
void activate_neurons();
bool is_input();
double (*get_activation_function())(double);
private:
unsigned int size;
bool input = false;
Expand Down
16 changes: 12 additions & 4 deletions matrix/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,25 @@ namespace Matrices{
double** Matrix::get_matrix(){
return this->matrix;
}

void Matrix::zeros(){
void Matrix::map_to_a_single_value(double value){
for(unsigned int i = 0; i < this->height; i++)
for(unsigned int j = 0; j < this->width; j++)
this->update_value(i, j, 0);
this->update_value(i, j, value);
}

void Matrix::zeros(){
this->Matrix::map_to_a_single_value(0);
}

void Matrix::ones(){
this->Matrix::map_to_a_single_value(1);
}

void Matrix::random(int start, int end){
for(unsigned int i = 0; i < this->height; i++)
for(unsigned int j = 0; j < this->width; j++)
this->update_value(i, j, Utils::random(start, end));
this->update_value(i, j, Utils::random(start, end));
}

void Matrix::update_value(unsigned int i, unsigned int j, double value){
Expand Down
2 changes: 2 additions & 0 deletions matrix/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Matrices {
Matrix(unsigned int length);
Matrix(unsigned int width, unsigned int height);
void zeros();
void ones();
void random(int start, int end);
double** get_matrix();
void update_value(unsigned int i, unsigned int j, double value);
Expand All @@ -23,6 +24,7 @@ namespace Matrices {
void show();
~Matrix();
void transpose();
void map_to_a_single_value(double value);
private:
unsigned int width, height;
double** matrix;
Expand Down
34 changes: 34 additions & 0 deletions tests/activations_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <gtest/gtest.h>
#include "../machine/activation.h"

using Activations::relu;
using Activations::sigmoid;

namespace {

TEST(ValuesTest, ReluZeroTest){
ASSERT_EQ(relu(0), 0);
}

TEST(ValuesTest, ReluNegativeTest){
ASSERT_EQ(relu(-1), 0);
}

TEST(ValuesTest, ReluPositiveTest){
ASSERT_EQ(relu(2), 2);
}

TEST(ValuesTest, SigmoidZeroTest){
ASSERT_EQ(sigmoid(0), 0.5);
}

TEST(ValuesTest, SigmoidPositiveTest){
EXPECT_NEAR(sigmoid(20), 1, 0.2);
}


TEST(ValuesTest, SigmoidNegativeTest){
EXPECT_NEAR(sigmoid(-20), 0, 0.2);
}

}
45 changes: 43 additions & 2 deletions tests/layer_test.cc
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#include "../machine/layer.h"
#include <stdexcept>
#include "../matrix/matrix.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "../matrix/matrix.h"
#include "../machine/layer.h"
#include "../machine/activation.h"

using Matrices::Matrix;
using Layers::Layer;
using std::invalid_argument;
using ::testing::AllOf;
using ::testing::Ge;
using ::testing::Le;
using Activations::relu;

namespace {

Expand Down Expand Up @@ -45,4 +47,43 @@ namespace {
delete layer;
}

TEST(UpdateTest, SetActivationFunctionTest){
Layer *layer = new Layer(1, false);
layer->set_activation_function(&relu);
double (*activation)(double) = layer->get_activation_function();
ASSERT_EQ(activation, &relu);
delete layer;
}

TEST(UpdateTest, InvaliActivationFunctionForInputLayerTest){
Layer *layer = new Layer(1, true);
EXPECT_THROW({ layer->set_activation_function(&relu); }, invalid_argument);
delete layer;
}

TEST(UpdateTest, ActivateNeuronsTest){
Matrix* values = new Matrix(2, 1);
values->update_value(0, 0, -15);
values->update_value(0, 1, 12);

Layer *layer = new Layer(values, false);
layer->set_activation_function(&relu);

layer->activate_neurons();
ASSERT_EQ(layer->get_values()->get_position_value(0, 0), 0);
ASSERT_EQ(layer->get_values()->get_position_value(0, 1), 12);
delete layer;
}

TEST(ValuesTest, InputLayerTest){
Layer* layer = new Layer(1, true);
ASSERT_EQ(layer->is_input(), true);
delete layer;
}

TEST(ValuesTest, NonInputLayerTest){
Layer* layer = new Layer(1, false);
ASSERT_EQ(layer->is_input(), false);
delete layer;
}
}
9 changes: 9 additions & 0 deletions tests/nn_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,13 @@ namespace {
delete nn;
}

TEST(CreationTest, CheckInputAndHiddenLayersTest){
NN *nn = new NN;
nn->add_layer(1);
nn->add_layer(2);
ASSERT_EQ(nn->get_layer(0)->is_input(), true);
ASSERT_EQ(nn->get_layer(1)->is_input(), false);
delete nn;
}

}

0 comments on commit ccceecf

Please sign in to comment.