diff --git a/.idea/usage.statistics.xml b/.idea/usage.statistics.xml index ab4710f5d990878b66ad3f8a2849029e120f448b..317c2d7f6af1173e8e532d4fe3c83dbc34a255f7 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="37" /> + <entry key="cpp" value="58" /> <entry key="f90" value="4" /> - <entry key="h" value="45" /> - <entry key="txt" value="4" /> + <entry key="h" value="71" /> + <entry key="txt" value="7" /> </counts> </usages-collector> <usages-collector id="statistics.file.types.open"> <counts> - <entry key="CMakeLists.txt" value="4" /> - <entry key="ObjectiveC" value="82" /> + <entry key="CMakeLists.txt" value="7" /> + <entry key="ObjectiveC" value="129" /> <entry key="PLAIN_TEXT" value="5" /> </counts> </usages-collector> <usages-collector id="statistics.file.extensions.edit"> <counts> - <entry key="cpp" value="2761" /> - <entry key="h" value="5108" /> - <entry key="txt" value="451" /> + <entry key="cpp" value="6278" /> + <entry key="h" value="7184" /> + <entry key="txt" value="704" /> </counts> </usages-collector> <usages-collector id="statistics.file.types.edit"> <counts> - <entry key="CMakeLists.txt" value="112" /> - <entry key="Doxygen file" value="70" /> - <entry key="ObjectiveC" value="7799" /> - <entry key="PLAIN_TEXT" value="339" /> + <entry key="CMakeLists.txt" value="139" /> + <entry key="Doxygen file" value="104" /> + <entry key="ObjectiveC" value="13358" /> + <entry key="PLAIN_TEXT" value="565" /> </counts> </usages-collector> </session> diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 69b1d8c454f660e61a69a66f06f5a2399c05fb49..77f9d8ed79657401452d5fe80b887eaa0a671c6d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,6 @@ -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) +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 NetConnection/ConnectionWeight.cpp NetConnection/ConnectionWeight.h NetConnection/ConnectionWeightIdentity.cpp NetConnection/ConnectionWeightIdentity.h) +add_executable(test_cases main.cpp) + +target_link_libraries(test_cases neuron) \ No newline at end of file diff --git a/src/NetConnection/Connection.cpp b/src/NetConnection/Connection.cpp index b349e2e5bac13448dd2526969381d30417c1cece..a11c1703812b546c0dd5edff2f46ecec015ea791 100644 --- a/src/NetConnection/Connection.cpp +++ b/src/NetConnection/Connection.cpp @@ -7,20 +7,22 @@ #include "Connection.h" -Connection::Connection(Neuron *n_in, Neuron *n_out, double edge_weight) { +Connection::Connection(Neuron *n_in, Neuron *n_out, ConnectionWeight* con) { this->neuron_in = n_in; this->neuron_out = n_out; - this->weight = edge_weight; + + this->con = con; } //Connection::~Connection() { // //} -void Connection::adjust_weight(double value) { - this->weight += value; +void Connection::adjust_weights(double* values) { + this->con->adjust_weights(values); } + Neuron* Connection::get_neuron_in() { return this->neuron_in; } @@ -29,14 +31,14 @@ Neuron* Connection::get_neuron_out() { return this->neuron_out; } -double Connection::get_weight() { - return this->weight; -} +//double Connection::get_weight() { +// return this->weight; +//} void Connection::pass_signal() { - this->neuron_out->adjust_potential(this->neuron_in->get_state() * this->weight); + this->neuron_out->adjust_potential(this->neuron_in->get_state() * this->con->eval()); } -void Connection::set_weight(double value) { - this->weight = value; +void Connection::set_weights(double *values) { + this->con->set_weights(values); } diff --git a/src/NetConnection/Connection.h b/src/NetConnection/Connection.h index 038537cff812da3d3379c448acfe6753fc2c49fc..bdcbfb3cecb76d6639f3d77147481d36b4b40042 100644 --- a/src/NetConnection/Connection.h +++ b/src/NetConnection/Connection.h @@ -9,27 +9,31 @@ #define INC_4NEURO_CONNECTION_H #include "../Neuron/Neuron.h" +#include "ConnectionWeight.h" + class Neuron; +class ConnectionWeight; /** * */ class Connection { private: + /** * */ - Neuron *neuron_in = nullptr; - + ConnectionWeight *con = nullptr; /** * */ - Neuron *neuron_out = nullptr; + Neuron *neuron_in = nullptr; /** * */ - double weight = 0.0; + Neuron *neuron_out = nullptr; + //TODO pridat gradient @@ -39,9 +43,8 @@ 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, ConnectionWeight* con); // Connection(Neuron *n_in, Neuron *n_out, Connection* ref_con); @@ -52,21 +55,21 @@ public: /** * - * @param[in] value + * @param[in] values */ - void adjust_weight(double value); + void adjust_weights(double *values); /** * - * @param[in] value + * @param[in] values */ - void set_weight(double value); + void set_weights(double *values); - /** - * - * @return - */ - double get_weight(); +// /** +// * +// * @return +// */ +// double get_weight(); /** diff --git a/src/NetConnection/ConnectionWeight.cpp b/src/NetConnection/ConnectionWeight.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4fdf7e27983f43d021a38e716bba6f1f134936c --- /dev/null +++ b/src/NetConnection/ConnectionWeight.cpp @@ -0,0 +1,53 @@ +/** + * DESCRIPTION OF THE FILE + * + * @author Michal KravÄŤenko + * @date 14.6.18 - + */ + +#include "ConnectionWeight.h" + +ConnectionWeight::ConnectionWeight() { + + +} + +ConnectionWeight::ConnectionWeight(int param_count, std::function<double(double **, int)> f) { + this->param_ptrs = new double*[param_count]; + this->n_params = param_count; + + this->weight_function = f; +} + +ConnectionWeight::~ConnectionWeight() { + if(this->param_ptrs){ + delete [] this->param_ptrs; + this->param_ptrs = nullptr; + } +} + +void ConnectionWeight::adjust_weights(double *values) { + for(int i = 0; i < this->n_params; ++i){ + (*this->param_ptrs[i]) += values[i]; + } +} + +void ConnectionWeight::set_weights(double *values) { + for(int i = 0; i < this->n_params; ++i){ + (*this->param_ptrs[i]) = values[i]; + } +} + +void ConnectionWeight::SetParamPointer(double **param_ptr) { + for(int i = 0; i < this->n_params; ++i){ + this->param_ptrs[i] = param_ptr[i]; + } +} + +void ConnectionWeight::SetParamPointer(double *param_ptr, int idx) { + this->param_ptrs[idx] = param_ptr; +} + +double ConnectionWeight::eval() { + return this->weight_function(this->param_ptrs, this->n_params); +} \ No newline at end of file diff --git a/src/NetConnection/ConnectionWeight.h b/src/NetConnection/ConnectionWeight.h new file mode 100644 index 0000000000000000000000000000000000000000..295549abedb57f4b856321bee09d2becd9015740 --- /dev/null +++ b/src/NetConnection/ConnectionWeight.h @@ -0,0 +1,83 @@ +/** + * DESCRIPTION OF THE FILE + * + * @author Michal KravÄŤenko + * @date 14.6.18 - + */ + +#ifndef INC_4NEURO_CONNECTIONWEIGHT_H +#define INC_4NEURO_CONNECTIONWEIGHT_H + +#include <functional> + +class ConnectionWeight { +protected: + /** + * + */ + double ** param_ptrs = nullptr; + + /** + * + */ + int n_params = 0; + + /** + * + */ + std::function<double(double **, int)> weight_function; + +public: + + /** + * + */ + ConnectionWeight(); + + /** + * + * @param param_count + * @param f + */ + ConnectionWeight(int param_count, std::function<double(double **, int)> f); + + /** + * + * @param param_ptr + * @param idx + */ + void SetParamPointer(double *param_ptr, int idx); + + /** + * + * @param param_ptr + */ + void SetParamPointer(double **param_ptr); + + /** + * + */ + ~ConnectionWeight(); + + /** + * + * @return + */ + virtual double eval(); + + /** + * + * @param values + */ + void set_weights(double *values); + + /** + * + * @param values + */ + void adjust_weights(double *values); + +}; + + +#endif //INC_4NEURO_CONNECTIONWEIGHT_H diff --git a/src/NetConnection/ConnectionWeightIdentity.cpp b/src/NetConnection/ConnectionWeightIdentity.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ae69a747511dffe07f3a519369dd96ce3a5ae9c --- /dev/null +++ b/src/NetConnection/ConnectionWeightIdentity.cpp @@ -0,0 +1,17 @@ +/** + * DESCRIPTION OF THE FILE + * + * @author Michal KravÄŤenko + * @date 14.6.18 - + */ + +#include "ConnectionWeightIdentity.h" + +ConnectionWeightIdentity::ConnectionWeightIdentity() { + this->n_params = 1; + this->param_ptrs = new double*[1]; +} + +double ConnectionWeightIdentity::eval() { + return (*this->param_ptrs[0]); +} \ No newline at end of file diff --git a/src/NetConnection/ConnectionWeightIdentity.h b/src/NetConnection/ConnectionWeightIdentity.h new file mode 100644 index 0000000000000000000000000000000000000000..e121ddf706e30162250e31fe47fdb3e4a15bcec6 --- /dev/null +++ b/src/NetConnection/ConnectionWeightIdentity.h @@ -0,0 +1,28 @@ +/** + * DESCRIPTION OF THE FILE + * + * @author Michal KravÄŤenko + * @date 14.6.18 - + */ + +#ifndef INC_4NEURO_CONNECTIONWEIGHTIDENTITY_H +#define INC_4NEURO_CONNECTIONWEIGHTIDENTITY_H + +#include "ConnectionWeight.h" + +class ConnectionWeight; + +/** + * + */ +class ConnectionWeightIdentity:public ConnectionWeight { +private: +public: + + ConnectionWeightIdentity(); + + double eval() override; +}; + + +#endif //INC_4NEURO_CONNECTIONWEIGHTIDENTITY_H diff --git a/src/Network/NeuralNetwork.cpp b/src/Network/NeuralNetwork.cpp index c0adb1163ab4ea84ee01248aaf85e5d4c7a82472..a5090d0ac1cb5989195b79b216aaa3176c44aa6c 100644 --- a/src/Network/NeuralNetwork.cpp +++ b/src/Network/NeuralNetwork.cpp @@ -6,3 +6,149 @@ */ #include "NeuralNetwork.h" + +NeuralNetwork::NeuralNetwork() { + this->neurons = new std::vector<Neuron*>(0); +} + +NeuralNetwork::~NeuralNetwork() { + if(this->neurons){ + delete this->neurons; + this->neurons = nullptr; + } + if(this->output_neurons){ + delete this->output_neurons; + this->output_neurons = nullptr; + } + if(this->input_neurons){ + delete this->input_neurons; + this->input_neurons = nullptr; + } + if(this->active_eval_set){ + delete this->active_eval_set; + this->active_eval_set = nullptr; + } +} + +void NeuralNetwork::add_neuron(Neuron *n) { + this->neurons->push_back(n); + this->in_out_determined = false; +} + +void NeuralNetwork::determine_inputs_outputs() { + if(this->output_neurons){ + delete this->output_neurons; + } + + if(this->input_neurons){ + delete this->input_neurons; + } + + this->output_neurons = new std::vector<Neuron*>(0); + this->input_neurons = new std::vector<Neuron*>(0); + + if(this->active_eval_set == nullptr){ + this->active_eval_set = new std::vector<Neuron*>(this->neurons->size() * 2); + } + else{ + this->active_eval_set->resize(this->neurons->size() * 2); + } + + for(Neuron* neuron: *this->neurons){ + if(neuron->get_connections_out()->empty()){ + //this neuron has no outgoing connections, it is the output neuron + this->output_neurons->push_back(neuron); + } + else if(neuron->get_connections_in()->empty()){ + //this neuron has no incoming connections, it is the input neuron + this->input_neurons->push_back(neuron); + } + } + + this->n_inputs = (int)this->input_neurons->size(); + this->n_outputs = (int)this->output_neurons->size(); + + this->in_out_determined = true; +} + +void NeuralNetwork::eval_single(std::vector<double> &input, std::vector<double> &output) { + if(!this->in_out_determined){ + this->determine_inputs_outputs(); + } + + + if(this->n_inputs != input.size()){ + printf("Error, input size != Network input size\n"); + return; + } + + if(this->n_outputs != output.size()){ + printf("Error, output size != Network output size\n"); + return; + } + + std::fill(output.begin(), output.end(), 0.0); + + //reset of the potentials + for(Neuron* neuron: *this->neurons){ + neuron->set_potential(0.0); + neuron->set_saturation_in(0); + neuron->set_saturation_out(0); + } + + + int active_set_size[2]; + active_set_size[0] = 0; + active_set_size[1] = 0; + + int idx1 = 0, idx2 = 1; + + active_set_size[0] = this->n_inputs; + int i = 0; + auto n = this->neurons->size(); + + for(Neuron* neuron: *this->input_neurons){ + + this->active_eval_set->at(i) = neuron; + + neuron->set_potential(input[i]); + + + ++i; + } + Neuron* active_neuron; + Neuron* target_neuron; + + while(active_set_size[idx1] > 0){ + + //we iterate through the active neurons and propagate the signal + for(i = 0; i < active_set_size[idx1]; ++i){ + active_neuron = this->active_eval_set->at(i + n * idx1); + active_neuron->activate(); + + for(Connection* connection: *(active_neuron->get_connections_out())){ + connection->pass_signal(); + + target_neuron = connection->get_neuron_out(); + target_neuron->adjust_saturation_in(1); + + if(target_neuron->is_saturated_in()){ + this->active_eval_set->at(active_set_size[idx2] + n * idx2) = target_neuron; + active_set_size[idx2]++; + } + } + } + + idx1 = idx2; + idx2 = (idx1 + 1) % 2; + + active_set_size[idx2] = 0; + } + + i = 0; + for(Neuron* neuron: *this->output_neurons){ + + output[i] = neuron->get_state(); + ++i; + } +} \ No newline at end of file diff --git a/src/Network/NeuralNetwork.h b/src/Network/NeuralNetwork.h index 5036801cc3a0292ed466b68e930eedb773f869ad..d6acc945fd8dab5e216a39b61ab06f0f6bc2b7c0 100644 --- a/src/Network/NeuralNetwork.h +++ b/src/Network/NeuralNetwork.h @@ -5,10 +5,17 @@ * @date 13.6.18 - */ + //TODO pouvazovat o pridani indexu k neuronum, abychom meli urcitou kontrolu nad poradim vstupu a vystupu? + #ifndef INC_4NEURO_NEURALNETWORK_H #define INC_4NEURO_NEURALNETWORK_H +#include <vector> +#include "../Neuron/Neuron.h" + enum NET_TYPE{GENERAL}; + + /** * */ @@ -19,21 +26,72 @@ private: */ NET_TYPE network_type = GENERAL; + + /** + * + */ + std::vector<Neuron*> *neurons = nullptr; + + /** + * + */ + std::vector<Neuron*>* input_neurons = nullptr; + + /** + * + */ + std::vector<Neuron*>* output_neurons = nullptr; + + /** + * + */ + int n_inputs = -1; + + /** + * + */ + int n_outputs = -1; + + /** + * + */ + bool in_out_determined = false; + + std::vector<Neuron*>* active_eval_set = nullptr; /** * */ - int n_neurons = 0; - + void determine_inputs_outputs(); public: /** * - * @param input - * @param expected_output - * @return */ - //double get_error(DataSet &input, DataSet &expected_output); + NeuralNetwork(); + + /** + * + */ + ~NeuralNetwork(); + + /** + * + * @param[in] input + * @param[in,out] output + */ + void eval_single(std::vector<double> &input, std::vector<double> &output); + + + + /** + * + * @param n + */ + void add_neuron(Neuron* n); + + + }; diff --git a/src/Neuron/Neuron.cpp b/src/Neuron/Neuron.cpp index aa248c73911534da804456635225c3b125b6de50..bb074f84a5baa996746230306d152ec4b988664f 100644 --- a/src/Neuron/Neuron.cpp +++ b/src/Neuron/Neuron.cpp @@ -8,11 +8,6 @@ Neuron::~Neuron() { } if(this->edges_out){ - - for(auto& i: *this->edges_out){ - delete i; - } - delete this->edges_out; this->edges_out = nullptr; } @@ -23,6 +18,40 @@ Neuron::~Neuron() { } } +void Neuron::set_saturation_in(int value) { + this->n_saturated_connections_in = value; +} + +void Neuron::adjust_saturation_in(int value) { + this->n_saturated_connections_in += value; +} + +bool Neuron::is_saturated_in() { + if(this->n_saturated_connections_in == this->edges_in->size()){ + return true; + } + + return false; +} + +void Neuron::set_saturation_out(int value) { + this->n_saturated_connections_out = value; +} + +void Neuron::adjust_saturation_out(int value) { + this->n_saturated_connections_out += value; +} + + +bool Neuron::is_saturated_out() { + if(this->n_saturated_connections_out == this->edges_out->size()){ + return true; + } + + return false; +} + + void Neuron::adjust_potential(double input_signal) { this->potential += input_signal; } diff --git a/src/Neuron/Neuron.h b/src/Neuron/Neuron.h index 375095e3b478ab6d86b116bec02d62d38a0e76b8..582f36b3ee1f8ab2d8799fdcd7ae400b76d32c94 100644 --- a/src/Neuron/Neuron.h +++ b/src/Neuron/Neuron.h @@ -42,6 +42,16 @@ protected: */ int n_activation_function_parameters = 0; + /** + * Keeps track of the number of saturated INCOMING connections + */ + int n_saturated_connections_in = 0; + + /** + * Keeps track of the number of saturated OUTGOING connections + */ + int n_saturated_connections_out = 0; + /** * A pointer to a vector containing pointers to incoming connections */ @@ -66,6 +76,42 @@ public: */ virtual void activate( ) = 0; + /** + * Sets the number of saturated INPUT connections to the prescribed value + * @param[in] value The number of saturated INCOMING connections + */ + virtual void set_saturation_in(int value) final; + + /** + * Increases the saturation counter of the INCOMING connections by the specified amount + * @param value Amount to be added to the counter + */ + virtual void adjust_saturation_in(int value) final; + + /** + * Returns true if the neuron is saturated via the INCOMING connections + * @return true/false + */ + virtual bool is_saturated_in() final; + + /** + * Returns true if the neuron is saturated via the OUTGOING connections + * @return true/false + */ + virtual bool is_saturated_out() final; + + /** + * Sets the number of saturated OUTPUT connections to the prescribed value + * @param[in] value The number of saturated OUTGOING connections + */ + virtual void set_saturation_out(int value) final; + + /** + * Increases the saturation counter of the OUTGOING connections by the specified amount + * @param value Amount to be added to the counter + */ + virtual void adjust_saturation_out(int value) final; + /** * Adds the input signal value to the current potential * @param[in] input_signal Input value diff --git a/src/Neuron/NeuronBinary.cpp b/src/Neuron/NeuronBinary.cpp index 4d08568a907df321e5cd4540a07a03a80cf56f3f..28a61282c796c06c03bb41bd29e834f77186126f 100644 --- a/src/Neuron/NeuronBinary.cpp +++ b/src/Neuron/NeuronBinary.cpp @@ -7,6 +7,9 @@ NeuronBinary::NeuronBinary(double threshold) { this->activation_function_parameters = new double[1]; this->activation_function_parameters[0] = threshold; + + this->edges_in = new std::vector<Connection*>(0); + this->edges_out = new std::vector<Connection*>(0); } void NeuronBinary::activate( ) { diff --git a/src/Neuron/NeuronBinary.h b/src/Neuron/NeuronBinary.h index 758681df45790e8af2e2584cd08d768f36990cc7..1f4b4906eaec69c7b18c53b239109fb5a1231df6 100644 --- a/src/Neuron/NeuronBinary.h +++ b/src/Neuron/NeuronBinary.h @@ -15,7 +15,7 @@ /** * Binary neuron class - uses unit-step as the activation function */ -class NeuronBinary:Neuron { +class NeuronBinary:public Neuron { private: public: diff --git a/src/Neuron/NeuronLinear.cpp b/src/Neuron/NeuronLinear.cpp index dc7491459dd40fc03344074f261411b2f19b5f60..e08fcc09af12337f7f5f523a8740bbe9c05083e8 100644 --- a/src/Neuron/NeuronLinear.cpp +++ b/src/Neuron/NeuronLinear.cpp @@ -11,6 +11,9 @@ NeuronLinear::NeuronLinear(double a, double b) { this->activation_function_parameters[0] = a; this->activation_function_parameters[1] = b; + this->edges_in = new std::vector<Connection*>(0); + this->edges_out = new std::vector<Connection*>(0); + } void NeuronLinear::activate( ) { diff --git a/src/Neuron/NeuronLinear.h b/src/Neuron/NeuronLinear.h index df125374b56394929089f6f99d0d85c2d2a8f7bb..b315e976c15d62e7b8d2d7ec009b3f348ac46e95 100644 --- a/src/Neuron/NeuronLinear.h +++ b/src/Neuron/NeuronLinear.h @@ -16,7 +16,7 @@ * Linear neuron class - uses activation function in the form f(x)=a*x + b, * 'x' being the neuron's potential */ -class NeuronLinear:Neuron { +class NeuronLinear:public Neuron { public: /** @@ -51,36 +51,3 @@ public: #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 index a975154f34cee0735bf2b73a3e9f41f649baff3b..10e586fc30be5706257025e8f48fc8bfbbd0c74e 100644 --- a/src/Neuron/NeuronLogistic.cpp +++ b/src/Neuron/NeuronLogistic.cpp @@ -11,6 +11,9 @@ NeuronLogistic::NeuronLogistic(double a, double b) { this->activation_function_parameters[0] = a; this->activation_function_parameters[1] = b; + + this->edges_in = new std::vector<Connection*>(0); + this->edges_out = new std::vector<Connection*>(0); } void NeuronLogistic::activate( ) { diff --git a/src/Neuron/NeuronLogistic.h b/src/Neuron/NeuronLogistic.h index a3eb8ad51c0be28ca10611ccf02afc542baf9da1..99e8de11fabac22009dd992607f60b8525be2a57 100644 --- a/src/Neuron/NeuronLogistic.h +++ b/src/Neuron/NeuronLogistic.h @@ -14,7 +14,7 @@ #include "Neuron.h" #include "../constants.h" -class NeuronLogistic:Neuron { +class NeuronLogistic:public Neuron { /** * Constructs the object of the Logistic neuron with activation function * f(x) = (1 + e^(-x + b))^(-a) diff --git a/src/Neuron/NeuronNeuralNet.h b/src/Neuron/NeuronNeuralNet.h index b081dc0988cb32550d73d4847e8185dadcc0d701..35cb2a3ac180c2ca8a8201e4dbb66bcccb78568d 100644 --- a/src/Neuron/NeuronNeuralNet.h +++ b/src/Neuron/NeuronNeuralNet.h @@ -8,8 +8,9 @@ #ifndef INC_4NEURO_NEURONNEURALNET_H #define INC_4NEURO_NEURONNEURALNET_H +#include "Neuron.h" -class NeuronNeuralNet { +class NeuronNeuralNet:public Neuron { }; diff --git a/src/Neuron/NeuronTanh.cpp b/src/Neuron/NeuronTanh.cpp index c9d47dd6f1586fe337de2e722c7de35ae6cb032f..b6cd25a4353261d96c9c232d4a228e579c8179cf 100644 --- a/src/Neuron/NeuronTanh.cpp +++ b/src/Neuron/NeuronTanh.cpp @@ -9,6 +9,9 @@ NeuronTanh::NeuronTanh(double a) { this->activation_function_parameters = new double[1]; this->activation_function_parameters[0] = a; + this->edges_in = new std::vector<Connection*>(0); + this->edges_out = new std::vector<Connection*>(0); + } void NeuronTanh::activate( ) { diff --git a/src/Neuron/NeuronTanh.h b/src/Neuron/NeuronTanh.h index 8559eeea774108dc937a071be2ad789912c7dfd9..d8dca5f832a242e60ab97ebe27c61c432c2025ad 100644 --- a/src/Neuron/NeuronTanh.h +++ b/src/Neuron/NeuronTanh.h @@ -14,7 +14,7 @@ #include "Neuron.h" #include "../constants.h" -class NeuronTanh:Neuron { +class NeuronTanh:public 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)) diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e601007053fccd05facc360af83ba64b58641bc --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,68 @@ +#include <cstdio> + +/** + * DESCRIPTION OF THE FILE + * + * @author Michal KravÄŤenko + * @date 14.6.18 - + */ + +#include "Network/NeuralNetwork.h" + +#include "Neuron/NeuronLinear.h" + +#include "NetConnection/Connection.h" + +#include "NetConnection/ConnectionWeightIdentity.h" + +/** + * Test of simple neural network + * Network should evaluate the function f(x) = x + 1 + */ +void test1(){ + std::vector<double> in(1); + std::vector<double> out(1); + NeuralNetwork net; + + NeuronLinear u1(1.0, 1.0); //f(x) = x + 1.0 + NeuronLinear u2(0.0, 1.0); //f(x) = x + + auto * parameters = new double[1]; + parameters[0] = 1.0; + +// ConnectionWeight con_weight_u1u2(1, [](double ** params, int n_params){ return (*params[0]);}); + ConnectionWeightIdentity con_weight_u1u2; + +// con_weight_u1u2.SetParamPointer(¶meters[0], 0); + con_weight_u1u2.SetParamPointer(¶meters); + + + Connection u1u2(&u1, &u2, &con_weight_u1u2); + + u1.add_connection_out(&u1u2); + u2.add_connection_in(&u1u2); + + net.add_neuron(&u1); + net.add_neuron(&u2); + for(int i = 0; i < 20; ++i){ + in[0] = 0.05 * i; + net.eval_single(in, out); + + printf("x = %3.2f, f(x) = %3.2f, expected output = %3.2f\n", in[0], out[0], in[0] + 1.0); + } + + + + + + delete [] parameters; +} + +int main(int argc, char** argv){ + + test1(); + + printf("hello biatch\n"); + + return 0; +} \ No newline at end of file