Commit f220e8e6 authored by Michal Kravcenko's avatar Michal Kravcenko

Added an interface for future use in the learning methods

parent 089d5a60
......@@ -15,7 +15,7 @@ add_library(4neuro SHARED
DataSet/DataSet.cpp
ErrorFunction/ErrorFunctions.cpp
Solvers/DESolver.cpp
)
LearningMethods/ILearningMethods.h)
target_link_libraries(4neuro boost_serialization)
......
/**
* This file contains an interface for all learning methods in the library
*
* @author Michal Kravčenko
* @date 12.8.18 -
*/
#ifndef LIB4NEURO_ILEARNINGMETHODS_H
#define LIB4NEURO_ILEARNINGMETHODS_H
#include <vector>
#include "../ErrorFunction/ErrorFunctions.h"
class ILearningMethods {
public:
/*
* Runs the method specific learning algorithm minimizing the given error function
*/
virtual void optimize( ErrorFunction &error ) = 0;
/*
* Updates the optimal weight&bias settings in the passed vector
*/
virtual void get_optimal_configuration( std::vector<double> &config ) = 0;
};
#endif //LIB4NEURO_ILEARNINGMETHODS_H
......@@ -70,8 +70,6 @@ Particle::Particle(ErrorFunction* ef, double *domain_bounds) {
this->optimal_value = this->ef->eval(this->coordinate);
this->print_coordinate();
}
Particle::~Particle() {
......@@ -312,11 +310,11 @@ void ParticleSwarm::optimize( double gamma, double epsilon, double delta) {
// TODO - only in verbose mode
// only for info purposes
// euclidean_dist += this->get_euclidean_distance(particle->get_coordinate(), centroid);
//
// if(this->get_euclidean_distance(particle->get_coordinate(), centroid) < epsilon) {
// cluster.insert(particle);
// }
euclidean_dist += this->get_euclidean_distance(particle->get_coordinate(), centroid);
if(this->get_euclidean_distance(particle->get_coordinate(), centroid) < epsilon) {
cluster.insert(particle);
}
}
//}
......@@ -325,7 +323,7 @@ void ParticleSwarm::optimize( double gamma, double epsilon, double delta) {
//TODO only in verbose mode
euclidean_dist /= this->n_particles;
if(outer_it % 1 == 0){
if(outer_it % 10 == 0){
printf("Iteration %d, avg euclidean distance: %f, cluster percent: %f, f-value: %f\r", (int)outer_it, euclidean_dist,
double(cluster.size())/this->n_particles, optimal_value);
std::cout.flush();
......
......@@ -357,8 +357,8 @@ void DESolver::solve_via_particle_swarm(double *domain_bounds, double c1, double
this->solution->copy_parameter_space(swarm_01.get_solution());
}
NeuralNetwork* DESolver::get_solution() {
return this->solution;
NeuralNetwork* DESolver::get_solution( MultiIndex &alpha ) {
return this->map_multiindices2nn[ alpha ];
}
double DESolver::eval_equation( size_t equation_idx, std::vector<double> *weight_and_biases, std::vector<double> &input ) {
......
......@@ -133,19 +133,7 @@ public:
void set_error_function(size_t equation_idx, ErrorFunctionType F, DataSet *conditions);
/**
* solves the PDE with its boundary conditions via the particle swarm algorithm
* @param domain_bounds
* @param c1
* @param c2
* @param w
* @param n_particles
* @param max_iters
* @param gamma
* @param epsilon
* @param delta
* @return
*/
void solve_via_particle_swarm(
double * domain_bounds,
double c1,
......@@ -159,10 +147,10 @@ public:
);
/**
* returns the pointer to the object representing the solution y(x_1, x_2, ..., x_n)
* returns the pointer to the object representing the given partial derivative of the solution
* @return
*/
NeuralNetwork* get_solution( );
NeuralNetwork* get_solution( MultiIndex &alpha );
/**
* For testing purposes only
......
......@@ -25,68 +25,6 @@
#include "../../include/4neuro.h"
#include "../Solvers/DESolver.h"
void export_approximation_analysis(std::string fn, NeuralNetwork &approximation, DataSet &ds){
/* SOLUTION EXPORT */
printf("Exporting file '%s' : %7.3f%%\r", fn.c_str(), 0.0);
std::cout.flush();
std::vector<double> *weight_params = approximation.get_parameter_ptr_weights( );
std::vector<double> *biases_params = approximation.get_parameter_ptr_biases( );
std::vector<double> input, output(1);
std::ofstream ofs(fn, std::ofstream::out);
size_t i = 0, max_i = ds.get_data()->size();
for(auto tp: *ds.get_data()){
input = tp.first;
approximation.eval_single(input, output);
ofs << input[0] << " " << input[1] << " " << output[0] << std::endl;
i++;
printf("Exporting file %s : %7.3f%%\r", fn.c_str(), (100.0 * i) / max_i);
std::cout.flush();
}
ofs.close();
printf("Exporting file %s : %7.3f%% -> OK\n", fn.c_str(), 100.0);
std::cout.flush();
// /* governing equation error */
// ofs = std::ofstream("data_2d_pde1_first_equation_error.txt", std::ofstream::out);
// for(auto tp: *test_set_1.get_data()){
// input = tp.first;
//
// double eq_value = solver_01.eval_equation(0, nullptr, input);
//
// ofs << input[0] << " " << input[1] << " " << std::fabs(eq_value) << std::endl;
// }
// ofs.close();
//
// /* ISOTROPIC TEST SET FOR BOUNDARY CONDITIONS */
// frac = (de - ds) / (n_test_points - 1);
// /* first boundary condition & its error */
// ofs = std::ofstream("data_1d_pde1_yt.txt", std::ofstream::out);
// std::ofstream ofs2("data_1d_pde1_yx.txt", std::ofstream::out);
// for(unsigned int i = 0; i < n_test_points; ++i){
// double x = frac * i;
// double t = frac * i;
//
// double yt = std::sin(t);
// double yx = std::pow(E, -0.707106781 * x) * std::sin( -0.707106781 * x );
//
// input = {0.0, t};
// solution->eval_single( input, output, nullptr );
// ofs << i + 1 << " " << t << " " << yt << " " << output[0] << " " << std::fabs(output[0] - yt) << std::endl;
//
// input = {x, 0.0};
// solution->eval_single( input, output, nullptr );
// ofs2 << i + 1 << " " << x << " " << yx << " " << output[0] << " " << std::fabs(output[0] - yx) << std::endl;
// }
// ofs2.close();
// ofs.close();
//
// printf("done!\n");
//
}
//y(x, t) = ... ai * f(wxi * x + wti * t - bi)
double eval_approx_y(double x, double t, size_t n_inner_neurons, std::vector<double> &parameters){
double value= 0.0, wxi, wti, ai, bi, ei, ei1;
......@@ -682,7 +620,78 @@ void solve_example_particle_swarm(double accuracy, size_t n_inner_neurons, size_
/* PRACTICAL END OF THE EXAMPLE */
export_approximation_analysis("data_2d_pde1_y.txt", *solver_01.get_solution(), ds_00);
/* SOLUTION EXPORT */
size_t i, j;
std::vector<double> *w1_ptr = solver_01.get_solution( alpha_00 )->get_parameter_ptr_weights();
std::vector<double> *w2_ptr = solver_01.get_solution( alpha_00 )->get_parameter_ptr_biases();
std::vector<double> export_params(4 * n_inner_neurons);
for(i = 0; i < n_inner_neurons; ++i){
export_params[4 * i + 0] = w1_ptr->at(i);
export_params[4 * i + 1] = w1_ptr->at(n_inner_neurons + i);
export_params[4 * i + 2] = w1_ptr->at(2 * n_inner_neurons + i);
export_params[4 * i + 3] = w2_ptr->at( i );
}
printf("Exporting file 'data_2d_pde1_y.txt' : %7.3f%%\r", 0.0);
std::cout.flush();
std::vector<double> input, output(1);
std::ofstream ofs("data_2d_pde1_y.txt", std::ofstream::out);
frac = (te - ts) / (n_test_points - 1);
for(i = 0; i < n_test_points; ++i){
x = i * frac + ts;
for(j = 0; j < n_test_points; ++j){
t = j * frac + ts;
ofs << x << " " << t << " " << eval_approx_y(x, t, n_inner_neurons, export_params) << std::endl;
printf("Exporting file 'data_2d_pde1_y.txt' : %7.3f%%\r", (100.0 * (j + i * n_test_points)) / (n_test_points * n_test_points - 1));
std::cout.flush();
}
}
printf("Exporting file 'data_2d_pde1_y.txt' : %7.3f%%\n", 100.0);
std::cout.flush();
ofs.close();
/* governing equation error */
ofs = std::ofstream("data_2d_pde1_first_equation_error.txt", std::ofstream::out);
printf("Exporting file 'data_2d_pde1_first_equation_error.txt' : %7.3f%%\r", 0.0);
for(i = 0; i < n_test_points; ++i){
x = i * frac + ts;
for(j = 0; j < n_test_points; ++j){
t = j * frac + ts;
ofs << x << " " << t << " " << std::fabs(eval_approx_yxx(x, t, n_inner_neurons, export_params) - eval_approx_yt(x, t, n_inner_neurons, export_params)) << std::endl;
printf("Exporting file 'data_2d_pde1_first_equation_error.txt' : %7.3f%%\r", (100.0 * (j + i * n_test_points)) / (n_test_points * n_test_points - 1));
std::cout.flush();
}
}
printf("Exporting file 'data_2d_pde1_first_equation_error.txt' : %7.3f%%\n", 100.0);
std::cout.flush();
ofs.close();
/* ISOTROPIC TEST SET FOR BOUNDARY CONDITIONS */
/* first boundary condition & its error */
ofs = std::ofstream("data_1d_pde1_yt.txt", std::ofstream::out);
std::ofstream ofs2("data_1d_pde1_yx.txt", std::ofstream::out);
printf("Exporting files 'data_1d_pde1_yt.txt' and 'data_1d_pde1_yx.txt' : %7.3f%%\r", 0.0);
for(i = 0; i < n_test_points; ++i){
x = frac * i + ts;
t = frac * i + ts;
double yt = std::sin(t);
double yx = std::pow(E, -0.707106781 * x) * std::sin( -0.707106781 * x );
double evalt = eval_approx_y(0, t, n_inner_neurons, export_params);
double evalx = eval_approx_y(x, 0, n_inner_neurons, export_params);
ofs << i + 1 << " " << t << " " << yt << " " << evalt << " " << std::fabs(evalt - yt) << std::endl;
ofs2 << i + 1 << " " << x << " " << yx << " " << evalx << " " << std::fabs(evalx - yx) << std::endl;
printf("Exporting files 'data_1d_pde1_yt.txt' and 'data_1d_pde1_yx.txt' : %7.3f%%\r", (100.0 * i) / (n_test_points - 1));
std::cout.flush();
}
printf("Exporting files 'data_1d_pde1_yt.txt' and 'data_1d_pde1_yx.txt' : %7.3f%%\r", 100.0);
std::cout.flush();
ofs2.close();
ofs.close();
}
int main() {
......@@ -697,21 +706,21 @@ int main() {
double ts = ds;
double te = de + 0;
// size_t particle_swarm_max_iters = 10;
// size_t n_particles = 200;
// solve_example_particle_swarm(accuracy, n_inner_neurons, train_size, ds, de, test_size, ts, te, particle_swarm_max_iters, n_particles);
size_t particle_swarm_max_iters = 100;
size_t n_particles = 200;
solve_example_particle_swarm(accuracy, n_inner_neurons, train_size, ds, de, test_size, ts, te, particle_swarm_max_iters, n_particles);
std::vector<double> init_guess(4 * n_inner_neurons);
std::random_device seeder;
std::mt19937 gen(seeder());
std::uniform_real_distribution<double> dist(-1.0, 1.0);
for(unsigned int i = 0; i < init_guess.size(); ++i){
init_guess[i] = dist(gen);
}
// init_guess = {-0.21709230, -0.26189447, 0.77853923, 0.41091127, -0.44311897, -0.99036349, 0.84912023, -0.16920743};
solve_example_gradient(init_guess, accuracy, n_inner_neurons, train_size, ds, de, test_size, ts, te);
//
// std::vector<double> init_guess(4 * n_inner_neurons);
// std::random_device seeder;
// std::mt19937 gen(seeder());
// std::uniform_real_distribution<double> dist(-1.0, 1.0);
// for(unsigned int i = 0; i < init_guess.size(); ++i){
// init_guess[i] = dist(gen);
// }
//
//// init_guess = {-0.21709230, -0.26189447, 0.77853923, 0.41091127, -0.44311897, -0.99036349, 0.84912023, -0.16920743};
// solve_example_gradient(init_guess, accuracy, n_inner_neurons, train_size, ds, de, test_size, ts, te);
return 0;
}
\ No newline at end of file
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