Commit a4cb0d7e authored by Michal Kravcenko's avatar Michal Kravcenko

corrected DESolver error and altered Particle initialization to be more usefull in NNs

parent 4e564c9b
......@@ -28,7 +28,7 @@ target_link_libraries(test_cases 4neuro)
add_executable(neuron_serialization_example neuron_serialization_example.cpp)
target_link_libraries(neuron_serialization_example 4neuro)
add_executable(net_test_1 net_test_1.cpp)
add_executable(net_test_1 net_test_1.cpp settings.h)
target_link_libraries(net_test_1 4neuro)
add_executable(net_test_2 net_test_2.cpp)
......
......@@ -42,7 +42,7 @@ Particle::Particle(ErrorFunction* ef, double *domain_bounds) {
this->domain_bounds = domain_bounds;
for(unsigned int i = 0; i < this->coordinate_dim; ++i){
this->coordinate[i] = (rand() % 100001) / (double)100000 * (domain_bounds[2 * i + 1] - domain_bounds[2 * i]) + domain_bounds[2 * i];
this->coordinate[i] = (rand() % 100001) / (double)100000 + domain_bounds[2 * i] / (domain_bounds[2 * i + 1] - domain_bounds[2 * i]);
this->optimal_coordinate[i] = this->coordinate[i];
}
......@@ -192,7 +192,7 @@ void ParticleSwarm::optimize( double gamma, double epsilon, double delta) {
unsigned int outer_it = 0;
Particle *particle;
double *p_min_glob = new double[this->func_dim];
double optimal_value;
double optimal_value = 0.0;
std::set<Particle*> cluster; //!< Particles in a cluster
double* coords;
......
......@@ -45,7 +45,9 @@ Neuron* Connection::get_neuron_out() {
//}
void Connection::pass_signal() {
// printf(" passing signal between neurons %d -> %d, value: %f * %f\n", this->neuron_in, this->neuron_out, this->neuron_in->get_state(), this->con->eval());
#ifdef VERBOSE_NN_EVAL
printf(" passing signal between neurons %d -> %d, value: %f * %f\n", this->neuron_in, this->neuron_out, this->neuron_in->get_state(), this->con->eval());
#endif
this->neuron_out->adjust_potential(this->neuron_in->get_state() * this->con->eval());
}
......
......@@ -10,6 +10,7 @@
#include "../Neuron/Neuron.h"
#include "ConnectionWeight.h"
#include "../settings.h"
class Neuron;
class ConnectionWeight;
......
......@@ -472,8 +472,9 @@ void NeuralNetwork::eval_single(std::vector<double> &input, std::vector<double>
neuron->set_potential(input[i]);
// printf("INPUT NEURON %d, POTENTIAL: %f\n", neuron, input[i]);
#ifdef VERBOSE_NN_EVAL
printf("INPUT NEURON %d, POTENTIAL: %f\n", neuron, input[i]);
#endif
++i;
}
Neuron* active_neuron;
......@@ -485,8 +486,9 @@ void NeuralNetwork::eval_single(std::vector<double> &input, std::vector<double>
for(i = 0; i < active_set_size[idx1]; ++i){
active_neuron = this->active_eval_set->at(i + n * idx1);
active_neuron->activate();//computes the activation function on its potential
// printf(" active neuron %d, potential: %f, state: %f\n", active_neuron, active_neuron->get_potential(), active_neuron->get_state());
#ifdef VERBOSE_NN_EVAL
printf(" active neuron %d, potential: %f, state: %f\n", active_neuron, active_neuron->get_potential(), active_neuron->get_state());
#endif
for(Connection* connection: *(active_neuron->get_connections_out())){
connection->pass_signal();
......@@ -499,7 +501,9 @@ void NeuralNetwork::eval_single(std::vector<double> &input, std::vector<double>
}
}
}
// printf("-----------\n");
#ifdef VERBOSE_NN_EVAL
printf("-----------\n");
#endif
idx1 = idx2;
idx2 = (idx1 + 1) % 2;
......@@ -512,7 +516,9 @@ void NeuralNetwork::eval_single(std::vector<double> &input, std::vector<double>
output[i] = neuron->get_state();
// printf("OUTPUT NEURON %d, VALUE: %f\n", neuron, output[i]);
#ifdef VERBOSE_NN_EVAL
printf("OUTPUT NEURON %d, VALUE: %f\n", neuron, output[i]);
#endif
++i;
}
......
......@@ -13,6 +13,7 @@
#include <vector>
#include "../Neuron/Neuron.h"
#include "../settings.h"
enum NET_TYPE{GENERAL};
......
......@@ -64,7 +64,7 @@ DESolver::DESolver( unsigned int n_equations, unsigned int n_inputs, unsigned in
}
printf("Differential Equation Solver with %d equations\n--------------------------------------------------------------------------\n", n_equations);
printf("Constructing NN structure representing the solution [%d input neurons][%d inner neurons][%d output neurons]...", n_inputs, m, n_outputs);
printf("Constructing NN structure representing the solution [%d input neurons][%d inner neurons][%d output neurons]...\n", n_inputs, m, n_outputs);
this->dim_i = n_inputs;
this->dim_o = n_outputs;
......@@ -102,17 +102,20 @@ DESolver::DESolver( unsigned int n_equations, unsigned int n_inputs, unsigned in
}
/* connections between input neurons and inner neurons */
size_t weight_idx;
unsigned int dim_shift = this->dim_i + this->dim_o;
for(unsigned int i = 0; i < this->dim_i; ++i){
for(unsigned int j = 0; j < this->dim_inn; ++j){
this->solution->add_connection_simple(i, dim_shift + j);
weight_idx = this->solution->add_connection_simple(i, dim_shift + j);
printf(" adding a connection between input neuron %2d and inner neuron %2d, weight index %d\n", i, j, weight_idx);
}
}
/* connections between inner neurons and output neurons */
for(unsigned int i = 0; i < this->dim_inn; ++i){
for(unsigned int j = 0; j < this->dim_o; ++j){
this->solution->add_connection_simple(dim_shift + i, this->dim_i + j);
weight_idx = this->solution->add_connection_simple(dim_shift + i, this->dim_i + j);
printf(" adding a connection between inner neuron %2d and output neuron %2d, weight index %d\n", i, j, weight_idx);
}
}
......@@ -129,7 +132,7 @@ DESolver::DESolver( unsigned int n_equations, unsigned int n_inputs, unsigned in
this->errors_functions_types = new std::vector<ErrorFunctionType >(this->n_equations);
this->errors_functions_data_sets = new std::vector<DataSet*>(this->n_equations);
printf(" done\n");
printf("done\n\n");
}
......@@ -248,7 +251,7 @@ void DESolver::add_to_differential_equation( unsigned int equation_idx, MultiInd
size_t weight_idx = 0;
for(unsigned int i = 0; i < this->dim_i; ++i){
for(unsigned int j = 0; j < this->dim_inn; ++j){
// printf(" adding a connection between input neuron %2d and inner neuron %2d, weight index %d\n", i, j, weight_idx);
printf(" adding a connection between input neuron %2d and inner neuron %2d, weight index %d\n", i, j, weight_idx);
new_net->add_connection_simple(i, dim_shift + j, weight_idx);
weight_idx++;
}
......@@ -260,7 +263,7 @@ void DESolver::add_to_differential_equation( unsigned int equation_idx, MultiInd
// size_t weight_idx_mem = weight_idx;
for(unsigned int i = 0; i < this->dim_inn; ++i){
for(unsigned int j = 0; j < this->dim_o; ++j){
// printf(" adding a connection between inner neuron %2d and glue neuron %2d, weight index %d\n", i, i * this->dim_o + j, weight_idx);
printf(" adding a connection between inner neuron %2d and glue neuron %2d, weight index %d\n", i, i * this->dim_o + j, weight_idx);
new_net->add_connection_simple(dim_shift + i, glue_shift + i * this->dim_o + j, weight_idx );
weight_idx++;
}
......@@ -274,7 +277,7 @@ void DESolver::add_to_differential_equation( unsigned int equation_idx, MultiInd
unsigned int local_weight_idx = pi * this->dim_inn + i;
for(unsigned int j = 0; j < this->dim_o; ++j){
// printf(" adding a connection between glue neuron %2d and output neuron %2d, weight index %d\n", i * this->dim_o + j, j, local_weight_idx);
printf(" adding a connection between glue neuron %2d and output neuron %2d, weight index %d\n", i * this->dim_o + j, j, local_weight_idx);
new_net->add_connection_simple(glue_shift + i * this->dim_o + j, j + this->dim_i, local_weight_idx );
}
}
......@@ -288,7 +291,7 @@ void DESolver::add_to_differential_equation( unsigned int equation_idx, MultiInd
unsigned int local_weight_idx = pi * this->dim_inn + i;
for(unsigned int j = 0; j < this->dim_o; ++j){
// printf(" adding a connection between glue neuron[1] %2d and glue neuron[2] %2d, weight index %d\n", i * this->dim_o + j, i * this->dim_o + j, local_weight_idx);
printf(" adding a connection between glue neuron[1] %2d and glue neuron[2] %2d, weight index %d\n", i * this->dim_o + j, i * this->dim_o + j, local_weight_idx);
new_net->add_connection_simple(glue_shift + i * this->dim_o + j, glue_shift_2 + i * this->dim_o + j, local_weight_idx );
}
}
......@@ -300,8 +303,8 @@ void DESolver::add_to_differential_equation( unsigned int equation_idx, MultiInd
unsigned int local_weight_idx = pi * this->dim_inn + i;
for(unsigned int j = 0; j < this->dim_o; ++j){
// printf(" adding a connection between glue neuron[2] %2d and output neuron %2d, weight index %d\n", i * this->dim_o + j, j, local_weight_idx);
new_net->add_connection_simple(glue_shift_2 + i * this->dim_o + j, j, local_weight_idx );
printf(" adding a connection between glue neuron[2] %2d and output neuron %2d, weight index %d\n", i * this->dim_o + j, j, local_weight_idx);
new_net->add_connection_simple(glue_shift_2 + i * this->dim_o + j, this->dim_i + j, local_weight_idx );
}
}
}
......@@ -349,10 +352,28 @@ void DESolver::solve_via_particle_swarm(double *domain_bounds, double c1, double
ParticleSwarm swarm_01(&total_error, domain_bounds, c1, c2, w, n_particles, max_iters);
this->solution->randomize_weights();
swarm_01.optimize(gamma, epsilon, delta);
}
NeuralNetwork* DESolver::get_solution() {
return this->solution;
}
void DESolver::eval_equation( unsigned int equation_idx, double *weight_values, std::vector<double> &input ) {
std::vector<double> output(this->dim_o);
this->differential_equations->at( equation_idx )->eval_single( input, output, weight_values );
printf("Input: ");
for( auto e: input ){
printf("%f, ", e);
}
printf("\nOutput: ");
for( auto e: output ){
printf("%f, ", e);
}
printf("\n");
}
\ No newline at end of file
......@@ -162,6 +162,11 @@ public:
* @return
*/
NeuralNetwork* get_solution( );
/**
* For testing purposes only
*/
void eval_equation( unsigned int equation_idx, double * weight_values, std::vector<double> &input );
};
......
......@@ -18,7 +18,7 @@
int main() {
unsigned int n_inputs = 1;
unsigned int n_inner_neurons = 1;
unsigned int n_inner_neurons = 64;
unsigned int n_outputs = 1;
unsigned int n_equations = 3;
......@@ -42,6 +42,10 @@ int main() {
/* neumann boundary condition */
solver_01.add_to_differential_equation( 2, alpha_1, 1.0 );
// double weights[2] = {0.5, 1.0};
// std::vector<double> inp = { 1.0 };
// solver_01.eval_equation( 0, weights, inp );
/* SETUP OF THE TRAINING DATA */
std::vector<double> inp, out;
......@@ -94,7 +98,7 @@ int main() {
/* PARTICLE SWARM TRAINING METHOD SETUP */
unsigned int max_iters = 100;
unsigned int max_iters = 1000;
//must encapsulate each of the partial error functions
double *domain_bounds = new double[ 2 * n_inner_neurons * (n_inputs + n_outputs) ];
......@@ -105,11 +109,11 @@ int main() {
double c1 = 0.5, c2 = 1.5, w = 0.8;
unsigned int n_particles = 10;
unsigned int n_particles = 100;
double gamma = 0.5, epsilon = 0.02, delta = 0.9;
solver_01.solve_via_particle_swarm( domain_bounds, c1, c2, w, n_particles, max_iters, gamma, epsilon, delta);
solver_01.solve_via_particle_swarm( domain_bounds, c1, c2, w, n_particles, max_iters, gamma, epsilon, delta );
NeuralNetwork *solution = solver_01.get_solution();
......
/**
* DESCRIPTION OF THE FILE
*
* @author Michal Kravčenko
* @date 24.7.18 -
*/
#ifndef INC_4NEURO_SETTINGS_H
#define INC_4NEURO_SETTINGS_H
/**
* If defined, the NN feed-forward will print out whats happening
*/
//#define VERBOSE_NN_EVAL
#endif //INC_4NEURO_SETTINGS_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment