Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
RoberLopez committed Dec 27, 2024
1 parent 231bade commit 5c57fd2
Show file tree
Hide file tree
Showing 30 changed files with 311 additions and 408 deletions.
3 changes: 2 additions & 1 deletion opennn/convolutional_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ void ConvolutionalLayer::forward_propagate(const vector<pair<type*, dimensions>>
else
calculate_activations(outputs, empty);

/*auto end = chrono::high_resolution_clock::now();
/*
auto end = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(end - start);
cout << "Tiempo convolution forward propagate: "
<< duration.count() / 1000 << "::"
Expand Down
16 changes: 8 additions & 8 deletions opennn/correlations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ Correlation correlation(const ThreadPoolDevice* thread_pool_device,
const Tensor<type, 2>& x,
const Tensor<type, 2>& y)
{
if(is_constant_matrix(x) || is_constant_matrix(y))
if(is_constant(x) || is_constant(y))
return Correlation();

const Index x_rows = x.dimension(0);
const Index x_columns = x.dimension(1);
const Index y_columns = y.dimension(1);

const bool x_binary = is_binary_matrix(x);
const bool y_binary = is_binary_matrix(y);
const bool x_binary = is_binary(x);
const bool y_binary = is_binary(y);

const Eigen::array<Index, 1> vector{{x_rows}};

Expand Down Expand Up @@ -117,8 +117,8 @@ Correlation correlation_spearman(const ThreadPoolDevice* thread_pool_device,
const Index x_columns = x.dimension(1);
const Index y_columns = y.dimension(1);

const bool x_binary = is_binary_matrix(x);
const bool y_binary = is_binary_matrix(y);
const bool x_binary = is_binary(x);
const bool y_binary = is_binary(y);

const Eigen::array<Index, 1> vector{{x_rows}};

Expand Down Expand Up @@ -361,7 +361,7 @@ Correlation linear_correlation(const ThreadPoolDevice* thread_pool_device,
if(x.size() != y.size())
throw runtime_error("Y size must be equal to X size.\n");

if(is_constant_vector(x) || is_constant_vector(y))
if(is_constant(x) || is_constant(y))
return Correlation();

const pair<Tensor<type, 1>, Tensor<type, 1>> filter_vectors = filter_missing_values_vector_vector(x,y);
Expand Down Expand Up @@ -524,8 +524,8 @@ Correlation logistic_correlation_vector_vector(const ThreadPoolDevice* thread_po
const Tensor<type, 1> y_filtered = filtered_elements.second;

if (x_filtered.size() == 0
|| is_constant_vector(x_filtered)
|| is_constant_vector(y_filtered))
|| is_constant(x_filtered)
|| is_constant(y_filtered))
{
correlation.r = type(NAN);
correlation.form = Correlation::Form::Logistic;
Expand Down
4 changes: 2 additions & 2 deletions opennn/cross_entropy_error_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ void CrossEntropyError3D::calculate_error(const Batch& batch,
accuracy.device(*thread_pool_device) = matches.cast<type>().sum() / mask_sum(0);

if(isnan(error())) throw runtime_error("Error is NAN");

}


void CrossEntropyError3D::calculate_output_delta(const Batch&,
ForwardPropagation&,
BackPropagation&) const
{
// ProbabilisticLayer3D does not have deltas. Error combinations derivatives are calculated directly.
// ProbabilisticLayer3D does not have deltas.
// Error combinations derivatives are calculated directly.
}


Expand Down
29 changes: 16 additions & 13 deletions opennn/data_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,11 +1258,12 @@ void DataSet::set_binary_raw_variables()

if(raw_variable.type == RawVariableType::Numeric)
{
/* // @todo tensor map does not work
const TensorMap<Tensor<type, 1>> data_column = tensor_map(data, variable_index);
if(is_binary_vector(data_column))
if(is_binary(data_column))
raw_variable.type = RawVariableType::Binary;

*/
variable_index++;
}
else if(raw_variable.type == RawVariableType::Categorical)
Expand Down Expand Up @@ -1291,11 +1292,12 @@ void DataSet::unuse_constant_raw_variables()

if(raw_variable.type == RawVariableType::Numeric)
{
/* // @todo
const TensorMap<Tensor<type, 1>> data_column = tensor_map(data, variable_index);
if(is_constant_vector(data_column))
if(is_constant(data_column))
raw_variable.set(raw_variable.name, VariableUse::None, RawVariableType::Constant);

*/
variable_index++;
}
else if(raw_variable.type == RawVariableType::DateTime || raw_variable.type == RawVariableType::Constant)
Expand Down Expand Up @@ -2553,7 +2555,7 @@ Tensor<Correlation, 2> DataSet::calculate_input_raw_variable_pearson_correlation

