diff --git a/include/4neuro.h b/include/4neuro.h new file mode 100644 index 0000000000000000000000000000000000000000..21e4e3935b8d16c61d6045e353ea7eea661b1da0 --- /dev/null +++ b/include/4neuro.h @@ -0,0 +1,24 @@ +// +// Created by martin on 7/16/18. +// + +#ifndef INC_4NEURO_4NEURO_H +#define INC_4NEURO_4NEURO_H + +//TODO make only public interface visible + +#include "../src/DataSet/DataSet.h" +#include "../src/ErrorFunction/ErrorFunctions.h" +#include "../src/LearningMethods/ParticleSwarm.h" +#include "../src/NetConnection/Connection.h" +#include "../src/NetConnection/ConnectionWeight.h" +#include "../src/NetConnection/ConnectionWeightIdentity.h" +#include "../src/Network/NeuralNetwork.h" +#include "../src/Neuron/Neuron.h" +#include "../src/Neuron/NeuronBinary.h" +#include "../src/Neuron/NeuronLinear.h" +#include "../src/Neuron/NeuronLogistic.h" +#include "../src/Neuron/NeuronNeuralNet.h" +#include "../src/Neuron/NeuronTanh.h" + +#endif //INC_4NEURO_4NEURO_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3698dcc107b70e6910ab071e28278571d42983a7..aacce1df3b8e509affecaf3cf7b107dd4d57ee24 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,51 +1,35 @@ #TODO make atomic libraries AND also one large library containing all others + one large header -add_library(neuron SHARED +add_library(4neuro SHARED Neuron/Neuron.cpp - Neuron/Neuron.h Neuron/NeuronBinary.cpp - Neuron/NeuronBinary.h Neuron/NeuronLinear.cpp - Neuron/NeuronLinear.h Neuron/NeuronLogistic.cpp - Neuron/NeuronLogistic.h Neuron/NeuronTanh.cpp - Neuron/NeuronTanh.h - constants.h NetConnection/Connection.cpp - NetConnection/Connection.h Network/NeuralNetwork.cpp - Network/NeuralNetwork.h Neuron/NeuronNeuralNet.cpp - Neuron/NeuronNeuralNet.h NetConnection/ConnectionWeight.cpp - NetConnection/ConnectionWeight.h NetConnection/ConnectionWeightIdentity.cpp - NetConnection/ConnectionWeightIdentity.h) - -add_library(particle_swarm SHARED LearningMethods/ParticleSwarm.cpp - LearningMethods/ParticleSwarm.h) - -add_library(boost_unit_test SHARED tests/boost_test_lib_dummy.cpp) + DataSet/DataSet.cpp + ErrorFunction/ErrorFunctions.cpp) -add_library(data_set SHARED DataSet/DataSet.cpp) -target_link_libraries(data_set boost_serialization) +target_link_libraries(4neuro boost_serialization) -add_library(error_functions SHARED ErrorFunction/ErrorFunctions.cpp) -target_link_libraries(error_functions data_set) +add_library(boost_unit_test SHARED tests/boost_test_lib_dummy.cpp) ############ # EXAMPLES # ############ add_executable(test_cases main.cpp) -target_link_libraries(test_cases neuron particle_swarm boost_serialization data_set) +target_link_libraries(test_cases 4neuro) add_executable(neuron_serialization_example neuron_serialization_example.cpp) -target_link_libraries(neuron_serialization_example neuron boost_serialization) +target_link_libraries(neuron_serialization_example 4neuro) add_executable(net_test_1 net_test_1.cpp) -target_link_libraries(net_test_1 neuron particle_swarm data_set error_functions) +target_link_libraries(net_test_1 4neuro) ############## # UNIT TESTS # @@ -53,37 +37,37 @@ target_link_libraries(net_test_1 neuron particle_swarm data_set error_functions) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unit-tests) add_executable(neuron_test tests/neuron_test.cpp) -target_link_libraries(neuron_test boost_unit_test neuron) +target_link_libraries(neuron_test boost_unit_test 4neuro) add_executable(linear_neuron_test tests/NeuronLinear_test.cpp) -target_link_libraries(linear_neuron_test boost_unit_test neuron) +target_link_libraries(linear_neuron_test boost_unit_test 4neuro) add_executable(binary_neuron_test tests/NeuronBinary_test.cpp) -target_link_libraries(binary_neuron_test boost_unit_test neuron) +target_link_libraries(binary_neuron_test boost_unit_test 4neuro) add_executable(logistic_neuron_test tests/NeuronLogistic_test.cpp) -target_link_libraries(logistic_neuron_test boost_unit_test neuron) +target_link_libraries(logistic_neuron_test boost_unit_test 4neuro) add_executable(tanh_neuron_test tests/NeuronTanh.cpp) -target_link_libraries(tanh_neuron_test boost_unit_test neuron) +target_link_libraries(tanh_neuron_test boost_unit_test 4neuro) add_executable(connection_weight_test tests/ConnectionWeight_test.cpp) -target_link_libraries(connection_weight_test boost_unit_test neuron) +target_link_libraries(connection_weight_test boost_unit_test 4neuro) add_executable(connection_test tests/Connection_test.cpp) -target_link_libraries(connection_test boost_unit_test neuron) +target_link_libraries(connection_test boost_unit_test 4neuro) add_executable(neural_network_test tests/NeuralNetwork_test.cpp) -target_link_libraries(neural_network_test boost_unit_test neuron) +target_link_libraries(neural_network_test boost_unit_test 4neuro) add_executable(connection_weight_identity_test tests/ConnectionWeightIdentity_test.cpp) -target_link_libraries(connection_weight_identity_test boost_unit_test neuron) +target_link_libraries(connection_weight_identity_test boost_unit_test 4neuro) add_executable(particle_swarm_test tests/ParticleSwarm_test.cpp) -target_link_libraries(particle_swarm_test boost_unit_test neuron particle_swarm) +target_link_libraries(particle_swarm_test boost_unit_test 4neuro) add_executable(particle_test tests/Particle_test.cpp) -target_link_libraries(particle_test boost_unit_test neuron particle_swarm) +target_link_libraries(particle_test boost_unit_test 4neuro) diff --git a/src/NetConnection/ConnectionWeight.cpp b/src/NetConnection/ConnectionWeight.cpp index c05b7e18a07c4ce9ecba1c88594d033068da9956..88f4fd579f6951b5fa8856902fc6a66e7db51f17 100644 --- a/src/NetConnection/ConnectionWeight.cpp +++ b/src/NetConnection/ConnectionWeight.cpp @@ -13,12 +13,10 @@ ConnectionWeight::ConnectionWeight() { } -ConnectionWeight::ConnectionWeight(int param_count, std::vector<double>* w_array, std::function<double(double *, int*, int)> *f) { +ConnectionWeight::ConnectionWeight(int param_count, std::vector<double>* w_array) { this->param_indices = new int[param_count]; this->n_params = param_count; this->weight_array = w_array; - - this->weight_function = f; } ConnectionWeight::~ConnectionWeight() { @@ -55,6 +53,11 @@ void ConnectionWeight::SetParamIndex(int value, int idx) { } double ConnectionWeight::eval() { - return (*this->weight_function)(&this->weight_array->at(0),this->param_indices, this->n_params); + double product = 1; + for(auto e : *this->weight_array) { + product *= e; + } + + return product; } diff --git a/src/NetConnection/ConnectionWeight.h b/src/NetConnection/ConnectionWeight.h index 6d771a40a979502e64f7174b4c0b16d324a5a7c6..6f2447da46a49cc2cb16b0bdc431550b2196659e 100644 --- a/src/NetConnection/ConnectionWeight.h +++ b/src/NetConnection/ConnectionWeight.h @@ -28,11 +28,6 @@ protected: */ int n_params = 0; - /** - * - */ - std::function<double(double *, int*, int)> *weight_function = nullptr; - public: /** @@ -45,7 +40,7 @@ public: * @param param_count * @param f */ - ConnectionWeight(int param_count, std::vector<double>* w_array, std::function<double(double *, int*, int)> *f); + ConnectionWeight(int param_count, std::vector<double>* w_array); /** * diff --git a/src/Network/NeuralNetwork.cpp b/src/Network/NeuralNetwork.cpp index 518c9233f86b828816acfbd26c879f1a0f1e53b8..aaf8f3d0964c9bb91350471d79a275b34d2184da 100644 --- a/src/Network/NeuralNetwork.cpp +++ b/src/Network/NeuralNetwork.cpp @@ -75,7 +75,8 @@ void NeuralNetwork::add_connection_simple(int n1_idx, int n2_idx, int weight_idx neuron_in->add_connection_in(u1u2); } -void NeuralNetwork::add_connection_general(int n1_idx, int n2_idx, std::function<double(double *, int*, int)> *f, int* weight_indices, double* weight_values, int n_weights) { +void NeuralNetwork::add_connection_general(int n1_idx, int n2_idx, std::function<double(double *, int*, int)> *f, + int* weight_indices, double* weight_values, int n_weights) { ConnectionWeight *con_weight_u1u2 = new ConnectionWeight(n_weights, this->connection_weights, f); //we analyze weights diff --git a/src/Network/NeuralNetwork.h b/src/Network/NeuralNetwork.h index 38884e0cde95c6fce753a19e9aae5753eea3c259..959b995d2cebdf69cf26bfc5b5845b818a29197a 100644 --- a/src/Network/NeuralNetwork.h +++ b/src/Network/NeuralNetwork.h @@ -68,8 +68,6 @@ private: */ std::vector<Neuron*>* active_eval_set = nullptr; - - /** * */ @@ -136,7 +134,8 @@ public: * @param weight_values * @param n_weights */ - void add_connection_general(int n1_idx, int n2_idx, std::function<double(double *, int*, int)> *f, int* weight_indices, double* weight_values, int n_weights); + void add_connection_general(int n1_idx, int n2_idx, std::function<double(double *, int*, int)> *f, + int* weight_indices, double* weight_values, int n_weights); /** * diff --git a/src/net_test_1.cpp b/src/net_test_1.cpp index 6e322c5a590804899d574a148045bad7df1c9da3..1eddf73ceb8bd7ed3a8ef8fbf084f0845a1c8b63 100644 --- a/src/net_test_1.cpp +++ b/src/net_test_1.cpp @@ -10,11 +10,7 @@ #include <vector> #include <utility> -#include "Network/NeuralNetwork.h" -#include "DataSet/DataSet.h" -#include "Neuron/NeuronLinear.h" -#include "LearningMethods/ParticleSwarm.h" -#include "ErrorFunction/ErrorFunctions.h" +#include "../include/4neuro.h" int main() { diff --git a/src/net_test_2.cpp b/src/net_test_2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fdae028cae233fc11fc9aad05af68cdbcd32580d --- /dev/null +++ b/src/net_test_2.cpp @@ -0,0 +1,82 @@ +/** + * Example of a set neural networks dependent on each other + */ + +// +// Created by martin on 7/16/18. +// + +#include <vector> +#include <utility> + +#include "../include/4neuro.h" + +int main() { + + /* TRAIN DATA DEFINITION */ + std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec; + std::vector<double> inp, out; + + inp = {0, 1}; + out = {0.5}; + data_vec.emplace_back(std::make_pair(inp, out)); + + inp = {1, 0.5}; + out = {0.75}; + data_vec.emplace_back(std::make_pair(inp, out)); + + DataSet ds(&data_vec); + + /* NETWORK DEFINITION */ + NeuralNetwork net, net2; + + /* Input neurons */ + NeuronLinear *i1 = new NeuronLinear(0.0, 1.0); //f(x) = x + NeuronLinear *i2 = new NeuronLinear(0.0, 1.0); //f(x) = x + + NeuronLinear *i3 = new NeuronLinear(1, 1); //f(x) = x + 1 + + /* Output neurons */ + NeuronLinear *o1 = new NeuronLinear(1.0, 2.0); //f(x) = 2x + 1 + NeuronLinear *o2 = new NeuronLinear(1, 2); //f(x) = 2x + 1 + + + + /* Adding neurons to the nets */ + int idx1 = net.add_neuron(i1); + int idx2 = net.add_neuron(i2); + int idx3 = net.add_neuron(o1); + + int idx4 = net2.add_neuron(i3); + int idx5 = net2.add_neuron(i4); + + /* Adding connections */ + //net.add_connection_simple(idx1, idx3, -1, 1.0); + //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(); + + /* ERROR FUNCTION SPECIFICATION */ + MSE mse(&net, &ds); + + /* TRAINING METHOD SETUP */ + unsigned int n_edges = 2; + unsigned int dim = n_edges, max_iters = 2000; + + + 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); + + swarm_01.optimize(0.5, 0.02); + + return 0; +} \ No newline at end of file diff --git a/src/neuron_serialization_example.cpp b/src/neuron_serialization_example.cpp index 26e42cbd9d62fd0bf3cba5171119c1d068b57235..e5f4436797dc0b23e04c0c84c48b7c0829b7dcd9 100644 --- a/src/neuron_serialization_example.cpp +++ b/src/neuron_serialization_example.cpp @@ -3,7 +3,7 @@ // /** - * Test of the binary serialization + * Test of the neuron binary serialization */ @@ -12,11 +12,7 @@ #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> -#include "Neuron/Neuron.h" -#include "Neuron/NeuronLinear.h" -#include "Neuron/NeuronLogistic.h" -#include "Neuron/NeuronBinary.h" -#include "Neuron/NeuronTanh.h" +#include "../include/4neuro.h" int main() { NeuronLinear n(2, 3);