Commit 083d2be6 authored by Michal Kravcenko's avatar Michal Kravcenko

Commit before merge

parent 0802a4da
This diff is collapsed.
...@@ -10,16 +10,13 @@ ...@@ -10,16 +10,13 @@
#include "../src/DataSet/DataSet.h" #include "../src/DataSet/DataSet.h"
#include "../src/ErrorFunction/ErrorFunctions.h" #include "../src/ErrorFunction/ErrorFunctions.h"
#include "../src/LearningMethods/ParticleSwarm.h" #include "../src/LearningMethods/ParticleSwarm.h"
#include "../src/NetConnection/Connection.h" #include "../src/NetConnection/ConnectionFunctionGeneral.h"
#include "../src/NetConnection/ConnectionWeight.h" #include "../src/NetConnection/ConnectionFunctionIdentity.h"
#include "../src/NetConnection/ConnectionWeightIdentity.h"
#include "../src/Network/NeuralNetwork.h" #include "../src/Network/NeuralNetwork.h"
#include "../src/Network/NeuralNetworkSum.h" #include "../src/Network/NeuralNetworkSum.h"
#include "../src/Neuron/Neuron.h" #include "../src/Neuron/Neuron.h"
#include "../src/Neuron/NeuronBinary.h" #include "../src/Neuron/NeuronBinary.h"
#include "../src/Neuron/NeuronLinear.h" #include "../src/Neuron/NeuronLinear.h"
#include "../src/Neuron/NeuronLogistic.h" #include "../src/Neuron/NeuronLogistic.h"
#include "../src/Neuron/NeuronNeuralNet.h"
#include "../src/Neuron/NeuronTanh.h"
#endif //INC_4NEURO_4NEURO_H #endif //INC_4NEURO_4NEURO_H
...@@ -5,15 +5,12 @@ add_library(4neuro SHARED ...@@ -5,15 +5,12 @@ add_library(4neuro SHARED
Neuron/NeuronBinary.cpp Neuron/NeuronBinary.cpp
Neuron/NeuronLinear.cpp Neuron/NeuronLinear.cpp
Neuron/NeuronLogistic.cpp Neuron/NeuronLogistic.cpp
Neuron/NeuronTanh.cpp
NetConnection/Connection.cpp
Network/NeuralNetwork.cpp Network/NeuralNetwork.cpp
Neuron/NeuronNeuralNet.cpp NetConnection/ConnectionFunctionGeneral.cpp
NetConnection/ConnectionWeight.cpp NetConnection/ConnectionFunctionIdentity.cpp
NetConnection/ConnectionWeightIdentity.cpp
LearningMethods/ParticleSwarm.cpp LearningMethods/ParticleSwarm.cpp
DataSet/DataSet.cpp DataSet/DataSet.cpp
ErrorFunction/ErrorFunctions.cpp Network/NeuralNetworkSum.cpp Network/NeuralNetworkSum.h Solvers/DESolver.cpp Solvers/DESolver.h) ErrorFunction/ErrorFunctions.cpp Network/NeuralNetworkSum.cpp Network/NeuralNetworkSum.h Solvers/DESolver.cpp Solvers/DESolver.h Neuron/NeuronConstant.cpp Neuron/NeuronConstant.h)
target_link_libraries(4neuro boost_serialization) target_link_libraries(4neuro boost_serialization)
...@@ -40,6 +37,9 @@ target_link_libraries(net_test_3 4neuro) ...@@ -40,6 +37,9 @@ target_link_libraries(net_test_3 4neuro)
add_executable(net_test_ode_1 net_test_ode_1.cpp) add_executable(net_test_ode_1 net_test_ode_1.cpp)
target_link_libraries(net_test_ode_1 4neuro) target_link_libraries(net_test_ode_1 4neuro)
add_executable(net_test_pde_1 net_test_pde_1.cpp)
target_link_libraries(net_test_pde_1 4neuro)
############## ##############
# UNIT TESTS # # UNIT TESTS #
############## ##############
...@@ -57,15 +57,10 @@ target_link_libraries(binary_neuron_test boost_unit_test 4neuro) ...@@ -57,15 +57,10 @@ target_link_libraries(binary_neuron_test boost_unit_test 4neuro)
add_executable(logistic_neuron_test tests/NeuronLogistic_test.cpp) add_executable(logistic_neuron_test tests/NeuronLogistic_test.cpp)
target_link_libraries(logistic_neuron_test boost_unit_test 4neuro) target_link_libraries(logistic_neuron_test boost_unit_test 4neuro)
add_executable(tanh_neuron_test tests/NeuronTanh.cpp)
target_link_libraries(tanh_neuron_test boost_unit_test 4neuro)
add_executable(connection_weight_test tests/ConnectionWeight_test.cpp) add_executable(connection_weight_test tests/ConnectionWeight_test.cpp)
target_link_libraries(connection_weight_test boost_unit_test 4neuro) target_link_libraries(connection_weight_test boost_unit_test 4neuro)
add_executable(connection_test tests/Connection_test.cpp)
target_link_libraries(connection_test boost_unit_test 4neuro)
add_executable(neural_network_test tests/NeuralNetwork_test.cpp) add_executable(neural_network_test tests/NeuralNetwork_test.cpp)
target_link_libraries(neural_network_test boost_unit_test 4neuro) target_link_libraries(neural_network_test boost_unit_test 4neuro)
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
// //
#include <vector> #include <vector>
#include <utility>
#include "ErrorFunctions.h" #include "ErrorFunctions.h"
...@@ -14,10 +13,10 @@ size_t ErrorFunction::get_dimension() { ...@@ -14,10 +13,10 @@ size_t ErrorFunction::get_dimension() {
MSE::MSE(NeuralNetwork *net, DataSet *ds) { MSE::MSE(NeuralNetwork *net, DataSet *ds) {
this->net = net; this->net = net;
this->ds = ds; this->ds = ds;
this->dimension = net->get_n_weights(); this->dimension = net->get_n_weights() + net->get_n_biases();
} }
double MSE::eval(double *weights) { double MSE::eval(std::vector<double> *weights) {
unsigned int dim_out = this->ds->get_output_dim(); unsigned int dim_out = this->ds->get_output_dim();
// unsigned int dim_in = this->ds->get_input_dim(); // unsigned int dim_in = this->ds->get_input_dim();
size_t n_elements = this->ds->get_n_elements(); size_t n_elements = this->ds->get_n_elements();
...@@ -66,7 +65,7 @@ ErrorSum::~ErrorSum(){ ...@@ -66,7 +65,7 @@ ErrorSum::~ErrorSum(){
} }
} }
double ErrorSum::eval(double *weights) { double ErrorSum::eval(std::vector<double> *weights) {
double output = 0.0; double output = 0.0;
for( unsigned int i = 0; i < this->summand->size(); ++i ){ for( unsigned int i = 0; i < this->summand->size(); ++i ){
......
...@@ -20,7 +20,7 @@ public: ...@@ -20,7 +20,7 @@ public:
* @param weights * @param weights
* @return * @return
*/ */
virtual double eval(double* weights) = 0; virtual double eval(std::vector<double>* weights = nullptr) = 0;
/** /**
* *
...@@ -51,7 +51,7 @@ public: ...@@ -51,7 +51,7 @@ public:
* @param weights * @param weights
* @return * @return
*/ */
virtual double eval(double* weights = nullptr); virtual double eval(std::vector<double>* weights = nullptr);
private: private:
...@@ -76,7 +76,7 @@ public: ...@@ -76,7 +76,7 @@ public:
* @param weights * @param weights
* @return * @return
*/ */
virtual double eval(double* weights = nullptr); virtual double eval(std::vector<double>* weights = nullptr);
/** /**
* *
......
This diff is collapsed.
...@@ -26,10 +26,10 @@ class Particle{ ...@@ -26,10 +26,10 @@ class Particle{
private: private:
size_t coordinate_dim; size_t coordinate_dim;
double *coordinate = nullptr; std::vector<double> *coordinate = nullptr;
double *velocity; std::vector<double> *velocity = nullptr;
double *optimal_coordinate = nullptr; std::vector<double> *optimal_coordinate = nullptr;
double optimal_value; double optimal_value;
double r1; double r1;
...@@ -42,10 +42,15 @@ private: ...@@ -42,10 +42,15 @@ private:
double *domain_bounds; double *domain_bounds;
void print_coordinate();
public: public:
/**
*
*/
void print_coordinate();
/** /**
* *
* @param f_dim * @param f_dim
...@@ -57,7 +62,7 @@ public: ...@@ -57,7 +62,7 @@ public:
* *
* @return * @return
*/ */
double* get_coordinate(); std::vector<double>* get_coordinate();
/** /**
* *
...@@ -85,7 +90,7 @@ public: ...@@ -85,7 +90,7 @@ public:
* @param glob_min_coord * @param glob_min_coord
* @param penalty_coef * @param penalty_coef
*/ */
double change_coordinate(double w, double c1, double c2, std::vector<double> glob_min_coord, std::vector<std::vector<double>> global_min_vec, double penalty_coef=0.25); double change_coordinate(double w, double c1, double c2, std::vector<double> &glob_min_coord, std::vector<std::vector<double>> &global_min_vec, double penalty_coef=0.25);
}; };
...@@ -101,13 +106,13 @@ private: ...@@ -101,13 +106,13 @@ private:
/** /**
* *
*/ */
double (*func)(NeuralNetwork, double*, DataSet); ErrorFunction* f;
size_t func_dim; size_t func_dim;
unsigned int n_particles; size_t n_particles;
unsigned int iter_max; size_t iter_max;
double c1; double c1;
...@@ -121,6 +126,8 @@ private: ...@@ -121,6 +126,8 @@ private:
double *domain_bounds; double *domain_bounds;
std::vector<double> *p_min_glob = nullptr;
protected: protected:
/** /**
* *
...@@ -134,7 +141,7 @@ protected: ...@@ -134,7 +141,7 @@ protected:
* *
* @return * @return
*/ */
double* get_centroid_coordinates(); std::vector<double>* get_centroid_coordinates();
/** /**
* *
...@@ -143,7 +150,7 @@ protected: ...@@ -143,7 +150,7 @@ protected:
* @param n * @param n
* @return * @return
*/ */
double get_euclidean_distance(double* a, double* b, unsigned int n); double get_euclidean_distance(std::vector<double>* a, std::vector<double>* b);
public: public:
...@@ -158,7 +165,7 @@ public: ...@@ -158,7 +165,7 @@ public:
* @param n_particles * @param n_particles
* @param iter_max * @param iter_max
*/ */
ParticleSwarm( ErrorFunction* ef, double* domain_bounds, double c1, double c2, double w, unsigned int n_particles, unsigned int iter_max = 1 ); ParticleSwarm( ErrorFunction* ef, double* domain_bounds, double c1, double c2, double w, size_t n_particles, size_t iter_max = 1 );
/** /**
* *
...@@ -174,6 +181,12 @@ public: ...@@ -174,6 +181,12 @@ public:
*/ */
void optimize( double gamma, double epsilon, double delta=0.7 ); void optimize( double gamma, double epsilon, double delta=0.7 );
/**
*
* @return
*/
std::vector<double>* get_solution();
}; };
......
...@@ -6,58 +6,31 @@ ...@@ -6,58 +6,31 @@
*/ */
#include "ConnectionWeight.h" #include "ConnectionFunctionGeneral.h"
ConnectionWeight::ConnectionWeight() { ConnectionFunctionGeneral::ConnectionFunctionGeneral() {
} }
ConnectionWeight::ConnectionWeight(int param_count, std::vector<double>* w_array) { ConnectionFunctionGeneral::ConnectionFunctionGeneral(std::vector<double>* w_array, std::vector<unsigned int> &param_indices, std::string &function_string) {
this->param_indices = new int[param_count]; this->param_indices = new std::vector<unsigned int>(param_indices);
this->n_params = param_count;
this->weight_array = w_array; this->weight_array = w_array;
} }
ConnectionWeight::~ConnectionWeight() { ConnectionFunctionGeneral::~ConnectionFunctionGeneral() {
if(this->param_indices){ if(this->param_indices){
delete [] this->param_indices; delete this->param_indices;
this->param_indices = nullptr; this->param_indices = nullptr;
} }
} }
void ConnectionWeight::adjust_weights(double *values) { double ConnectionFunctionGeneral::eval() {
for(int i = 0; i < this->n_params; ++i){ //TODO
this->weight_array->at(this->param_indices[i]) += values[i];
}
}
std::vector<double> ConnectionWeight::get_weights(){ return 0.0;
return *this->weight_array;
} }
void ConnectionWeight::set_weights(double *values) { void ConnectionFunctionGeneral::eval_partial_derivative(std::vector<double> &weight_gradient, double alpha) {
for(int i = 0; i < this->n_params; ++i){ //TODO
this->weight_array->at(this->param_indices[i]) = values[i];
}
} }
void ConnectionWeight::SetParamIndices(int *param_indices) {
for(int i = 0; i < this->n_params; ++i){
this->SetParamIndex(param_indices[i], i);
}
}
void ConnectionWeight::SetParamIndex(int value, int idx) {
this->param_indices[idx] = value;
}
double ConnectionWeight::eval() {
double product = 1;
for(auto e : *this->weight_array) {
product *= e;
}
return product;
}
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include <functional> #include <functional>
#include <vector> #include <vector>
class ConnectionWeight { class ConnectionFunctionGeneral {
protected: protected:
/** /**
* *
...@@ -21,68 +21,40 @@ protected: ...@@ -21,68 +21,40 @@ protected:
/** /**
* *
*/ */
int* param_indices = nullptr; std::vector<unsigned int> *param_indices = nullptr;
/**
*
*/
int n_params = 0;
public: public:
/** /**
* *
*/ */
ConnectionWeight(); ConnectionFunctionGeneral();
/** /**
* *
* @param param_count * @param param_count
* @param f * @param f
*/ */
ConnectionWeight(int param_count, std::vector<double>* w_array); ConnectionFunctionGeneral(std::vector<double>* w_array, std::vector<unsigned int> &param_indices, std::string &function_string);
/** /**
* *
* @param value
* @param idx
*/ */
void SetParamIndex(int value, int idx); virtual ~ConnectionFunctionGeneral();
/**
*
* @param param_ptr
*/
void SetParamIndices(int* param_ptr);
/**
*
*/
virtual ~ConnectionWeight();
/** /**
* *
* @return * @return
*/ */
virtual double eval(); virtual double eval( );
/** /**
* * Performs partial derivative of this transfer function according to all parameters. Adds the values multiplied
* @param values * by alpha to the corresponding gradient vector
*/ */
virtual void eval_partial_derivative(std::vector<double> &weight_gradient, double alpha);
void set_weights(double *values);
/**
*
* @return vector of weights
*/
std::vector<double> get_weights();
/**
*
* @param values
*/
void adjust_weights(double *values);
}; };
......
...@@ -5,12 +5,17 @@ ...@@ -5,12 +5,17 @@
* @date 14.6.18 - * @date 14.6.18 -
*/ */
#include "ConnectionWeightIdentity.h" #include "ConnectionFunctionIdentity.h"
ConnectionWeightIdentity::ConnectionWeightIdentity(double * val) { ConnectionFunctionIdentity::ConnectionFunctionIdentity(double * val, size_t pidx) {
this->value = val; this->value = val;
this->param_idx = pidx;
} }
double ConnectionWeightIdentity::eval() { double ConnectionFunctionIdentity::eval() {
return (*this->value); return (*this->value);
}
void ConnectionFunctionIdentity::eval_partial_derivative(std::vector<double> &weight_gradient, double alpha) {
weight_gradient[this->param_idx] += alpha;
} }
\ No newline at end of file
...@@ -15,21 +15,29 @@ class ConnectionFunctionGeneral; ...@@ -15,21 +15,29 @@ class ConnectionFunctionGeneral;
/** /**
* *
*/ */
class ConnectionWeightIdentity:public ConnectionFunctionGeneral { class ConnectionFunctionIdentity:public ConnectionFunctionGeneral {
private: private:
double * value = nullptr; double * value = nullptr;
size_t param_idx;
public: public:
/** /**
* *
*/ */
ConnectionWeightIdentity(double * value_ptr); ConnectionFunctionIdentity(double * value_ptr, size_t pidx);
/** /**
* *
* @return * @return
*/ */
double eval() override; double eval() override;
/**
*
* @param weight_gradient
* @param alpha
*/
void eval_partial_derivative(std::vector<double> &weight_gradient, double alpha) override;
}; };
......
This diff is collapsed.
/** /**
* DESCRIPTION OF THE FILE * This file contains the header for the NeuralNetwork class representing a function in the form of a directed graph,
* in which the vertices are called Neurons (with activation functions) and the edges Connections (with transfer functions)
* *
* @author Michal Kravčenko * @author Michal Kravčenko
* @date 13.6.18 - * @date 13.6.18 -
*/ */
//TODO pouvazovat o pridani indexu k neuronum, abychom meli urcitou kontrolu nad poradim vstupu a vystupu? //TODO preprocess the feed-forward and backward passes for more efficient parallelism
//TODO rucni nastaveni vstupnich a vystu[nich neuronu
#ifndef INC_4NEURO_NEURALNETWORK_H #ifndef INC_4NEURO_NEURALNETWORK_H
#define INC_4NEURO_NEURALNETWORK_H #define INC_4NEURO_NEURALNETWORK_H
#include <vector> #include <vector>
#include <algorithm>
#include <utility>
#include "../Neuron/Neuron.h" #include "../Neuron/Neuron.h"
#include "../NetConnection/ConnectionFunctionGeneral.h"
#include "../NetConnection/ConnectionFunctionIdentity.h"
#include "../settings.h" #include "../settings.h"
enum NET_TYPE{GENERAL}; enum NET_TYPE{GENERAL};
...@@ -28,58 +32,134 @@ private: ...@@ -28,58 +32,134 @@ private:
*/ */
NET_TYPE network_type = GENERAL; NET_TYPE network_type = GENERAL;
/** /**
* *
*/ */
std::vector<Neuron*> *neurons = nullptr; std::vector<Neuron*> *neurons = nullptr;
/**
*
*/
std::vector<size_t>* input_neuron_indices = nullptr;
/**
*
*/
std::vector<size_t>* output_neuron_indices = nullptr;
/**
*
*/
std::vector<double>* connection_weights = nullptr;
/**
*
*/
std::vector<double>* neuron_biases = nullptr;
/**
*
*/
std::vector<double>* neuron_potentials = nullptr;
/**
*
*/
std::vector<ConnectionFunctionGeneral*> * connection_list = nullptr;
/**
*
*/
std::vector<std::vector<std::pair<size_t, size_t>>*> * inward_adjacency = nullptr;
/**
*
*/
std::vector<std::vector<std::pair<size_t, size_t>>*> * outward_adjacency = nullptr;
/**
*
*/
std::vector<std::vector<size_t>*> *neuron_layers_feedforward = nullptr;
/**
*
*/
std::vector<std::vector<size_t>*> *neuron_layers_feedbackward = nullptr;
/**
*
*/
size_t n_inputs = 0;
/** /**
* *
*/ */
std::vector<Neuron*>* input_neurons = nullptr; size_t n_outputs = 0;