diff --git a/src/Network/NeuralNetwork.cpp b/src/Network/NeuralNetwork.cpp index fe8bc379b9b378fc9e5cbe21cae8fce49c6f1447..2f6bb5c92b7f94f454fcdc1b55e9ca8efebe1ef8 100644 --- a/src/Network/NeuralNetwork.cpp +++ b/src/Network/NeuralNetwork.cpp @@ -25,21 +25,8 @@ namespace lib4neuro { } NeuralNetwork::NeuralNetwork(std::string filepath) { - ::std::ifstream ifs(filepath); - if (ifs.is_open()) { - try { - boost::archive::text_iarchive ia(ifs); - ia >> *this; - } - catch (boost::archive::archive_exception& e) { - THROW_RUNTIME_ERROR( - "Serialized archive error: '" + e.what() + "'! Please, check if your file is really " - "the serialized DataSet."); - } - ifs.close(); - } else { - THROW_RUNTIME_ERROR("File '" + filepath + "' couldn't be open!"); - } + + this->init_from_file( filepath ); } @@ -131,8 +118,9 @@ namespace lib4neuro { } potential = this->neurons.at(si)->activate(this->neuron_potentials.at(si), bias); - std::cout << " applying bias: " << bias << " to neuron potential: " << this->neuron_potentials.at(si) - << " -> " << potential << std::endl; + std::cout << "Neuron" << si << " (" << this->neuron_potentials.at(si) << " - " << bias << ") -> (" << potential << ")" << std::endl; +// std::cout << " applying bias: " << bias << " to neuron potential: " << this->neuron_potentials.at(si) +// << " -> " << potential << std::endl; for (auto c: *this->outward_adjacency.at(si)) { size_t ti = c.first; @@ -140,10 +128,11 @@ namespace lib4neuro { this->neuron_potentials.at(ti) += this->connection_list.at(ci)->eval(this->connection_weights) * potential; + std::cout << " EDGE(" << si << ", " << ti << ")" << this->connection_list.at(ci)->eval(this->connection_weights) << std::endl; - std::cout << " adding input to neuron " << ti << " += " - << this->connection_list.at(ci)->eval(this->connection_weights) << "*" << potential - << std::endl; +// std::cout << " adding input to neuron " << ti << " += " +// << this->connection_list.at(ci)->eval(this->connection_weights) << "*" << potential +// << std::endl; } } } @@ -157,7 +146,7 @@ namespace lib4neuro { } output[i] = this->neurons.at(oi)->activate(this->neuron_potentials.at(oi), bias); - std::cout << "setting the output[" << i << "] = " << output[i] << "(bias = " << bias << ")" << std::endl; +// std::cout << "setting the output[" << i << "] = " << output[i] << "(bias = " << bias << ")" << std::endl; ++i; } } @@ -203,7 +192,7 @@ namespace lib4neuro { NeuralNetwork::add_connection_constant(size_t n1_idx, size_t n2_idx, double weight) { - std::shared_ptr<ConnectionFunctionConstant> cfc = std::make_shared<ConnectionFunctionConstant>(ConnectionFunctionConstant()); + std::shared_ptr<ConnectionFunctionConstant> cfc = std::make_shared<ConnectionFunctionConstant>(ConnectionFunctionConstant(weight)); size_t conn_idx = this->add_new_connection_to_list(cfc); @@ -831,6 +820,25 @@ namespace lib4neuro { this->layers_analyzed = true; } + void NeuralNetwork::init_from_file(const std::string &filepath) { + ::std::ifstream ifs(filepath); + if (ifs.is_open()) { + try { + boost::archive::text_iarchive ia(ifs); + ia >> *this; + } + catch (boost::archive::archive_exception& e) { + THROW_RUNTIME_ERROR( + "Serialized archive error: '" + e.what() + "'! Please, check if your file is really " + "the serialized DataSet."); + } + ifs.close(); + } else { + THROW_RUNTIME_ERROR("File '" + filepath + "' couldn't be open!"); + } + + } + void NeuralNetwork::save_text(std::string filepath) { ::std::ofstream ofs(filepath); { diff --git a/src/Network/NeuralNetwork.h b/src/Network/NeuralNetwork.h index c498374f955887ea6c789f8fa1bb8a9125a22d08..b3c7537b62b15181631cad40c300fc7dd1908c36 100644 --- a/src/Network/NeuralNetwork.h +++ b/src/Network/NeuralNetwork.h @@ -127,6 +127,13 @@ namespace lib4neuro { */ bool delete_biases = true; + /** + * helpful arrays to be used in derived classes which are not neccessarily part of the lib4neauro library + */ + std::vector<size_t> ___ind1, ___ind2; + std::vector<std::vector<size_t>> ___ind_m1, ___ind_m2; + std::vector<std::vector<double>> ___val_m1; + /** * */ @@ -166,6 +173,12 @@ namespace lib4neuro { */ void analyze_layer_structure(); + /** + * for loading purposes outside the scope of this library + */ + + virtual void init_from_file(const std::string &fn); + public: /** diff --git a/src/Network/NeuralNetworkSerialization.h b/src/Network/NeuralNetworkSerialization.h index 6c834fbe557b80131f3511e00cbfb654f36d2ca5..bef1a82642b9833cd34ae9c4d820264abfccfb77 100644 --- a/src/Network/NeuralNetworkSerialization.h +++ b/src/Network/NeuralNetworkSerialization.h @@ -44,6 +44,12 @@ namespace lib4neuro { ar & nn.delete_weights; ar & nn.delete_biases; ar & nn.normalization_strategy; + + ar & nn.___ind1; + ar & nn.___ind2; + ar & nn.___ind_m1; + ar & nn.___ind_m2; + ar & nn.___val_m1; } }; }