Commit 1a06e926 authored by Michal Kravcenko's avatar Michal Kravcenko

Fixed a few errors in the first ode example

parent 8f759877
......@@ -643,17 +643,17 @@ void test_ode(double accuracy, size_t n_inner_neurons, size_t train_size, double
double analytical_value_df = eval_approx_df(x, n_inner_neurons, params_analytical);
double analytical_value_ddf = eval_approx_ddf(x, n_inner_neurons, params_analytical);
double de_solver_value_eq1 = solver_01.eval_equation(0, params, input);
double de_solver_value_eq1 = solver_01.eval_equation(0, &params, input);
double analytical_value_eq1 = 4 * analytical_value_f + 4 * analytical_value_df + analytical_value_ddf;
test_error_eq1 += (de_solver_value_eq1 - analytical_value_eq1) * (de_solver_value_eq1 - analytical_value_eq1);
}
input[0] = 0.0;
double de_solver_value_eq2 = solver_01.eval_equation(1, params, input);
double de_solver_value_eq2 = solver_01.eval_equation(1, &params, input);
double analytical_value_eq2 = eval_approx_f(0.0, n_inner_neurons, params_analytical);
double test_error_eq2 = (de_solver_value_eq2 - analytical_value_eq2) * (de_solver_value_eq2 - analytical_value_eq2);
double de_solver_value_eq3 = solver_01.eval_equation(2, params, input);
double de_solver_value_eq3 = solver_01.eval_equation(2, &params, input);
double analytical_value_eq3 = eval_approx_df(0.0, n_inner_neurons, params_analytical);
double test_error_eq3 = (de_solver_value_eq3 - analytical_value_eq3) * (de_solver_value_eq3 - analytical_value_eq3);
......@@ -681,7 +681,7 @@ void test_ode(double accuracy, size_t n_inner_neurons, size_t train_size, double
solver_01.solve_via_particle_swarm( domain_bounds, c1, c2, w, n_particles, max_iters, gamma, epsilon, delta );
NeuralNetwork *solution = solver_01.get_solution();
NeuralNetwork *solution = solver_01.get_solution( alpha_0 );
std::vector<double> parameters(3 * n_inner_neurons);//w1, a1, b1, w2, a2, b2, ... , wm, am, bm
std::vector<double> *weight_params = solution->get_parameter_ptr_weights();
std::vector<double> *biases_params = solution->get_parameter_ptr_biases();
......
......@@ -247,6 +247,36 @@ double eval_approx_db_yxx(double x, double t, size_t neuron_idx, std::vector<dou
return (ai * wxi * wxi * eb * ebp) / (ei1 * ei1 * ei1) - (ai * wxi * wxi * ebp * (etx - eb)) / (ei1 * ei1 * ei1) + (3 * ai * wxi * wxi * eb * ebp * (etx - eb)) / (ei1 * ei1 * ei1 * ei1);
}
double get_step_size_simple(double gamma, double val, double prev_val, double sk, double grad_norm, double grad_norm_prev){
if(val > prev_val){
gamma *= 0.99999;
}
if(sk <= 1e-3 || grad_norm < grad_norm_prev){
/* movement on a line */
/* new slope is less steep, speed up */
gamma *= 1.0005;
}
else if(grad_norm > grad_norm_prev){
/* new slope is more steep, slow down*/
gamma /= 1.0005;
}
else{
gamma /= 1.005;
}
// gamma *= 0.999999;
// gamma = 0.000001;
return gamma;
}
double get_step_size_mk(double gamma, double val, double prev_val, double sk, double grad_norm, double grad_norm_prev){
}
void solve_example_gradient(std::vector<double> &guess, double accuracy, size_t n_inner_neurons, size_t train_size, double ds, double de, size_t n_test_points, double ts, double te){
/* SETUP OF THE TRAINING DATA */
std::vector<double> inp, out;
......@@ -391,31 +421,14 @@ void solve_example_gradient(std::vector<double> &guess, double accuracy, size_t
}
sk = std::sqrt(sk);
if(val > prev_val){
gamma *= 0.99999;
}
if(sk <= 1e-3 || grad_norm < grad_norm_prev){
/* movement on a line */
/* new slope is less steep, speed up */
gamma *= 1.0005;
}
else if(grad_norm > grad_norm_prev){
/* new slope is more steep, slow down*/
gamma /= 1.0005;
}
else{
gamma /= 1.005;
}
// gamma *= 0.999999;
grad_norm = 0.0;
for(auto v: *gradient_current){
grad_norm += v * v;
}
grad_norm = std::sqrt(grad_norm);
// gamma = 0.000001;
gamma = get_step_size_simple(gamma, val, prev_val, sk, grad_norm, grad_norm_prev);
for(i = 0; i < gradient_current->size(); ++i){
(*params_prev)[i] = (*params_current)[i] - gamma * (*gradient_current)[i];
......@@ -696,7 +709,7 @@ void solve_example_particle_swarm(double accuracy, size_t n_inner_neurons, size_
int main() {
unsigned int n_inner_neurons = 4;
unsigned int n_inner_neurons = 2;
unsigned int train_size = 20;
double accuracy = 1e-4;
double ds = 0.0;
......@@ -706,21 +719,21 @@ int main() {
double ts = ds;
double te = de + 0;
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);
// size_t particle_swarm_max_iters = 500;
// size_t n_particles = 10;
// 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);
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