Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
RoberLopez committed Jan 29, 2025
1 parent 3d1ed9d commit 6d237e9
Show file tree
Hide file tree
Showing 21 changed files with 168 additions and 362 deletions.
9 changes: 4 additions & 5 deletions opennn/auto_associative_data_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ void AutoAssociativeDataSet::transform_associative_raw_variables()
{
raw_variable_index = i%raw_variables_number;

if(i < raw_variables_number)
new_raw_variables[index].set(raw_variables[raw_variable_index].name,
i < raw_variables_number
? new_raw_variables[index].set(raw_variables[raw_variable_index].name,
DataSet::VariableUse::Input,
raw_variables[raw_variable_index].type,
raw_variables[raw_variable_index].scaler,
raw_variables[raw_variable_index].categories);
else
new_raw_variables[index].set(raw_variables[raw_variable_index].name + "_output",
raw_variables[raw_variable_index].categories)
: new_raw_variables[index].set(raw_variables[raw_variable_index].name + "_output",
DataSet::VariableUse::Target,
raw_variables[raw_variable_index].type,
raw_variables[raw_variable_index].scaler,
Expand Down
13 changes: 4 additions & 9 deletions opennn/conjugate_gradient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,20 +477,15 @@ void ConjugateGradient::update_parameters(
{
const Index parameters_number = back_propagation.parameters.dimension(0);

if(optimization_data.epoch == 0 || optimization_data.epoch % parameters_number == 0)
{
calculate_gradient_descent_training_direction(
optimization_data.epoch == 0 || optimization_data.epoch % parameters_number == 0
? calculate_gradient_descent_training_direction(
back_propagation.gradient,
optimization_data.training_direction);
}
else
{
calculate_conjugate_gradient_training_direction(
optimization_data.training_direction)
: calculate_conjugate_gradient_training_direction(
optimization_data.old_gradient,
back_propagation.gradient,
optimization_data.old_training_direction,
optimization_data.training_direction);
}

optimization_data.training_slope.device(*thread_pool_device)
= back_propagation.gradient.contract(optimization_data.training_direction, AT_B);
Expand Down
14 changes: 6 additions & 8 deletions opennn/convolutional_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ bool ConvolutionalLayer::get_batch_normalization() const
void ConvolutionalLayer::preprocess_inputs(const Tensor<type, 4>& inputs,
Tensor<type, 4>& preprocessed_inputs) const
{
if (convolution_type == ConvolutionType::Same)
preprocessed_inputs.device(*thread_pool_device) = inputs.pad(get_paddings());
else
preprocessed_inputs.device(*thread_pool_device) = inputs;
convolution_type == ConvolutionType::Same
? preprocessed_inputs.device(*thread_pool_device) = inputs.pad(get_paddings())
: preprocessed_inputs.device(*thread_pool_device) = inputs;

if (row_stride != 1 || column_stride != 1)
preprocessed_inputs.device(*thread_pool_device) = preprocessed_inputs.stride(get_strides());
Expand Down Expand Up @@ -218,10 +217,9 @@ void ConvolutionalLayer::forward_propagate(const vector<pair<type*, dimensions>>
*/
}

if (is_training)
calculate_activations(outputs, activation_derivatives);
else
calculate_activations(outputs, empty);
is_training
? calculate_activations(outputs, activation_derivatives)
: calculate_activations(outputs, empty);
}


Expand Down
152 changes: 47 additions & 105 deletions opennn/data_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ DataSet::RawVariable::RawVariable(const string& new_name,
const Scaler& new_scaler,
const vector<string>& new_categories)
{
name = new_name;
use = new_raw_variable_use;
type = new_type;
scaler = new_scaler;
categories = new_categories;
set(new_name, new_raw_variable_use, new_type, new_scaler, new_categories);
}


Expand All @@ -73,22 +69,11 @@ void DataSet::RawVariable::set_scaler(const Scaler& new_scaler)
}


void DataSet::RawVariable::set_scaler(const string& new_scaler)
void DataSet::RawVariable::set_scaler(const string& new_scaler_string)
{
if(new_scaler == "None")
set_scaler(Scaler::None);
else if(new_scaler == "MinimumMaximum")
set_scaler(Scaler::MinimumMaximum);
else if(new_scaler == "MeanStandardDeviation")
set_scaler(Scaler::MeanStandardDeviation);
else if(new_scaler == "StandardDeviation")
set_scaler(Scaler::StandardDeviation);
else if(new_scaler == "Logarithm")
set_scaler(Scaler::Logarithm);
else if (new_scaler == "ImageMinMax")
set_scaler(Scaler::ImageMinMax);
else
throw runtime_error("Unknown scaler: " + new_scaler + "\n");
const Scaler new_scaler = string_to_scaler(new_scaler_string);

set_scaler(new_scaler);
}


Expand All @@ -99,7 +84,8 @@ void DataSet::RawVariable::set_use(const VariableUse& new_raw_variable_use)


void DataSet::RawVariable::set_use(const string& new_raw_variable_use)
{ if(new_raw_variable_use == "Decoder")
{
if(new_raw_variable_use == "Decoder")
set_use(VariableUse::Decoder);
else if(new_raw_variable_use == "Input")
set_use(VariableUse::Input);
Expand Down Expand Up @@ -133,8 +119,6 @@ void DataSet::RawVariable::set_type(const string& new_raw_variable_type)

void DataSet::RawVariable::set_categories(const vector<string>& new_categories)
{
categories.resize(new_categories.size());

categories = new_categories;
}

Expand All @@ -157,7 +141,7 @@ void DataSet::RawVariable::from_XML(const XMLDocument& document)
void DataSet::RawVariable::to_XML(XMLPrinter& printer) const
{
add_xml_element(printer,"Name", name);
add_xml_element(printer,"Scaler", get_scaler_string());
add_xml_element(printer,"Scaler", scaler_to_string(scaler));
add_xml_element(printer,"Use", get_use_string());
add_xml_element(printer,"Type", get_type_string());

Expand All @@ -177,7 +161,7 @@ void DataSet::RawVariable::print() const
<< "Name: " << name << endl
<< "Use: " << get_use_string() << endl
<< "Type: " << get_type_string() << endl
<< "Scaler: " << get_scaler_string() << endl;
<< "Scaler: " << scaler_to_string(scaler) << endl;

if (categories.size() != 0)
{
Expand Down Expand Up @@ -219,23 +203,12 @@ string DataSet::RawVariable::get_use_string() const
{
switch (use)
{
case VariableUse::Decoder:
return "Decoder";

case VariableUse::Input:
return "Input";

case VariableUse::Target:
return "Target";

case VariableUse::Time:
return "Time";

case VariableUse::None:
return "None";

default:
throw runtime_error("Unknown raw variable use");
case VariableUse::Decoder: return "Decoder";
case VariableUse::Input: return "Input";
case VariableUse::Target: return "Target";
case VariableUse::Time: return "Time";
case VariableUse::None: return "None";
default: throw runtime_error("Unknown raw variable use");
}
}

Expand Down Expand Up @@ -443,10 +416,9 @@ vector<vector<Index>> DataSet::get_batches(const vector<Index>& sample_indices,

const Index batch_size = min(batch_samples_number, samples_number);

if(samples_number < batch_size)
batches_number = 1;
else
batches_number = samples_number / batch_size;
samples_number < batch_size
? batches_number = 1
: batches_number = samples_number / batch_size;

vector<vector<Index>> batches(batches_number);

Expand Down Expand Up @@ -669,30 +641,29 @@ void DataSet::set_default_raw_variables_uses()
if(raw_variables_number == 1)
{
raw_variables[0].set_use(VariableUse::None);
return;
}
else
{
set(VariableUse::Input);

for(Index i = raw_variables.size()-1; i >= 0; i--)
set(VariableUse::Input);

for(Index i = raw_variables.size()-1; i >= 0; i--)
{
if(raw_variables[i].type == RawVariableType::Constant
|| raw_variables[i].type == RawVariableType::DateTime)
{
if(raw_variables[i].type == RawVariableType::Constant
|| raw_variables[i].type == RawVariableType::DateTime)
{
raw_variables[i].set_use(VariableUse::None);
continue;
}
raw_variables[i].set_use(VariableUse::None);
continue;
}

if(!target)
{
raw_variables[i].set_use(VariableUse::Target);
if(!target)
{
raw_variables[i].set_use(VariableUse::Target);

target = true;
target = true;

continue;
}
continue;
}
}
}
}


Expand Down Expand Up @@ -820,10 +791,9 @@ vector<Index> DataSet::get_variable_indices(const VariableUse& variable_use) con
{
if (raw_variables[i].use != variable_use)
{
if (raw_variables[i].type == RawVariableType::Categorical)
variable_index += raw_variables[i].get_categories_number();
else
variable_index++;
raw_variables[i].type == RawVariableType::Categorical
? variable_index += raw_variables[i].get_categories_number()
: variable_index++;

continue;
}
Expand Down Expand Up @@ -2918,10 +2888,9 @@ void DataSet::from_XML(const XMLDocument& data_set_document)

const Index samples_number = read_xml_index(samples_element, "SamplesNumber");

if (raw_variables[(Index)raw_variables.size() - 1].type == RawVariableType::Categorical)
data.resize(samples_number, (Index)raw_variables.size() + raw_variables[(Index)raw_variables.size() - 1].get_categories_number() - 1);
else
data.resize(samples_number, (Index)raw_variables.size());
raw_variables[(Index)raw_variables.size() - 1].type == RawVariableType::Categorical
? data.resize(samples_number, (Index)raw_variables.size() + raw_variables[(Index)raw_variables.size() - 1].get_categories_number() - 1)
: data.resize(samples_number, (Index)raw_variables.size());

sample_uses.resize(samples_number);
set_sample_uses(get_tokens(read_xml_string(samples_element, "SampleUses"), " "));
Expand Down Expand Up @@ -3916,10 +3885,10 @@ void DataSet::read_csv()

const vector<vector<Index>> all_variable_indices = get_variable_indices();

if (raw_variables[columns_number - 1].type == RawVariableType::Categorical)
data.resize(samples_number, variables_number + raw_variables[columns_number - 1].get_categories_number() - 1);
else
data.resize(samples_number, variables_number);
raw_variables[columns_number - 1].type == RawVariableType::Categorical
? data.resize(samples_number, variables_number + raw_variables[columns_number - 1].get_categories_number() - 1)
: data.resize(samples_number, variables_number);

data.setZero();

rows_missing_values_number = 0;
Expand Down Expand Up @@ -4071,28 +4040,6 @@ string DataSet::RawVariable::get_type_string() const
}


string DataSet::RawVariable::get_scaler_string() const
{
switch(scaler)
{
case Scaler::None:
return "None";
case Scaler::MinimumMaximum:
return "MinimumMaximum";
case Scaler::MeanStandardDeviation:
return "MeanStandardDeviation";
case Scaler::StandardDeviation:
return "StandardDeviation";
case Scaler::Logarithm:
return "Logarithm";
case Scaler::ImageMinMax:
return "ImageMinMax";
default:
return "";
}
}


void DataSet::read_data_file_preview(ifstream& file)
{
if(display) cout << "Reading data file preview..." << endl;
Expand Down Expand Up @@ -4294,8 +4241,6 @@ Index DataSet::count_nan() const

void DataSet::fix_repeated_names()
{
// Fix raw_variables names

const Index raw_variables_number = raw_variables.size();

map<string, Index> raw_variables_count_map;
Expand Down Expand Up @@ -4427,8 +4372,6 @@ Tensor<type, 2> DataSet::read_input_csv(const filesystem::path& input_data_file_

ifstream file(input_data_file_name);

// Count samples number

Index input_samples_count = 0;

string line;
Expand Down Expand Up @@ -4549,10 +4492,9 @@ Tensor<type, 2> DataSet::read_input_csv(const filesystem::path& input_data_file_
}
else if(raw_variable.type == RawVariableType::DateTime)
{
if(token == missing_values_label || token.empty())
input_data(line_number, variable_index) = type(NAN);
else
input_data(line_number, variable_index) = type(date_to_timestamp(token, gmt));
token == missing_values_label || token.empty()
? input_data(line_number, variable_index) = type(NAN)
: input_data(line_number, variable_index) = type(date_to_timestamp(token, gmt));

variable_index++;
}
Expand Down
1 change: 0 additions & 1 deletion opennn/data_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class DataSet

string get_use_string() const;
string get_type_string() const;
string get_scaler_string() const;

Index get_categories_number() const;

Expand Down
6 changes: 2 additions & 4 deletions opennn/genetic_algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,8 +817,7 @@ Tensor<bool, 1> GeneticAlgorithm::get_individual_raw_genes(const Tensor<bool,1>&
}
else
{
raw_variables_from_variables(i) = individual(genes_count);
genes_count++;
raw_variables_from_variables(i) = individual(genes_count++);
}
}

Expand Down Expand Up @@ -940,8 +939,7 @@ Tensor<bool, 1> GeneticAlgorithm::get_individual_genes(const Tensor<bool, 1>& in
}
else
{
original_inputs_variables(unused_index) = true;
unused_index++;
original_inputs_variables(unused_index++) = true;
}
}
else
Expand Down
7 changes: 3 additions & 4 deletions opennn/growing_inputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ void GrowingInputs::set_default()
maximum_epochs_number = 1000;
maximum_time = type(3600.0);

if (training_strategy && training_strategy->has_neural_network())
maximum_inputs_number = training_strategy->get_data_set()->get_raw_variables_number(DataSet::VariableUse::Input);
else
maximum_inputs_number = 100;
training_strategy && training_strategy->has_neural_network()
? maximum_inputs_number = training_strategy->get_data_set()->get_raw_variables_number(DataSet::VariableUse::Input)
: maximum_inputs_number = 100;
}


Expand Down
7 changes: 1 addition & 6 deletions opennn/learning_rate_algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ class LearningRateAlgorithm

bool operator == (const Triplet& other_triplet) const
{
if(A == other_triplet.A
&& U == other_triplet.U
&& B == other_triplet.B)
return true;
else
return false;
return (A == other_triplet.A && U == other_triplet.U && B == other_triplet.B);
}

type get_length() const;
Expand Down
Loading

0 comments on commit 6d237e9

Please sign in to comment.