16 #include "performance_term.h"
28 : neural_network_pointer(NULL),
29 data_set_pointer(NULL),
30 mathematical_model_pointer(NULL),
31 numerical_differentiation_pointer(NULL)
46 : neural_network_pointer(new_neural_network_pointer),
47 data_set_pointer(NULL),
48 mathematical_model_pointer(NULL),
49 numerical_differentiation_pointer(NULL)
64 : neural_network_pointer(NULL),
65 data_set_pointer(new_data_set_pointer),
66 mathematical_model_pointer(NULL),
67 numerical_differentiation_pointer(NULL)
82 : neural_network_pointer(NULL),
83 data_set_pointer(NULL),
84 mathematical_model_pointer(new_mathematical_model_pointer),
85 numerical_differentiation_pointer(NULL)
101 : neural_network_pointer(new_neural_network_pointer),
102 data_set_pointer(new_data_set_pointer),
103 mathematical_model_pointer(NULL),
104 numerical_differentiation_pointer(NULL)
120 : neural_network_pointer(new_neural_network_pointer),
121 data_set_pointer(NULL),
122 mathematical_model_pointer(new_mathematical_model_pointer),
123 numerical_differentiation_pointer(NULL)
140 : neural_network_pointer(new_neural_network_pointer),
141 data_set_pointer(new_data_set_pointer),
142 mathematical_model_pointer(new_mathematical_model_pointer),
143 numerical_differentiation_pointer(NULL)
157 : neural_network_pointer(NULL),
158 data_set_pointer(NULL),
159 mathematical_model_pointer(NULL),
160 numerical_differentiation_pointer(NULL)
164 from_XML(performance_term_document);
175 : neural_network_pointer(NULL),
176 data_set_pointer(NULL),
177 mathematical_model_pointer(NULL),
178 numerical_differentiation_pointer(NULL)
215 if(
this != &other_performance_term)
613 std::ostringstream buffer;
617 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
618 <<
"void check(void) const.\n"
619 <<
"Pointer to neural network is NULL.\n";
621 throw std::logic_error(buffer.str());
645 const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
649 std::ostringstream buffer;
651 if(!multilayer_perceptron_pointer)
653 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
654 <<
"Vector< Vector<double> > calculate_layers_delta(const Vector< Vector<double> >&, const Vector<double>&) const method.\n"
655 <<
"Pointer to multilayer perceptron in neural network is NULL.\n";
657 throw std::logic_error(buffer.str());
671 const size_t layers_activation_derivative_size = layers_activation_derivative.size();
673 if(layers_activation_derivative_size != layers_number)
675 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
676 <<
"Vector< Vector<double> > calculate_layers_delta(const Vector< Vector<double> >&, const Vector<double>&) method.\n"
677 <<
"Size of forward propagation activation derivative vector must be equal to number of layers.\n";
679 throw std::logic_error(buffer.str());
682 if(layers_number > 0)
684 const size_t output_objective_gradient_size = output_objective_gradient.size();
686 if(output_objective_gradient_size != layers_perceptrons_number[layers_number-1])
688 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
689 <<
"Vector<double> calculate_layers_delta(const Vector< Vector<double> >&, const Vector<double>&) method.\n"
690 <<
"Size of outputs objective gradient (" << output_objective_gradient_size <<
") must be equal to "
691 <<
"number of outputs (" << layers_perceptrons_number[layers_number-1] <<
").\n";
693 throw std::logic_error(buffer.str());
709 if(layers_number > 0)
711 layers_delta[layers_number-1] = layers_activation_derivative[layers_number-1]*output_objective_gradient;
715 for(
int i = (
int)layers_number-2; i >= 0; i--)
717 layer_synaptic_weights = neural_network_pointer->get_multilayer_perceptron_pointer()->get_layer(i+1).arrange_synaptic_weights();
719 layers_delta[i] = layers_activation_derivative[i]*(layers_delta[i+1].
dot(layer_synaptic_weights));
723 return(layers_delta);
740 const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
751 const size_t layers_activation_derivative_size = layers_activation_derivative.size();
753 if(layers_activation_derivative_size != layers_number)
755 std::ostringstream buffer;
757 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
758 <<
"Vector< Vector<double> > calculate_layers_delta(const Vector< Vector<double> >&, const Vector<double>&) const method.\n"
759 <<
"Size of forward propagation activation derivative vector must be equal to number of layers.\n";
761 throw std::logic_error(buffer.str());
764 const size_t objective_function_output_gradient_size = output_objective_gradient.size();
766 if(objective_function_output_gradient_size != layers_perceptrons_number[layers_number-1])
768 std::ostringstream buffer;
770 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
771 <<
"Vector<double> calculate_layers_delta(const Vector< Vector<double> >&, const Vector<double>&) const method.\n"
772 <<
"Size of objective function outputs derivative (" << objective_function_output_gradient_size <<
")must be equal to "
773 <<
"number of outputs (" << layers_perceptrons_number[layers_number-1] <<
").\n";
775 throw std::logic_error(buffer.str());
790 for(
size_t i = 0; i < layers_number; i++)
792 layers_delta[i].
set(layers_perceptrons_number[i]);
797 layers_delta[layers_number-1] = layers_activation_derivative[layers_number-1]*homogeneous_solution*output_objective_gradient;
801 for(
int h = (
int)layers_number-2; h >= 0; h--)
803 for(
size_t i = 0; i < layers_perceptrons_number[h]; i++)
807 for(
size_t j = 0; j < layers_perceptrons_number[h+1]; j++)
809 synaptic_weights = layers[h+1].arrange_synaptic_weights();
811 sum += (synaptic_weights(i,j))*layers_delta[h+1][j];
814 layers_delta[h][i] = layers_activation_derivative[h][i]*sum;
818 return(layers_delta);
842 const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
846 std::ostringstream buffer;
848 if(!multilayer_perceptron_pointer)
850 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
851 <<
"Vector<double> calculate_point_gradient(const Vector<double>&, const Vector< Vector<double> >&, const Vector<double>&) const method.\n"
852 <<
"Pointer to multilayer perceptron is NULL.\n";
854 throw std::logic_error(buffer.str());
869 const size_t inputs_size = inputs.size();
871 if(inputs_size != inputs_number)
873 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
874 <<
"Vector< Vector<double> > calculate_layers_objective_gradient(const Vector< Vector<double> >&, const Vector<double>&, const Vector<double>&) method.\n"
875 <<
"Size of inputs (" << inputs_size <<
") must be equal to inputs number (" << inputs_number <<
").\n";
877 throw std::logic_error(buffer.str());
882 const size_t layers_activation_size = layers_activation.size();
884 if(layers_activation_size != layers_number)
886 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
887 <<
"Vector< Vector<double> > calculate_layers_objective_gradient(const Vector< Vector<double> >&, const Vector<double>&, const Vector<double>&) method.\n"
888 <<
"Size of forward propagation activation (" << layers_activation_size <<
") must be equal to number of layers (" << layers_number <<
").\n";
890 throw std::logic_error(buffer.str());
895 const size_t layers_delta_size = layers_delta.size();
897 if(layers_delta_size != layers_number)
899 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
900 <<
"Vector< Vector<double> > calculate_layers_objective_gradient(const Vector< Vector<double> >&, const Vector<double>&) method.\n"
901 <<
"Size of layers delta ("<< layers_delta_size <<
") must be equal to number of layers (" << layers_number <<
").\n";
903 throw std::logic_error(buffer.str());
908 const size_t parameters_number = neural_network_pointer->count_parameters_number();
918 for(
size_t i = 0; i < layers_number; i++)
920 point_gradient.
tuck_in(index, layers_delta[i].dot(layers_combination_parameters_Jacobian[i]));
925 if(layers_number != 0)
933 for(
size_t i = 0; i < layers_perceptrons_number[0]; i++)
937 point_gradient[index] = layers_delta[0][i];
944 for(
size_t j = 0; j < inputs_number; j++)
946 point_gradient[index] = layers_delta[0][i]*inputs[j];
953 for(
size_t h = 1; h < layers_number; h++)
955 for(
size_t i = 0; i < layers_perceptrons_number[h]; i++)
959 point_gradient[index] = layers_delta[h][i];
966 for(
size_t j = 0; j < layers_perceptrons_number[h-1]; j++)
968 point_gradient[index] = layers_delta[h][i]*layers_activation[h-1][j];
976 return(point_gradient);
998 const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
1004 std::ostringstream buffer;
1006 if(!multilayer_perceptron_pointer)
1008 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
1009 <<
"Vector<double> calculate_point_gradient(const Vector<double>&, const Vector< Vector<double> >&, const Vector<double>&) const method.\n"
1010 <<
"Pointer to network is NULL.\n";
1012 throw std::logic_error(buffer.str());
1017 const size_t layers_number = multilayer_perceptron_pointer->
get_layers_number();
1026 const size_t layers_combination_parameters_Jacobian_size = layers_combination_parameters_Jacobian.size();
1028 if(layers_combination_parameters_Jacobian_size != layers_number)
1030 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
1031 <<
"Vector< Vector<double> > calculate_layers_objective_gradient(const Vector< Vector<double> >&, const Vector<double>&, const Vector<double>&) method.\n"
1032 <<
"Size of forward propagation activation (" << layers_combination_parameters_Jacobian_size <<
") must be equal to number of layers (" << layers_number <<
").\n";
1034 throw std::logic_error(buffer.str());
1039 const size_t layers_delta_size = layers_delta.size();
1041 if(layers_delta_size != layers_number)
1043 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
1044 <<
"Vector< Vector<double> > calculate_layers_objective_gradient(const Vector< Vector<double> >&, const Vector<double>&) method.\n"
1045 <<
"Size of layers delta ("<< layers_delta_size <<
") must be equal to number of layers (" << layers_number <<
").\n";
1047 throw std::logic_error(buffer.str());
1058 for(
size_t i = 0; i < layers_number; i++)
1060 point_gradient.
tuck_in(index, layers_delta[i].dot(layers_combination_parameters_Jacobian[i]));
1062 index += neural_network_pointer->get_multilayer_perceptron_pointer()->get_layer(i).count_parameters_number();
1065 return(point_gradient);
1097 const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
1101 std::ostringstream buffer;
1103 if(!multilayer_perceptron_pointer)
1105 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
1106 <<
"Matrix< Matrix<double> > calculate_interlayers_Delta() method.\n"
1107 <<
"Multilayer perceptron pointer is NULL.\n";
1109 throw std::logic_error(buffer.str());
1114 const size_t layers_number = multilayer_perceptron_pointer->
get_layers_number();
1121 if(layers_number != 0)
1123 const size_t output_objective_gradient_size = output_objective_gradient.size();
1125 if(output_objective_gradient_size != layers_size[layers_number-1])
1127 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
1128 <<
"Vector<double> calculate_interlayers_Delta() method.\n"
1129 <<
"Size of layer " << layers_number-1 <<
" must be equal to size of output objective gradient (" << output_objective_gradient_size <<
")."
1132 throw std::logic_error(buffer.str());
1135 const size_t output_objective_Hessian_rows_number = output_objective_Hessian.
get_rows_number();
1136 const size_t output_objective_Hessian_columns_number = output_objective_Hessian.
get_columns_number();
1138 if(output_objective_Hessian_rows_number != layers_size[layers_number-1])
1140 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
1141 <<
"Vector<double> calculate_interlayers_Delta() method.\n"
1142 <<
"Size of layer " << layers_number-1 <<
" must be equal to number of rows in output objective Hessian (" << output_objective_Hessian_rows_number <<
")."
1145 throw std::logic_error(buffer.str());
1148 if(output_objective_Hessian_columns_number != layers_size[layers_number-1])
1150 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
1151 <<
"Vector<double> calculate_interlayers_Delta() method.\n"
1152 <<
"Size of layer " << layers_number-1 <<
") must be equal to number of columns in output objective Hessian (" << output_objective_Hessian_columns_number <<
")."
1155 throw std::logic_error(buffer.str());
1167 for(
size_t i = 0; i < layers_number; i++)
1169 for(
size_t j = 0; j < layers_number; j++)
1171 interlayers_Delta(i,j).
set(layers_size[i], layers_size[j]);
1177 if(layers_number > 0)
1181 interlayers_Delta(layers_number-1,layers_number-1) = (output_objective_Hessian*(layers_activation_derivative[layers_number-1]*layers_activation_derivative[layers_number-1])).
sum_diagonal(output_objective_gradient*layers_activation_second_derivative[layers_number-1]);
1185 for(
int i = (
int)layers_number-1; i >= 0; i--)
1187 for(
int j = (
int)layers_number-1; j >= 0; j--)
1189 if((
int)i != (
int)layers_number-1 && (
int)j != (
int)layers_number-1)
1191 interlayers_Delta(i,j)
1192 = layers_activation_second_derivative[j]
1193 *interlayers_combination_combination_Jacobian_form(i,j)
1194 *(layers_delta[j+1].dot(layers_synaptic_weights[j+1]))
1195 + layers_activation_second_derivative[j]
1196 *(interlayers_Delta(i,j+1).
dot(layers_synaptic_weights[j+1]));
1202 return(interlayers_Delta);
1231 const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
1235 std::ostringstream buffer;
1237 if(!multilayer_perceptron_pointer)
1239 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
1240 <<
"Matrix<double> calculate_point_Hessian(const Vector<double>&, const Matrix< Matrix<double> >&, const Vector< Vector<double> >&, const Matrix< Matrix<double> >&) const method.\n"
1241 <<
"Multilayer perceptron pointer is NULL.\n";
1243 throw std::logic_error(buffer.str());
1248 const size_t layers_number = multilayer_perceptron_pointer->
get_layers_number();
1254 const size_t layers_activation_derivative_size = layers_activation_derivative.size();
1256 if(layers_activation_derivative_size != layers_number)
1258 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
1259 <<
"Matrix<double> calculate_point_Hessian(const Vector<double>&, const Matrix< Matrix<double> >&, const Vector< Vector<double> >&, const Matrix< Matrix<double> >&) const method.\n"
1260 <<
"Size of layers activation derivative must be equal to number of layers in multilayer perceptron.\n";
1262 throw std::logic_error(buffer.str());
1279 Matrix<double> point_Hessian(parameters_number, parameters_number, 0.0);
1283 size_t layer_index_i;
1284 size_t neuron_index_i;
1285 size_t parameter_index_i;
1287 size_t layer_index_j;
1288 size_t neuron_index_j;
1289 size_t parameter_index_j;
1294 if(layers_number > 0)
1296 for(
size_t i = 0; i < parameters_number; i++)
1299 layer_index_i = parameter_indices[0];
1300 neuron_index_i = parameter_indices[1];
1301 parameter_index_i = parameter_indices[2];
1303 for(
size_t j = 0; j < parameters_number; j++)
1306 layer_index_j = parameter_indices[0];
1307 neuron_index_j = parameter_indices[1];
1308 parameter_index_j = parameter_indices[2];
1311 = perceptrons_combination_parameters_gradient[layer_index_i][neuron_index_i][parameter_index_i]
1312 *perceptrons_combination_parameters_gradient[layer_index_j][neuron_index_j][parameter_index_j]
1313 *interlayers_Delta(layer_index_j,layer_index_i)(neuron_index_j,neuron_index_i)
1314 + perceptrons_combination_parameters_gradient[layer_index_i][neuron_index_i][parameter_index_i]
1315 *layers_delta[layer_index_j][neuron_index_j]
1316 *layers_activation_derivative[layer_index_j][neuron_index_j]
1317 *interlayers_combination_combination_Jacobian(layer_index_j,layer_index_i)(neuron_index_j,neuron_index_i);
1321 for(
size_t i = 0; i < parameters_number; i++)
1323 for(
size_t j = 0; j < i; j++)
1325 point_Hessian(i,j) = point_Hessian(j,i);
1330 return(point_Hessian);
1353 std::ostringstream buffer;
1357 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
1358 <<
"Vector<double> calculate_gradient(void) const method.\n"
1359 <<
"Numerical differentiation pointer is NULL.\n";
1361 throw std::logic_error(buffer.str());
1391 std::ostringstream buffer;
1395 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
1396 <<
"Vector<double> calculate_gradient(const Vector<double>&) const method.\n"
1397 <<
"Numerical differentiation pointer is NULL.\n";
1399 throw std::logic_error(buffer.str());
1452 std::ostringstream buffer;
1454 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
1455 <<
"Vector<double> calculate_terms(void) const method.\n"
1456 <<
"The terms function is not defined for this performance term.\n";
1458 throw std::logic_error(buffer.str());
1468 std::ostringstream buffer;
1470 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
1471 <<
"Vector<double> calculate_terms(const Vector<double>&) const method.\n"
1472 <<
"The terms function is not defined for this performance term.\n";
1474 throw std::logic_error(buffer.str());
1484 std::ostringstream buffer;
1486 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
1487 <<
"Matrix<double> calculate_terms_Jacobian(void) const method.\n"
1488 <<
"The terms function is not defined for this performance term.\n";
1490 throw std::logic_error(buffer.str());
1500 std::ostringstream buffer;
1502 buffer <<
"OpenNN Exception: PerformanceTerm class.\n"
1503 <<
"Vector<double> calculate_terms(void) const method.\n"
1504 <<
"The terms function is not defined for this performance term.\n";
1506 throw std::logic_error(buffer.str());
1516 return(
"USER_PERFORMANCE_TERM");
1538 std::ostringstream buffer;
1540 buffer <<
"Performance term\n"
1541 <<
"Display: " <<
display <<
"\n";
1543 return(buffer.str());
1554 std::ostringstream buffer;
1556 tinyxml2::XMLDocument* document =
new tinyxml2::XMLDocument;
1560 tinyxml2::XMLElement* root_element = document->NewElement(
"PerformanceTerm");
1562 document->InsertFirstChild(root_element);
1577 const tinyxml2::XMLElement* display_element = document.FirstChildElement(
"Display");
1581 std::string new_display_string = display_element->GetText();
1587 catch(
const std::logic_error& e)
1589 std::cout << e.what() << std::endl;
Matrix< double > calculate_Hessian(const T &t, double(T::*f)(const Vector< double > &) const, const Vector< double > &x) const
const Vector< PerceptronLayer > & get_layers(void) const
Vector< double > arrange_parameters(void) const
Vector< double > calculate_gradient(const T &t, double(T::*f)(const Vector< double > &) const, const Vector< double > &x) const
size_t count_parameters_number(void) const
Returns the number of parameters (biases and synaptic weights) of the layer.
size_t get_inputs_number(void) const
Returns the number of inputs to the multilayer perceptron.
void set(void)
Sets the size of a vector to zero.
size_t get_layers_number(void) const
Returns the number of layers in the multilayer perceptron.
Vector< size_t > arrange_layers_perceptrons_numbers(void) const
Returns a vector with the size of each layer.
Vector< size_t > arrange_parameter_indices(const size_t &) const
const size_t & get_columns_number(void) const
Returns the number of columns in the matrix.
const PerceptronLayer & get_layer(const size_t &) const
const Perceptron & get_perceptron(const size_t &) const
Vector< Matrix< double > > calculate_layers_combination_parameters_Jacobian(const Vector< Vector< double > > &) const
void tuck_in(const size_t &, const Vector< T > &)
double dot(const Vector< double > &) const
void set(void)
This method set the numbers of rows and columns of the matrix to zero.
const size_t & get_rows_number(void) const
Returns the number of rows in the matrix.
const Vector< double > & arrange_synaptic_weights(void) const
Returns the synaptic weight values of the neuron.
Vector< double > dot(const Vector< double > &) const
Matrix< T > sum_diagonal(const T &) const
Vector< Matrix< double > > arrange_layers_synaptic_weights(void) const
Vector< Vector< double > > arrange_layers_input(const Vector< double > &, const Vector< Vector< double > > &) const
size_t count_parameters_number(void) const
Returns the number of parameters (biases and synaptic weights) in the multilayer perceptron.