diff --git a/opennn/growing_inputs.cpp b/opennn/growing_inputs.cpp index 0a49f796c..325fd42ac 100644 --- a/opennn/growing_inputs.cpp +++ b/opennn/growing_inputs.cpp @@ -157,156 +157,161 @@ InputsSelectionResults GrowingInputs::perform_inputs_selection() time(&beginning_time); bool stop = false; -/* + for(Index i = 0; i < maximum_epochs_number; i++) { + data_set->set_raw_variable_use(correlations_rank_descending[raw_variable_index], DataSet::VariableUse::Input); Index input_raw_variables_number = data_set->get_raw_variables_number(DataSet::VariableUse::Input); Index input_variables_number = data_set->get_variables_number(DataSet::VariableUse::Input); - if(input_raw_variables_number >= minimum_inputs_number) + if (input_raw_variables_number < minimum_inputs_number) { - Index epoch = input_raw_variables_number - minimum_inputs_number + 1; - neural_network->set_input_dimensions({ input_variables_number }); + // @todo - if(display) - { - cout << endl - << "Epoch: " << epoch << endl - << "Input raw_variables number: " << input_raw_variables_number << endl - << "Inputs: " << endl; + continue; + } + + Index epoch = input_raw_variables_number - minimum_inputs_number + 1; + neural_network->set_input_dimensions({ input_variables_number }); - input_raw_variables_names = data_set->get_raw_variable_names(DataSet::VariableUse::Input); + if(display) + { + cout << endl + << "Epoch: " << epoch << endl + << "Input raw_variables number: " << input_raw_variables_number << endl + << "Inputs: " << endl; - print_vector(input_raw_variables_names); - } + input_raw_variables_names = data_set->get_raw_variable_names(DataSet::VariableUse::Input); - type minimum_training_error = numeric_limits::max(); - type minimum_selection_error = numeric_limits::max(); + print_vector(input_raw_variables_names); + } - for(Index j = 0; j < trials_number; j++) - { - neural_network->set_parameters_random(); + type minimum_training_error = numeric_limits::max(); + type minimum_selection_error = numeric_limits::max(); - if(data_set->has_nan()) - data_set->scrub_missing_values(); + for(Index j = 0; j < trials_number; j++) + { + neural_network->set_parameters_random(); - training_results = training_strategy->perform_training(); + if(data_set->has_nan()) + data_set->scrub_missing_values(); - if(training_results.get_selection_error() < minimum_selection_error) - { - minimum_training_error = training_results.get_training_error(); - minimum_selection_error = training_results.get_selection_error(); + training_results = training_strategy->perform_training(); - input_selection_results.training_error_history(input_raw_variables_number-1) = minimum_training_error; - input_selection_results.selection_error_history(input_raw_variables_number-1) = minimum_selection_error; - } + if(training_results.get_selection_error() < minimum_selection_error) + { + minimum_training_error = training_results.get_training_error(); + minimum_selection_error = training_results.get_selection_error(); - if(training_results.get_selection_error() < input_selection_results.optimum_selection_error) - { - // Neural network + input_selection_results.training_error_history(input_raw_variables_number-1) = minimum_training_error; + input_selection_results.selection_error_history(input_raw_variables_number-1) = minimum_selection_error; + } - input_selection_results.optimal_input_raw_variables_indices = data_set->get_raw_variable_indices(DataSet::VariableUse::Input); - input_selection_results.optimal_input_raw_variables_names = data_set->get_raw_variable_names(DataSet::VariableUse::Input); + if(training_results.get_selection_error() < input_selection_results.optimum_selection_error) + { + // Neural network - input_selection_results.optimal_parameters = neural_network->get_parameters(); + input_selection_results.optimal_input_raw_variables_indices = data_set->get_raw_variable_indices(DataSet::VariableUse::Input); + input_selection_results.optimal_input_raw_variables_names = data_set->get_raw_variable_names(DataSet::VariableUse::Input); - // Loss index + input_selection_results.optimal_parameters = neural_network->get_parameters(); - input_selection_results.optimum_training_error = training_results.get_training_error(); - input_selection_results.optimum_selection_error = training_results.get_selection_error(); - } + // Loss index - if(display) - cout << "Trial number: " << j+1 << endl - << " Training error: " << training_results.get_training_error() << endl - << " Selection error: " << training_results.get_selection_error() << endl; + input_selection_results.optimum_training_error = training_results.get_training_error(); + input_selection_results.optimum_selection_error = training_results.get_selection_error(); } - if(previus_training_error < minimum_training_error) - { - cout << "Selection failure" << endl; + if(display) + cout << "Trial number: " << j+1 << endl + << " Training error: " << training_results.get_training_error() << endl + << " Selection error: " << training_results.get_selection_error() << endl; + } - selection_failures++; + if(previus_training_error < minimum_training_error) + { + cout << "Selection failure" << endl; - data_set->set_raw_variable_use(correlations_rank_descending[raw_variable_index], DataSet::VariableUse::None); + selection_failures++; - input_raw_variables_number += -1; - } - else - { - previus_training_error = minimum_training_error; + data_set->set_raw_variable_use(correlations_rank_descending[raw_variable_index], DataSet::VariableUse::None); + + input_raw_variables_number += -1; + } + else + { + previus_training_error = minimum_training_error; // previus_selection_error = minimum_selection_error; - input_selection_results.training_error_history(input_raw_variables_number) = minimum_training_error; - input_selection_results.selection_error_history(input_raw_variables_number) = minimum_selection_error; - } + input_selection_results.training_error_history(input_raw_variables_number) = minimum_training_error; + input_selection_results.selection_error_history(input_raw_variables_number) = minimum_selection_error; + } - time(¤t_time); + time(¤t_time); - elapsed_time = type(difftime(current_time,beginning_time)); + elapsed_time = type(difftime(current_time,beginning_time)); - // Stopping criteria + // Stopping criteria - stop = true; - - if(elapsed_time >= maximum_time) - { - if(display) cout << "Epoch " << epoch << "\nMaximum time reached: " << write_time(elapsed_time) << endl; - input_selection_results.stopping_condition = InputsSelection::StoppingCondition::MaximumTime; - } - else if(input_selection_results.optimum_selection_error <= selection_error_goal) - { - if(display) cout << "\nSelection error reached: " << input_selection_results.optimum_selection_error << endl; - input_selection_results.stopping_condition = InputsSelection::StoppingCondition::SelectionErrorGoal; - } - else if(epoch >= maximum_epochs_number) - { - if(display) cout << "\nMaximum number of epochs reached." << endl; - input_selection_results.stopping_condition = InputsSelection::StoppingCondition::MaximumEpochs; - } - else if(selection_failures >= maximum_selection_failures) - { - if(display) cout << "\nMaximum selection failures ("<= maximum_inputs_number || input_raw_variables_number >= original_input_raw_variables_number) - { - if(display) cout << "\nMaximum inputs (" << input_raw_variables_number << ") reached." << endl; - input_selection_results.stopping_condition = InputsSelection::StoppingCondition::MaximumInputs; - } - else if(raw_variable_index >= correlations_rank_descending.size() - 1 ) - { - if(display) cout << "\nAll the raw_variables has been used." << endl; - input_selection_results.stopping_condition = InputsSelection::StoppingCondition::MaximumInputs; - } - else - { - stop = false; - } + stop = true; + if(elapsed_time >= maximum_time) + { + if(display) cout << "Epoch " << epoch << "\nMaximum time reached: " << write_time(elapsed_time) << endl; + input_selection_results.stopping_condition = InputsSelection::StoppingCondition::MaximumTime; + } + else if(input_selection_results.optimum_selection_error <= selection_error_goal) + { + if(display) cout << "\nSelection error reached: " << input_selection_results.optimum_selection_error << endl; + input_selection_results.stopping_condition = InputsSelection::StoppingCondition::SelectionErrorGoal; + } + else if(epoch >= maximum_epochs_number) + { + if(display) cout << "\nMaximum number of epochs reached." << endl; + input_selection_results.stopping_condition = InputsSelection::StoppingCondition::MaximumEpochs; + } + else if(selection_failures >= maximum_selection_failures) + { + if(display) cout << "\nMaximum selection failures ("<= maximum_inputs_number || input_raw_variables_number >= original_input_raw_variables_number) + { + if(display) cout << "\nMaximum inputs (" << input_raw_variables_number << ") reached." << endl; + input_selection_results.stopping_condition = InputsSelection::StoppingCondition::MaximumInputs; + } + else if(raw_variable_index >= correlations_rank_descending.size() - 1 ) + { + if(display) cout << "\nAll the raw_variables has been used." << endl; + input_selection_results.stopping_condition = InputsSelection::StoppingCondition::MaximumInputs; + } + else + { + stop = false; + } - if(stop) - { - input_selection_results.elapsed_time = write_time(elapsed_time); + if(stop) + { + input_selection_results.elapsed_time = write_time(elapsed_time); - input_selection_results.resize_history(input_raw_variables_number); + input_selection_results.resize_history(input_raw_variables_number); - break; - } + break; } + - raw_variable_index++; + raw_variable_index++; } -*/ + // Set data set stuff data_set->set_input_target_raw_variable_indices(input_selection_results.optimal_input_raw_variables_indices, target_raw_variable_indices); data_set->print(); - /* + const vector input_variable_scalers = data_set->get_variable_scalers(DataSet::VariableUse::Input); const vector input_variable_descriptives = data_set->calculate_variable_descriptives(DataSet::VariableUse::Input); @@ -325,11 +330,11 @@ InputsSelectionResults GrowingInputs::perform_inputs_selection() scaling_layer_2d->set_descriptives(input_variable_descriptives); scaling_layer_2d->set_scalers(input_variable_scalers); } - +/* neural_network->set_parameters(input_selection_results.optimal_parameters); - - if(display) input_selection_results.print(); */ + if(display) input_selection_results.print(); + return input_selection_results; } diff --git a/opennn/neural_network.cpp b/opennn/neural_network.cpp index 71ce754ea..18980248c 100644 --- a/opennn/neural_network.cpp +++ b/opennn/neural_network.cpp @@ -677,20 +677,21 @@ void NeuralNetwork::set_output_namess(const vector& new_output_namess) void NeuralNetwork::set_input_dimensions(const dimensions& new_input_dimensions) { -/* - input_names.resize(new_inputs_number); + input_names.resize(new_input_dimensions[0]); if(has(Layer::Type::Scaling2D)) { - ScalingLayer2D* scaling_layer_2d = get_scaling_layer_2d(); + ScalingLayer2D* scaling_layer = static_cast(get_first(Layer::Type::Scaling2D)); - scaling_layer_2d->set_inputs_number(new_inputs_number); + scaling_layer->set_input_dimensions(new_input_dimensions); } - const Index first_trainable_layer_index = get_first_trainable_layer_index(); + if (has(Layer::Type::Perceptron)) + { + PerceptronLayer* perceptron_layer = static_cast(get_first(Layer::Type::Perceptron)); - layers[first_trainable_layer_index]->set_inputs_number(new_inputs_number); -*/ + perceptron_layer->set_input_dimensions(new_input_dimensions); + } } @@ -698,7 +699,7 @@ void NeuralNetwork::set_default() { display = true; - layer_input_indices = vector>(); + layer_input_indices.clear(); } @@ -849,6 +850,9 @@ vector NeuralNetwork::get_layer_parameter_numbers() const void NeuralNetwork::set_parameters(const Tensor& new_parameters) const { + if (new_parameters.size() != get_parameters_number()) + throw runtime_error("New parameters size is not equal to parameters size."); + const Index layers_number = get_layers_number(); const vector layer_parameter_numbers = get_layer_parameter_numbers();