From 3c0ec38b7848b397bda4d38ed353a3dcfa111e8e Mon Sep 17 00:00:00 2001 From: Michal Kravcenko <michal.kravcenko@vsb.cz> Date: Mon, 11 Jun 2018 17:25:09 +0200 Subject: [PATCH] added a constant file and more neuron classes --- .idea/usage.statistics.xml | 24 +++++++-------- CMakeLists.txt | 6 ++-- src/CMakeLists.txt | 2 +- src/Neuron/NeuronBinary.cpp | 1 - src/Neuron/NeuronBinary.h | 2 +- src/Neuron/NeuronLinear.cpp | 29 ++++++++++++++++++ src/Neuron/NeuronLinear.h | 36 ++++++++++++++++++++++- src/Neuron/NeuronLogistic.cpp | 55 +++++++++++++++++++++++++++++++++++ src/Neuron/NeuronLogistic.h | 31 +++++++++++++++++++- src/Neuron/neuron.h | 6 ++++ src/constants.h | 11 +++++++ 11 files changed, 182 insertions(+), 21 deletions(-) create mode 100644 src/constants.h diff --git a/.idea/usage.statistics.xml b/.idea/usage.statistics.xml index bf4765c4..6a02ae88 100644 --- a/.idea/usage.statistics.xml +++ b/.idea/usage.statistics.xml @@ -5,32 +5,32 @@ <usages-collector id="statistics.file.extensions.open"> <counts> <entry key="Makefile" value="1" /> - <entry key="cpp" value="8" /> + <entry key="cpp" value="15" /> <entry key="f90" value="1" /> - <entry key="h" value="5" /> - <entry key="txt" value="2" /> + <entry key="h" value="13" /> + <entry key="txt" value="4" /> </counts> </usages-collector> <usages-collector id="statistics.file.types.open"> <counts> - <entry key="CMakeLists.txt" value="2" /> - <entry key="ObjectiveC" value="13" /> + <entry key="CMakeLists.txt" value="4" /> + <entry key="ObjectiveC" value="28" /> <entry key="PLAIN_TEXT" value="2" /> </counts> </usages-collector> <usages-collector id="statistics.file.extensions.edit"> <counts> - <entry key="cpp" value="731" /> - <entry key="h" value="2161" /> - <entry key="txt" value="208" /> + <entry key="cpp" value="1455" /> + <entry key="h" value="2890" /> + <entry key="txt" value="250" /> </counts> </usages-collector> <usages-collector id="statistics.file.types.edit"> <counts> - <entry key="CMakeLists.txt" value="109" /> - <entry key="Doxygen file" value="31" /> - <entry key="ObjectiveC" value="2861" /> - <entry key="PLAIN_TEXT" value="99" /> + <entry key="CMakeLists.txt" value="112" /> + <entry key="Doxygen file" value="38" /> + <entry key="ObjectiveC" value="4307" /> + <entry key="PLAIN_TEXT" value="138" /> </counts> </usages-collector> </session> diff --git a/CMakeLists.txt b/CMakeLists.txt index 8472e157..37b2c992 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,14 +1,12 @@ cmake_minimum_required(VERSION 3.0) -project(NeuronLib) -set (NeuronLib_VERSION_MAJOR 0) -set (NeuronLib_VERSION_MINOR 1) + message ("Before enable language") enable_language(Fortran) if (WIN32) message ("cmake for " ${CMAKE_Fortran_COMPILER}) set (CMAKE_FORTRAN_COMPILER ${CMAKE_Fortran_COMPILER}) - project(4Neuro FORTRAN) + project(4Neuro) else () project(4Neuro) endif () diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9bbbfd56..f26751da 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,3 @@ -add_library(neuron SHARED Neuron/neuron.cpp Neuron/neuron.h Neuron/NeuronBinary.cpp Neuron/NeuronBinary.h Neuron/NeuronLinear.cpp Neuron/NeuronLinear.h Neuron/NeuronLogistic.cpp Neuron/NeuronLogistic.h Neuron/NeuronTanh.cpp Neuron/NeuronTanh.h) +add_library(neuron SHARED Neuron/neuron.cpp Neuron/neuron.h Neuron/NeuronBinary.cpp Neuron/NeuronBinary.h Neuron/NeuronLinear.cpp Neuron/NeuronLinear.h Neuron/NeuronLogistic.cpp Neuron/NeuronLogistic.h Neuron/NeuronTanh.cpp Neuron/NeuronTanh.h constants.h) diff --git a/src/Neuron/NeuronBinary.cpp b/src/Neuron/NeuronBinary.cpp index 86fbae27..bba39d07 100644 --- a/src/Neuron/NeuronBinary.cpp +++ b/src/Neuron/NeuronBinary.cpp @@ -17,6 +17,5 @@ void NeuronBinary::activate() { else{ this->state = 0.0; } - } diff --git a/src/Neuron/NeuronBinary.h b/src/Neuron/NeuronBinary.h index a08c3d41..d545cf78 100644 --- a/src/Neuron/NeuronBinary.h +++ b/src/Neuron/NeuronBinary.h @@ -20,7 +20,7 @@ public: * @param threshold Denotes when the neuron is activated * When neuron potential exceeds 'threshold' value it becomes excited */ - explicit NeuronBinary(double threshold); + explicit NeuronBinary(double threshold = 0.0); /** * Performs the activation function and stores the result into the 'state' property diff --git a/src/Neuron/NeuronLinear.cpp b/src/Neuron/NeuronLinear.cpp index 0642b241..ea374fab 100644 --- a/src/Neuron/NeuronLinear.cpp +++ b/src/Neuron/NeuronLinear.cpp @@ -3,3 +3,32 @@ // #include "NeuronLinear.h" + +NeuronLinear::NeuronLinear(double a, double b) { + + this->activation_function_parameters = new double[2]; + + this->activation_function_parameters[0] = a; + this->activation_function_parameters[1] = b; + +} + +void NeuronLinear::activate() { + + this->state = this->activation_function_parameters[0] * this->potential + this->activation_function_parameters[1]; + +} + +double NeuronLinear::activation_function_get_partial_derivative(int param_idx) { + + if(param_idx == 0){ + return this->potential; + } + else{ + return 1.0; + } +} + +double NeuronLinear::activation_function_get_derivative() { + return this->activation_function_parameters[0]; +} \ No newline at end of file diff --git a/src/Neuron/NeuronLinear.h b/src/Neuron/NeuronLinear.h index b63298fc..564f242c 100644 --- a/src/Neuron/NeuronLinear.h +++ b/src/Neuron/NeuronLinear.h @@ -5,9 +5,43 @@ #ifndef INC_4NEURO_NEURONLINEAR_H #define INC_4NEURO_NEURONLINEAR_H +#include "neuron.h" -class NeuronLinear { +/** + * Linear neuron class - uses activation function in the form f(x)=a*x + b, + * 'x' being the neuron's potential + */ +class NeuronLinear:Neuron { +public: + /** + * Constructs the object of the Linear neuron with activation function + * f(x) = a * x + b + * @param a First coefficient, stored in activation_function_parameters[0] + * @param b Second coefficient, stored in activation_function_parameters[1] + */ + explicit NeuronLinear(double a = 0.0, double b = 0.0); + + /** + * Evaluates 'a*x + b' and stores the result into the 'state' property + */ + void activate() override; + + /** + * Calculates the partial derivative of the activation function + * f(x) = a*x + b + * @param param_idx Index of the parameter to calculate derivative of + * @return Partial derivative of the activation function according to the + * 'param_idx'-th parameter. For 'param_idx'=0 returns 'x', otherwise + * returns 1 + */ + double activation_function_get_partial_derivative(int param_idx) override; + + /** + * Calculates d/dx of (a*x + b) + * @return a + */ + double activation_function_get_derivative() override; }; diff --git a/src/Neuron/NeuronLogistic.cpp b/src/Neuron/NeuronLogistic.cpp index e8d8589a..ece032b8 100644 --- a/src/Neuron/NeuronLogistic.cpp +++ b/src/Neuron/NeuronLogistic.cpp @@ -2,4 +2,59 @@ // Created by fluffymoo on 11.6.18. // + #include "NeuronLogistic.h" + +NeuronLogistic::NeuronLogistic(double a, double b) { + + this->activation_function_parameters = new double[2]; + + this->activation_function_parameters[0] = a; + this->activation_function_parameters[1] = b; +} + +void NeuronLogistic::activate() { + double a = this->activation_function_parameters[0]; + double b = this->activation_function_parameters[1]; + double x = this->potential; + + double ex = std::pow(E, b - x); + this->state = std::pow(1.0 + ex, -a); + +} + +double NeuronLogistic::activation_function_get_partial_derivative(int param_idx) { + + double a = this->activation_function_parameters[0]; + double b = this->activation_function_parameters[1]; + double x = this->potential; + + if(param_idx == 0){ + double ex = std::pow(E, b - x); + + double exa= -std::pow(ex + 1.0, -a); + + return exa * std::log(ex + 1.0); + } + else{ + + double ex = std::pow(E, b - x); + double ex2 = std::pow(ex + 1.0, -a - 1.0); + + return -a * ex * ex2; + } + +} + +double NeuronLogistic::activation_function_get_derivative() { + + double a = this->activation_function_parameters[0]; + double b = this->activation_function_parameters[1]; + double x = this->potential; + + double ex = std::pow(E, b - x); + double ex2 = std::pow(ex + 1.0, -a - 1.0); + + return a * ex * ex2; + +} \ No newline at end of file diff --git a/src/Neuron/NeuronLogistic.h b/src/Neuron/NeuronLogistic.h index 02b9e0ed..25751613 100644 --- a/src/Neuron/NeuronLogistic.h +++ b/src/Neuron/NeuronLogistic.h @@ -5,9 +5,38 @@ #ifndef INC_4NEURO_NEURONLOGISTIC_H #define INC_4NEURO_NEURONLOGISTIC_H +#include <cmath> +#include "neuron.h" +#include "../constants.h" -class NeuronLogistic { +class NeuronLogistic:Neuron { + /** + * Constructs the object of the Logistic neuron with activation function + * f(x) = (1 + e^(-x + b))^(-a) + * @param a First coefficient, stored in activation_function_parameters[0] + * @param b Second coefficient, stored in activation_function_parameters[1] + */ + explicit NeuronLogistic(double a = 0.0, double b = 0.0); + /** + * Evaluates '(1 + e^(-x + b))^(-a)' and stores the result into the 'state' property + */ + void activate() override; + + /** + * Calculates the partial derivative of the activation function + * f(x) = (1 + e^(-x + b))^(-a) + * @param param_idx Index of the parameter to calculate derivative of + * @return Partial derivative of the activation function according to the + * 'param_idx'-th parameter. + */ + double activation_function_get_partial_derivative(int param_idx) override; + + /** + * Calculates d/dx of (1 + e^(-x + b))^(-a) + * @return a * e^(b - x) * [e^(b - x) + 1]^(-a) + */ + double activation_function_get_derivative() override; }; diff --git a/src/Neuron/neuron.h b/src/Neuron/neuron.h index f6c22fd4..6394cc2d 100644 --- a/src/Neuron/neuron.h +++ b/src/Neuron/neuron.h @@ -96,6 +96,12 @@ public: */ virtual double activation_function_get_partial_derivative(int param_idx) = 0; + /** + * Calculates the derivative with respect to the argument, ie the 'potential' + * @return f'(x), where 'f(x)' is the activation function and 'x' = 'potential' + */ + virtual double activation_function_get_derivative( ) = 0; + /** * Adjusts the parameter with index 'param_idx' of the activation function * by the value prescribed by 'value' diff --git a/src/constants.h b/src/constants.h new file mode 100644 index 00000000..8c2674a5 --- /dev/null +++ b/src/constants.h @@ -0,0 +1,11 @@ +// +// Created by fluffymoo on 11.6.18. +// + +#ifndef INC_4NEURO_CONSTANTS_H +#define INC_4NEURO_CONSTANTS_H + +#define E 2.7182818284590 +#define PI 3.14159265358979323846 + +#endif //INC_4NEURO_CONSTANTS_H -- GitLab