Newer
Older
/**
* DESCRIPTION OF THE FILE
*
* @author Michal Kravčenko
* @date 10.9.18 -
*/
#include <random>
#include <iostream>
#include <fstream>
int main() {
std::cout << std::endl << "Running lib4neuro Moldyn Seminar example" << std::endl;
<< "********************************************************************************************************************************************"
<< std::endl;
std::shared_ptr<l4n::NeuronLinear> in1 = std::make_shared<l4n::NeuronLinear>();
std::shared_ptr<l4n::NeuronLinear> in2 = std::make_shared<l4n::NeuronLinear>();
size_t i1 = XOR.add_neuron(in1,
l4n::BIAS_TYPE::NO_BIAS);
size_t i2 = XOR.add_neuron(in2,
l4n::BIAS_TYPE::NO_BIAS);
std::shared_ptr<l4n::NeuronLogistic> hn1 = std::make_shared<l4n::NeuronLogistic>();
std::shared_ptr<l4n::NeuronLogistic> hn2 = std::make_shared<l4n::NeuronLogistic>();
size_t h1 = XOR.add_neuron(hn1);
size_t h2 = XOR.add_neuron(hn2);
std::shared_ptr<l4n::NeuronLinear> on1 = std::make_shared<l4n::NeuronLinear>();
size_t o1 = XOR.add_neuron(on1,
l4n::BIAS_TYPE::NO_BIAS);
XOR.add_connection_simple(i1,
h1);
XOR.add_connection_simple(i2,
h1);
XOR.add_connection_simple(i1,
h2);
XOR.add_connection_simple(i2,
h2);
XOR.add_connection_simple(h1,
o1);
XOR.add_connection_simple(h2,
o1);
/* TRAIN DATA DEFINITION */
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
inp = {0, 0};
out = {0};
data_vec.emplace_back(std::make_pair(inp,
out));
inp = {0, 1};
out = {1};
data_vec.emplace_back(std::make_pair(inp,
out));
inp = {1, 0};
out = {1};
data_vec.emplace_back(std::make_pair(inp,
out));
inp = {1, 1};
out = {0};
data_vec.emplace_back(std::make_pair(inp,
out));
l4n::DataSet ds(&data_vec);
/* specification of the input/output neurons */
std::vector<size_t> net_input_neurons_indices(2);
std::vector<size_t> net_output_neurons_indices(1);
net_input_neurons_indices[0] = i1;
net_input_neurons_indices[1] = i2;
net_output_neurons_indices[0] = o1;
XOR.specify_input_neurons(net_input_neurons_indices);
XOR.specify_output_neurons(net_output_neurons_indices);
/* ERROR FUNCTION SPECIFICATION */
/* TRAINING METHOD SETUP */
std::vector<double> domain_bounds(2 * (XOR.get_n_weights() + XOR.get_n_biases()));
for (size_t i = 0; i < domain_bounds.size() / 2; ++i) {
domain_bounds[2 * i + 1] = 10;
}
double c1 = 1.7;
double c2 = 1.7;
double w = 0.7;
size_t n_particles = 5;
/* if the maximal velocity from the previous step is less than 'gamma' times the current maximal velocity, then one
* terminating criterion is met */
double gamma = 0.5;
/* if 'delta' times 'n' particles are in the centroid neighborhood given by the radius 'epsilon', then the second
* terminating criterion is met ('n' is the total number of particles) */
double epsilon = 0.02;
l4n::ParticleSwarm swarm_01(
&domain_bounds,
c1,
c2,
w,
gamma,
epsilon,
delta,
n_particles,
iter_max
XOR.copy_parameter_space(swarm_01.get_parameters());
/* ERROR CALCULATION */
double error = 0.0;
inp = {0, 0};
error += (0 - out[0]) * (0 - out[0]);
std::cout << "x = (0, 0), expected output: 0, real output: " << out[0] << std::endl;
inp = {0, 1};
error += (1 - out[0]) * (1 - out[0]);
std::cout << "x = (0, 1), expected output: 1, real output: " << out[0] << std::endl;
inp = {1, 0};
error += (1 - out[0]) * (1 - out[0]);
std::cout << "x = (1, 0), expected output: 1, real output: " << out[0] << std::endl;
inp = {1, 1};
error += (0 - out[0]) * (0 - out[0]);
std::cout << "x = (1, 1), expected output: 0, real output: " << out[0] << std::endl;
return 0;
}