From 02707da185b1a8867e96e792d71ad6b1d1f9dfba Mon Sep 17 00:00:00 2001 From: Michal Kravcenko <michal.kravcenko@vsb.cz> Date: Wed, 13 Jun 2018 17:13:01 +0200 Subject: [PATCH] preliminary work on connections, neural networks and neurons with neural networks as activation functions --- .idea/usage.statistics.xml | 22 ++++---- src/CMakeLists.txt | 2 +- src/NetConnection/Connection.cpp | 42 +++++++++++++++ src/NetConnection/Connection.h | 93 ++++++++++++++++++++++++++++++++ src/Network/NeuralNetwork.cpp | 8 +++ src/Network/NeuralNetwork.h | 40 ++++++++++++++ src/Neuron/Neuron.cpp | 37 +++++++++++++ src/Neuron/Neuron.h | 65 +++++++++++++++++----- src/Neuron/NeuronBinary.h | 11 ++-- src/Neuron/NeuronLinear.h | 11 ++-- src/Neuron/NeuronLogistic.h | 11 ++-- src/Neuron/NeuronNeuralNet.cpp | 8 +++ src/Neuron/NeuronNeuralNet.h | 17 ++++++ src/Neuron/NeuronTanh.h | 11 ++-- 14 files changed, 341 insertions(+), 37 deletions(-) create mode 100644 src/NetConnection/Connection.cpp create mode 100644 src/NetConnection/Connection.h create mode 100644 src/Network/NeuralNetwork.cpp create mode 100644 src/Network/NeuralNetwork.h create mode 100644 src/Neuron/NeuronNeuralNet.cpp create mode 100644 src/Neuron/NeuronNeuralNet.h diff --git a/.idea/usage.statistics.xml b/.idea/usage.statistics.xml index d7c1a009..ab4710f5 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="29" /> - <entry key="f90" value="2" /> - <entry key="h" value="27" /> + <entry key="cpp" value="37" /> + <entry key="f90" value="4" /> + <entry key="h" value="45" /> <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="56" /> - <entry key="PLAIN_TEXT" value="3" /> + <entry key="ObjectiveC" value="82" /> + <entry key="PLAIN_TEXT" value="5" /> </counts> </usages-collector> <usages-collector id="statistics.file.extensions.edit"> <counts> - <entry key="cpp" value="2096" /> - <entry key="h" value="3432" /> - <entry key="txt" value="290" /> + <entry key="cpp" value="2761" /> + <entry key="h" value="5108" /> + <entry key="txt" value="451" /> </counts> </usages-collector> <usages-collector id="statistics.file.types.edit"> <counts> <entry key="CMakeLists.txt" value="112" /> - <entry key="Doxygen file" value="39" /> - <entry key="ObjectiveC" value="5489" /> - <entry key="PLAIN_TEXT" value="178" /> + <entry key="Doxygen file" value="70" /> + <entry key="ObjectiveC" value="7799" /> + <entry key="PLAIN_TEXT" value="339" /> </counts> </usages-collector> </session> diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b4f15dad..69b1d8c4 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 NetConnection/Connection.cpp NetConnection/Connection.h Network/NeuralNetwork.cpp Network/NeuralNetwork.h Neuron/NeuronNeuralNet.cpp Neuron/NeuronNeuralNet.h) diff --git a/src/NetConnection/Connection.cpp b/src/NetConnection/Connection.cpp new file mode 100644 index 00000000..b349e2e5 --- /dev/null +++ b/src/NetConnection/Connection.cpp @@ -0,0 +1,42 @@ +/** + * DESCRIPTION OF THE FILE + * + * @author Michal KravÄŤenko + * @date 13.6.18 - + */ + +#include "Connection.h" + +Connection::Connection(Neuron *n_in, Neuron *n_out, double edge_weight) { + this->neuron_in = n_in; + this->neuron_out = n_out; + this->weight = edge_weight; +} + +//Connection::~Connection() { + // +//} + +void Connection::adjust_weight(double value) { + this->weight += value; +} + +Neuron* Connection::get_neuron_in() { + return this->neuron_in; +} + +Neuron* Connection::get_neuron_out() { + return this->neuron_out; +} + +double Connection::get_weight() { + return this->weight; +} + +void Connection::pass_signal() { + this->neuron_out->adjust_potential(this->neuron_in->get_state() * this->weight); +} + +void Connection::set_weight(double value) { + this->weight = value; +} diff --git a/src/NetConnection/Connection.h b/src/NetConnection/Connection.h new file mode 100644 index 00000000..038537cf --- /dev/null +++ b/src/NetConnection/Connection.h @@ -0,0 +1,93 @@ +/** + * DESCRIPTION OF THE FILE + * + * @author Michal KravÄŤenko + * @date 13.6.18 - + */ + +#ifndef INC_4NEURO_CONNECTION_H +#define INC_4NEURO_CONNECTION_H + +#include "../Neuron/Neuron.h" +class Neuron; + +/** + * + */ +class Connection { +private: + /** + * + */ + Neuron *neuron_in = nullptr; + + /** + * + */ + Neuron *neuron_out = nullptr; + + /** + * + */ + double weight = 0.0; + + //TODO pridat gradient + +public: + + /** + * + * @param[in] n_in + * @param[in] n_out + * @param[in] edge_weight + */ + Connection(Neuron *n_in, Neuron *n_out, double edge_weight = 1.0); + +// Connection(Neuron *n_in, Neuron *n_out, Connection* ref_con); + + /** + * + */ + ~Connection()=default; + + /** + * + * @param[in] value + */ + void adjust_weight(double value); + + /** + * + * @param[in] value + */ + void set_weight(double value); + + /** + * + * @return + */ + double get_weight(); + + + /** + * + */ + void pass_signal(); + + + /** + * + * @return + */ + Neuron* get_neuron_in(); + + /** + * + * @return + */ + Neuron* get_neuron_out(); + +}; + + +#endif //INC_4NEURO_CONNECTION_H diff --git a/src/Network/NeuralNetwork.cpp b/src/Network/NeuralNetwork.cpp new file mode 100644 index 00000000..c0adb116 --- /dev/null +++ b/src/Network/NeuralNetwork.cpp @@ -0,0 +1,8 @@ +/** + * DESCRIPTION OF THE FILE + * + * @author Michal KravÄŤenko + * @date 13.6.18 - + */ + +#include "NeuralNetwork.h" diff --git a/src/Network/NeuralNetwork.h b/src/Network/NeuralNetwork.h new file mode 100644 index 00000000..5036801c --- /dev/null +++ b/src/Network/NeuralNetwork.h @@ -0,0 +1,40 @@ +/** + * DESCRIPTION OF THE FILE + * + * @author Michal KravÄŤenko + * @date 13.6.18 - + */ + +#ifndef INC_4NEURO_NEURALNETWORK_H +#define INC_4NEURO_NEURALNETWORK_H + +enum NET_TYPE{GENERAL}; +/** + * + */ +class NeuralNetwork { +private: + /** + * + */ + NET_TYPE network_type = GENERAL; + + /** + * + */ + int n_neurons = 0; + + +public: + + /** + * + * @param input + * @param expected_output + * @return + */ + //double get_error(DataSet &input, DataSet &expected_output); +}; + + +#endif //INC_4NEURO_NEURALNETWORK_H diff --git a/src/Neuron/Neuron.cpp b/src/Neuron/Neuron.cpp index 592c5384..aa248c73 100644 --- a/src/Neuron/Neuron.cpp +++ b/src/Neuron/Neuron.cpp @@ -6,6 +6,21 @@ Neuron::~Neuron() { delete [] this->activation_function_parameters; this->activation_function_parameters = nullptr; } + + if(this->edges_out){ + + for(auto& i: *this->edges_out){ + delete i; + } + + delete this->edges_out; + this->edges_out = nullptr; + } + + if(this->edges_in){ + delete this->edges_in; + this->edges_in = nullptr; + } } void Neuron::adjust_potential(double input_signal) { @@ -43,3 +58,25 @@ 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]; } + +void Neuron::add_connection_in(Connection *con) { + if(!this->edges_in){ + this->edges_in = new std::vector<Connection*>(0); + } + this->edges_in->push_back(con); +} + +void Neuron::add_connection_out(Connection *con) { + if(!this->edges_out){ + this->edges_out = new std::vector<Connection*>(0); + } + this->edges_out->push_back(con); +} + +std::vector<Connection*>* Neuron::get_connections_in() { + return this->edges_in; +} + +std::vector<Connection*>* Neuron::get_connections_out() { + return this->edges_out; +} \ No newline at end of file diff --git a/src/Neuron/Neuron.h b/src/Neuron/Neuron.h index 72bf1596..375095e3 100644 --- a/src/Neuron/Neuron.h +++ b/src/Neuron/Neuron.h @@ -1,18 +1,22 @@ -#ifndef NEURON_H_ -#define NEURON_H_ - - /** - * A file containing the mother classe representing neurons - * in neural networks. - * - * @author Martin Beseda - * @author Martin Mrovec - * @author Michal KravÄŤenko - * @date 2017 - 2018 - */ +/** + * DESCRIPTION OF THE CLASS + * + * @author Martin Beseda + * @author Martin Mrovec + * @author Michal KravÄŤenko + * @date 2017 - 2018 + */ + + + #ifndef NEURON_H_ + #define NEURON_H_ - /** +#include <vector> +#include "../NetConnection/Connection.h" + +class Connection; +/** * Abstract class representing a general neuron */ class Neuron{ @@ -38,11 +42,22 @@ protected: */ int n_activation_function_parameters = 0; + /** + * A pointer to a vector containing pointers to incoming connections + */ + std::vector<Connection*> *edges_in = nullptr; + + /** + * A pointer to a vector containing pointers to outgoing connections + */ + std::vector<Connection*> *edges_out = nullptr; + public: /** * Destructor of the Neuron object * this level deallocates the array 'activation_function_parameters' + * also deallocates the OUTGOING connections */ virtual ~Neuron(); @@ -124,6 +139,30 @@ public: */ virtual double activation_function_get_parameter(int param_idx); + /** + * Adds a new incoming connection with this neuron as its end-point + * @param[in] con Incoming connection + */ + virtual void add_connection_in(Connection *con) final; + + /** + * Adds a new outgoing connection with this neuron as its source-point + * @param[in] con Outgoing connection + */ + virtual void add_connection_out(Connection *con) final; + + /** + * Returns the pointer to the incoming connection vector + * @return + */ + virtual std::vector<Connection*>* get_connections_in( ) final; + + /** + * Returns the pointer to the outgoing connection vector + * @return + */ + virtual std::vector<Connection*>* get_connections_out( ) final; + }; /* end of Neuron class */ diff --git a/src/Neuron/NeuronBinary.h b/src/Neuron/NeuronBinary.h index 4da2071b..758681df 100644 --- a/src/Neuron/NeuronBinary.h +++ b/src/Neuron/NeuronBinary.h @@ -1,6 +1,11 @@ -// -// Created by fluffymoo on 11.6.18. -// +/** + * DESCRIPTION OF THE CLASS + * + * @author Martin Beseda + * @author Martin Mrovec + * @author Michal KravÄŤenko + * @date 2017 - 2018 + */ #ifndef INC_4NEURO_NEURONBINARY_H #define INC_4NEURO_NEURONBINARY_H diff --git a/src/Neuron/NeuronLinear.h b/src/Neuron/NeuronLinear.h index acff3d80..df125374 100644 --- a/src/Neuron/NeuronLinear.h +++ b/src/Neuron/NeuronLinear.h @@ -1,6 +1,11 @@ -// -// Created by fluffymoo on 11.6.18. -// +/** + * DESCRIPTION OF THE CLASS + * + * @author Martin Beseda + * @author Martin Mrovec + * @author Michal KravÄŤenko + * @date 2017 - 2018 + */ #ifndef INC_4NEURO_NEURONLINEAR_H #define INC_4NEURO_NEURONLINEAR_H diff --git a/src/Neuron/NeuronLogistic.h b/src/Neuron/NeuronLogistic.h index 88333e2a..a3eb8ad5 100644 --- a/src/Neuron/NeuronLogistic.h +++ b/src/Neuron/NeuronLogistic.h @@ -1,6 +1,11 @@ -// -// Created by fluffymoo on 11.6.18. -// +/** + * DESCRIPTION OF THE CLASS + * + * @author Martin Beseda + * @author Martin Mrovec + * @author Michal KravÄŤenko + * @date 2017 - 2018 + */ #ifndef INC_4NEURO_NEURONLOGISTIC_H #define INC_4NEURO_NEURONLOGISTIC_H diff --git a/src/Neuron/NeuronNeuralNet.cpp b/src/Neuron/NeuronNeuralNet.cpp new file mode 100644 index 00000000..70d1307d --- /dev/null +++ b/src/Neuron/NeuronNeuralNet.cpp @@ -0,0 +1,8 @@ +/** + * DESCRIPTION OF THE FILE + * + * @author Michal KravÄŤenko + * @date 13.6.18 - + */ + +#include "NeuronNeuralNet.h" diff --git a/src/Neuron/NeuronNeuralNet.h b/src/Neuron/NeuronNeuralNet.h new file mode 100644 index 00000000..b081dc09 --- /dev/null +++ b/src/Neuron/NeuronNeuralNet.h @@ -0,0 +1,17 @@ +/** + * DESCRIPTION OF THE FILE + * + * @author Michal KravÄŤenko + * @date 13.6.18 - + */ + +#ifndef INC_4NEURO_NEURONNEURALNET_H +#define INC_4NEURO_NEURONNEURALNET_H + + +class NeuronNeuralNet { + +}; + + +#endif //INC_4NEURO_NEURONNEURALNET_H diff --git a/src/Neuron/NeuronTanh.h b/src/Neuron/NeuronTanh.h index 9a802b3f..8559eeea 100644 --- a/src/Neuron/NeuronTanh.h +++ b/src/Neuron/NeuronTanh.h @@ -1,6 +1,11 @@ -// -// Created by fluffymoo on 11.6.18. -// +/** + * DESCRIPTION OF THE CLASS + * + * @author Martin Beseda + * @author Martin Mrovec + * @author Michal KravÄŤenko + * @date 2017 - 2018 + */ #ifndef INC_4NEURO_NEURONTANH_H #define INC_4NEURO_NEURONTANH_H -- GitLab