Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
Version 6.0.3 ND
  • Loading branch information
SantiagoArtelnics committed Jan 4, 2023
2 parents 47fcd72 + d88bb66 commit 5b596cb
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 54 deletions.
10 changes: 2 additions & 8 deletions opennn/adaptive_moment_estimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,6 @@ TrainingResults AdaptiveMomentEstimation::perform_training()
LossIndexBackPropagation training_back_propagation(batch_size_training, loss_index_pointer);
LossIndexBackPropagation selection_back_propagation(batch_size_selection, loss_index_pointer);

Index parameters_size = training_back_propagation.parameters.size();

type training_error = type(0);
type training_loss = type(0);

Expand Down Expand Up @@ -377,7 +375,6 @@ TrainingResults AdaptiveMomentEstimation::perform_training()
training_loss += training_back_propagation.loss;

update_parameters(training_back_propagation, optimization_data);

}

// Loss
Expand Down Expand Up @@ -580,6 +577,7 @@ void AdaptiveMomentEstimation::update_parameters(LossIndexBackPropagation& back_
saxpby(&parameters_number, &a, back_propagation.gradient.data(), &incx, &b, optimization_data.gradient_exponential_decay.data(), &incy);

#else

optimization_data.gradient_exponential_decay.device(*thread_pool_device)
= back_propagation.gradient * (type(1) - beta_1)
+ optimization_data.gradient_exponential_decay * beta_1;
Expand All @@ -590,11 +588,8 @@ void AdaptiveMomentEstimation::update_parameters(LossIndexBackPropagation& back_
= back_propagation.gradient * back_propagation.gradient * (type(1) - beta_2)
+ optimization_data.square_gradient_exponential_decay * beta_2;

optimization_data.square_gradient_exponential_decay_square_root.device(*thread_pool_device)
= optimization_data.square_gradient_exponential_decay.sqrt() + epsilon;

back_propagation.parameters.device(*thread_pool_device)
-= learning_rate * optimization_data.gradient_exponential_decay / optimization_data.square_gradient_exponential_decay_square_root;
-= learning_rate * optimization_data.gradient_exponential_decay / (optimization_data.square_gradient_exponential_decay.sqrt() + epsilon);

optimization_data.iteration++;

Expand Down Expand Up @@ -834,7 +829,6 @@ void AdaptiveMomentEstimationData::set(AdaptiveMomentEstimation* new_adaptive_mo
square_gradient_exponential_decay.resize(parameters_number);
square_gradient_exponential_decay.setZero();

square_gradient_exponential_decay_square_root.resize(parameters_number);
square_gradient_exponential_decay.setZero();
}

Expand Down
4 changes: 0 additions & 4 deletions opennn/adaptive_moment_estimation.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,6 @@ struct AdaptiveMomentEstimationData : public OptimizationAlgorithmData
Tensor<type, 1> gradient_exponential_decay;
Tensor<type, 1> square_gradient_exponential_decay;


Tensor<type, 1> square_gradient_exponential_decay_square_root;


Index iteration = 0;

Index learning_rate_iteration = 0;
Expand Down
3 changes: 1 addition & 2 deletions opennn/conjugate_gradient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ type ConjugateGradient::calculate_PR_parameter(const Tensor<type, 1>& old_gradie
}

return PR_parameter;

}


Expand Down Expand Up @@ -987,7 +986,7 @@ void ConjugateGradient::update_parameters(
}

optimization_data.training_slope.device(*thread_pool_device)
= (back_propagation.gradient).contract(optimization_data.training_direction, AT_B);
= back_propagation.gradient.contract(optimization_data.training_direction, AT_B);

if(optimization_data.training_slope(0) >= type(0))
{
Expand Down
6 changes: 5 additions & 1 deletion opennn/cross_entropy_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ void CrossEntropyError::calculate_binary_error(const DataSetBatch& batch,

const TensorMap<Tensor<type, 2>> targets(batch.targets_data, batch.targets_dimensions(0), batch.targets_dimensions(1));

Tensor<type, 2> binary_cross_entropy = - ((type(1)-targets)*((type(1)-outputs).log()));

std::replace_if(binary_cross_entropy.data(), binary_cross_entropy.data()+binary_cross_entropy.size(), [](type x){return isnan(x);}, 0);

Tensor<type, 0> cross_entropy_error;

cross_entropy_error.device(*thread_pool_device) = -(targets*(outputs.log())).sum() - ((type(1)-targets)*((type(1)-outputs).log())).sum();
cross_entropy_error.device(*thread_pool_device) = -(targets*(outputs.log())).sum() + binary_cross_entropy.sum();

back_propagation.error = cross_entropy_error()/static_cast<type>(batch_samples_number);
}
Expand Down
31 changes: 12 additions & 19 deletions opennn/gradient_descent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,44 +284,37 @@ void GradientDescent::update_parameters(

if(abs(optimization_data.learning_rate) > type(0))
{
optimization_data.parameters_increment.device(*thread_pool_device)
= optimization_data.training_direction*optimization_data.learning_rate;

back_propagation.parameters.device(*thread_pool_device) += optimization_data.parameters_increment;
back_propagation.parameters.device(*thread_pool_device)
-= back_propagation.gradient*optimization_data.learning_rate;
}
else
{
const Index parameters_number = neural_network_pointer->get_parameters_number();

for(Index i = 0; i < parameters_number; i++)
{
if(abs(back_propagation.gradient(i)) < type(NUMERIC_LIMITS_MIN))
if(abs(back_propagation.gradient(i)) >= type(NUMERIC_LIMITS_MIN))
{
optimization_data.parameters_increment(i) = type(0);
}
else if(back_propagation.gradient(i) > type(0))
{
back_propagation.parameters(i) -= numeric_limits<type>::epsilon();

optimization_data.parameters_increment(i) = -numeric_limits<type>::epsilon();
}
else if(back_propagation.gradient(i) < type(0))
{
back_propagation.parameters(i) += numeric_limits<type>::epsilon();

optimization_data.parameters_increment(i) = numeric_limits<type>::epsilon();
if(back_propagation.gradient(i) > type(0))
{
back_propagation.parameters(i) -= numeric_limits<type>::epsilon();
}
else if(back_propagation.gradient(i) < type(0))
{
back_propagation.parameters(i) += numeric_limits<type>::epsilon();
}
}
}

optimization_data.learning_rate = optimization_data.old_learning_rate;

}

// Update parameters

optimization_data.old_learning_rate = optimization_data.learning_rate;

forward_propagation.neural_network_pointer->set_parameters(back_propagation.parameters);

}


Expand Down
10 changes: 0 additions & 10 deletions opennn/gradient_descent.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ struct GradientDescentData : public OptimizationAlgorithmData

potential_parameters.resize(parameters_number);

parameters_increment.resize(parameters_number);

// Optimization algorithm data

training_direction.resize(parameters_number);
Expand All @@ -198,14 +196,6 @@ struct GradientDescentData : public OptimizationAlgorithmData

GradientDescent* gradient_descent_pointer = nullptr;

// Neural network data

// Tensor<type, 1> potential_parameters;
// Tensor<type, 1> training_direction;
// type initial_learning_rate = type(0);

Tensor<type, 1> parameters_increment;

// Optimization algorithm data

Index epoch = 0;
Expand Down
8 changes: 7 additions & 1 deletion opennn/statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ Histogram::Histogram(const Tensor<type, 1>& data,

corresponding_bin = int((value - data_minimum) / step);


if(corresponding_bin >= number_of_bins)
corresponding_bin = number_of_bins - 1;

Expand Down Expand Up @@ -1932,6 +1931,13 @@ Tensor<Descriptives, 1> descriptives(const Tensor<type, 2>& matrix,
standard_deviation(i) = sqrt(variance);
}
}
else
{
for(Index i = 0; i < columns_indices_size; i++)
{
standard_deviation(i) = 0;
}
}

for(Index i = 0; i < columns_indices_size; i++)
{
Expand Down
7 changes: 1 addition & 6 deletions opennn/stochastic_gradient_descent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,6 @@ void StochasticGradientDescent::set_maximum_time(const type& new_maximum_time)
}


/// Set hardware to use. Default: Multi-core.

void StochasticGradientDescent::update_parameters(LossIndexBackPropagation& back_propagation,
StochasticGradientDescentData& optimization_data) const
{
Expand All @@ -290,10 +288,7 @@ void StochasticGradientDescent::update_parameters(LossIndexBackPropagation& back
}
else
{
optimization_data.nesterov_increment.device(*thread_pool_device)
= optimization_data.parameters_increment*momentum - back_propagation.gradient*learning_rate;

back_propagation.parameters.device(*thread_pool_device) += optimization_data.nesterov_increment;
back_propagation.parameters.device(*thread_pool_device) += optimization_data.parameters_increment*momentum - back_propagation.gradient*learning_rate;;
}
}
else
Expand Down
3 changes: 0 additions & 3 deletions opennn/stochastic_gradient_descent.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,9 @@ struct StochasticGradientDescentData : public OptimizationAlgorithmData
const Index parameters_number = neural_network_pointer->get_parameters_number();

parameters_increment.resize(parameters_number);
nesterov_increment.resize(parameters_number);
last_parameters_increment.resize(parameters_number);

parameters_increment.setZero();
nesterov_increment.setZero();
last_parameters_increment.setZero();
}

Expand All @@ -196,7 +194,6 @@ struct StochasticGradientDescentData : public OptimizationAlgorithmData
Index iteration = 0;

Tensor<type, 1> parameters_increment;
Tensor<type, 1> nesterov_increment;
Tensor<type, 1> last_parameters_increment;
};

Expand Down

0 comments on commit 5b596cb

Please sign in to comment.