Skip to content
Snippets Groups Projects
Commit aa726081 authored by Martin Beseda's avatar Martin Beseda
Browse files

WIP: trying to solve memory errors

parent 8a8549f1
No related branches found
No related tags found
No related merge requests found
......@@ -26,7 +26,7 @@ namespace lib4neuro {
this->delimiter = delimiter;
this->ignore_first_line = ignore_first_line;
this->header_included = ignore_first_line;
this->data = std::make_unique<std::vector<std::vector<std::string>>>();
// this->data = std::make_unique<std::vector<std::vector<std::string>>>();
}
void CSVReader::read() {
......@@ -56,18 +56,18 @@ namespace lib4neuro {
separated_line.emplace_back(line.substr(last));
/* Store the elements from the line to the vector with data */
this->data->emplace_back(separated_line);
this->data.emplace_back(separated_line);
}
ifs.close();
}
std::unique_ptr<std::vector<std::vector<std::string>>>* CSVReader::get_data() {
std::vector<std::vector<std::string>>* CSVReader::get_data() {
return &this->data;
}
void CSVReader::print_data() {
for(auto line : *this->data) {
for(auto line : this->data) {
for(auto e : line) {
std::cout << e << " ";
}
......@@ -80,12 +80,12 @@ namespace lib4neuro {
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_set_contents;
if(this->data->empty()) {
if(this->data.empty()) {
THROW_LOGIC_ERROR("DataSet can not be created as there were no data read beforehand! Did you forget to call "
"the method 'read()'?");
}
for (auto line : *this->data) {
for (auto line : this->data) {
//TODO check empty values in data
std::vector<double> input;
for (auto ind : *input_col_indices) {
......
......@@ -43,7 +43,7 @@ namespace lib4neuro {
/**
*
*/
std::unique_ptr<std::vector<std::vector<std::string>>> data;
std::vector<std::vector<std::string>> data;
public:
......@@ -64,7 +64,8 @@ namespace lib4neuro {
*
* @return
*/
LIB4NEURO_API std::unique_ptr<std::vector<std::vector<std::string>>>* get_data();
// LIB4NEURO_API std::shared_ptr<std::vector<std::vector<std::string>>> get_data();
LIB4NEURO_API std::vector<std::vector<std::string>>* get_data();
/**
*
......
......@@ -31,7 +31,12 @@ namespace lib4neuro {
void LearningSequence::optimize(lib4neuro::ErrorFunction &ef, std::ofstream *ofs) {
puts("*********************** 11");
double error = ef.eval();
puts("*********************** 12");
double the_best_error = error;
int mcycles = this->max_number_of_cycles, cycle_idx = 0;
......@@ -39,11 +44,21 @@ namespace lib4neuro {
mcycles--;
cycle_idx++;
puts("*********************** 7");
for( auto m: this->learning_sequence ){
puts("*********************** 8");
m->optimize( ef, ofs );
error = ef.eval(m->get_parameters());
puts("*********************** 9");
ef.get_network_instance()->copy_parameter_space(m->get_parameters());
puts("*********************** 10");
if( error < the_best_error ){
the_best_error = error;
this->best_parameters = *ef.get_parameters();
......
......@@ -387,7 +387,7 @@ namespace lib4neuro {
return output_net;
}
size_t NeuralNetwork::add_neuron(Neuron *n, BIAS_TYPE bt, size_t bias_idx) {
size_t NeuralNetwork::add_neuron(std::shared_ptr<Neuron> n, BIAS_TYPE bt, size_t bias_idx) {
if (bt == BIAS_TYPE::NO_BIAS) {
this->neuron_bias_indices.push_back(-1);
......@@ -668,7 +668,7 @@ namespace lib4neuro {
for (size_t i = 0; i < current_layer->size(); ++i) {
neuron_idx = current_layer->at(i);
active_neuron = dynamic_cast<NeuronDifferentiable *> (this->neurons.at(neuron_idx));
active_neuron = dynamic_cast<NeuronDifferentiable *> (this->neurons.at(neuron_idx).get());
if (active_neuron) {
bias_idx = this->neuron_bias_indices.at(neuron_idx);
......@@ -741,7 +741,7 @@ namespace lib4neuro {
for (size_t i = 0; i < current_layer->size(); ++i) {
neuron_idx = current_layer->at(i);
active_neuron = dynamic_cast<NeuronDifferentiable *> (this->neurons.at(neuron_idx));
active_neuron = dynamic_cast<NeuronDifferentiable *> (this->neurons.at(neuron_idx).get());
if (active_neuron) {
std::cout << " [backpropagation] active neuron: " << neuron_idx << std::endl;
......@@ -1275,7 +1275,9 @@ namespace lib4neuro {
current_layer_neuron_indices.reserve(inp_dim);
input_layer_neuron_indices.reserve(inp_dim);
for(unsigned int i = 0; i < inp_dim; i++) {
size_t neuron_id = this->add_neuron(new NeuronLinear, BIAS_TYPE::NO_BIAS);
std::shared_ptr<Neuron> new_neuron;
new_neuron.reset(new NeuronLinear());
size_t neuron_id = this->add_neuron(new_neuron, BIAS_TYPE::NO_BIAS);
current_layer_neuron_indices.emplace_back(neuron_id);
}
input_layer_neuron_indices = current_layer_neuron_indices;
......@@ -1294,9 +1296,11 @@ namespace lib4neuro {
size_t neuron_id;
/* Create new hidden neuron */
switch (hidden_layer_neuron_types->at(i-1)) {
switch (hidden_layer_neuron_types->at(i-1)) {
case NEURON_TYPE::BINARY: {
neuron_id = this->add_neuron(new NeuronBinary, BIAS_TYPE::NEXT_BIAS);
std::shared_ptr<Neuron> new_neuron;
new_neuron.reset(new NeuronBinary());
neuron_id = this->add_neuron(new_neuron, BIAS_TYPE::NEXT_BIAS);
COUT_DEBUG("Added BINARY neuron." << std::endl);
WRITE_TO_OFS_DEBUG(ofs, "Added BINARY neuron." << std::endl);
break;
......@@ -1308,14 +1312,18 @@ namespace lib4neuro {
}
case NEURON_TYPE::LINEAR: {
neuron_id = this->add_neuron(new NeuronLinear, BIAS_TYPE::NEXT_BIAS);
std::shared_ptr<Neuron> new_neuron;
new_neuron.reset(new NeuronLinear());
neuron_id = this->add_neuron(new_neuron, BIAS_TYPE::NEXT_BIAS);
COUT_DEBUG("Added LINEAR neuron." << std::endl);
WRITE_TO_OFS_DEBUG(ofs, "Added LINEAR neuron." << std::endl);
break;
}
case NEURON_TYPE::LOGISTIC: {
neuron_id = this->add_neuron(new NeuronLogistic, BIAS_TYPE::NEXT_BIAS);
std::shared_ptr<Neuron> new_neuron;
new_neuron.reset(new NeuronLogistic());
neuron_id = this->add_neuron(new_neuron, BIAS_TYPE::NEXT_BIAS);
COUT_DEBUG("Added LOGISTIC neuron." << std::endl);
WRITE_TO_OFS_DEBUG(ofs, "Added LINEAR neuron." << std::endl);
break;
......@@ -1338,7 +1346,9 @@ namespace lib4neuro {
/* Creation of OUTPUT layer neurons */
for(unsigned int i = 0; i < out_dim; i++) {
size_t neuron_id = this->add_neuron(new NeuronLinear, BIAS_TYPE::NO_BIAS);
std::shared_ptr<Neuron> new_neuron;
new_neuron.reset(new NeuronLinear());
size_t neuron_id = this->add_neuron(new_neuron, BIAS_TYPE::NO_BIAS);
current_layer_neuron_indices.emplace_back(neuron_id);
/* Connect new neuron with all neuron from the previous layer */
......
......@@ -55,7 +55,7 @@ namespace lib4neuro {
/**
*
*/
std::vector<Neuron *> neurons; // = nullptr;
std::vector<std::shared_ptr<Neuron>> neurons; // = nullptr;
/**
*
......@@ -264,7 +264,7 @@ namespace lib4neuro {
* @param[in] n
* @return
*/
LIB4NEURO_API size_t add_neuron(Neuron *n, BIAS_TYPE bt = BIAS_TYPE::NEXT_BIAS, size_t bias_idx = 0);
LIB4NEURO_API size_t add_neuron(std::shared_ptr<Neuron> n, BIAS_TYPE bt = BIAS_TYPE::NEXT_BIAS, size_t bias_idx = 0);
/**
*
......
......@@ -91,8 +91,10 @@ namespace lib4neuro {
std::vector<size_t> input_set(this->dim_i);
size_t idx;
for (size_t i = 0; i < this->dim_i; ++i) {
NeuronLinear *input_i = new NeuronLinear(); //f(x) = x
idx = this->solution->add_neuron(input_i, BIAS_TYPE::NO_BIAS);
// NeuronLinear *input_i = new NeuronLinear(); //f(x) = x
std::shared_ptr<Neuron> new_neuron;
new_neuron.reset(new NeuronLinear());
idx = this->solution->add_neuron(new_neuron, BIAS_TYPE::NO_BIAS);
input_set[i] = idx;
}
this->solution->specify_input_neurons(input_set);
......@@ -100,7 +102,9 @@ namespace lib4neuro {
/* output neuron */
std::vector<size_t> output_set(1);
idx = this->solution->add_neuron(new NeuronLinear(), BIAS_TYPE::NO_BIAS);//f(x) = x
std::shared_ptr<Neuron> new_neuron;
new_neuron.reset(new NeuronLinear());
idx = this->solution->add_neuron(new_neuron, BIAS_TYPE::NO_BIAS);//f(x) = x
output_set[0] = idx;
this->solution->specify_output_neurons(output_set);
size_t first_output_neuron = idx;
......@@ -108,9 +112,11 @@ namespace lib4neuro {
/* inner neurons */
size_t first_inner_neuron = 0;
for (size_t i = 0; i < this->dim_inn; ++i) {
NeuronLogistic *inner_i = new NeuronLogistic(); //f(x) = 1.0 / (1.0 + e^(-x))
this->solution_inner_neurons->push_back(inner_i);
idx = this->solution->add_neuron(inner_i, BIAS_TYPE::NEXT_BIAS);
// NeuronLogistic *inner_i = new NeuronLogistic(); //f(x) = 1.0 / (1.0 + e^(-x))
std::shared_ptr<Neuron> new_neuron2;
new_neuron2.reset(new NeuronLogistic());
this->solution_inner_neurons->push_back(new_neuron2.get());
idx = this->solution->add_neuron(new_neuron2, BIAS_TYPE::NEXT_BIAS);
if (i == 0) {
first_inner_neuron = idx;
......
......@@ -93,17 +93,17 @@ namespace lib4neuro {
std::map<MultiIndex, NeuralNetwork *> map_multiindices2nn;
/* A list of the differential equations */
std::vector<NeuralNetworkSum *> *differential_equations = nullptr;
std::vector<NeuralNetworkSum *> differential_equations; // = nullptr;
/* Error functions for differential equations */
std::vector<ErrorFunctionType> *errors_functions_types = nullptr;
std::vector<DataSet *> *errors_functions_data_sets = nullptr;
std::vector<ErrorFunctionType> errors_functions_types; // = nullptr;
std::vector<DataSet *> errors_functions_data_sets; // = nullptr;
/* NN as the unknown function */
NeuralNetwork *solution = nullptr;
/* auxilliary variables */
std::vector<NeuronLogistic *> *solution_inner_neurons = nullptr;
std::vector<NeuronLogistic *> solution_inner_neurons; // = nullptr;
size_t dim_i = 0, dim_inn = 0, n_equations = 0;
public:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment