From c1de31dd159102619124df7f1bdeb2fa4ea88b38 Mon Sep 17 00:00:00 2001 From: Michal Kravcenko <michal.kravcenko@vsb.cz> Date: Tue, 12 Jun 2018 11:29:17 +0200 Subject: [PATCH] polished documentation and finished all Neuron classes --- .idea/usage.statistics.xml | 22 ++++----- src/CMakeLists.txt | 2 +- src/Neuron/{neuron.cpp => Neuron.cpp} | 2 +- src/Neuron/{neuron.h => Neuron.h} | 18 +++---- src/Neuron/NeuronBinary.cpp | 7 ++- src/Neuron/NeuronBinary.h | 41 ++-------------- src/Neuron/NeuronLinear.cpp | 23 ++++++--- src/Neuron/NeuronLinear.h | 22 ++++----- src/Neuron/NeuronLogistic.cpp | 30 ++++++------ src/Neuron/NeuronLogistic.h | 51 ++++---------------- src/Neuron/NeuronTanh.cpp | 44 +++++++++++++++++ src/Neuron/NeuronTanh.h | 69 ++++++++++++--------------- 12 files changed, 158 insertions(+), 173 deletions(-) rename src/Neuron/{neuron.cpp => Neuron.cpp} (98%) rename src/Neuron/{neuron.h => Neuron.h} (87%) diff --git a/.idea/usage.statistics.xml b/.idea/usage.statistics.xml index 6a02ae88..d7c1a009 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="15" /> - <entry key="f90" value="1" /> - <entry key="h" value="13" /> + <entry key="cpp" value="29" /> + <entry key="f90" value="2" /> + <entry key="h" value="27" /> <entry key="txt" value="4" /> </counts> </usages-collector> <usages-collector id="statistics.file.types.open"> <counts> <entry key="CMakeLists.txt" value="4" /> - <entry key="ObjectiveC" value="28" /> - <entry key="PLAIN_TEXT" value="2" /> + <entry key="ObjectiveC" value="56" /> + <entry key="PLAIN_TEXT" value="3" /> </counts> </usages-collector> <usages-collector id="statistics.file.extensions.edit"> <counts> - <entry key="cpp" value="1455" /> - <entry key="h" value="2890" /> - <entry key="txt" value="250" /> + <entry key="cpp" value="2096" /> + <entry key="h" value="3432" /> + <entry key="txt" value="290" /> </counts> </usages-collector> <usages-collector id="statistics.file.types.edit"> <counts> <entry key="CMakeLists.txt" value="112" /> - <entry key="Doxygen file" value="38" /> - <entry key="ObjectiveC" value="4307" /> - <entry key="PLAIN_TEXT" value="138" /> + <entry key="Doxygen file" value="39" /> + <entry key="ObjectiveC" value="5489" /> + <entry key="PLAIN_TEXT" value="178" /> </counts> </usages-collector> </session> diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f26751da..b4f15dad 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 constants.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/neuron.cpp b/src/Neuron/Neuron.cpp similarity index 98% rename from src/Neuron/neuron.cpp rename to src/Neuron/Neuron.cpp index c6af11f7..592c5384 100644 --- a/src/Neuron/neuron.cpp +++ b/src/Neuron/Neuron.cpp @@ -1,5 +1,5 @@ -#include "neuron.h" +#include "Neuron.h" Neuron::~Neuron() { if(this->activation_function_parameters){ diff --git a/src/Neuron/neuron.h b/src/Neuron/Neuron.h similarity index 87% rename from src/Neuron/neuron.h rename to src/Neuron/Neuron.h index 6394cc2d..72bf1596 100644 --- a/src/Neuron/neuron.h +++ b/src/Neuron/Neuron.h @@ -49,7 +49,7 @@ public: /** * Performs the activation function and stores the result into the 'state' property */ - virtual void activate() = 0; + virtual void activate( ) = 0; /** * Adds the input signal value to the current potential @@ -65,7 +65,7 @@ public: /** * Setter to the 'potential' property - * @param value Value of the neuron's potential (real number) + * @param[in] value Value of the neuron's potential (real number) */ virtual void set_potential(double value); @@ -77,7 +77,7 @@ public: /** * Setter to the 'state' component - * @param value Value of the current neuron's state + * @param[in] value Value of the current neuron's state */ virtual void set_state(double value); @@ -90,7 +90,7 @@ public: /** * Calculates the partial derivative of the activation function * with respect to the parameter and the current potential - * @param param_idx Index of the parameter to calculate derivative of + * @param[in] param_idx Index of the parameter to calculate derivative of * @return Partial derivative of the activation function according to the * 'param_idx'-th parameter */ @@ -105,22 +105,22 @@ public: /** * Adjusts the parameter with index 'param_idx' of the activation function * by the value prescribed by 'value' - * @param param_idx Index of the parameter to be adjusted - * @param value Value of the adjustment + * @param[in] param_idx Index of the parameter to be adjusted + * @param[in] value Value of the adjustment */ virtual void activation_function_adjust_parameter(int param_idx, double value); /** * Sets the parameter with index 'param_idx' of the activation function * to the value prescribed by 'value' - * @param param_idx - * @param value + * @param[in] param_idx + * @param[in] value */ virtual void activation_function_set_parameter(int param_idx, double value); /** * Gets the parameter with index 'param_idx' of the activation function - * @param param_idx + * @param[in] param_idx */ virtual double activation_function_get_parameter(int param_idx); diff --git a/src/Neuron/NeuronBinary.cpp b/src/Neuron/NeuronBinary.cpp index bba39d07..4d08568a 100644 --- a/src/Neuron/NeuronBinary.cpp +++ b/src/Neuron/NeuronBinary.cpp @@ -9,9 +9,12 @@ NeuronBinary::NeuronBinary(double threshold) { this->activation_function_parameters[0] = threshold; } -void NeuronBinary::activate() { +void NeuronBinary::activate( ) { - if(this->potential >= this->activation_function_parameters[0]){ + double x = this->potential; + double threshold = this->activation_function_parameters[0]; + + if(x >= threshold){ this->state = 1.0; } else{ diff --git a/src/Neuron/NeuronBinary.h b/src/Neuron/NeuronBinary.h index d545cf78..4da2071b 100644 --- a/src/Neuron/NeuronBinary.h +++ b/src/Neuron/NeuronBinary.h @@ -5,7 +5,7 @@ #ifndef INC_4NEURO_NEURONBINARY_H #define INC_4NEURO_NEURONBINARY_H -#include "neuron.h" +#include "Neuron.h" /** * Binary neuron class - uses unit-step as the activation function @@ -17,7 +17,7 @@ public: /** * Default constructor for the binary Neuron - * @param threshold Denotes when the neuron is activated + * @param[in] threshold Denotes when the neuron is activated * When neuron potential exceeds 'threshold' value it becomes excited */ explicit NeuronBinary(double threshold = 0.0); @@ -25,45 +25,10 @@ public: /** * Performs the activation function and stores the result into the 'state' property */ - void activate() override; + void activate( ) override; }; #endif //INC_4NEURO_NEURONBINARY_H - -/* - !-----------------------!---------------------------------------------------------------------- - ! class binary_neuron_t ! - !-----------------------! - - !> Binary neuron class - uses unit-step as the activation function - type, extends(neuron_t) :: binary_neuron_t - private - - real(kind=real_4neuro) :: threshold !< When neuron potential exceeds this value, neuron becomes excited - - contains - - !> Activation function - transforms potential into the output state value - !! AND assigns it into the 'state' component - !! - !! Unit-step function - (returns 1 for potential > threshold, otherwise returns 0) - procedure, private :: activate => unit_step_activate_impl - - end type binary_neuron_t - - interface binary_neuron_t - !> Non-parametric constructor of binary_neuron_t class (threshold - !! will be initialized by a random number from Gaussian distribution) - !! @return An instance of the class binary_neuron_t - module procedure :: new_binary_neuron - - !> Parametric constructor of binary_neuron_t class - !! @param[in] threshold Threshold for the unit-step activation function - !! @return An instance of the class binary_neuron_t - module procedure :: new_binary_neuron_1 - end interface binary_neuron_t - - */ \ No newline at end of file diff --git a/src/Neuron/NeuronLinear.cpp b/src/Neuron/NeuronLinear.cpp index ea374fab..dc749145 100644 --- a/src/Neuron/NeuronLinear.cpp +++ b/src/Neuron/NeuronLinear.cpp @@ -13,22 +13,31 @@ NeuronLinear::NeuronLinear(double a, double b) { } -void NeuronLinear::activate() { +void NeuronLinear::activate( ) { - this->state = this->activation_function_parameters[0] * this->potential + this->activation_function_parameters[1]; + double x = this->potential; + double a = this->activation_function_parameters[0]; + double b = this->activation_function_parameters[1]; + + this->state = b * x + a; } double NeuronLinear::activation_function_get_partial_derivative(int param_idx) { if(param_idx == 0){ - return this->potential; - } - else{ return 1.0; } + else if(param_idx == 1){ + double x = this->potential; + return x; + } + + return 0.0; } -double NeuronLinear::activation_function_get_derivative() { - return this->activation_function_parameters[0]; +double NeuronLinear::activation_function_get_derivative( ) { + double b = this->activation_function_parameters[1]; + + return b; } \ No newline at end of file diff --git a/src/Neuron/NeuronLinear.h b/src/Neuron/NeuronLinear.h index 564f242c..acff3d80 100644 --- a/src/Neuron/NeuronLinear.h +++ b/src/Neuron/NeuronLinear.h @@ -5,7 +5,7 @@ #ifndef INC_4NEURO_NEURONLINEAR_H #define INC_4NEURO_NEURONLINEAR_H -#include "neuron.h" +#include "Neuron.h" /** * Linear neuron class - uses activation function in the form f(x)=a*x + b, @@ -16,21 +16,21 @@ 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] + * f(x) = b * x + a + * @param[in] a First coefficient, stored in activation_function_parameters[0] + * @param[in] 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 + * Evaluates 'b*x + a' and stores the result into the 'state' property */ - void activate() override; + 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 + * f(x) = b*x + a + * @param[in] 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 @@ -38,10 +38,10 @@ public: double activation_function_get_partial_derivative(int param_idx) override; /** - * Calculates d/dx of (a*x + b) - * @return a + * Calculates d/dx of (b*x + a) + * @return b */ - double activation_function_get_derivative() override; + double activation_function_get_derivative( ) override; }; diff --git a/src/Neuron/NeuronLogistic.cpp b/src/Neuron/NeuronLogistic.cpp index ece032b8..a975154f 100644 --- a/src/Neuron/NeuronLogistic.cpp +++ b/src/Neuron/NeuronLogistic.cpp @@ -13,48 +13,50 @@ NeuronLogistic::NeuronLogistic(double a, double b) { this->activation_function_parameters[1] = b; } -void NeuronLogistic::activate() { +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 ex = std::pow(E, a - x); + this->state = std::pow(1.0 + ex, -b); } -double NeuronLogistic::activation_function_get_partial_derivative(int param_idx) { +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 ex = std::pow(E, a - x); - double exa= -std::pow(ex + 1.0, -a); + double exa= -std::pow(ex + 1.0, -b); return exa * std::log(ex + 1.0); } - else{ + else if(param_idx == 1){ - double ex = std::pow(E, b - x); - double ex2 = std::pow(ex + 1.0, -a - 1.0); + double ex = std::pow(E, a - x); + double ex2 = std::pow(ex + 1.0, -b - 1.0); - return -a * ex * ex2; + return -b * ex * ex2; } + return 0.0; + } -double NeuronLogistic::activation_function_get_derivative() { +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); + double ex = std::pow(E, a - x); + double ex2 = std::pow(ex + 1.0, -b - 1.0); - return a * ex * ex2; + return b * ex * ex2; } \ No newline at end of file diff --git a/src/Neuron/NeuronLogistic.h b/src/Neuron/NeuronLogistic.h index 25751613..88333e2a 100644 --- a/src/Neuron/NeuronLogistic.h +++ b/src/Neuron/NeuronLogistic.h @@ -6,69 +6,38 @@ #define INC_4NEURO_NEURONLOGISTIC_H #include <cmath> -#include "neuron.h" +#include "Neuron.h" #include "../constants.h" 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] + * @param[in] a First coefficient, stored in activation_function_parameters[0] + * @param[in] 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 + * Evaluates '(1 + e^(-x + a))^(-b)' and stores the result into the 'state' property */ - void activate() override; + 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 + * f(x) = (1 + e^(-x + a))^(-b) + * @param[in] 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) + * Calculates d/dx of (1 + e^(-x + a))^(-b) + * @return b * e^(a - x) * [e^(a - x) + 1]^(-b) */ - double activation_function_get_derivative() override; + double activation_function_get_derivative( ) override; }; #endif //INC_4NEURO_NEURONLOGISTIC_H -/* -!-------------------------!-------------------------------------------------------------------- - ! class logistic_neuron_t ! - !-------------------------! - - !> Logistic neuron class - uses generalised logistic function as an activation function - !! in the form f(x) = (1 + e^(-x))^(-alpha), - !! 'x' being the neuron potential here - type, extends(neuron_t) :: logistic_neuron_t - private - - real(kind=real_4neuro) :: alpha_coef !< The alpha coefficient used in the activation function - - contains - - !> Activation function - generalised logistic f. - procedure, private :: activate => logistic_activate_impl - end type logistic_neuron_t - - interface logistic_neuron_t - !> Non-parametric constructor of logistic_neuron_t class - !! Alpha coefficient is set to 1 - !! @return An instance of the class logistic_neuron_t - module procedure :: new_logistic_neuron - - !> Constructor of the logistic_neuron_t class - !! @param[in] alpha_coef Alpha coefficient in the logistic activation function - !! @return An instance of the class logistic_neuron_t - module procedure :: new_logistic_neuron_1 - end interface logistic_neuron_t - */ \ No newline at end of file diff --git a/src/Neuron/NeuronTanh.cpp b/src/Neuron/NeuronTanh.cpp index a19d2d19..c9d47dd6 100644 --- a/src/Neuron/NeuronTanh.cpp +++ b/src/Neuron/NeuronTanh.cpp @@ -3,3 +3,47 @@ // #include "NeuronTanh.h" + +NeuronTanh::NeuronTanh(double a) { + + this->activation_function_parameters = new double[1]; + this->activation_function_parameters[0] = a; + +} + +void NeuronTanh::activate( ) { + + double a = this->activation_function_parameters[0]; + double x = this->potential; + double ex = std::pow(E, x - a); + double exi = 1.0 / ex; + + this->state = (ex - exi) / (ex + exi); + +} + +double NeuronTanh::activation_function_get_partial_derivative(int param_idx) { + + double a = this->activation_function_parameters[0]; + double x = this->potential; + + if(param_idx == 0){ + double ex = -4.0 * std::pow(E, 2.0 * (x + a)); + double exi = std::pow(E, 2.0 * a) + std::pow(E, 2.0 * x); + + return ex / (exi * exi); + } + + return 0.0; + +} + +double NeuronTanh::activation_function_get_derivative( ) { + + double a = this->activation_function_parameters[0]; + double x = this->potential; + double ex = -4.0 * std::pow(E, 2.0 * (x + a)); + double exi = std::pow(E, 2.0 * a) + std::pow(E, 2.0 * x); + + return ex / (exi * exi); +} \ No newline at end of file diff --git a/src/Neuron/NeuronTanh.h b/src/Neuron/NeuronTanh.h index 1943e44b..9a802b3f 100644 --- a/src/Neuron/NeuronTanh.h +++ b/src/Neuron/NeuronTanh.h @@ -5,45 +5,38 @@ #ifndef INC_4NEURO_NEURONTANH_H #define INC_4NEURO_NEURONTANH_H - -class NeuronTanh { - +#include <cmath> +#include "Neuron.h" +#include "../constants.h" + +class NeuronTanh:Neuron { + /** + * Constructs the object of the Hyperbolic Tangent >neuron with activation function + * f(x) = (e^(x-a) - e^(a-x))/(e^(x-a) + e^(a-x)) + * @param[in] a First coefficient, stored in activation_function_parameters[0] + */ + explicit NeuronTanh(double a = 0.0); + + /** + * Evaluates '(e^(x-a) - e^(a-x))/(e^(x-a) + e^(a-x))' and stores the result into the 'state' property + */ + void activate( ) override; + + /** + * Calculates the partial derivative of the activation function + * f(x) = (e^(x-a) - e^(a-x))/(e^(x-a) + e^(a-x)) + * @param[in] 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 (e^(x-a) - e^(a-x))/(e^(x-a) + e^(a-x)) + * @return a * e^(b - x) * [e^(b - x) + 1]^(-a) + */ + double activation_function_get_derivative( ) override; }; #endif //INC_4NEURO_NEURONTANH_H -/* -!---------------------!------------------------------------------------------------------------ - ! class tanh_neuron_t ! - !---------------------! - - !> Hyperbolic tangent neuron class - uses f(x) = (e^x - e^(-x))/(e^x + e^(-x)) as - !! an activation function, 'x' being the neuron potential. - type, extends(neuron_t) :: tanh_neuron_t - contains - - !> Activation function - hyperbolic tangent - procedure, private :: activate => hyperbolic_tangent_activate_impl - end type tanh_neuron_t - - interface tanh_neuron_t - !> Constructor for an instance of the class tanh_neuron_t - module procedure :: new_tanh_neuron - end interface tanh_neuron_t - - !> Arcus tangents neuron class - uses f(x)=tan^(-1)(x) as an - !! activation function, 'x' being the neuron potential - type, extends(neuron_t) :: arctan_neuron_t - contains - - !> Activation function - arcus tangens - procedure, private :: activate => arcus_tangens_activate_impl - end type arctan_neuron_t - - interface arctan_neuron_t - !> Constructor for an instance of the class arctan_neuron_t - module procedure :: new_arctan_neuron - end interface arctan_neuron_t - - contains - */ \ No newline at end of file -- GitLab