From 6d7ca604eb82ce2faba681b9e98cf713dbe87fdd Mon Sep 17 00:00:00 2001 From: Michal Kravcenko <michal.kravcenko@vsb.cz> Date: Mon, 11 Jun 2018 15:46:26 +0200 Subject: [PATCH] added child classes of the Neuron class --- .idea/usage.statistics.xml | 19 +++++----- src/CMakeLists.txt | 2 +- src/Neuron/NeuronBinary.cpp | 22 +++++++++++ src/Neuron/NeuronBinary.h | 69 +++++++++++++++++++++++++++++++++++ src/Neuron/NeuronLinear.cpp | 5 +++ src/Neuron/NeuronLinear.h | 47 ++++++++++++++++++++++++ src/Neuron/NeuronLogistic.cpp | 5 +++ src/Neuron/NeuronLogistic.h | 45 +++++++++++++++++++++++ src/Neuron/NeuronTanh.cpp | 5 +++ src/Neuron/NeuronTanh.h | 49 +++++++++++++++++++++++++ src/{ => Neuron}/neuron.cpp | 2 +- src/{ => Neuron}/neuron.h | 4 +- 12 files changed, 261 insertions(+), 13 deletions(-) create mode 100644 src/Neuron/NeuronBinary.cpp create mode 100644 src/Neuron/NeuronBinary.h create mode 100644 src/Neuron/NeuronLinear.cpp create mode 100644 src/Neuron/NeuronLinear.h create mode 100644 src/Neuron/NeuronLogistic.cpp create mode 100644 src/Neuron/NeuronLogistic.h create mode 100644 src/Neuron/NeuronTanh.cpp create mode 100644 src/Neuron/NeuronTanh.h rename src/{ => Neuron}/neuron.cpp (99%) rename src/{ => Neuron}/neuron.h (97%) diff --git a/.idea/usage.statistics.xml b/.idea/usage.statistics.xml index 901600e2..bf4765c4 100644 --- a/.idea/usage.statistics.xml +++ b/.idea/usage.statistics.xml @@ -5,31 +5,32 @@ <usages-collector id="statistics.file.extensions.open"> <counts> <entry key="Makefile" value="1" /> - <entry key="cpp" value="1" /> + <entry key="cpp" value="8" /> <entry key="f90" value="1" /> - <entry key="h" value="1" /> + <entry key="h" value="5" /> <entry key="txt" value="2" /> </counts> </usages-collector> <usages-collector id="statistics.file.types.open"> <counts> <entry key="CMakeLists.txt" value="2" /> - <entry key="ObjectiveC" value="2" /> + <entry key="ObjectiveC" value="13" /> <entry key="PLAIN_TEXT" value="2" /> </counts> </usages-collector> <usages-collector id="statistics.file.extensions.edit"> <counts> - <entry key="cpp" value="478" /> - <entry key="h" value="1861" /> - <entry key="txt" value="74" /> + <entry key="cpp" value="731" /> + <entry key="h" value="2161" /> + <entry key="txt" value="208" /> </counts> </usages-collector> <usages-collector id="statistics.file.types.edit"> <counts> - <entry key="CMakeLists.txt" value="74" /> - <entry key="Doxygen file" value="27" /> - <entry key="ObjectiveC" value="2312" /> + <entry key="CMakeLists.txt" value="109" /> + <entry key="Doxygen file" value="31" /> + <entry key="ObjectiveC" value="2861" /> + <entry key="PLAIN_TEXT" value="99" /> </counts> </usages-collector> </session> diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4457cf05..9bbbfd56 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,3 @@ -add_library(${SRC_DIR} neuron.cpp) +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) diff --git a/src/Neuron/NeuronBinary.cpp b/src/Neuron/NeuronBinary.cpp new file mode 100644 index 00000000..86fbae27 --- /dev/null +++ b/src/Neuron/NeuronBinary.cpp @@ -0,0 +1,22 @@ +// +// Created by fluffymoo on 11.6.18. +// + +#include "NeuronBinary.h" + +NeuronBinary::NeuronBinary(double threshold) { + this->activation_function_parameters = new double[1]; + this->activation_function_parameters[0] = threshold; +} + +void NeuronBinary::activate() { + + if(this->potential >= this->activation_function_parameters[0]){ + this->state = 1.0; + } + else{ + this->state = 0.0; + } + +} + diff --git a/src/Neuron/NeuronBinary.h b/src/Neuron/NeuronBinary.h new file mode 100644 index 00000000..a08c3d41 --- /dev/null +++ b/src/Neuron/NeuronBinary.h @@ -0,0 +1,69 @@ +// +// Created by fluffymoo on 11.6.18. +// + +#ifndef INC_4NEURO_NEURONBINARY_H +#define INC_4NEURO_NEURONBINARY_H + +#include "neuron.h" + +/** + * Binary neuron class - uses unit-step as the activation function + */ +class NeuronBinary:Neuron { +private: + +public: + + /** + * Default constructor for the binary Neuron + * @param threshold Denotes when the neuron is activated + * When neuron potential exceeds 'threshold' value it becomes excited + */ + explicit NeuronBinary(double threshold); + + /** + * Performs the activation function and stores the result into the 'state' property + */ + 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 new file mode 100644 index 00000000..0642b241 --- /dev/null +++ b/src/Neuron/NeuronLinear.cpp @@ -0,0 +1,5 @@ +// +// Created by fluffymoo on 11.6.18. +// + +#include "NeuronLinear.h" diff --git a/src/Neuron/NeuronLinear.h b/src/Neuron/NeuronLinear.h new file mode 100644 index 00000000..b63298fc --- /dev/null +++ b/src/Neuron/NeuronLinear.h @@ -0,0 +1,47 @@ +// +// Created by fluffymoo on 11.6.18. +// + +#ifndef INC_4NEURO_NEURONLINEAR_H +#define INC_4NEURO_NEURONLINEAR_H + + +class NeuronLinear { + +}; + + +#endif //INC_4NEURO_NEURONLINEAR_H + +/* +!-----------------------!---------------------------------------------------------------------- + ! class linear_neuron_t ! + !-----------------------! + + !> Linear neuron class - uses activation function in the form f(x)=a*x + b, + !! 'x' being the neuron's potential + type, extends(neuron_t) :: linear_neuron_t + private + + !! Coefficients for the linear activation function in format 'f(x)=a*x + b' + real(kind=real_4neuro) :: a_coef !< The coefficient 'a' in the activation function f(x)=a*x + b + real(kind=real_4neuro) :: b_coef !< The coefficient 'b' in the activation function f(x)=a*x + b + + contains + + !> Activation function - f(x)=a*x + b + procedure, private :: activate => identity_potential_activate_impl + end type linear_neuron_t + + interface linear_neuron_t + !> Non-parametric constructor of linear_neuron_t class - a coef. is 1, b coef. is 0 + !! @return An instance of the class linear_neuron_t + module procedure :: new_linear_neuron + + !> Constructor of linear_neuron_t class + !! @param[in] a_coef a coef. of the linear activation function + !! @param[in] b_coef b coef. of the linear activation fucntion + !! @return An instance of the class linear_neuron_t + module procedure :: new_linear_neuron_2 + end interface linear_neuron_t + */ \ No newline at end of file diff --git a/src/Neuron/NeuronLogistic.cpp b/src/Neuron/NeuronLogistic.cpp new file mode 100644 index 00000000..e8d8589a --- /dev/null +++ b/src/Neuron/NeuronLogistic.cpp @@ -0,0 +1,5 @@ +// +// Created by fluffymoo on 11.6.18. +// + +#include "NeuronLogistic.h" diff --git a/src/Neuron/NeuronLogistic.h b/src/Neuron/NeuronLogistic.h new file mode 100644 index 00000000..02b9e0ed --- /dev/null +++ b/src/Neuron/NeuronLogistic.h @@ -0,0 +1,45 @@ +// +// Created by fluffymoo on 11.6.18. +// + +#ifndef INC_4NEURO_NEURONLOGISTIC_H +#define INC_4NEURO_NEURONLOGISTIC_H + + +class NeuronLogistic { + +}; + + +#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 new file mode 100644 index 00000000..a19d2d19 --- /dev/null +++ b/src/Neuron/NeuronTanh.cpp @@ -0,0 +1,5 @@ +// +// Created by fluffymoo on 11.6.18. +// + +#include "NeuronTanh.h" diff --git a/src/Neuron/NeuronTanh.h b/src/Neuron/NeuronTanh.h new file mode 100644 index 00000000..1943e44b --- /dev/null +++ b/src/Neuron/NeuronTanh.h @@ -0,0 +1,49 @@ +// +// Created by fluffymoo on 11.6.18. +// + +#ifndef INC_4NEURO_NEURONTANH_H +#define INC_4NEURO_NEURONTANH_H + + +class NeuronTanh { + +}; + + +#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 diff --git a/src/neuron.cpp b/src/Neuron/neuron.cpp similarity index 99% rename from src/neuron.cpp rename to src/Neuron/neuron.cpp index f2a8a473..c6af11f7 100644 --- a/src/neuron.cpp +++ b/src/Neuron/neuron.cpp @@ -42,4 +42,4 @@ void Neuron::activation_function_set_parameter(int param_idx, double value) { double Neuron::activation_function_get_parameter(int param_idx) { return this->activation_function_parameters[param_idx]; -} \ No newline at end of file +} diff --git a/src/neuron.h b/src/Neuron/neuron.h similarity index 97% rename from src/neuron.h rename to src/Neuron/neuron.h index 08ae745a..f6c22fd4 100644 --- a/src/neuron.h +++ b/src/Neuron/neuron.h @@ -2,7 +2,7 @@ #define NEURON_H_ /** - * Module containing classes representing neurons + * A file containing the mother classe representing neurons * in neural networks. * * @author Martin Beseda @@ -17,7 +17,7 @@ */ class Neuron{ -private: +protected: /** * Inner potential of the neuron (input of the activation function) */ -- GitLab