//if(display) cout << "Calculating " << raw_variables(current_input_index_i).name << " correlations. " << endl;

if (is_constant_matrix(input_i)) continue;
if (is_constant(input_i)) continue;

correlations_pearson(i, i).set_perfect();
correlations_pearson(i, i).method = Correlation::Method::Pearson;
Expand Down Expand Up @@ -2592,7 +2594,7 @@ Tensor<Correlation, 2> DataSet::calculate_input_raw_variable_spearman_correlatio

//if(display) cout << "Calculating " << raw_variables(current_input_index_i).name << " correlations. " << endl;

if (is_constant_matrix(input_i)) continue;
if (is_constant(input_i)) continue;

correlations_spearman(i, i).set_perfect();
correlations_spearman(i, i).method = Correlation::Method::Spearman;
Expand Down Expand Up @@ -3454,19 +3456,20 @@ void DataSet::set_data_rosenbrock()
}


/*

void DataSet::set_data_classification()
{

const Index samples_number = get_samples_number();
const Index input_variables_number = get_variables_number(VariableUse::Input);
const Index target_variables_number = get_variables_number(VariableUse::Target);

data.setConstant(0.0);

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dist(0, 1);
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<int> dist(0, 1);
/*
#pragma omp parallel for
for(Index i = 0; i < samples_number; i++)
{
Expand All @@ -3477,9 +3480,9 @@ void DataSet::set_data_classification()
? data(i, input_variables_number) = dist(gen)
: data(i, input_variables_number + get_random_index(0, target_variables_number-1)) = 1;
}
}
*/
}



void DataSet::set_data_sum()
Expand Down
3 changes: 2 additions & 1 deletion opennn/response_optimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ Tensor<ResponseOptimization::Condition, 1> ResponseOptimization::get_conditions(
}


