16 #include "multilayer_perceptron.h"
57 set(new_architecture);
75 set(new_inputs_number, new_neurons_number);
92 set(new_inputs_number, new_hidden_neurons_number, new_outputs_number);
106 set(other_multilayer_perceptron);
128 if(
this != &other_multilayer_perceptron)
189 if(i >= layers_number)
191 std::ostringstream buffer;
193 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
194 <<
"const PerceptronLayer get_layer(const size_t&) const method.\n"
195 <<
"Index of layer must be less than number of layers.\n";
197 throw std::logic_error(buffer.str());
219 if(i >= layers_number)
221 std::ostringstream buffer;
223 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
224 <<
"PerceptronLayer* get_layer_pointer(const size_t&) const method.\n"
225 <<
"Index of layer must be less than number of layers.\n";
227 throw std::logic_error(buffer.str());
259 if(layers_number != 0)
263 cumulative_neurons_number[0] = layers_size[0];
265 for(
size_t i = 1; i < layers_number; i++)
267 cumulative_neurons_number[i] = cumulative_neurons_number[i-1] + layers_size[i];
271 return(cumulative_neurons_number);
286 for(
size_t i = 0; i < layers_number; i++)
288 layers_parameters_number[i] =
layers[i].count_parameters_number();
291 return(layers_parameters_number);
304 Vector<size_t> layers_cumulative_parameters_number(layers_number);
306 layers_cumulative_parameters_number[0] =
layers[0].count_parameters_number();
308 for(
size_t i = 1; i < layers_number; i++)
310 layers_cumulative_parameters_number[i] = layers_cumulative_parameters_number[i-1] +
layers[i].count_parameters_number();
313 return(layers_cumulative_parameters_number);
330 for(
size_t i = 0; i < layers_number; i++)
332 layers_biases[i] =
layers[i].arrange_biases();
335 return(layers_biases);
353 for(
size_t i = 0; i < layers_number; i++)
355 layers_synaptic_weights[i] =
layers[i].arrange_synaptic_weights();
358 return(layers_synaptic_weights);
377 for(
size_t i = 0; i < layers_number; i++)
379 layers_parameters[i] =
layers[i].arrange_parameters();
382 return(layers_parameters);
394 size_t parameters_number = 0;
396 for(
size_t i = 0; i < layers_number; i++)
398 parameters_number +=
layers[i].count_parameters_number();
401 return(parameters_number);
419 for(
size_t i = 0; i < layers_number; i++)
422 const size_t layer_parameters_number = layer_parameters.size();
424 parameters.
tuck_in(position, layer_parameters);
425 position += layer_parameters_number;
444 for(
size_t i = 0; i < layers_number; i++)
446 layers_parameters_numbers[i] =
layers[i].count_parameters_number();
449 return(layers_parameters_numbers);
466 if(neuron_index >= neurons_number)
468 std::ostringstream buffer;
470 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
471 <<
"int get_layer_index(const size_t&) const method.\n"
472 <<
"Index of neuron must be less than number of neurons.\n";
474 throw std::logic_error(buffer.str());
497 return(perceptron_position);
503 return(cumulative_neurons_number[layer_index-1] + perceptron_position);
522 if(layer_index >= layers_number)
524 std::ostringstream buffer;
526 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
527 <<
"size_t get_layer_bias_index(const size_t&, const size_t&) const method.\n"
528 <<
"Index of layer must be less than number of layers.\n";
530 throw std::logic_error(buffer.str());
533 const size_t layer_perceptrons_number =
layers[layer_index].get_perceptrons_number();
535 if(perceptron_index >= layer_perceptrons_number)
537 std::ostringstream buffer;
539 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
540 <<
"size_t get_layer_bias_index(const size_t&, const size_t&) const method.\n"
541 <<
"Index of perceptron must be less than number of perceptrons in that layer.\n";
543 throw std::logic_error(buffer.str());
548 size_t layer_bias_index = 0;
552 for(
size_t i = 0; i < layer_index; i++)
554 layer_bias_index +=
layers[i].count_parameters_number();
559 for(
size_t j = 0; j < perceptron_index; j++)
561 layer_bias_index +=
layers[layer_index].get_perceptron(j).count_parameters_number();
564 return(layer_bias_index);
576 (
const size_t& layer_index,
const size_t& perceptron_index,
const size_t& input_index)
const
582 const size_t layers_number = get_layers_number();
584 if(layer_index >= layers_number)
586 std::ostringstream buffer;
588 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
589 <<
"size_t get_layer_synaptic_weight_index(const size_t&, const size_t&, const size_t&) method.\n"
590 <<
"Index of layer must be less than number of layers.\n";
592 throw std::logic_error(buffer.str());
595 const size_t layer_perceptrons_number = layers[layer_index].get_perceptrons_number();
597 if(perceptron_index >= layer_perceptrons_number)
599 std::ostringstream buffer;
601 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
602 <<
"size_t get_layer_synaptic_weight_index(const size_t&, const size_t&, const size_t&) method.\n"
603 <<
"Index of perceptron must be less than number of perceptrons in layer.\n";
605 throw std::logic_error(buffer.str());
608 const size_t layer_inputs_number = layers[layer_index].get_inputs_number();
610 if(input_index >= layer_inputs_number)
612 std::ostringstream buffer;
614 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
615 <<
"size_t get_layer_synaptic_weight_index(const size_t&, const size_t&, const size_t&) method.\n"
616 <<
"Index of inputs must be less than number of inputs in perceptron.\n";
618 throw std::logic_error(buffer.str());
623 size_t layer_synaptic_weight_index = 0;
629 for(
size_t i = 0; i < layer_index-1; i++)
631 layer_synaptic_weight_index += layers[layer_index].count_parameters_number();
637 if(perceptron_index > 0)
639 for(
size_t i = 0; i < perceptron_index-1; i++)
641 layer_synaptic_weight_index += layers[layer_index].get_perceptron(i).count_parameters_number();
647 layer_synaptic_weight_index += 1;
651 layer_synaptic_weight_index += input_index;
653 return(layer_synaptic_weight_index);
670 if(parameter_index >= parameters_number)
672 std::ostringstream buffer;
674 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
675 <<
"Vector<int> arrange_parameter_indices(const size_t&) const method.\n"
676 <<
"Index of neural parameter must be less than number of multilayer perceptron parameters.\n";
678 throw std::logic_error(buffer.str());
696 size_t perceptron_parameters_number;
705 size_t parameter_index = 0;
707 for(
size_t i = 0; i < layers_number; i++)
709 for(
size_t j = 0; j < layers_size[i]; j++)
711 perceptron_parameters_number =
layers[i].get_perceptron(j).count_parameters_number();
713 for(
size_t k = 0; k < perceptron_parameters_number; k++)
715 parameters_indices(parameter_index,0) = i;
716 parameters_indices(parameter_index,1) = j;
717 parameters_indices(parameter_index,2) = k;
723 return(parameters_indices);
737 for(
size_t i = 0; i < layers_number; i++)
739 layers_activation_function[i] =
layers[i].get_activation_function();
742 return(layers_activation_function);
757 for(
size_t i = 0; i < layers_number; i++)
759 layers_activation_function[i] =
layers[i].write_activation_function();
762 return(layers_activation_function);
792 if(layers_number > 0)
794 for(
size_t i = 0; i < layers_number-1; i++)
796 layers[i].set_activation_function(Perceptron::HyperbolicTangent);
799 layers[layers_number-1].set_activation_function(Perceptron::Linear);
841 std::ostringstream buffer;
843 const size_t new_architecture_size = new_architecture.size();
845 if(new_architecture_size == 0)
849 else if(new_architecture_size == 1)
851 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
852 <<
"void set_architecture(const Vector<size_t>&) method.\n"
853 <<
"Size of architecture cannot be one.\n";
855 throw std::logic_error(buffer.str());
859 for(
size_t i = 0; i < new_architecture_size; i++)
861 if(new_architecture[i] == 0)
863 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
864 <<
"void set_architecture(const Vector<size_t>&) method.\n"
865 <<
"Size " << i <<
" must be greater than zero.\n";
867 throw std::logic_error(buffer.str());
871 const size_t new_layers_number = new_architecture_size-1;
872 layers.set(new_layers_number);
876 for(
size_t i = 0; i < new_layers_number; i++)
878 layers[i].set(new_architecture[i], new_architecture[i+1]);
883 for(
size_t i = 0; i < new_layers_number-1; i++)
885 layers[i].set_activation_function(Perceptron::HyperbolicTangent);
888 layers[new_layers_number-1].set_activation_function(Perceptron::Linear);
905 std::ostringstream buffer;
907 if(new_inputs_number == 0)
909 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
910 <<
"void set(const size_t&, const size_t&) method.\n"
911 <<
"Number of inputs cannot be zero.\n";
913 throw std::logic_error(buffer.str());
915 else if(new_perceptrons_number == 0)
917 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
918 <<
"void set_architecture(const size_t&, const size_t&) method.\n"
919 <<
"Number of perceptrons cannot be zero.\n";
921 throw std::logic_error(buffer.str());
928 layers[0].set(new_inputs_number, new_perceptrons_number);
939 void MultilayerPerceptron::set(
const size_t& new_inputs_number,
const size_t& new_hidden_neurons_number,
const size_t& new_outputs_number)
945 std::ostringstream buffer;
947 if(new_inputs_number == 0)
949 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
950 <<
"void set(const size_t&, const size_t&, const size_t&) method.\n"
951 <<
"Number of inputs must be greater than zero.\n";
953 throw std::logic_error(buffer.str());
955 else if(new_hidden_neurons_number == 0)
957 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
958 <<
"void set(const size_t&, const size_t&, const size_t&) method.\n"
959 <<
"Number of hidden neurons must be greater than zero.\n";
961 throw std::logic_error(buffer.str());
963 else if(new_outputs_number == 0)
965 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
966 <<
"void set(const size_t&, const size_t&, const size_t&) method.\n"
967 <<
"Number of outputs must be greater than zero.\n";
969 throw std::logic_error(buffer.str());
976 layers[0].set(new_inputs_number, new_hidden_neurons_number);
977 layers[0].set_activation_function(Perceptron::HyperbolicTangent);
979 layers[1].set(new_hidden_neurons_number, new_outputs_number);
980 layers[1].set_activation_function(Perceptron::Linear);
1006 std::ostringstream buffer;
1008 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1009 <<
"void set_inputs_number(const size_t&) method.\n"
1010 <<
"Multilayer perceptron is empty.\n";
1012 throw std::logic_error(buffer.str());
1015 layers[0].set_inputs_number(new_inputs_number);
1043 const size_t layer_inputs_number =
layers[layer_index].get_inputs_number();
1045 layers[layer_index].set_perceptrons_number(new_layer_perceptrons_number);
1047 layers[layer_index].set_inputs_number(layer_inputs_number);
1051 if(layer_index < layers_number-1)
1053 layers[layer_index+1].set_inputs_number(new_layer_perceptrons_number);
1085 const size_t size = new_layers_biases.size();
1087 if(size != layers_number)
1089 std::ostringstream buffer;
1091 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1092 <<
"void set_layers_biases(const Vector< Vector<double> >&) method.\n"
1093 <<
"Size (" << size <<
") must be equal to number of layers (" << layers_number <<
").\n";
1095 throw std::logic_error(buffer.str());
1102 for(
size_t i = 0; i < layers_number; i++)
1104 layers[i].set_biases(new_layers_biases[i]);
1126 const size_t size = new_layers_synaptic_weights.size();
1128 if(size != layers_number)
1130 std::ostringstream buffer;
1132 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1133 <<
"void set_layers_synaptic_weights(const Vector< Matrix<double> >&) method.\n"
1134 <<
"Size must be equal to number of layers.\n";
1136 throw std::logic_error(buffer.str());
1141 for(
size_t i = 0; i < layers_number; i++)
1143 layers[i].set_synaptic_weights(new_layers_synaptic_weights[i]);
1165 const size_t new_layers_parameters_size = new_layers_parameters.size();
1167 if(new_layers_parameters_size != layers_number)
1169 std::ostringstream buffer;
1171 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1172 <<
"void set_layers_parameters(const Vector< Vector<double> >&) method.\n"
1173 <<
"Size of layer parameters must be equal to number of layers.\n";
1175 throw std::logic_error(buffer.str());
1180 for(
size_t i = 0; i < layers_number; i++)
1182 layers[i].set_parameters(new_layers_parameters[i]);
1198 const size_t size = new_parameters.size();
1202 if(size != parameters_number)
1204 std::ostringstream buffer;
1206 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1207 <<
"void set_parameters(const Vector<double>&) method.\n"
1208 <<
"Size (" << size <<
") must be equal to number of biases and synaptic weights (" << parameters_number <<
").\n";
1210 throw std::logic_error(buffer.str());
1217 size_t layer_parameters_number;
1220 size_t position = 0;
1222 for(
size_t i = 0; i < layers_number; i++)
1224 layer_parameters_number =
layers[i].count_parameters_number();
1225 layer_parameters =
layers[i].arrange_parameters();
1227 layer_parameters = new_parameters.
take_out(position, layer_parameters_number);
1229 layers[i].set_parameters(layer_parameters);
1230 position += layer_parameters_number;
1245 const size_t layers_number = get_layers_number();
1251 const size_t new_layers_activation_function_size = new_layers_activation_function.size();
1253 if(new_layers_activation_function_size != layers_number)
1255 std::ostringstream buffer;
1257 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1258 <<
"void set_layers_activation_function(const Vector<Perceptron::ActivationFunction>&) method.\n"
1259 <<
"Size of activation function of layers must be equal to number of layers.\n";
1261 throw std::logic_error(buffer.str());
1266 for(
size_t i = 0; i < layers_number; i++)
1268 layers[i].set_activation_function(new_layers_activation_function[i]);
1286 const size_t layers_number = get_layers_number();
1288 if(i >= layers_number)
1290 std::ostringstream buffer;
1292 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1293 <<
"void set_layer_activation_function(const size_t&, const Perceptron::ActivationFunction&) method.\n"
1294 <<
"Index of layer is equal or greater than number of layers.\n";
1296 throw std::logic_error(buffer.str());
1301 layers[i].set_activation_function(new_layer_activation_function);
1319 const size_t size = new_layers_activation_function.size();
1321 if(size != layers_number)
1323 std::ostringstream buffer;
1325 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1326 <<
"void set_layer_activation_function(const Vector<std::string>&) method.\n"
1327 <<
"Size of layers activation function is not equal to number of layers.\n";
1329 throw std::logic_error(buffer.str());
1335 for(
size_t i = 0; i < layers_number; i++)
1337 layers[i].set_activation_function(new_layers_activation_function[i]);
1380 if(layers_number > 0)
1397 if(layer_index >= (
layers.size()-1))
1399 std::ostringstream buffer;
1401 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1402 <<
"void grow_layer(const size_t&, const size_t&) method.\n"
1403 <<
"Index of layer is equal or greater than number of layers-1.\n";
1405 throw std::logic_error(buffer.str());
1408 if(perceptrons_number == 0)
1410 std::ostringstream buffer;
1412 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1413 <<
"void grow_layer(const size_t&, const size_t&) method.\n"
1414 <<
"Number of perceptrons must be greater than 0.\n";
1416 throw std::logic_error(buffer.str());
1421 for (
size_t i = 0; i < perceptrons_number; i++)
1423 layers[layer_index].grow_perceptrons(1);
1424 layers[layer_index+1].grow_inputs(1);
1442 if(index >= inputs_number)
1444 std::ostringstream buffer;
1446 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1447 <<
"void prune_input(const size_t&) method.\n"
1448 <<
"Index of input is equal or greater than number of inputs.\n";
1450 throw std::logic_error(buffer.str());
1457 if(layers_number > 0)
1459 layers[0].prune_input(index);
1477 if(index >= outputs_number)
1479 std::ostringstream buffer;
1481 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1482 <<
"void prune_output(const size_t&) method.\n"
1483 <<
"Index of output is equal or greater than number of outputs.\n";
1485 throw std::logic_error(buffer.str());
1492 if(layers_number > 0)
1494 layers[layers_number-1].prune_perceptron(index);
1509 if(layer_index >= (
layers.size()-1))
1511 std::ostringstream buffer;
1513 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1514 <<
"void prune_layer(const size_t&, const size_t&) method.\n"
1515 <<
"Index of layer is equal or greater than number of layers-1.\n";
1517 throw std::logic_error(buffer.str());
1520 if(perceptron_index >=
layers[layer_index].get_perceptrons_number())
1522 std::ostringstream buffer;
1524 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1525 <<
"void prune_layer(const size_t&, const size_t&) method.\n"
1526 <<
"Index of perceptron is equal or greater than number of perceptrons in the layer.\n";
1528 throw std::logic_error(buffer.str());
1533 layers[layer_index].prune_perceptron(perceptron_index);
1534 layers[layer_index+1].prune_input(perceptron_index);
1546 const size_t architecture_size = rand()%10 + 2;
1550 for(
size_t i = 0; i < architecture_size; i++)
1552 architecture[i] = rand()%10 + 1;
1561 for(
size_t i = 0; i < layers_number; i++)
1567 layers[i].set_activation_function(Perceptron::Logistic);
1573 layers[i].set_activation_function(Perceptron::HyperbolicTangent);
1579 layers[i].set_activation_function(Perceptron::Threshold);
1585 layers[i].set_activation_function(Perceptron::SymmetricThreshold);
1591 layers[i].set_activation_function(Perceptron::Linear);
1597 std::ostringstream buffer;
1599 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1600 <<
"void initialize_random(void) method.\n"
1601 <<
"Unknown layer activation function.\n";
1603 throw std::logic_error(buffer.str());
1624 for(
size_t i = 0; i < layers_number; i++)
1626 layers[i].initialize_biases(value);
1641 for(
size_t i = 0; i < layers_number; i++)
1643 layers[i].initialize_synaptic_weights(value);
1809 parameters.
randomize_normal(mean_standard_deviation[0], mean_standard_deviation[1]);
1834 if(perturbation < 0)
1836 std::ostringstream buffer;
1838 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1839 <<
"void perturbate_parameters(const double&) method.\n"
1840 <<
"Perturbation must be equal or greater than 0.\n";
1842 throw std::logic_error(buffer.str());
1853 parameters = parameters + parameters_perturbation;
1869 return(parameters_norm);
1885 const size_t size = inputs.size();
1889 if(size != inputs_number)
1891 std::ostringstream buffer;
1893 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1894 <<
"Vector<double> calculate_outputs(const Vector<double>&) const method.\n"
1895 <<
"Size of inputs (" << size <<
") must be equal to number of inputs (" << inputs_number <<
").\n";
1897 throw std::logic_error(buffer.str());
1906 if(layers_number == 0)
1912 outputs =
layers[0].calculate_outputs(inputs);
1914 for(
size_t i = 1; i < layers_number; i++)
1916 outputs =
layers[i].calculate_outputs(outputs);
1934 const size_t size = inputs.size();
1938 if(size != inputs_number)
1940 std::ostringstream buffer;
1942 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1943 <<
"Matrix<double> calculate_Jacobian(const Vector<double>&) const method.\n"
1944 <<
"Size must be equal to number of inputs.\n";
1946 throw std::logic_error(buffer.str());
1953 if(layers_number == 0)
1965 for(
int i = (
int)layers_number-2; i > -1; i--)
1967 Jacobian = Jacobian.
dot(layers_Jacobian[i]);
1985 const size_t size = inputs.size();
1989 if(size != inputs_number)
1991 std::ostringstream buffer;
1993 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
1994 <<
"Vector< Matrix<double> > calculate_Hessian_form(const Vector<double>&) const method.\n"
1995 <<
"Size must be equal to number of inputs.\n";
1997 throw std::logic_error(buffer.str());
2017 for(
size_t layer_index = layers_number-2; layer_index != 0; layer_index--)
2019 for(
size_t output_index = 0; output_index < outputs_number; output_index++)
2021 Hessian_form[output_index] = (layers_Jacobian[layer_index].calculate_transpose()).dot(Hessian_form[output_index]).
dot(layers_Jacobian[layer_index]);
2023 for(
size_t neuron_index = 0; neuron_index < layers_size[layer_index]; neuron_index++)
2025 Hessian_form[output_index] += layers_Hessian_form[layer_index][neuron_index]*Jacobian(output_index,neuron_index);
2029 Jacobian = Jacobian.
dot(layers_Jacobian[layer_index]);
2050 return(Hessian_form);
2068 if(layer_index == 0)
2070 std::ostringstream buffer;
2072 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2073 <<
"Matrix<double> calculate_layer_combination_combination(const size_t&, const Vector<double>&) const.\n"
2074 <<
"Index of layer must be greater than zero.\n";
2076 throw std::logic_error(buffer.str());
2078 else if(layer_index >= layers_number)
2080 std::ostringstream buffer;
2082 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2083 <<
"Matrix<double> calculate_layer_combination_combination(const size_t&, const Vector<double>&) const.\n"
2084 <<
"Index of layer must be less than numnber of layers.\n";
2086 throw std::logic_error(buffer.str());
2089 const size_t size = previous_layer_combination.size();
2090 const size_t previous_layer_perceptrons_number =
layers[layer_index-1].get_perceptrons_number();
2092 if(size != previous_layer_perceptrons_number)
2094 std::ostringstream buffer;
2096 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2097 <<
"Matrix<double> calculate_layer_combination_combination(const size_t&, const Vector<double>&) const.\n"
2098 <<
"Size must be equal to size of previous layer.\n";
2100 throw std::logic_error(buffer.str());
2107 const Vector<double> previous_layer_activation =
layers[layer_index-1].calculate_activations(previous_layer_combination);
2109 const Vector<double> layer_combination_combination =
layers[layer_index].calculate_combinations(previous_layer_activation);
2111 return(layer_combination_combination);
2125 const size_t previous_layer_perceptrons_number =
layers[layer_index-1].get_perceptrons_number();
2133 if(layer_index == 0)
2135 std::ostringstream buffer;
2137 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2138 <<
"Matrix<double> calculate_layer_combination_combination_Jacobian(const size_t&, const Vector<double>&) const.\n"
2139 <<
"Index of layer must be greater than zero.\n";
2141 throw std::logic_error(buffer.str());
2143 else if(layer_index >= layers_number)
2145 std::ostringstream buffer;
2147 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2148 <<
"Matrix<double> calculate_layer_combination_combination_Jacobian(const size_t&, const Vector<double>&) const.\n"
2149 <<
"Index of layer must be less than numnber of layers.\n";
2151 throw std::logic_error(buffer.str());
2154 const size_t size = previous_layer_activation_derivative.size();
2156 if(size != previous_layer_perceptrons_number)
2158 std::ostringstream buffer;
2160 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2161 <<
"Matrix<double> calculate_layer_combination_combination_Jacobian(const size_t&, const Vector<double>&).\n"
2162 <<
"Size of activation derivative must be equal to size of previous layer.\n";
2164 throw std::logic_error(buffer.str());
2171 Matrix<double> previous_layer_activation_Jacobian(previous_layer_perceptrons_number, previous_layer_perceptrons_number, 0.0);
2172 previous_layer_activation_Jacobian.
set_diagonal(previous_layer_activation_derivative);
2174 return(layer_synaptic_weights.
dot(previous_layer_activation_Jacobian));
2187 if(domain_layer_index > image_layer_index)
2189 std::ostringstream buffer;
2191 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2192 <<
"Vector<double> calculate_interlayer_combination_combination(const size_t&, const size_t&, const Vector<double>&) const method.\n"
2193 <<
"Index of domain layer must be less or equal than index of image layer.\n";
2195 throw std::logic_error(buffer.str());
2198 if(domain_layer_index == image_layer_index)
2200 return(domain_layer_combination);
2204 Vector<double> interlayer_combination_combination(domain_layer_combination);
2206 for(
size_t i = domain_layer_index+1; i <= image_layer_index; i++)
2211 return(interlayer_combination_combination);
2228 if(domain_layer_index < image_layer_index)
2230 const size_t size = image_layer_index-domain_layer_index;
2238 for(
size_t i = 1; i < size; i++)
2245 interlayer_combination_combination_Jacobian = layers_combination_combination_Jacobian[size-1];
2247 for(
int i = (
int)size-2; i > -1; i--)
2249 interlayer_combination_combination_Jacobian = interlayer_combination_combination_Jacobian.
dot(layers_combination_combination_Jacobian[i]);
2252 else if(domain_layer_index == image_layer_index)
2254 const size_t image_layer_perceptrons_number =
layers[image_layer_index].get_perceptrons_number();
2256 interlayer_combination_combination_Jacobian.
set_identity(image_layer_perceptrons_number);
2260 const size_t image_layer_perceptrons_number =
layers[image_layer_index].get_perceptrons_number();
2261 const size_t domain_layer_perceptrons_number =
layers[domain_layer_index].get_perceptrons_number();
2263 interlayer_combination_combination_Jacobian.
set(image_layer_perceptrons_number, domain_layer_perceptrons_number, 0.0);
2266 return(interlayer_combination_combination_Jacobian);
2282 const size_t size = layer_combinations.size();
2284 const size_t layer_perceptrons_number =
layers[layer_index].get_perceptrons_number();
2286 if(size != layer_perceptrons_number)
2288 std::ostringstream buffer;
2290 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2291 <<
"Vector<double> calculate_output_layer_combination(const size_t&, const Vector<double>&) const method.\n"
2292 <<
"Size must be equal to layer size.\n";
2294 throw std::logic_error(buffer.str());
2303 if(layer_index < layers_number)
2305 for(
size_t i = layer_index+1; i < layers_number; i++)
2307 outputs =
layers[i].calculate_outputs(outputs);
2334 const size_t layers_activation_derivative_size = layers_activation_derivative.size();
2336 if(layers_activation_derivative_size != layers_number)
2338 std::ostringstream buffer;
2340 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2341 <<
"Vector< Vector<double> > calculate_output_layers_delta(const Vector< Vector<double> >&) method.\n"
2342 <<
"Size of forward propagation activation derivative vector must be equal to number of layers.\n";
2344 throw std::logic_error(buffer.str());
2351 if(layers_number > 0)
2355 output_layers_delta[layers_number-1] =
layers[layers_number-1].arrange_activations_Jacobian(layers_activation_derivative[layers_number-1]);
2359 for(
int i = (
int)layers_number-2; i >= 0; i--)
2361 output_layers_delta[i] = output_layers_delta[i+1].
dot(
layers[i+1].arrange_synaptic_weights()).dot(
layers[i].arrange_activations_Jacobian(layers_activation_derivative[i]));
2365 return(output_layers_delta);
2381 const size_t layers_number = get_layers_number();
2382 const Vector<size_t> layers_size = arrange_layers_perceptrons_numbers();
2383 const size_t outputs_number = get_outputs_number();
2389 std::ostringstream buffer;
2393 const size_t second_order_forward_propagation_size = second_order_forward_propagation.size();
2395 if(second_order_forward_propagation_size != 3)
2397 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2398 <<
"Matrix< Vector< Matrix<double> > > calculate_output_interlayers_delta() method.\n"
2399 <<
"Size of second order forward propagation must be three.\n";
2401 throw std::logic_error(buffer.str());
2406 const size_t interlayers_combination_combination_Jacobian_rows_number = interlayers_combination_combination_Jacobian.get_rows_number();
2408 if(interlayers_combination_combination_Jacobian_rows_number != layers_number)
2410 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2411 <<
"Matrix< Vector< Matrix<double> > > calculate_output_interlayers_delta() method.\n"
2412 <<
"Number of rows of interlayers combination-combination Jacobian must be equal to number of layers.\n";
2414 throw std::logic_error(buffer.str());
2419 const size_t output_layers_delta_size = output_layers_delta.size();
2421 if(output_layers_delta_size != layers_number)
2423 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2424 <<
"Matrix< Vector< Matrix<double> > > calculate_output_interlayers_delta() method.\n"
2425 <<
"Size of multilayer perceptron outputs layers delta must be equal to number of layers.\n";
2427 throw std::logic_error(buffer.str());
2435 const Vector< Vector<double> >& layers_activation_second_derivative = second_order_forward_propagation[2];
2443 for(
size_t i = 0; i < layers_number; i++)
2445 for(
size_t j = 0; j < layers_number; j++)
2447 output_interlayers_Delta(i,j).
set(outputs_number);
2453 for(
size_t i = 0; i < layers_number; i++)
2455 for(
size_t j = 0; j < layers_number; j++)
2457 for(
size_t k = 0; k < outputs_number; k++)
2459 output_interlayers_Delta(i,j)[k].
set(layers_size[i], layers_size[j]);
2465 if(layers_number > 0)
2469 output_interlayers_Delta(layers_number-1,layers_number-1) = layers[layers_number-1].arrange_activations_Hessian_form(layers_activation_second_derivative[layers_number-1]);
2476 for(
size_t L = layers_number-1; L != 0; L--)
2478 for(
size_t M = layers_number-1; M >= L; M--)
2480 if(!(L == layers_number-1 && M == layers_number-1))
2482 for(
size_t i = 0; i < outputs_number; i++)
2484 for(
size_t j = 0; j < layers_size[L]; j++)
2488 for(
size_t l = 0; l < layers_size[L+1]; l++)
2490 sum_1 += output_layers_delta[L+1](i,l)*layers_synaptic_weights[L+1](l,j);
2493 for(
size_t k = 0; k < layers_size[M]; k++)
2497 for(
size_t l = 0; l < layers_size[L+1]; l++)
2499 sum_2 += output_interlayers_Delta(L+1,M)[i](l,k)*layers_synaptic_weights[L+1](l,j);
2502 output_interlayers_Delta(L,M)[i](j,k)
2503 = layers_activation_second_derivative[L][j]
2504 *interlayers_combination_combination_Jacobian(L,M)(j,k)*sum_1
2505 + layers_activation_derivative[L][j]*sum_2;
2507 output_interlayers_Delta(M,L)[i](k,j) = output_interlayers_Delta(L,M)[i](j,k);
2516 return(output_interlayers_Delta);
2533 const size_t size = inputs.size();
2537 if(size != inputs_number)
2539 std::ostringstream buffer;
2541 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2542 <<
"Vector<double> calculate_outputs(const Vector<double>&, const Vector<double>&) const method.\n"
2543 <<
"Size of inputs (" << size <<
") must be equal to number of inputs (" << inputs_number <<
").\n";
2545 throw std::logic_error(buffer.str());
2548 const size_t parameters_size = parameters.size();
2552 if(parameters_size != parameters_number)
2554 std::ostringstream buffer;
2556 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2557 <<
"Vector<double> calculate_outputs(const Vector<double>&, const Vector<double>&) const method.\n"
2558 <<
"Size of parameters (" << parameters_size <<
") must be equal to number of parameters (" << parameters_number <<
").\n";
2560 throw std::logic_error(buffer.str());
2569 if(layers_number == 0)
2581 layer_parameters = parameters.
take_out(0, layers_parameters_numbers[0]);
2583 outputs =
layers[0].calculate_outputs(inputs, layer_parameters);
2585 for(
size_t i = 1; i < layers_number; i++)
2587 layer_parameters = parameters.
take_out(layers_cumulative_parameters_number[i-1], layers_parameters_numbers[i]);
2589 outputs =
layers[i].calculate_outputs(outputs, layer_parameters);
2607 const size_t size = inputs.size();
2609 if(size != inputs_number)
2611 std::ostringstream buffer;
2613 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2614 <<
"void calculate_Jacobian(Vector<double>&, const Vector<double>&) const method.\n"
2615 <<
"Size must be equal to number of inputs.\n";
2617 throw std::logic_error(buffer.str());
2639 Matrix<double> parameters_Jacobian = output_layers_delta[0].
dot(layers_combination_parameters_Jacobian[0]);
2641 for(
size_t i = 1; i < layers_number; i++)
2643 parameters_Jacobian = parameters_Jacobian.
assemble_columns(output_layers_delta[i].dot(layers_combination_parameters_Jacobian[i]));
2646 return(parameters_Jacobian);
2690 for(
size_t i = 0; i < outputs_number; i++)
2692 parameters_Hessian_form[i].
set(parameters_number, parameters_number);
2697 if(layers_number > 0)
2712 for(
size_t i = 0; i < outputs_number; i++)
2714 for(
size_t j = 0; j < parameters_number; j++)
2716 layer_j = parameters_indices(j,0);
2717 neuron_j = parameters_indices(j,1);
2718 parameter_j = parameters_indices(j,2);
2720 for(
size_t k = j; k < parameters_number; k++)
2722 layer_k = parameters_indices(k,0);
2723 neuron_k = parameters_indices(k,1);
2724 parameter_k = parameters_indices(k,2);
2726 parameters_Hessian_form[i](j,k) =
2727 output_interlayers_Delta(layer_j,layer_k)[i](neuron_j,neuron_k)
2728 *perceptrons_combination_parameters_gradient[layer_j][neuron_j][parameter_j]
2729 *perceptrons_combination_parameters_gradient[layer_k][neuron_k][parameter_k]
2732 if(layer_j != 0 && parameter_j != 0)
2742 if(layer_k != 0 && parameter_k != 0)
2744 parameters_Hessian_form[i](j,k) +=
2745 output_layers_delta[layer_k](i,neuron_k)
2746 *layers_activation_derivative[layer_k-1][parameter_k-1]
2747 *interlayers_combination_combination_Jacobian(layer_k-1,layer_j)(parameter_k-1,neuron_j)
2748 *perceptrons_combination_parameters_gradient[layer_j][neuron_j][parameter_j]
2752 parameters_Hessian_form[i](k,j) = parameters_Hessian_form[i](j,k);
2758 return(parameters_Hessian_form);
2780 const size_t inputs_size = inputs.size();
2782 if(inputs_size != inputs_number)
2784 std::ostringstream buffer;
2786 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2787 <<
"Vector< Vector<double> > arrange_layers_input(const Vector<double>&, const Vector< Vector<double> >&) const method.\n"
2788 <<
"Size must be equal to number of inputs.\n";
2790 throw std::logic_error(buffer.str());
2793 const size_t layers_activation_size = layers_activation.size();
2795 if(layers_activation_size != layers_number)
2797 std::ostringstream buffer;
2799 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2800 <<
"Vector< Vector<double> > arrange_layers_input(const Vector<double>&, const Vector< Vector<double> >&) const method.\n"
2801 <<
"Size must be equal to number of inputs.\n";
2803 throw std::logic_error(buffer.str());
2812 if(layers_number != 0)
2814 layers_inputs[0] = inputs;
2816 for(
size_t j = 1; j < layers_number; j++)
2818 layers_inputs[j] = layers_activation[j-1];
2822 return(layers_inputs);
2843 layers_inputs[0] = input;
2845 for(
size_t j = 1; j < layers_number; j++)
2847 layers_inputs[j] = layers_activation[j-1];
2850 return(layers_inputs);
2866 layers_inputs[0] = inputs;
2868 for(
size_t i = 1; i < layers_number; i++)
2870 layers_inputs[i] =
layers[i-1].calculate_outputs(layers_inputs[i-1]);
2873 return(layers_inputs);
2889 const size_t inputs_size = inputs.size();
2893 if(inputs_size != inputs_number)
2895 std::ostringstream buffer;
2897 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
2898 <<
"Vector< Vector<double> > calculate_layers_combination(const Vector<double>&) const method.\n"
2899 <<
"Size must be equal to number of inputs.\n";
2901 throw std::logic_error(buffer.str());
2910 if(layers_number > 0)
2914 layers_combination[0] =
layers[0].calculate_combinations(inputs);
2916 layers_activation[0] =
layers[0].calculate_activations(layers_combination[0]);
2918 for(
size_t i = 1; i < layers_number; i++)
2920 layers_combination[i] =
layers[i].calculate_combinations(layers_activation[i-1]);
2922 layers_activation[i] =
layers[i].calculate_activations(layers_combination[i]);
2926 return(layers_combination);
2947 layers_output[0] =
layers[0].calculate_outputs(inputs);
2949 layers_combination_Jacobian[0] =
layers[0].calculate_combinations_Jacobian(inputs);
2951 for(
size_t i = 1; i < layers_number; i++)
2953 layers_output[i] =
layers[i].calculate_outputs(layers_output[i-1]);
2955 layers_Jacobian[i] =
layers[i].calculate_Jacobian(layers_output[i-1]);
2958 Matrix<double> output_layer_Jacobian =
layers[layers_number-1].calculate_Jacobian(layers_output[layers_number-1]);
2962 layers_Jacobian[layers_number] = output_layer_Jacobian;
2964 for(
int i = (
int)layers_number-1; i > -1; i--)
2966 layers_Jacobian[layers_number] = layers_Jacobian[layers_number].
dot(layers_Jacobian[i]);
2969 for(
int i = (
int)layers_number-1; i > -1; i--)
2971 layers_Jacobian[i] = layers_Jacobian[i];
2973 for(
int j = (
int)i-1; j > -1; j--)
2975 layers_Jacobian[i] = layers_Jacobian[i].
dot(layers_Jacobian[j]);
2979 return(layers_combination_Jacobian);
2998 for(
size_t i = 0; i < layers_number; i++)
3000 layers_combination_parameters_Jacobian[i] =
layers[i].calculate_combinations_Jacobian(layers_inputs[i], dummy);
3003 return(layers_combination_parameters_Jacobian);
3025 const size_t layers_input_size = layers_inputs.size();
3027 if(layers_input_size != layers_number)
3029 std::ostringstream buffer;
3031 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
3032 <<
"Vector< Vector< Vector<double> > > calculate_perceptrons_combination_parameters_gradient(const Vector< Vector<double> >&) const method.\n"
3033 <<
"Size must be equal to number of layers.\n";
3035 throw std::logic_error(buffer.str());
3044 for(
size_t i = 0; i < layers_number; i++)
3046 if(layers_inputs[i].size() != layers_inputs_number[i])
3048 std::ostringstream buffer;
3050 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
3051 <<
"Vector< Vector< Vector<double> > > calculate_perceptrons_combination_parameters_gradient(const Vector< Vector<double> >&) const method.\n"
3052 <<
"Size of inputs to layer " << i <<
" must be equal to size of that layer.\n";
3054 throw std::logic_error(buffer.str());
3066 for(
size_t i = 0; i < layers_number; i++)
3068 perceptrons_combination_gradient[i].
set(layers_size[i]);
3070 for(
size_t j = 0; j < layers_size[i]; j++)
3078 return(perceptrons_combination_gradient);
3094 const size_t inputs_size = inputs.size();
3098 if(inputs_size != inputs_number)
3100 std::ostringstream buffer;
3102 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
3103 <<
"Vector< Vector<double> > calculate_layers_activation(const Vector<double>&) const method.\n"
3104 <<
"Size must be equal to number of inputs.\n";
3106 throw std::logic_error(buffer.str());
3116 layers_combination[0] =
layers[0].calculate_combinations(inputs);
3117 layers_activation[0] =
layers[0].calculate_activations(layers_combination[0]);
3119 for(
size_t i = 1; i < layers_number; i++)
3121 layers_combination[i] =
layers[i].calculate_combinations(layers_activation[i-1]);
3122 layers_activation[i] =
layers[i].calculate_activations(layers_combination[i]);
3125 return(layers_activation);
3141 const size_t inputs_size = inputs.size();
3144 if(inputs_size != inputs_number)
3146 std::ostringstream buffer;
3148 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
3149 <<
"Vector< Vector<double> > calculate_layers_activation_derivative(const Vector<double>&) const method.\n"
3150 <<
"Size must be equal to number of inputs.\n";
3152 throw std::logic_error(buffer.str());
3163 if(layers_number != 0)
3165 layers_combination[0] =
layers[0].calculate_combinations(inputs);
3166 layers_activation[0] =
layers[0].calculate_activations(layers_combination[0]);
3167 layers_activation_derivatives[0] =
layers[0].calculate_activations_derivatives(layers_combination[0]);
3169 for(
size_t i = 1; i < layers_number; i++)
3171 layers_combination[i] =
layers[i].calculate_combinations(layers_activation[i-1]);
3172 layers_activation[i] =
layers[i].calculate_activations(layers_combination[i]);
3173 layers_activation_derivatives[i] =
layers[i].calculate_activations_derivatives(layers_combination[i]);
3177 return(layers_activation_derivatives);
3198 const size_t inputs_size = inputs.size();
3202 if(inputs_size != inputs_number)
3204 std::ostringstream buffer;
3206 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
3207 <<
"Vector< Vector<double> > calculate_layers_activation_second_derivative(const Vector<double>&) const method.\n"
3208 <<
"Size must be equal to number of inputs.\n";
3210 throw std::logic_error(buffer.str());
3221 layers_combination[0] =
layers[0].calculate_combinations(inputs);
3222 layers_activation[0] =
layers[0].calculate_activations(layers_combination[0]);
3223 layers_activation_second_derivatives[0] =
layers[0].calculate_activations_second_derivatives(layers_combination[0]);
3225 for(
size_t i = 1; i < layers_number; i++)
3227 layers_combination[i] =
layers[i].calculate_combinations(layers_activation[i-1]);
3229 layers_activation[i] =
layers[i].calculate_activations(layers_combination[i]);
3231 layers_activation_second_derivatives[i] =
layers[i].calculate_activations_second_derivatives(layers_combination[i]);
3234 return(layers_activation_second_derivatives);
3252 layers_output[0] =
layers[0].calculate_outputs(inputs);
3253 layers_Jacobian[0] =
layers[0].calculate_Jacobian(inputs);
3255 for(
size_t i = 1; i < layers_number; i++)
3257 layers_output[i] =
layers[i].calculate_outputs(layers_output[i-1]);
3259 layers_Jacobian[i] =
layers[i].calculate_Jacobian(layers_output[i-1]);
3262 return(layers_Jacobian);
3280 layers_output[0] =
layers[0].calculate_outputs(inputs);
3281 layers_Hessian_form[0] =
layers[0].calculate_Hessian_form(inputs);
3283 for(
size_t i = 1; i < layers_number; i++)
3285 layers_output[i] =
layers[i].calculate_outputs(layers_output[i-1]);
3287 layers_Hessian_form[i] =
layers[i].calculate_Hessian_form(layers_output[i-1]);
3290 return(layers_Hessian_form);
3304 const size_t size = inputs.size();
3308 if(size != inputs_number)
3310 std::ostringstream buffer;
3312 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
3313 <<
"Matrix< Matrix<double> > calculate_interlayers_combination_combination_Jacobian(const Vector<double>&) const method.\n"
3314 <<
"Size of inpouts must be equal to number of inputs.\n";
3316 throw std::logic_error(buffer.str());
3343 for(
size_t i = 0; i < layers_number; i++)
3345 size = layers_combination[i].size();
3347 if(size != layers_size[i])
3349 std::ostringstream buffer;
3351 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
3352 <<
"Matrix< Matrix<double> > calculate_interlayers_combination_combination_Jacobian(const Vector< Vector<double> >&) const method.\n"
3353 <<
"Size must be equal to size of layer.\n";
3355 throw std::logic_error(buffer.str());
3365 for(
size_t image_index = 0; image_index < layers_number; image_index++)
3367 for(
size_t domain_index = 0; domain_index < layers_number; domain_index++)
3369 interlayers_combination_combination_Jacobian(image_index,domain_index) =
3374 return(interlayers_combination_combination_Jacobian);
3394 const size_t inputs_size = inputs.size();
3398 if(inputs_size != inputs_number)
3400 std::ostringstream buffer;
3402 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
3403 <<
"Vector< Vector< Vector<double> > > calculate_first_order_forward_propagation(const Vector<double>&) const method.\n"
3404 <<
"Size must be equal to number of inputs.\n";
3406 throw std::logic_error(buffer.str());
3417 first_order_forward_propagation[0].
set(layers_number);
3418 first_order_forward_propagation[1].
set(layers_number);
3420 layers_combination[0] =
layers[0].calculate_combinations(inputs);
3422 first_order_forward_propagation[0][0] =
layers[0].calculate_activations(layers_combination[0]);
3424 first_order_forward_propagation[1][0] =
layers[0].calculate_activations_derivatives(layers_combination[0]);
3426 for(
size_t i = 1; i < layers_number; i++)
3428 layers_combination[i] =
layers[i].calculate_combinations(first_order_forward_propagation[0][i-1]);
3430 first_order_forward_propagation[0][i] =
layers[i].calculate_activations(layers_combination[i]);
3432 first_order_forward_propagation[1][i] =
layers[i].calculate_activations_derivatives(layers_combination[i]);
3435 return(first_order_forward_propagation);
3455 const size_t inputs_size = inputs.size();
3459 if(inputs_size != inputs_number)
3461 std::ostringstream buffer;
3463 buffer <<
"OpenNN Exception: MultilayerPerceptron class.\n"
3464 <<
"Vector< Vector< Vector<double> > > calculate_second_order_forward_propagation(const Vector<double>&) const method.\n"
3465 <<
"Size of multilayer perceptron inputs must be equal to number of inputs.\n";
3467 throw std::logic_error(buffer.str());
3478 second_order_forward_propagation[0].
set(layers_number);
3479 second_order_forward_propagation[1].
set(layers_number);
3480 second_order_forward_propagation[2].
set(layers_number);
3482 layers_combination[0] =
layers[0].calculate_combinations(inputs);
3484 second_order_forward_propagation[0][0] =
layers[0].calculate_activations(layers_combination[0]);
3486 second_order_forward_propagation[1][0] =
layers[0].calculate_activations_derivatives(layers_combination[0]);
3488 second_order_forward_propagation[2][0] =
layers[0].calculate_activations_second_derivatives(layers_combination[0]);
3490 for(
size_t i = 1; i < layers_number; i++)
3492 layers_combination[i] =
layers[i].calculate_combinations(second_order_forward_propagation[0][i-1]);
3494 second_order_forward_propagation[0][i] =
layers[i].calculate_activations(layers_combination[i]);
3496 second_order_forward_propagation[1][i] =
layers[i].calculate_activations_derivatives(layers_combination[i]);
3498 second_order_forward_propagation[2][i] =
layers[i].calculate_activations_second_derivatives(layers_combination[i]);
3501 return(second_order_forward_propagation);
3511 std::ostringstream buffer;
3513 buffer <<
"MultilayerPerceptron\n"
3517 <<
"Display: " <<
display <<
"\n";
3519 return(buffer.str());
3530 std::ostringstream buffer;
3532 tinyxml2::XMLDocument* document =
new tinyxml2::XMLDocument;
3534 tinyxml2::XMLElement* multilayer_perceptron_element = document->NewElement(
"MultilayerPerceptron");
3536 document->InsertFirstChild(multilayer_perceptron_element);
3540 tinyxml2::XMLElement* architecture_element = document->NewElement(
"Architecture");
3541 multilayer_perceptron_element->LinkEndChild(architecture_element);
3545 tinyxml2::XMLText* architecture_text = document->NewText(architecture_string.c_str());
3546 architecture_element->LinkEndChild(architecture_text);
3551 tinyxml2::XMLElement* layers_activation_function_element = document->NewElement(
"LayersActivationFunction");
3552 multilayer_perceptron_element->LinkEndChild(layers_activation_function_element);
3556 tinyxml2::XMLText* layers_activation_function_text = document->NewText(layers_activation_function_string.c_str());
3557 layers_activation_function_element->LinkEndChild(layers_activation_function_text);
3562 tinyxml2::XMLElement* parameters_element = document->NewElement(
"Parameters");
3563 multilayer_perceptron_element->LinkEndChild(parameters_element);
3567 tinyxml2::XMLText* parameters_text = document->NewText(parameters_string.c_str());
3568 parameters_element->LinkEndChild(parameters_text);
3573 tinyxml2::XMLElement* display_element = document->NewElement(
"Display");
3574 multilayer_perceptron_element->LinkEndChild(display_element);
3579 tinyxml2::XMLText* display_text = document->NewText(buffer.str().c_str());
3580 display_element->LinkEndChild(display_text);
3594 const tinyxml2::XMLElement* root_element = document.FirstChildElement(
"MultilayerPerceptron");
3603 const tinyxml2::XMLElement* architecture_element = root_element->FirstChildElement(
"Architecture");
3605 if(architecture_element)
3607 const char* architecture_text = architecture_element->GetText();
3609 if(architecture_text)
3612 new_architecture.
parse(architecture_text);
3616 set(new_architecture);
3618 catch(
const std::logic_error& e)
3620 std::cout << e.what() << std::endl;
3628 const tinyxml2::XMLElement* layers_activation_function_element = root_element->FirstChildElement(
"LayersActivationFunction");
3630 if(layers_activation_function_element)
3632 const char* layers_activation_function_text = layers_activation_function_element->GetText();
3634 if(layers_activation_function_text)
3637 new_layers_activation_function.
parse(layers_activation_function_text);
3643 catch(
const std::logic_error& e)
3645 std::cout << e.what() << std::endl;
3653 const tinyxml2::XMLElement* parameters_element = root_element->FirstChildElement(
"Parameters");
3655 if(parameters_element)
3657 const char* parameters_text = parameters_element->GetText();
3662 new_parameters.
parse(parameters_text);
3668 catch(
const std::logic_error& e)
3670 std::cout << e.what() << std::endl;
3678 const tinyxml2::XMLElement* display_element = root_element->FirstChildElement(
"Display");
3682 std::string new_display_string = display_element->GetText();
3688 catch(
const std::logic_error& e)
3690 std::cout << e.what() << std::endl;
3706 std::ostringstream buffer;
3710 if(layers_number == 0)
3714 return(information);
3721 for(
size_t i = 0; i < layers_number; i++)
3726 buffer <<
layers[i].get_inputs_number();
3728 information(i,0) = buffer.str();
3733 buffer <<
layers[i].get_perceptrons_number();
3735 information(i,1) = buffer.str();
3739 information(i,2) =
layers[i].write_activation_function();
3742 return(information);
3759 std::ostringstream buffer;
3761 if(layers_number == 0)
3764 else if(layers_number == 1)
3766 buffer <<
layers[0].write_expression(inputs_name, outputs_name) <<
"\n";
3772 for(
size_t i = 0; i < layers_number; i++)
3774 layers_outputs_name[i].
set(layers_perceptrons_number[i]);
3776 for(
size_t j = 0; j < layers_perceptrons_number[i]; j++)
3778 std::ostringstream new_buffer;
3779 new_buffer <<
"y_" << i+1 <<
"_" << j+1;
3780 layers_outputs_name[i][j] = new_buffer.str();
3784 buffer <<
layers[0].write_expression(inputs_name, layers_outputs_name[0]);
3786 for(
size_t i = 1; i < layers_number-1; i++)
3788 buffer <<
layers[i].write_expression(layers_outputs_name[i-1], layers_outputs_name[i]);
3791 buffer <<
layers[layers_number-1].write_expression(layers_outputs_name[layers_number-2], outputs_name);
3794 return(buffer.str());
void parse(const std::string &)
Vector< double > calculate_output_layer_combination(const size_t &, const Vector< double > &) const
void randomize_uniform(const double &=-1.0, const double &=1.0)
void set_layer_perceptrons_number(const size_t &, const size_t &)
tinyxml2::XMLDocument * to_XML(void) const
Vector< size_t > count_layers_parameters_numbers(void) const
Vector< std::string > write_layers_activation_function(void) const
bool is_empty(void) const
Returns true if the number of layers in the multilayer perceptron is zero, and false otherwise...
Vector< Perceptron::ActivationFunction > get_layers_activation_function(void) const
Returns the activation function of every layer in a single vector.
const Vector< PerceptronLayer > & get_layers(void) const
std::string write_expression(const Vector< std::string > &, const Vector< std::string > &) const
void set_layers_activation_function(const Vector< Perceptron::ActivationFunction > &)
Vector< Vector< double > > arrange_layers_biases(void) const
Matrix< double > calculate_interlayer_combination_combination_Jacobian(const size_t &, const size_t &, const Vector< double > &) const
Vector< T > take_out(const size_t &, const size_t &) const
void initialize_synaptic_weights(const double &)
size_t get_layer_synaptic_weight_index(const size_t &, const size_t &, const size_t &) const
void set_inputs_number(const size_t &)
Vector< Vector< Matrix< double > > > calculate_layers_Hessian_form(const Vector< double > &) const
void set_display(const bool &)
size_t get_layer_bias_index(const size_t &, const size_t &) const
size_t get_inputs_number(void) const
Returns the number of inputs to the multilayer perceptron.
Matrix< size_t > arrange_parameters_indices(void) const
Matrix< Matrix< double > > calculate_interlayers_combination_combination_Jacobian(const Vector< double > &) const
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< double > calculate_layer_combination_combination(const size_t &, const Vector< double > &) const
void from_XML(const tinyxml2::XMLDocument &)
Vector< Matrix< double > > calculate_layers_combination_Jacobian(const Vector< double > &) const
void perturbate_parameters(const double &)
Vector< size_t > arrange_layers_perceptrons_numbers(void) const
Returns a vector with the size of each layer.
ActivationFunction
Enumeration of available activation functions for the perceptron neuron model.
size_t get_outputs_number(void) const
Returns the number of outputs neurons in the multilayer perceptron.
Vector< double > calculate_outputs(const Vector< double > &) const
Vector< Vector< double > > get_layers_parameters(void) const
void randomize_parameters_uniform(void)
Matrix< Vector< Matrix< double > > > calculate_output_interlayers_Delta(const Vector< Vector< Vector< double > > > &, const Matrix< Matrix< double > > &, const Vector< Matrix< double > > &) const
Vector< Vector< double > > calculate_layers_combination(const Vector< double > &) const
void set_parameters(const Vector< double > &)
void set_diagonal(const T &)
void set(void)
Sets an empty multilayer_perceptron_pointer architecture.
void prune_input(const size_t &)
Vector< size_t > arrange_layers_parameters_number(void) const
Vector< size_t > arrange_parameter_indices(const size_t &) const
Vector< T > assemble(const Vector< T > &) const
Vector< Vector< double > > calculate_layers_input(const Vector< double > &) const
size_t count_perceptrons_number(void) const
void initialize_parameters(void)
Initializes the parameters at random with values chosen from a normal distribution with mean 0 and st...
void grow_layer(const size_t &, const size_t &)
Vector< Vector< Vector< double > > > calculate_perceptrons_combination_parameters_gradient(const Vector< Vector< double > > &) const
Vector< double > calculate_combination_gradient(const Vector< double > &) const
Returns the partial derivatives of the combination with respect to the inputs.
void set_layers_parameters(const Vector< Vector< double > > &)
double calculate_parameters_norm(void) const
Returns the norm of the vector of multilayer perceptron parameters.
void initialize_biases(const double &)
const bool & get_display(void) const
const PerceptronLayer & get_layer(const size_t &) const
size_t calculate_cumulative_index(const T &) const
double calculate_norm(void) const
Returns the vector norm.
Matrix< T > assemble_columns(const Matrix< T > &) const
virtual ~MultilayerPerceptron(void)
Vector< size_t > arrange_architecture(void) const
void prune_layer(const size_t &, const size_t &)
std::string to_string(void) const
Returns a string representation of the current multilayer perceptron object.
void randomize_parameters_normal(void)
Vector< Vector< double > > calculate_layers_activation_second_derivative(const Vector< double > &) const
void set_identity(const size_t &)
Matrix< double > calculate_Jacobian(const Vector< double > &) const
PerceptronLayer * get_layer_pointer(const size_t &)
Matrix< double > calculate_layer_combination_combination_Jacobian(const size_t &, const Vector< double > &) const
Vector< PerceptronLayer > layers
Vector< size_t > get_layers_inputs_number(void) const
Returns a vector with the number of inputs of each layer.
Vector< double > calculate_interlayer_combination_combination(const size_t &, const size_t &, const Vector< double > &) const
Vector< Matrix< double > > calculate_layers_combination_parameters_Jacobian(const Vector< Vector< double > > &) const
void tuck_in(const size_t &, const Vector< T > &)
void randomize_normal(const double &=0.0, const double &=1.0)
double dot(const Vector< double > &) const
void set(void)
This method set the numbers of rows and columns of the matrix to zero.
Vector< Vector< double > > calculate_layers_activation_derivative(const Vector< double > &) const
Vector< size_t > count_cumulative_perceptrons_number(void) const
Returns a vector of size the number of layers, where each element is equal to the total number of neu...
Vector< Matrix< double > > calculate_layers_Jacobian(const Vector< double > &) const
void set_layers_perceptrons_number(const Vector< size_t > &)
Vector< Vector< double > > calculate_layers_activation(const Vector< double > &) const
Vector< Matrix< double > > calculate_output_layers_delta(const Vector< Vector< double > > &) const
void set_layers_synaptic_weights(const Vector< Matrix< double > > &)
Vector< double > dot(const Vector< double > &) const
Matrix< std::string > write_information(void) const
size_t get_perceptron_index(const size_t &, const size_t &) const
Vector< double > arrange_parameters(void) const
Returns the values of all the biases and synaptic weights in the multilayer perceptron as a single ve...
Vector< Vector< Vector< double > > > calculate_first_order_forward_propagation(const Vector< double > &) const
Vector< size_t > arrange_layers_cumulative_parameters_number(void) const
virtual void set_default(void)
MultilayerPerceptron & operator=(const MultilayerPerceptron &)
void set_layers(const Vector< PerceptronLayer > &)
void set_layer_activation_function(const size_t &, const Perceptron::ActivationFunction &)
Vector< Matrix< double > > calculate_Hessian_form(const Vector< double > &) const
void prune_output(const size_t &)
bool operator==(const MultilayerPerceptron &) const
T calculate_sum(void) const
Returns the sum of the elements in the vector.
bool display
Display messages to screen.
void set_layers_biases(const Vector< Vector< double > > &)
std::string to_string(const std::string &=" ") const
Returns a string representation of this vector.
size_t get_layer_index(const size_t &) const
Vector< Vector< Vector< double > > > calculate_second_order_forward_propagation(const Vector< double > &) const
MultilayerPerceptron(void)
void initialize_random(void)
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.