Commit bbdcff15 authored by Michal Kravcenko's avatar Michal Kravcenko
Browse files

-added a support for multi-network systems sharing edge weights

-added an example testing multi-network error function
parent dfaf4073
......@@ -14,21 +14,27 @@
int main() {
/* TRAIN DATA DEFINITION */
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec_01, data_vec_02;
std::vector<double> inp, out;
inp = {0, 1};
out = {0.5};
data_vec.emplace_back(std::make_pair(inp, out));
data_vec_01.emplace_back(std::make_pair(inp, out));
inp = {1, 0.5};
out = {0.75};
data_vec.emplace_back(std::make_pair(inp, out));
data_vec_01.emplace_back(std::make_pair(inp, out));
DataSet ds(&data_vec);
DataSet ds_01(&data_vec_01);
inp = {1.25};
out = {0.63};
data_vec_02.emplace_back(std::make_pair(inp, out));
DataSet ds_02(&data_vec_02);
/* NETWORK DEFINITION */
NeuralNetwork net, net2;
NeuralNetwork net;
/* Input neurons */
NeuronLinear *i1 = new NeuronLinear(0.0, 1.0); //f(x) = x
......@@ -55,26 +61,58 @@ int main() {
//net.add_connection_simple(idx2, idx3, -1, 1.0);
net.add_connection_simple(idx1, idx3); // weight index 0
net.add_connection_simple(idx2, idx3); // weight index 1
net.add_connection_simple(idx4, idx5, 0); // AGAIN weight index 0 - same weight!
//net.randomize_weights();
net.randomize_weights();
/* specification of the input/output neurons */
std::vector<size_t> net_input_neurons_indices(3);
std::vector<size_t> net_output_neurons_indices(2);
net_input_neurons_indices[0] = idx1;
net_input_neurons_indices[1] = idx2;
net_input_neurons_indices[2] = idx4;
net_output_neurons_indices[0] = idx3;
net_output_neurons_indices[1] = idx5;
net.specify_input_neurons(net_input_neurons_indices);
net.specify_output_neurons(net_output_neurons_indices);
/* CONSTRUCTION OF SUBNETWORKS */
std::vector<size_t> subnet_01_input_neurons, subnet_01_output_neurons;
std::vector<size_t> subnet_02_input_neurons, subnet_02_output_neurons;
subnet_01_input_neurons.push_back(idx1);
subnet_01_input_neurons.push_back(idx2);
subnet_01_output_neurons.push_back(idx3);
NeuralNetwork subnet_01(net, subnet_01_input_neurons, subnet_01_output_neurons);
subnet_02_input_neurons.push_back(idx4);
subnet_02_output_neurons.push_back(idx5);
NeuralNetwork subnet_02(net, subnet_02_input_neurons, subnet_02_output_neurons);
/* ERROR FUNCTION SPECIFICATION */
MSE mse(&net, &ds);
/* ERROR FUNCTION SPECIFICATION */
MSE mse_01(&subnet_01, &ds_01);
MSE mse_02(&subnet_02, &ds_02);
MSE_SUM mse_sum( );
mse_sum.add_error_function( &mse_01 );
mse_sum.add_error_function( &mse_02 );
/* TRAINING METHOD SETUP */
unsigned int n_edges = 2;
unsigned int dim = n_edges, max_iters = 2000;
//must encapsulate each of the partial error functions
double domain_bounds[4] = {-800.0, 800.0, -800.0, 800.0};
double c1 = 0.5, c2 = 1.5, w = 0.8;
unsigned int n_particles = 10;
ParticleSwarm swarm_01(&mse, domain_bounds, c1, c2, w, n_particles, max_iters);
ParticleSwarm swarm_01(&mse_sum, domain_bounds, c1, c2, w, n_particles, max_iters);
swarm_01.optimize(0.5, 0.02);
......
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