Tensor<Tensor<type, 1>, 1> ResponseOptimization::get_values_conditions(const Tensor<ResponseOptimization::Condition, 1>& conditions, const Tensor<type, 1>& values) const
Tensor<Tensor<type, 1>, 1> ResponseOptimization::get_values_conditions(const Tensor<ResponseOptimization::Condition, 1>& conditions,
const Tensor<type, 1>& values) const
{
const Index conditions_size = conditions.size();

Expand Down
2 changes: 1 addition & 1 deletion opennn/response_optimization.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ResponseOptimization

Tensor<Condition, 1> input_conditions;
Tensor<Condition, 1> output_conditions;
Tensor<Condition, 1> conditions;
//Tensor<Condition, 1> conditions;

Tensor<type, 1> input_minimums;
Tensor<type, 1> input_maximums;
Expand Down
62 changes: 0 additions & 62 deletions opennn/tensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,68 +516,6 @@ void substract_matrices(const ThreadPoolDevice* thread_pool_device, const Tensor
}


bool is_binary_vector(const Tensor<type, 1>& vector)
{
const Index size = vector.size();

for(Index i = 0; i < size; i++)
if(vector(i) != type(0) && vector(i) != type(1) && !isnan(vector(i)))
return false;

return true;
}


bool is_binary_matrix(const Tensor<type, 2>& matrix)
{
const Index size = matrix.size();

for(Index i = 0; i < size; i++)
if(matrix(i) != type(0) && matrix(i) != type(1) && !isnan(matrix(i)))
return false;

return true;
}


bool is_constant_vector(const Tensor<type, 1>& vector)
{
const Index size = vector.size();

Index first_non_nan_index = 0;

while (first_non_nan_index < size && isnan(vector[first_non_nan_index]))
first_non_nan_index++;

if (first_non_nan_index == size)
return true;

type first_not_nan_element = vector[first_non_nan_index];

for (Index i = first_non_nan_index + 1; i < size; ++i)
if (!isnan(vector[i]) && abs(first_not_nan_element - vector[i]) > std::numeric_limits<float>::min())
return false;

return true;
}


bool is_constant_matrix(const Tensor<type, 2>& matrix)
{
if(matrix.size() == 0)
return true;

type first_value = matrix(0,0);

for(Index i = 0; i < matrix.dimension(0); i++)
for(Index j = 0; j < matrix.dimension(1); j++)
if(matrix(i, j) != first_value)
return false;

return true;
}


Tensor<bool, 2> elements_are_equal(const Tensor<type, 2>& x, const Tensor<type, 2>& y)
{
Tensor<bool, 2> result(x.dimension(0), x.dimension(1));
Expand Down
41 changes: 37 additions & 4 deletions opennn/tensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,46 @@ Tensor<type, 2> self_kronecker_product(const ThreadPoolDevice*, const Tensor<typ
//void divide_columns(const ThreadPoolDevice*, Tensor<type, 2>&, const Tensor<type, 1>&);
void divide_columns(const ThreadPoolDevice*, TensorMap<Tensor<type, 2>>&, const Tensor<type, 1>&);

bool is_binary_vector(const Tensor<type, 1>&);
bool is_binary_matrix(const Tensor<type, 2>&);

bool is_constant_vector(const Tensor<type, 1>&);
bool is_constant_matrix(const Tensor<type, 2>&);
template <Index Rank>
bool is_binary(const Tensor<type, Rank>& tensor)
{
const Index size = tensor.size();

for (Index i = 0; i < size; i++)
if (tensor(i) != type(0) && tensor(i) != type(1) && !isnan(tensor(i)))
return false;

return true;
}


template <Index Rank>
bool is_constant(const Tensor<type, Rank>& tensor)
{

const Index size = tensor.size();

Index first_non_nan_index = 0;

while (first_non_nan_index < size && isnan(tensor(first_non_nan_index)))
first_non_nan_index++;

if (first_non_nan_index == size)
return true;

const type first_not_nan_element = tensor(first_non_nan_index);

for (Index i = first_non_nan_index + 1; i < size; ++i)
if (!isnan(tensor(i)) && abs(first_not_nan_element - tensor(i)) > numeric_limits<float>::min())
return false;

return true;
}


Tensor<bool, 2> elements_are_equal(const Tensor<type, 2>&, const Tensor<type, 2>&);

void save_csv(const Tensor<type,2>&, const filesystem::path&);

template<int rank>
Expand Down
12 changes: 6 additions & 6 deletions opennn/time_series_data_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ class TimeSeriesDataSet : public DataSet
public:

TimeSeriesDataSet(const Index& = 0,
const dimensions& = {},
const dimensions& = {});
const dimensions& = {},
const dimensions& = {});

TimeSeriesDataSet(const filesystem::path&,
const string&,
const bool& = true,
const bool& = false,
const Codification& = Codification::UTF8);
const string&,
const bool& = true,
const bool& = false,
const Codification& = Codification::UTF8);

void fill_gaps();

Expand Down
4 changes: 3 additions & 1 deletion tests/correlations_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

using namespace opennn;

class CorrelationsTest : public ::testing::Test {
class CorrelationsTest : public ::testing::Test
{
protected:

unique_ptr<ThreadPool> thread_pool;
Expand All @@ -23,6 +24,7 @@ class CorrelationsTest : public ::testing::Test {
}
};


TEST_F(CorrelationsTest, SpearmanCorrelation)
{
Tensor<type, 1> x(10);
Expand Down
4 changes: 2 additions & 2 deletions tests/cross_entropy_error_3d_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ TEST(CrossEntropyError3DTest, DefaultConstructor)

TEST(CrossEntropyError3DTest, BackPropagateZero)
{
/*

const Index samples_number = get_random_index(1, 10);
const Index inputs_number = get_random_index(1, 10);
const Index targets_number = get_random_index(1, 10);
Expand All @@ -46,7 +46,7 @@ TEST(CrossEntropyError3DTest, BackPropagateZero)
// neural_network.add_layer(probabilistic_layer_3d);

neural_network.set_parameters_constant(type(0));
/*
ForwardPropagation forward_propagation(samples_number, &neural_network);
neural_network.forward_propagate(batch.get_input_pairs(), forward_propagation, true);
Expand Down
9 changes: 5 additions & 4 deletions tests/cross_entropy_error_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ TEST(CrossEntropyErrorTest, BackPropagate)
const Index neurons_number = get_random_index(1, 10);

DataSet data_set(samples_number, { inputs_number }, { targets_number });
/*
data_set.set_data_classification();
//data_set.set_data_classification();

data_set.set(DataSet::SampleUse::Training);

Expand All @@ -61,14 +61,15 @@ TEST(CrossEntropyErrorTest, BackPropagate)
neural_network.set_parameters_random();

ForwardPropagation forward_propagation(samples_number, &neural_network);
/*
neural_network.forward_propagate(batch.get_input_pairs(), forward_propagation, true);
// Loss index
CrossEntropyError cross_entropy_error(&neural_network, &data_set);
BackPropagation back_propagation(samples_number, &cross_entropy_error);
cross_entropy_error.back_propagate(batch, forward_propagation, back_propagation);
const Tensor<type, 1> numerical_gradient = cross_entropy_error.calculate_numerical_gradient();
Expand Down
Loading

0 comments on commit 5c57fd2

Please sign in to comment.