From e362ef7a8eb4d065d4e42150c92ffdf454647785 Mon Sep 17 00:00:00 2001
From: Martin Beseda <martin.beseda@vsb.cz>
Date: Mon, 4 Mar 2019 12:51:35 +0100
Subject: [PATCH] FIX: Merge

---
 src/DataSet/DataSet.cpp       | 18 ++++-------
 src/DataSet/DataSet.h         |  7 +---
 src/Network/NeuralNetwork.cpp | 61 +++++++++++++++++------------------
 src/Network/NeuralNetwork.h   | 31 ++++++++----------
 4 files changed, 50 insertions(+), 67 deletions(-)

diff --git a/src/DataSet/DataSet.cpp b/src/DataSet/DataSet.cpp
index 820c1e61..822f1f40 100644
--- a/src/DataSet/DataSet.cpp
+++ b/src/DataSet/DataSet.cpp
@@ -17,13 +17,10 @@ namespace lib4neuro {
         this->n_elements = 0;
         this->input_dim = 0;
         this->output_dim = 0;
-        this->gen = boost::random::mt19937(std::time(0));
     }
 
     DataSet::DataSet(std::string file_path) {
         std::ifstream ifs(file_path);
-        this->gen = boost::random::mt19937(std::time(0));
-
         if(ifs.is_open()) {
             try {
                 boost::archive::text_iarchive ia(ifs);
@@ -45,7 +42,6 @@ namespace lib4neuro {
         this->data = *data_ptr;
         this->input_dim = this->data[0].first.size();
         this->output_dim = this->data[0].second.size();
-        this->gen = boost::random::mt19937(std::time(0));
 
         if(ns) {
             this->normalization_strategy = ns;
@@ -66,7 +62,6 @@ namespace lib4neuro {
         this->n_elements = 0;
         this->input_dim = 1;
         this->output_dim = 1;
-        this->gen = boost::random::mt19937(std::time(0));
 
         if(ns) {
             this->normalization_strategy = ns;
@@ -87,7 +82,6 @@ namespace lib4neuro {
         this->input_dim = bounds.size() / 2;
         this->output_dim = output_dim;
         this->n_elements = 0;
-        this->gen = boost::random::mt19937(std::time(0));
 
         if(ns) {
             this->normalization_strategy = ns;
@@ -152,7 +146,7 @@ namespace lib4neuro {
         }
 
         for (unsigned int i = 0; i < bounds.size(); i += 2) {
-             if (no_elems_in_one_dim == 1) {
+            if (no_elems_in_one_dim == 1) {
                 frac = 1;
             } else {
                 frac = (bounds[i] - bounds[i+1]) / (no_elems_in_one_dim - 1);
@@ -406,17 +400,19 @@ namespace lib4neuro {
      * Method returning random amount of data pairs between 1-max
      */
     std::vector<std::pair<std::vector<double>, std::vector<double>>> DataSet::get_random_data_batch(size_t max) {
-        if (max <= 0 || max >= this->data.size()) {
+        if (max <= 0) {
             return this->data;
         } else {
             std::vector<std::pair<std::vector<double>, std::vector<double>>> newData;
-            boost::random::uniform_int_distribution<> dist(0, this->data.size() - 1);
+            srand(time(NULL));  //TODO use Mersen twister from Boost
 
+            size_t n_chosen = rand() % std::min(max, this->data.size())+1;
+            n_chosen = max;
             std::vector<size_t> chosens;
             size_t chosen;
 
-            for (int i = 0; i < max; i++) {
-                chosen = dist(gen);
+            for (int i = 0; i < n_chosen; i++) {
+                chosen = rand() % this->data.size();
                 auto it = std::find(chosens.begin(), chosens.end(), chosen);
 
                 if (it != chosens.end()) {
diff --git a/src/DataSet/DataSet.h b/src/DataSet/DataSet.h
index fd20913a..87023134 100644
--- a/src/DataSet/DataSet.h
+++ b/src/DataSet/DataSet.h
@@ -12,9 +12,6 @@
 #include <string>
 #include <functional>
 #include <limits>
-#include <boost/random/mersenne_twister.hpp>
-#include <boost/random/uniform_int_distribution.hpp>
-#include <ctime>
 
 #include "../settings.h"
 #include "../NormalizationStrategy/NormalizationStrategy.h"
@@ -29,8 +26,6 @@ namespace lib4neuro {
 
     private:
 
-        boost::random::mt19937 gen;
-
         /**
          * Number of elements in the data set
          */
@@ -293,7 +288,7 @@ namespace lib4neuro {
          * @param max
          * @return
          */
-	    LIB4NEURO_API  std::vector<std::pair<std::vector<double>, std::vector<double>>> get_random_data_batch(size_t max);
+        LIB4NEURO_API  std::vector<std::pair<std::vector<double>, std::vector<double>>> get_random_data_batch(size_t max);
     };
 }
 #endif //INC_4NEURO_DATASET_H
diff --git a/src/Network/NeuralNetwork.cpp b/src/Network/NeuralNetwork.cpp
index 263c930e..262acd3a 100644
--- a/src/Network/NeuralNetwork.cpp
+++ b/src/Network/NeuralNetwork.cpp
@@ -35,8 +35,6 @@ namespace lib4neuro {
         this->delete_weights = true;
         this->delete_biases = true;
         this->layers_analyzed = false;
-
-        this->gen = boost::random::mt19937(std::time(0));
     }
 
     NeuralNetwork::NeuralNetwork(std::string filepath) {
@@ -547,30 +545,6 @@ namespace lib4neuro {
         this->delete_weights = false;
     }
 
-    void NeuralNetwork::get_jacobian(std::vector<std::vector<double>> &jacobian, std::pair<std::vector<double>, std::vector<double>> &data, std::vector<double> &error) {
-
-        std::vector<double> fv(this->get_n_outputs());
-
-        jacobian.resize(this->get_n_outputs());
-        error.resize(this->get_n_outputs());
-        for(size_t i = 0; i < this->get_n_outputs(); ++i){
-            jacobian[i].resize(this->get_n_weights() + this->get_n_biases());
-            std::fill(jacobian[i].begin(), jacobian[i].end(), 0);
-        }
-
-        this->eval_single( data.first, fv );
-
-        std::vector<double> error_partial(this->get_n_outputs());
-        std::fill(error_partial.begin(), error_partial.end(), 0.0);
-
-        for( size_t i = 0; i < this->get_n_outputs(); ++i){
-            error_partial[i] = 1;
-            this->add_to_gradient_single(data.first, error_partial, 1.0, jacobian[i]);
-            error[i] = data.second[i] - fv[i];
-            error_partial[i] = 0;
-        }
-    }
-
     void NeuralNetwork::eval_single(::std::vector<double>& input,
                                     ::std::vector<double>& output,
                                     ::std::vector<double>* custom_weights_and_biases) {
@@ -789,7 +763,7 @@ namespace lib4neuro {
 
     void NeuralNetwork::randomize_weights() {
 
-
+        boost::random::mt19937 gen(std::time(0));
 
         // Init weight guess ("optimal" for logistic activation functions)
         double r = 4 * sqrt(6. / (this->connection_weights->size()));
@@ -803,7 +777,7 @@ namespace lib4neuro {
 
     void NeuralNetwork::randomize_biases() {
 
-
+        boost::random::mt19937 gen(std::time(0));
 
         // Init weight guess ("optimal" for logistic activation functions)
         boost::random::uniform_real_distribution<> dist(-1, 1);
@@ -1226,8 +1200,6 @@ namespace lib4neuro {
                                          "doesn't specify input and output layers, which are compulsory!");
         }
 
-        this->gen = boost::random::mt19937(std::time(0));
-
         this->neurons = new ::std::vector<Neuron *>(0);
         this->neuron_biases = new ::std::vector<double>(0);
         this->neuron_potentials = new ::std::vector<double>(0);
@@ -1256,8 +1228,8 @@ namespace lib4neuro {
         COUT_DEBUG("# of outputs: " << out_dim << std::endl);
 
         WRITE_TO_OFS_DEBUG(ofs, "Fully connected feed-forward network being constructed:" << std::endl
-                                << "# of inputs: " << inp_dim << std::endl
-                                << "# of outputs: " << out_dim << std::endl);
+                                                                                          << "# of inputs: " << inp_dim << std::endl
+                                                                                          << "# of outputs: " << out_dim << std::endl);
 
         std::vector<size_t> input_layer_neuron_indices;
         std::vector<size_t> previous_layer_neuron_indices;
@@ -1348,5 +1320,30 @@ namespace lib4neuro {
 
         this->analyze_layer_structure();
     }
+
+    void NeuralNetwork::get_jacobian(std::vector<std::vector<double>> &jacobian, std::pair<std::vector<double>, std::vector<double>> &data, std::vector<double> &error) {
+
+        std::vector<double> fv(this->get_n_outputs());
+
+        jacobian.resize(this->get_n_outputs());
+        error.resize(this->get_n_outputs());
+        for(size_t i = 0; i < this->get_n_outputs(); ++i){
+            jacobian[i].resize(this->get_n_weights() + this->get_n_biases());
+            std::fill(jacobian[i].begin(), jacobian[i].end(), 0);
+        }
+
+        this->eval_single( data.first, fv );
+
+        std::vector<double> error_partial(this->get_n_outputs());
+        std::fill(error_partial.begin(), error_partial.end(), 0.0);
+
+        for( size_t i = 0; i < this->get_n_outputs(); ++i){
+            error_partial[i] = 1;
+            this->add_to_gradient_single(data.first, error_partial, 1.0, jacobian[i]);
+            error[i] = data.second[i] - fv[i];
+            error_partial[i] = 0;
+        }
+    }
+
 }
 
diff --git a/src/Network/NeuralNetwork.h b/src/Network/NeuralNetwork.h
index 0b06cd2b..8788a23c 100644
--- a/src/Network/NeuralNetwork.h
+++ b/src/Network/NeuralNetwork.h
@@ -17,10 +17,6 @@
 #include <algorithm>
 #include <utility>
 #include <fstream>
-#include <boost/random/mersenne_twister.hpp>
-#include <boost/random/uniform_real_distribution.hpp>
-#include <ctime>
-
 
 #include "../settings.h"
 #include "../Neuron/Neuron.h"
@@ -32,8 +28,6 @@
 #include "../NetConnection/ConnectionFunctionIdentity.h"
 #include "../NormalizationStrategy/NormalizationStrategy.h"
 
-
-
 namespace lib4neuro {
 
     /**
@@ -55,9 +49,8 @@ namespace lib4neuro {
      *
      */
     class NeuralNetwork {
-
     protected:
-        boost::random::mt19937 gen;
+
         /**
          *
          */
@@ -169,6 +162,18 @@ namespace lib4neuro {
 
     public:
 
+        /**
+         * Runs @data through the network and then computes partial derivatives with respect to each output function and adds them
+         * to seperate vectors in @jacobian. Also computes the out error and stores in the vector @error
+         * @param[out] jacobian
+         * @param[in] data
+         * @param[out] error
+         */
+        LIB4NEURO_API virtual void
+        get_jacobian(std::vector<std::vector<double>> &jacobian, std::pair<std::vector<double>, std::vector<double>> &data, std::vector<double> &error);
+
+
+
         /**
         *
         * @param input
@@ -279,16 +284,6 @@ namespace lib4neuro {
         LIB4NEURO_API void
         add_existing_connection(size_t n1_idx, size_t n2_idx, size_t connection_idx, NeuralNetwork &parent_network);
 
-        /**
-         * Runs @data through the network and then computes partial derivatives with respect to each output function and adds them
-         * to seperate vectors in @jacobian. Also computes the out error and stores in the vector @error
-         * @param[out] jacobian
-         * @param[in] data
-         * @param[out] error
-         */
-        LIB4NEURO_API virtual void
-        get_jacobian(std::vector<std::vector<double>> &jacobian, std::pair<std::vector<double>, std::vector<double>> &data, std::vector<double> &error);
-
         /**
          *
          */
-- 
GitLab