00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <iostream>
00017 #include <cstdlib>
00018 #include <fstream>
00019 #include <string>
00020 #include <sstream>
00021 #include <cmath>
00022
00023 #include "MultilayerPerceptron.h"
00024
00025 namespace Flood
00026 {
00027
00028
00029
00044
00045 MultilayerPerceptron::MultilayerPerceptron
00046 (int new_inputs_number, const Vector<int>& new_hidden_layers_size, int new_outputs_number)
00047 {
00048
00049
00050 set_network_architecture(new_inputs_number, new_hidden_layers_size, new_outputs_number);
00051
00052
00053
00054 set_independent_parameters_number(0);
00055
00056
00057
00058 set_default();
00059 }
00060
00061
00062
00063
00077
00078 MultilayerPerceptron::MultilayerPerceptron(int new_inputs_number, int new_hidden_layer_size, int new_outputs_number)
00079 {
00080
00081
00082 Vector<int> new_hidden_layers_size(1, new_hidden_layer_size);
00083
00084 set_network_architecture(new_inputs_number, new_hidden_layers_size, new_outputs_number);
00085
00086
00087
00088 set_independent_parameters_number(0);
00089
00090
00091
00092 set_default();
00093 }
00094
00095
00096
00097
00105
00106 MultilayerPerceptron::MultilayerPerceptron(int new_independent_parameters_number)
00107 {
00108
00109
00110 Vector<int> new_hidden_layers_size;
00111
00112 set_network_architecture(0, new_hidden_layers_size, 0);
00113
00114
00115
00116 set_independent_parameters_number(new_independent_parameters_number);
00117
00118
00119
00120 set_default();
00121 }
00122
00123
00124
00125
00129
00130 MultilayerPerceptron::MultilayerPerceptron(const char* filename)
00131 {
00132 load(filename);
00133 }
00134
00135
00136
00137
00140
00141 MultilayerPerceptron::MultilayerPerceptron(void)
00142 {
00143
00144
00145 Vector<int> new_hidden_layers_size;
00146
00147 set_network_architecture(0, new_hidden_layers_size, 0);
00148
00149
00150
00151 set_independent_parameters_number(0);
00152
00153
00154
00155 set_default();
00156 }
00157
00158
00159
00160
00163
00164 MultilayerPerceptron::MultilayerPerceptron(const MultilayerPerceptron& other_multilayer_perceptron)
00165 {
00166
00167
00168 inputs_number = other_multilayer_perceptron.inputs_number;
00169 hidden_layers_size = other_multilayer_perceptron.hidden_layers_size;
00170 outputs_number = other_multilayer_perceptron.outputs_number;
00171
00172 set_network_architecture(inputs_number, hidden_layers_size, outputs_number);
00173
00174 hidden_layers_activation_function = other_multilayer_perceptron.hidden_layers_activation_function;
00175 output_layer_activation_function = other_multilayer_perceptron.output_layer_activation_function;
00176
00177 hidden_layers = other_multilayer_perceptron.hidden_layers;
00178 output_layer = other_multilayer_perceptron.output_layer;
00179
00180
00181
00182 inputs_scaling_method = other_multilayer_perceptron.inputs_scaling_method;
00183 outputs_unscaling_method = other_multilayer_perceptron.outputs_unscaling_method;
00184
00185
00186
00187 input_variables_name = other_multilayer_perceptron.input_variables_name;
00188 output_variables_name = other_multilayer_perceptron.output_variables_name;
00189
00190 input_variables_units = other_multilayer_perceptron.input_variables_units;
00191 output_variables_units = other_multilayer_perceptron.output_variables_units;
00192
00193 input_variables_description = other_multilayer_perceptron.input_variables_description;
00194 output_variables_description = other_multilayer_perceptron.output_variables_description;
00195
00196
00197
00198 input_variables_mean = other_multilayer_perceptron.input_variables_mean;
00199 output_variables_mean = other_multilayer_perceptron.output_variables_mean;
00200
00201 input_variables_standard_deviation = other_multilayer_perceptron.input_variables_standard_deviation;
00202 output_variables_standard_deviation = other_multilayer_perceptron.output_variables_standard_deviation;
00203
00204 input_variables_minimum = other_multilayer_perceptron.input_variables_minimum;
00205 output_variables_minimum = other_multilayer_perceptron.output_variables_minimum;
00206
00207 input_variables_maximum = other_multilayer_perceptron.input_variables_maximum;
00208 output_variables_maximum = other_multilayer_perceptron.output_variables_maximum;
00209
00210 output_variables_lower_bound = other_multilayer_perceptron.output_variables_lower_bound;
00211 output_variables_upper_bound = other_multilayer_perceptron.output_variables_upper_bound;
00212
00213
00214
00215 independent_parameters = other_multilayer_perceptron.independent_parameters;
00216
00217
00218
00219 independent_parameters_scaling_method
00220 = other_multilayer_perceptron.independent_parameters_scaling_method;
00221
00222
00223
00224 independent_parameters_name = other_multilayer_perceptron.independent_parameters_name;
00225 independent_parameters_units = other_multilayer_perceptron.independent_parameters_units;
00226 independent_parameters_description = other_multilayer_perceptron.independent_parameters_description;
00227
00228
00229
00230 independent_parameters_mean = other_multilayer_perceptron.independent_parameters_mean;
00231 independent_parameters_standard_deviation = other_multilayer_perceptron.independent_parameters_standard_deviation;
00232
00233 independent_parameters_minimum = other_multilayer_perceptron.independent_parameters_minimum;
00234 independent_parameters_maximum = other_multilayer_perceptron.independent_parameters_maximum;
00235
00236 independent_parameters_lower_bound = other_multilayer_perceptron.independent_parameters_lower_bound;
00237 independent_parameters_upper_bound = other_multilayer_perceptron.independent_parameters_upper_bound;
00238
00239
00240
00241 numerical_differentiation_method = other_multilayer_perceptron.numerical_differentiation_method;
00242 numerical_epsilon_method = other_multilayer_perceptron.numerical_epsilon_method;
00243 numerical_epsilon = other_multilayer_perceptron.numerical_epsilon;
00244
00245
00246
00247 display = other_multilayer_perceptron.display;
00248 display_range_out_warning = other_multilayer_perceptron.display_range_out_warning;
00249 }
00250
00251
00252
00253
00255
00256 MultilayerPerceptron::~MultilayerPerceptron(void)
00257 {
00258 }
00259
00260
00261
00262
00265
00266 MultilayerPerceptron& MultilayerPerceptron::operator = (const MultilayerPerceptron& other_multilayer_perceptron)
00267 {
00268 if(this != &other_multilayer_perceptron)
00269 {
00270
00271
00272 inputs_number = other_multilayer_perceptron.inputs_number;
00273 hidden_layers_size = other_multilayer_perceptron.hidden_layers_size;
00274 outputs_number = other_multilayer_perceptron.outputs_number;
00275
00276 set_network_architecture(inputs_number, hidden_layers_size, outputs_number);
00277
00278 hidden_layers = other_multilayer_perceptron.hidden_layers;
00279 output_layer = other_multilayer_perceptron.output_layer;
00280
00281 hidden_layers_activation_function = other_multilayer_perceptron.hidden_layers_activation_function;
00282 output_layer_activation_function = other_multilayer_perceptron.output_layer_activation_function;
00283
00284
00285
00286 inputs_scaling_method = other_multilayer_perceptron.inputs_scaling_method;
00287 outputs_unscaling_method = other_multilayer_perceptron.outputs_unscaling_method;
00288
00289
00290
00291 input_variables_name = other_multilayer_perceptron.input_variables_name;
00292 output_variables_name = other_multilayer_perceptron.output_variables_name;
00293
00294 input_variables_units = other_multilayer_perceptron.input_variables_units;
00295 output_variables_units = other_multilayer_perceptron.output_variables_units;
00296
00297 input_variables_description = other_multilayer_perceptron.input_variables_description;
00298 output_variables_description = other_multilayer_perceptron.output_variables_description;
00299
00300
00301
00302 input_variables_mean = other_multilayer_perceptron.input_variables_mean;
00303 output_variables_mean = other_multilayer_perceptron.output_variables_mean;
00304
00305 input_variables_standard_deviation = other_multilayer_perceptron.input_variables_standard_deviation;
00306 output_variables_standard_deviation = other_multilayer_perceptron.output_variables_standard_deviation;
00307
00308 input_variables_minimum = other_multilayer_perceptron.input_variables_minimum;
00309 output_variables_minimum = other_multilayer_perceptron.output_variables_minimum;
00310
00311 input_variables_maximum = other_multilayer_perceptron.input_variables_maximum;
00312 output_variables_maximum = other_multilayer_perceptron.output_variables_maximum;
00313
00314 output_variables_lower_bound = other_multilayer_perceptron.output_variables_lower_bound;
00315 output_variables_upper_bound = other_multilayer_perceptron.output_variables_upper_bound;
00316
00317
00318
00319 independent_parameters = other_multilayer_perceptron.independent_parameters;
00320
00321
00322
00323 independent_parameters_scaling_method
00324 = other_multilayer_perceptron.independent_parameters_scaling_method;
00325
00326
00327
00328 independent_parameters_name = other_multilayer_perceptron.independent_parameters_name;
00329 independent_parameters_units = other_multilayer_perceptron.independent_parameters_units;
00330 independent_parameters_description = other_multilayer_perceptron.independent_parameters_description;
00331
00332
00333
00334 independent_parameters_mean = other_multilayer_perceptron.independent_parameters_mean;
00335 independent_parameters_standard_deviation = other_multilayer_perceptron.independent_parameters_standard_deviation;
00336
00337 independent_parameters_minimum = other_multilayer_perceptron.independent_parameters_minimum;
00338 independent_parameters_maximum = other_multilayer_perceptron.independent_parameters_maximum;
00339
00340 independent_parameters_lower_bound = other_multilayer_perceptron.independent_parameters_lower_bound;
00341 independent_parameters_upper_bound = other_multilayer_perceptron.independent_parameters_upper_bound;
00342
00343
00344
00345 numerical_differentiation_method = other_multilayer_perceptron.numerical_differentiation_method;
00346 numerical_epsilon_method = other_multilayer_perceptron.numerical_epsilon_method;
00347 numerical_epsilon = other_multilayer_perceptron.numerical_epsilon;
00348
00349
00350
00351 display = other_multilayer_perceptron.display;
00352 display_range_out_warning = other_multilayer_perceptron.display_range_out_warning;
00353 }
00354
00355 return(*this);
00356 }
00357
00358
00359
00360
00361
00362
00366
00367 Vector <Vector<Perceptron> >& MultilayerPerceptron::get_hidden_layers(void)
00368 {
00369 return(hidden_layers);
00370 }
00371
00372
00373
00374
00377
00378 Vector<Perceptron>& MultilayerPerceptron::get_hidden_layer(int i)
00379 {
00380
00381
00382 #ifdef _DEBUG
00383
00384 int hidden_layers_number = get_hidden_layers_number();
00385
00386 if(i < 0 || i >= hidden_layers_number)
00387 {
00388 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
00389 << "Vector<Perceptron>& get_hidden_layer(int) method." << std::endl
00390 << "Index of hidden layer must be equal or greater than zero and less than number of hidden layers."
00391 << std::endl;
00392
00393 exit(1);
00394 }
00395
00396 #endif
00397
00398 return(hidden_layers[i]);
00399 }
00400
00401
00402
00403
00405
00406 Vector<Perceptron>& MultilayerPerceptron::get_output_layer(void)
00407 {
00408 return(output_layer);
00409 }
00410
00411
00412
00413
00416
00417 int MultilayerPerceptron::get_hidden_layer_inputs_number(int i)
00418 {
00419 #ifdef _DEBUG
00420
00421 int hidden_layers_number = get_hidden_layers_number();
00422
00423 if(i < 0 || i >= hidden_layers_number)
00424 {
00425 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
00426 << "int get_hidden_layer_inputs_number(int) method." << std::endl
00427 << "Index of hidden layer must be equal or greater than zero and less than number of hidden layers."
00428 << std::endl;
00429
00430 exit(1);
00431 }
00432
00433 #endif
00434
00435 int hidden_layer_inputs_number;
00436
00437 if(i == 0)
00438 {
00439 hidden_layer_inputs_number = inputs_number;
00440 }
00441 else
00442 {
00443 hidden_layer_inputs_number = hidden_layers_size[i-1];
00444 }
00445
00446 return(hidden_layer_inputs_number);
00447 }
00448
00449
00450
00451
00453
00454 int MultilayerPerceptron::get_output_layer_inputs_number(void)
00455 {
00456 int hidden_layers_number = get_hidden_layers_number();
00457
00458 int output_layer_outputs_number = hidden_layers_size[hidden_layers_number-1];
00459
00460 return(output_layer_outputs_number);
00461 }
00462
00463
00464
00465
00467
00468 int MultilayerPerceptron::get_hidden_layers_parameters_number(void)
00469 {
00470 int hidden_layers_parameters_number = 0;
00471
00472 int hidden_layers_number = get_hidden_layers_number();
00473
00474 for(int i = 0; i < hidden_layers_number; i++)
00475 {
00476 for(int j = 0; j < hidden_layers_size[i]; j++)
00477 {
00478 hidden_layers_parameters_number += hidden_layers[i][j].get_parameters_number();
00479 }
00480 }
00481
00482 return(hidden_layers_parameters_number);
00483 }
00484
00485
00486
00487
00490
00491 int MultilayerPerceptron::get_hidden_layer_parameters_number(int i)
00492 {
00493
00494
00495 #ifdef _DEBUG
00496
00497 int hidden_layers_number = get_hidden_layers_number();
00498
00499 if(i < 0 || i >= hidden_layers_number)
00500 {
00501 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
00502 << "int get_hidden_layer_parameters_number(int) method." << std::endl
00503 << "Index of hidden layer must be equal or greater than zero and less than number of hidden layers."
00504 << std::endl;
00505
00506 exit(1);
00507 }
00508
00509 #endif
00510
00511 int hidden_layer_parameters_number = 0;
00512
00513 for(int j = 0; j < hidden_layers_size[i]; j++)
00514 {
00515 hidden_layer_parameters_number += hidden_layers[i][j].get_parameters_number();
00516 }
00517
00518 return(hidden_layer_parameters_number);
00519 }
00520
00521
00522
00523
00525
00526 int MultilayerPerceptron::get_output_layer_parameters_number(void)
00527 {
00528 int output_layer_parameters_number = 0;
00529
00530 for(int i = 0; i < outputs_number; i++)
00531 {
00532 output_layer_parameters_number += output_layer[i].get_parameters_number();
00533 }
00534
00535 return(output_layer_parameters_number);
00536 }
00537
00538
00539
00540
00542
00543 int MultilayerPerceptron::get_neural_parameters_number(void)
00544 {
00545 int hidden_layers_parameters_number = get_hidden_layers_parameters_number();
00546
00547 int output_layer_parameters_number = get_output_layer_parameters_number();
00548
00549 int neural_parameters_number = hidden_layers_parameters_number + output_layer_parameters_number;
00550
00551 return(neural_parameters_number);
00552 }
00553
00554
00555
00556
00561
00562 Vector< Vector<double> > MultilayerPerceptron::get_hidden_layers_biases(void)
00563 {
00564 int hidden_layers_number = get_hidden_layers_number();
00565
00566 Vector< Vector<double> > hidden_layers_biases(hidden_layers_number);
00567
00568 for(int i = 0; i < hidden_layers_number; i++)
00569 {
00570 hidden_layers_biases[i] = get_hidden_layer_biases(i);
00571 }
00572
00573 return(hidden_layers_biases);
00574 }
00575
00576
00577
00578
00583
00584 Vector<double> MultilayerPerceptron::get_hidden_layer_biases(int i)
00585 {
00586
00587
00588 #ifdef _DEBUG
00589
00590 int hidden_layers_number = get_hidden_layers_number();
00591
00592 if(i < 0 || i >= hidden_layers_number)
00593 {
00594 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
00595 << "Vector<double> get_hidden_layer_biases(int) method." << std::endl
00596 << "Index of hidden layer must be equal or greater than zero and less than number of hidden layers."
00597 << std::endl;
00598
00599 exit(1);
00600 }
00601
00602 #endif
00603
00604 int hidden_layer_size = hidden_layers_size[i];
00605
00606 Vector<double> hidden_layer_biases(hidden_layer_size);
00607
00608 for(int i = 0; i < hidden_layer_size; i++)
00609 {
00610 hidden_layer_biases[i] = hidden_layers[i][i].get_bias();
00611 }
00612
00613 return(hidden_layer_biases);
00614 }
00615
00616
00617
00618
00624
00625 Vector< Matrix<double> > MultilayerPerceptron::get_hidden_layers_synaptic_weights(void)
00626 {
00627 int hidden_layers_number = get_hidden_layers_number();
00628
00629 Vector< Matrix<double> > hidden_layers_synaptic_weights(hidden_layers_number);
00630
00631 for(int i = 0; i < hidden_layers_number; i++)
00632 {
00633 hidden_layers_synaptic_weights[i] = get_hidden_layer_synaptic_weights(i);
00634 }
00635
00636 return(hidden_layers_synaptic_weights);
00637 }
00638
00639
00640
00641
00647
00648 Matrix<double> MultilayerPerceptron::get_hidden_layer_synaptic_weights(int i)
00649 {
00650
00651
00652 #ifdef _DEBUG
00653
00654 int hidden_layers_number = get_hidden_layers_number();
00655
00656 if(i < 0 || i >= hidden_layers_number)
00657 {
00658 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
00659 << "Matrix<double> get_hidden_layer_synaptic_weights(int) method." << std::endl
00660 << "Index of hidden layer must be equal or greater than zero and less than number of hidden layers."
00661 << std::endl;
00662
00663 exit(1);
00664 }
00665
00666 #endif
00667
00668 int hidden_layer_size = hidden_layers_size[i];
00669
00670 int hidden_layer_inputs_number = get_hidden_layer_inputs_number(i);
00671
00672 Matrix<double> hidden_layer_synaptic_weights(hidden_layer_size, hidden_layer_inputs_number);
00673
00674 Vector<double> synaptic_weights(hidden_layer_inputs_number);
00675
00676 for(int i = 0; i < hidden_layer_size; i++)
00677 {
00678 synaptic_weights = hidden_layers[i][i].get_synaptic_weights();
00679
00680 hidden_layer_synaptic_weights.set_row(i, synaptic_weights);
00681 }
00682
00683 return(hidden_layer_synaptic_weights);
00684 }
00685
00686
00687
00688
00695
00696 Vector< Matrix<double> > MultilayerPerceptron::get_hidden_layers_parameters(void)
00697 {
00698 int hidden_layers_number = get_hidden_layers_number();
00699
00700 Vector< Matrix<double> > hidden_layers_parameters(hidden_layers_number);
00701
00702 for(int i = 0; i < hidden_layers_number; i++)
00703 {
00704 hidden_layers_parameters[i] = get_hidden_layer_parameters(i);
00705 }
00706
00707 return(hidden_layers_parameters);
00708 }
00709
00710
00711
00712
00718
00719 Matrix<double> MultilayerPerceptron::get_hidden_layer_parameters(int i)
00720 {
00721
00722
00723 #ifdef _DEBUG
00724
00725 int hidden_layers_number = get_hidden_layers_number();
00726
00727 if(i < 0 || i >= hidden_layers_number)
00728 {
00729 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
00730 << "Matrix<double> get_hidden_layer_parameters(int) method." << std::endl
00731 << "Index of hidden layer must be equal or greater than zero and less than number of hidden layers."
00732 << std::endl;
00733
00734 exit(1);
00735 }
00736
00737 #endif
00738
00739 int hidden_layer_size = hidden_layers_size[i];
00740
00741 int hidden_layer_parameters_number = get_hidden_layer_inputs_number(i) + 1;
00742
00743 Matrix<double> hidden_layer_parameters(hidden_layer_size, hidden_layer_parameters_number);
00744
00745 Vector<double> neuron_parameters(hidden_layer_parameters_number);
00746
00747 for(int i = 0; i < hidden_layer_size; i++)
00748 {
00749 neuron_parameters = hidden_layers[i][i].get_parameters();
00750
00751 hidden_layer_parameters.set_row(i, neuron_parameters);
00752 }
00753
00754 return(hidden_layer_parameters);
00755 }
00756
00757
00758
00759
00763
00764 Vector<double> MultilayerPerceptron::get_output_layer_biases(void)
00765 {
00766 Vector<double> outputLayerBiases(outputs_number);
00767
00768 for(int i = 0; i < outputs_number; i++)
00769 {
00770 outputLayerBiases[i] = output_layer[i].get_bias();
00771 }
00772
00773 return(outputLayerBiases);
00774 }
00775
00776
00777
00778
00783
00784 Matrix<double> MultilayerPerceptron::get_output_layer_synaptic_weights(void)
00785 {
00786 int hidden_layers_number = hidden_layers.get_size();
00787
00788 int output_layer_inputs_number = hidden_layers_size[hidden_layers_number-1];
00789
00790 Matrix<double> output_layer_synaptic_weights(outputs_number, output_layer_inputs_number);
00791
00792 Vector<double> synaptic_weights(output_layer_inputs_number);
00793
00794 for(int i = 0; i < outputs_number; i++)
00795 {
00796 synaptic_weights = output_layer[i].get_synaptic_weights();
00797
00798 output_layer_synaptic_weights.set_row(i, synaptic_weights);
00799 }
00800
00801 return(output_layer_synaptic_weights);
00802 }
00803
00804
00805
00806
00811
00812 Matrix<double> MultilayerPerceptron::get_output_layer_parameters(void)
00813 {
00814 int output_layer_inputs_number = get_output_layer_inputs_number();
00815
00816 Matrix<double> output_layer_parameters(outputs_number, 1+output_layer_inputs_number);
00817
00818 Vector<double> neuron_parameters(1+output_layer_inputs_number);
00819
00820 for(int i = 0; i < outputs_number; i++)
00821 {
00822 neuron_parameters = output_layer[i].get_parameters();
00823
00824 output_layer_parameters.set_row(i, neuron_parameters);
00825 }
00826
00827 return(output_layer_parameters);
00828 }
00829
00830
00831
00832
00834
00835 Vector<double> MultilayerPerceptron::get_neural_parameters(void)
00836 {
00837 int hidden_layers_number = get_hidden_layers_number();
00838
00839 int neural_parameters_number = get_neural_parameters_number();
00840
00841 Vector<double> neural_parameters(neural_parameters_number);
00842
00843 double bias;
00844 int synaptic_weights_number;
00845
00846
00847
00848 int position = 0;
00849
00850 for(int i = 0; i < hidden_layers_number; i++)
00851 {
00852 for(int j = 0; j < hidden_layers_size[i]; j++)
00853 {
00854
00855
00856 bias = hidden_layers[i][j].get_bias();
00857 neural_parameters[position] = bias;
00858 position++;
00859
00860
00861
00862 Vector<double>& synaptic_weights = hidden_layers[i][j].get_synaptic_weights();
00863 synaptic_weights_number = hidden_layers[i][j].get_inputs_number();
00864 neural_parameters.insert(position, synaptic_weights);
00865 position += synaptic_weights_number;
00866 }
00867 }
00868
00869
00870
00871 for(int i = 0; i < outputs_number; i++)
00872 {
00873
00874
00875 bias = output_layer[i].get_bias();
00876 neural_parameters[position] = bias;
00877 position++;
00878
00879
00880
00881 Vector<double>& synaptic_weights = output_layer[i].get_synaptic_weights();
00882 synaptic_weights_number = output_layer[i].get_inputs_number();
00883 neural_parameters.insert(position, synaptic_weights);
00884 position += synaptic_weights_number;
00885 }
00886
00887 return(neural_parameters);
00888 }
00889
00890
00891
00892
00896
00897 int MultilayerPerceptron::get_hidden_layer_bias_index(int hidden_layer_index, int hidden_neuron_index)
00898 {
00899
00900
00901 #ifdef _DEBUG
00902
00903 if(hidden_layer_index < 0)
00904 {
00905 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
00906 << "int get_hidden_layer_bias_index(int, int) method." << std::endl
00907 << "Index of hidden layer must be equal or greater than zero." << std::endl;
00908
00909 exit(1);
00910 }
00911
00912 int hidden_layers_number = get_hidden_layers_number();
00913
00914 if(hidden_layer_index >= hidden_layers_number)
00915 {
00916 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
00917 << "int get_hidden_layer_bias_index(int, int) method." << std::endl
00918 << "Index of hidden layer must be less than number of hidden layers." << std::endl;
00919
00920 exit(1);
00921 }
00922
00923 if(hidden_neuron_index < 0)
00924 {
00925 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
00926 << "int get_hidden_layer_bias_index(int, int) method." << std::endl
00927 << "Index of hidden neuron must be equal or greater than zero." << std::endl;
00928
00929 exit(1);
00930 }
00931
00932 int hidden_layer_size = hidden_layers_size[hidden_layer_index];
00933
00934 if(hidden_neuron_index >= hidden_layer_size)
00935 {
00936 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
00937 << "int get_hidden_layer_bias_index(int, int) method." << std::endl
00938 << "Index of hidden neuron must be less than size of hidden layer." << std::endl;
00939
00940 exit(1);
00941 }
00942
00943 #endif
00944
00945 int hidden_layer_bias_index = 0;
00946
00947
00948
00949 for(int i = 0; i < hidden_layer_index; i++)
00950 {
00951 hidden_layer_bias_index += get_hidden_layer_parameters_number(i);
00952 }
00953
00954
00955
00956 for(int j = 0; j < hidden_neuron_index; j++)
00957 {
00958 hidden_layer_bias_index += hidden_layers[hidden_layer_index][j].get_parameters_number();
00959 }
00960
00961 return(hidden_layer_bias_index);
00962 }
00963
00964
00965
00966
00971
00972 int MultilayerPerceptron::get_hidden_layer_synaptic_weight_index
00973 (int hidden_layer_index, int hidden_neuron_index, int hidden_neuron_input_index)
00974 {
00975
00976
00977 #ifdef _DEBUG
00978
00979 int hidden_layers_number = get_hidden_layers_number();
00980
00981 if(hidden_layer_index < 0 || hidden_layer_index >= hidden_layers_number)
00982 {
00983 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
00984 << "int get_hidden_layer_bias_index(int, int) method." << std::endl
00985 << "Index of hidden layer must be equal or greater than zero and less than number of hidden layers."
00986 << std::endl;
00987
00988 exit(1);
00989 }
00990
00991 if(hidden_neuron_index < 0 || hidden_neuron_index >= hidden_layers_size[hidden_layer_index])
00992 {
00993 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
00994 << "int get_hidden_layer_bias_index(int, int) method." << std::endl
00995 << "Index of hidden neuron must be equal or greater than zero and less than size of hidden layer."
00996 << std::endl;
00997
00998 exit(1);
00999 }
01000
01001 int hidden_layer_inputs_number = get_hidden_layer_inputs_number(hidden_layer_index);
01002
01003 if(hidden_neuron_input_index < 0 || hidden_neuron_input_index >= hidden_layer_inputs_number)
01004 {
01005 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01006 << "int get_hidden_layer_bias_index(int, int) method." << std::endl
01007 << "Index of hidden neuron must be equal or greater than zero and less than size of hidden layer."
01008 << std::endl;
01009
01010 exit(1);
01011 }
01012
01013 #endif
01014
01015 int hidden_layer_synaptic_weight_index = 0;
01016
01017
01018
01019 for(int i = 0; i < hidden_layer_index-1; i++)
01020 {
01021 hidden_layer_synaptic_weight_index += get_hidden_layer_parameters_number(hidden_layer_index);
01022 }
01023
01024
01025
01026 for(int i = 0; i < hidden_neuron_index-1; i++)
01027 {
01028 hidden_layer_synaptic_weight_index += hidden_layers[hidden_layer_index][i].get_parameters_number();
01029 }
01030
01031
01032
01033 hidden_layer_synaptic_weight_index += 1;
01034
01035
01036
01037 hidden_layer_synaptic_weight_index += hidden_neuron_input_index;
01038
01039 return(hidden_layer_synaptic_weight_index);
01040 }
01041
01042
01043
01044
01047
01048 int MultilayerPerceptron::get_output_layer_bias_index(int output_neuron_index)
01049 {
01050 int output_layer_bias_index = 0;
01051
01052
01053
01054 output_layer_bias_index += get_hidden_layers_parameters_number();
01055
01056
01057
01058 for(int i = 0; i < output_neuron_index-1; i++)
01059 {
01060 output_layer_bias_index += output_layer[i].get_parameters_number();
01061 }
01062
01063
01064
01065 output_layer_bias_index += 1;
01066
01067 return(output_layer_bias_index);
01068 }
01069
01070
01071
01072
01076
01077 int MultilayerPerceptron::
01078 get_output_layer_synaptic_weight_index(int output_neuron_index, int output_neuron_input_index)
01079 {
01080 int output_layer_synaptic_weight_index = 0;
01081
01082
01083
01084 output_layer_synaptic_weight_index += get_hidden_layers_parameters_number();
01085
01086
01087
01088 for(int i = 0; i < output_neuron_index-1; i++)
01089 {
01090 output_layer_synaptic_weight_index += output_layer[i].get_parameters_number();
01091 }
01092
01093
01094
01095 output_layer_synaptic_weight_index += 1;
01096
01097
01098
01099 output_layer_synaptic_weight_index += output_neuron_input_index;
01100
01101 return(output_layer_synaptic_weight_index);
01102 }
01103
01104
01105
01106
01108
01109 Vector<MultilayerPerceptron::LayerActivationFunction>&
01110 MultilayerPerceptron::get_hidden_layers_activation_function(void)
01111 {
01112 return(hidden_layers_activation_function);
01113 }
01114
01115
01116
01117
01120
01121 MultilayerPerceptron::LayerActivationFunction&
01122 MultilayerPerceptron::get_hidden_layer_activation_function(int i)
01123 {
01124
01125
01126 #ifdef _DEBUG
01127
01128 int hidden_layers_number = get_hidden_layers_number();
01129
01130 if(i < 0 || i >= hidden_layers_number)
01131 {
01132 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01133 << "LayerActivationFunction& get_hidden_layer_activation_function(int) method." << std::endl
01134 << "Index of hidden layer must be equal or greater than zero and less than number of hidden layers."
01135 << std::endl;
01136
01137 exit(1);
01138 }
01139
01140 #endif
01141
01142 return(hidden_layers_activation_function[i]);
01143 }
01144
01145
01146
01147
01149
01150 MultilayerPerceptron::LayerActivationFunction& MultilayerPerceptron::get_output_layer_activation_function(void)
01151 {
01152 return(output_layer_activation_function);
01153 }
01154
01155
01156
01157
01160
01161 Vector<std::string> MultilayerPerceptron::get_hidden_layers_activation_function_name(void)
01162 {
01163 int hidden_layers_number = get_hidden_layers_number();
01164
01165 Vector<std::string> hidden_layers_activation_function_name(hidden_layers_number);
01166
01167 for(int i = 0; i < hidden_layers_number; i++)
01168 {
01169 hidden_layers_activation_function_name[i] = get_hidden_layer_activation_function_name(i);
01170 }
01171
01172 return(hidden_layers_activation_function_name);
01173 }
01174
01175
01176
01177
01180
01181 std::string MultilayerPerceptron::get_hidden_layer_activation_function_name(int hidden_layer_index)
01182 {
01183 switch(hidden_layers_activation_function[hidden_layer_index])
01184 {
01185 case Logistic:
01186 {
01187 return("Logistic");
01188 }
01189 break;
01190
01191 case HyperbolicTangent:
01192 {
01193 return("HyperbolicTangent");
01194 }
01195 break;
01196
01197 case Threshold:
01198 {
01199 return("Threshold");
01200 }
01201 break;
01202
01203 case SymmetricThreshold:
01204 {
01205 return("SymmetricThreshold");
01206 }
01207 break;
01208
01209 case Linear:
01210 {
01211 return("Linear");
01212 }
01213 break;
01214
01215 default:
01216 {
01217 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01218 << "std::string get_hidden_layer_activation_function(int) method." << std::endl
01219 << "Unknown hidden layer activation function." << std::endl;
01220
01221 exit(1);
01222 }
01223 break;
01224 }
01225 }
01226
01227
01228
01229
01231
01232 std::string MultilayerPerceptron::get_output_layer_activation_function_name(void)
01233 {
01234 switch(output_layer_activation_function)
01235 {
01236 case Logistic:
01237 {
01238 return("Logistic");
01239 }
01240 break;
01241
01242 case HyperbolicTangent:
01243 {
01244 return("HyperbolicTangent");
01245 }
01246 break;
01247
01248 case Threshold:
01249 {
01250 return("Threshold");
01251 }
01252 break;
01253
01254 case SymmetricThreshold:
01255 {
01256 return("SymmetricThreshold");
01257 }
01258 break;
01259
01260 case Linear:
01261 {
01262 return("Linear");
01263 }
01264 break;
01265
01266 default:
01267 {
01268 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01269 << "std::string& get_output_layer_activation_function(void) method." << std::endl
01270 << "Unknown output layer activation function." << std::endl;
01271
01272 exit(1);
01273 }
01274 break;
01275 }
01276 }
01277
01278
01279
01280
01282
01283 MultilayerPerceptron::ScalingMethod& MultilayerPerceptron::get_inputs_scaling_method(void)
01284 {
01285 return(inputs_scaling_method);
01286 }
01287
01288
01289
01290
01292
01293 std::string MultilayerPerceptron::get_inputs_scaling_method_name(void)
01294 {
01295 if(inputs_scaling_method == None)
01296 {
01297 return("None");
01298 }
01299 else if(inputs_scaling_method == MeanStandardDeviation)
01300 {
01301 return("MeanStandardDeviation");
01302 }
01303 else if(inputs_scaling_method == MinimumMaximum)
01304 {
01305 return("MinimumMaximum");
01306 }
01307 else
01308 {
01309 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01310 << "std::string get_inputs_scaling_method(void) method." << std::endl
01311 << "Unknown inputs scaling method." << std::endl;
01312
01313 exit(1);
01314 }
01315 }
01316
01317
01318
01319
01321
01322 MultilayerPerceptron::ScalingMethod& MultilayerPerceptron::get_outputs_unscaling_method(void)
01323 {
01324 return(outputs_unscaling_method);
01325 }
01326
01327
01328
01329
01331
01332 std::string MultilayerPerceptron::get_outputs_unscaling_method_name(void)
01333 {
01334 if(outputs_unscaling_method == None)
01335 {
01336 return("None");
01337 }
01338 else if(outputs_unscaling_method == MeanStandardDeviation)
01339 {
01340 return("MeanStandardDeviation");
01341 }
01342 else if(outputs_unscaling_method == MinimumMaximum)
01343 {
01344 return("MinimumMaximum");
01345 }
01346 else
01347 {
01348 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01349 << "std::string get_outputs_unscaling_method(void) method." << std::endl
01350 << "Unknown outputs unscaling method." << std::endl;
01351
01352 exit(1);
01353 }
01354 }
01355
01356
01357
01358
01361
01362 Vector<std::string>& MultilayerPerceptron::get_input_variables_name(void)
01363 {
01364 return(input_variables_name);
01365 }
01366
01367
01368
01369
01373
01374 std::string& MultilayerPerceptron::get_input_variable_name(int input_variable_index)
01375 {
01376
01377
01378 #ifdef _DEBUG
01379
01380 if(input_variable_index < 0)
01381 {
01382 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01383 << "std::string get_input_variable_name(int) method." << std::endl
01384 << "Input variable index must be equal or greater than zero." << std::endl;
01385
01386 exit(1);
01387 }
01388 else if(input_variable_index >= inputs_number)
01389 {
01390 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01391 << "std::string get_input_variable_name(int) method." << std::endl
01392 << "Input variable index must be less than number of inputs." << std::endl;
01393
01394 exit(1);
01395 }
01396
01397 if(input_variables_name.get_size() == 0)
01398 {
01399 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01400 << "std::string get_input_variable_name(int) method." << std::endl
01401 << "Input variables size is zero." << std::endl;
01402
01403 exit(1);
01404 }
01405
01406 #endif
01407
01408 return(input_variables_name[input_variable_index]);
01409 }
01410
01411
01412
01413
01416
01417 Vector<std::string>& MultilayerPerceptron::get_output_variables_name(void)
01418 {
01419 return(output_variables_name);
01420 }
01421
01422
01423
01424
01428
01429 std::string& MultilayerPerceptron::get_output_variable_name(int output_variable_index)
01430 {
01431
01432
01433 #ifdef _DEBUG
01434
01435 if(output_variable_index < 0 || output_variable_index >= outputs_number)
01436 {
01437 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01438 << "std::string get_output_variable_name(int) method." << std::endl
01439 << "Output variable index must be equal or greater than zero and less than number of outputs."
01440 << std::endl;
01441
01442 exit(1);
01443 }
01444
01445 #endif
01446
01447 return(output_variables_name[output_variable_index]);
01448 }
01449
01450
01451
01452
01455
01456 Vector<std::string>& MultilayerPerceptron::get_input_variables_units(void)
01457 {
01458 return(input_variables_units);
01459 }
01460
01461
01462
01463
01467
01468 std::string& MultilayerPerceptron::get_input_variable_units(int input_variable_index)
01469 {
01470
01471
01472 #ifdef _DEBUG
01473
01474 if(input_variable_index < 0 || input_variable_index >= inputs_number)
01475 {
01476 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01477 << "std::string get_input_variable_units(int) method." << std::endl
01478 << "Index of input variable must be equal or greater than zero and less than number of inputs." << std::endl;
01479
01480 exit(1);
01481 }
01482
01483 #endif
01484
01485 return(input_variables_units[input_variable_index]);
01486 }
01487
01488
01489
01490
01493
01494 Vector<std::string>& MultilayerPerceptron::get_output_variables_units(void)
01495 {
01496 return(output_variables_units);
01497 }
01498
01499
01500
01501
01505
01506 std::string& MultilayerPerceptron::get_output_variable_units(int output_variable_index)
01507 {
01508
01509
01510 #ifdef _DEBUG
01511
01512 if(output_variable_index < 0 || output_variable_index >= outputs_number)
01513 {
01514 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01515 << "std::string get_output_variable_units(int) method." << std::endl
01516 << "Index of output variable must be equal or greater than zero and less than number of outputs."
01517 << std::endl;
01518
01519 exit(1);
01520 }
01521
01522 #endif
01523
01524 return(output_variables_units[output_variable_index]);
01525 }
01526
01527
01528
01529
01532
01533 Vector<std::string>& MultilayerPerceptron::get_input_variables_description(void)
01534 {
01535 return(input_variables_description);
01536 }
01537
01538
01539
01540
01544
01545 std::string& MultilayerPerceptron::get_input_variable_description(int input_variable_index)
01546 {
01547
01548
01549 #ifdef _DEBUG
01550
01551 if(input_variable_index < 0 || input_variable_index >= inputs_number)
01552 {
01553 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01554 << "std::string get_input_variable_description(int) method." << std::endl
01555 << "Index of input variable must be equal or greater than zero and less than number of inputs."
01556 << std::endl;
01557
01558 exit(1);
01559 }
01560
01561 #endif
01562
01563 return(input_variables_description[input_variable_index]);
01564 }
01565
01566
01567
01568
01571
01572 Vector<std::string>& MultilayerPerceptron::get_output_variables_description(void)
01573 {
01574 return(output_variables_description);
01575 }
01576
01577
01578
01579
01582
01583
01585
01586 std::string& MultilayerPerceptron::get_output_variable_description(int output_variable_index)
01587 {
01588
01589
01590 #ifdef _DEBUG
01591
01592 if(output_variable_index < 0 || output_variable_index >= outputs_number)
01593 {
01594 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01595 << "std::string get_output_variable_description(int) method." << std::endl
01596 << "Index of output variable must be less than number of outputs."
01597 << std::endl;
01598
01599 exit(1);
01600 }
01601
01602 #endif
01603
01604 return(output_variables_description[output_variable_index]);
01605 }
01606
01607
01608
01609
01620
01621 Vector< Vector<std::string>* > MultilayerPerceptron::get_variables_information(void)
01622 {
01623 Vector< Vector<std::string>* > variables_information(6);
01624
01625 variables_information[0] = &input_variables_name;
01626 variables_information[1] = &output_variables_name;
01627
01628 variables_information[2] = &input_variables_units;
01629 variables_information[3] = &output_variables_units;
01630
01631 variables_information[4] = &input_variables_description;
01632 variables_information[5] = &output_variables_description;
01633
01634 return(variables_information);
01635 }
01636
01637
01638
01639
01642
01643 Vector<double>& MultilayerPerceptron::get_input_variables_mean(void)
01644 {
01645 return(input_variables_mean);
01646 }
01647
01648
01649
01650
01654
01655 double MultilayerPerceptron::get_input_variable_mean(int input_variable_index)
01656 {
01657
01658
01659 #ifdef _DEBUG
01660
01661 if(input_variable_index < 0 || input_variable_index >= inputs_number)
01662 {
01663 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01664 << "double get_input_variable_mean(int) method." << std::endl
01665 << "Index of input variable must be equal or greater than zero and less than number of inputs."
01666 << std::endl;
01667
01668 exit(1);
01669 }
01670
01671 #endif
01672
01673 return(input_variables_mean[input_variable_index]);
01674 }
01675
01676
01677
01678
01681
01682 Vector<double>& MultilayerPerceptron::get_output_variables_mean(void)
01683 {
01684 return(output_variables_mean);
01685 }
01686
01687
01688
01689
01694
01695 double MultilayerPerceptron::get_output_variable_mean(int output_variable_index)
01696 {
01697
01698
01699 #ifdef _DEBUG
01700
01701 if(output_variable_index < 0 || output_variable_index >= outputs_number)
01702 {
01703 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01704 << "double get_output_variable_mean(int) method." << std::endl
01705 << "Index must be less than number of outputs." << std::endl;
01706
01707 exit(1);
01708 }
01709
01710 #endif
01711
01712 return(output_variables_mean[output_variable_index]);
01713 }
01714
01715
01716
01717
01720
01721 Vector<double>& MultilayerPerceptron::get_input_variables_standard_deviation(void)
01722 {
01723 return(input_variables_standard_deviation);
01724 }
01725
01726
01727
01728
01732
01733 double MultilayerPerceptron::get_input_variable_standard_deviation(int input_variable_index)
01734 {
01735
01736
01737 #ifdef _DEBUG
01738
01739 if(input_variable_index < 0 || input_variable_index >= inputs_number)
01740 {
01741 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01742 << "double get_input_variable_standard_deviation(int) method." << std::endl
01743 << "Index of input variable must be equal or greater than zero and less than number of inputs."
01744 << std::endl;
01745
01746 exit(1);
01747 }
01748
01749 #endif
01750
01751 return(input_variables_standard_deviation[input_variable_index]);
01752 }
01753
01754
01755
01756
01759
01760 Vector<double>& MultilayerPerceptron::get_output_variables_standard_deviation(void)
01761 {
01762 return(output_variables_standard_deviation);
01763 }
01764
01765
01766
01767
01771
01772 double MultilayerPerceptron::get_output_variable_standard_deviation(int output_variable_index)
01773 {
01774
01775
01776 #ifdef _DEBUG
01777
01778 if(output_variable_index < 0 || output_variable_index >= outputs_number)
01779 {
01780 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01781 << "double get_output_variable_standard_deviation(int) method." << std::endl
01782 << "Index of output variable must be equal or greater than zero and less than number of outputs." << std::endl;
01783
01784 exit(1);
01785 }
01786
01787 #endif
01788
01789 return(output_variables_standard_deviation[output_variable_index]);
01790 }
01791
01792
01793
01794
01800
01801 Vector< Vector<double>* > MultilayerPerceptron::get_input_variables_mean_standard_deviation(void)
01802 {
01803 Vector< Vector<double>* > input_variables_mean_standard_deviation(2);
01804
01805 input_variables_mean_standard_deviation[0] = &input_variables_mean;
01806 input_variables_mean_standard_deviation[1] = &input_variables_standard_deviation;
01807
01808 return(input_variables_mean_standard_deviation);
01809 }
01810
01811
01812
01813
01819
01820 Vector< Vector<double>* > MultilayerPerceptron::get_output_variables_mean_standard_deviation(void)
01821 {
01822 Vector< Vector<double>* > output_variables_mean_standard_deviation(2);
01823
01824 output_variables_mean_standard_deviation[0] = &output_variables_mean;
01825 output_variables_mean_standard_deviation[1] = &output_variables_standard_deviation;
01826
01827 return(output_variables_mean_standard_deviation);
01828 }
01829
01830
01831
01832
01835
01836 Vector<double>& MultilayerPerceptron::get_input_variables_minimum(void)
01837 {
01838 return(input_variables_minimum);
01839 }
01840
01841
01842
01843
01846
01847 double MultilayerPerceptron::get_input_variable_minimum(int i)
01848 {
01849
01850
01851 #ifdef _DEBUG
01852
01853 if(inputs_number == 0)
01854 {
01855 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01856 << "double get_input_variable_minimum(int) method." << std::endl
01857 << "Number of inputs is zero." << std::endl;
01858
01859 exit(1);
01860 }
01861 else if(i >= inputs_number)
01862 {
01863 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01864 << "double get_input_variable_minimum(int) method." << std::endl
01865 << "Index must be less than number of inputs." << std::endl;
01866
01867 exit(1);
01868 }
01869
01870 #endif
01871
01872 return(input_variables_minimum[i]);
01873 }
01874
01875
01876
01877
01880
01881 Vector<double>& MultilayerPerceptron::get_output_variables_minimum(void)
01882 {
01883 return(output_variables_minimum);
01884 }
01885
01886
01887
01888
01891
01892 double MultilayerPerceptron::get_output_variable_minimum(int i)
01893 {
01894
01895
01896 #ifdef _DEBUG
01897
01898 if(outputs_number == 0)
01899 {
01900 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01901 << "double get_output_variable_minimum(int) method." << std::endl
01902 << "Number of outputs is zero." << std::endl;
01903
01904 exit(1);
01905 }
01906 else if(i >= outputs_number)
01907 {
01908 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01909 << "double get_output_variable_minimum(int) method." << std::endl
01910 << "Index must be less than number of outputs." << std::endl;
01911
01912 exit(1);
01913 }
01914
01915 #endif
01916
01917 return(output_variables_minimum[i]);
01918 }
01919
01920
01921
01922
01925
01926 Vector<double>& MultilayerPerceptron::get_input_variables_maximum(void)
01927 {
01928 return(input_variables_maximum);
01929 }
01930
01931
01932
01933
01936
01937 double MultilayerPerceptron::get_input_variable_maximum(int i)
01938 {
01939
01940
01941 #ifdef _DEBUG
01942
01943 if(inputs_number == 0)
01944 {
01945 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01946 << "double get_input_variable_maximum(int) method." << std::endl
01947 << "Number of inputs is zero." << std::endl;
01948
01949 exit(1);
01950 }
01951 else if(i >= inputs_number)
01952 {
01953 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01954 << "double get_input_variable_maximum(int) method." << std::endl
01955 << "Index must be less than number of inputs." << std::endl;
01956
01957 exit(1);
01958 }
01959
01960 #endif
01961
01962 return(input_variables_maximum[i]);
01963 }
01964
01965
01966
01967
01970
01971 Vector<double>& MultilayerPerceptron::get_output_variables_maximum(void)
01972 {
01973 return(output_variables_maximum);
01974 }
01975
01976
01977
01978
01981
01982 double MultilayerPerceptron::get_output_variable_maximum(int i)
01983 {
01984
01985
01986 #ifdef _DEBUG
01987
01988 if(outputs_number == 0)
01989 {
01990 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01991 << "double get_output_variable_maximum(int) method." << std::endl
01992 << "Number of outputs is zero." << std::endl;
01993
01994 exit(1);
01995 }
01996 else if(i >= outputs_number)
01997 {
01998 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
01999 << "double get_output_variable_maximum(int) method." << std::endl
02000 << "Index must be less than number of outputs." << std::endl;
02001
02002 exit(1);
02003 }
02004
02005 #endif
02006
02007 return(output_variables_maximum[i]);
02008 }
02009
02010
02011
02012
02018
02019 Vector< Vector<double>* > MultilayerPerceptron::get_input_variables_minimum_maximum(void)
02020 {
02021 Vector< Vector<double>* > input_variables_minimum_maximum(2);
02022
02023 input_variables_minimum_maximum[0] = &input_variables_minimum;
02024 input_variables_minimum_maximum[1] = &input_variables_maximum;
02025
02026 return(input_variables_minimum_maximum);
02027 }
02028
02029
02030
02031
02037
02038 Vector< Vector<double>* > MultilayerPerceptron::get_output_variables_minimum_maximum(void)
02039 {
02040 Vector< Vector<double>* > output_variables_minimum_maximum(2);
02041
02042 output_variables_minimum_maximum[0] = &output_variables_minimum;
02043 output_variables_minimum_maximum[1] = &output_variables_maximum;
02044
02045 return(output_variables_minimum_maximum);
02046 }
02047
02048
02049
02050
02053
02054 Vector<double>& MultilayerPerceptron::get_output_variables_lower_bound(void)
02055 {
02056 return(output_variables_lower_bound);
02057 }
02058
02059
02060
02061
02064
02065 double MultilayerPerceptron::get_output_variables_lower_bound(int i)
02066 {
02067
02068
02069 #ifdef _DEBUG
02070
02071 if(outputs_number == 0)
02072 {
02073 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02074 << "double get_output_variables_lower_bound(int) method." << std::endl
02075 << "Number of outputs is zero." << std::endl;
02076
02077 exit(1);
02078 }
02079 else if(i >= outputs_number)
02080 {
02081 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02082 << "double get_output_variables_lower_bound(int) method." << std::endl
02083 << "Index must be less than number of output." << std::endl;
02084
02085 exit(1);
02086 }
02087
02088 #endif
02089
02090 return(output_variables_lower_bound[i]);
02091 }
02092
02093
02094
02095
02098
02099 Vector<double>& MultilayerPerceptron::get_output_variables_upper_bound(void)
02100 {
02101 return(output_variables_upper_bound);
02102 }
02103
02104
02105
02106
02109
02110 double MultilayerPerceptron::get_output_variable_upper_bound(int i)
02111 {
02112
02113
02114 #ifdef _DEBUG
02115
02116 if(outputs_number == 0)
02117 {
02118 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02119 << "double get_output_variable_upper_bound(int) method." << std::endl
02120 << "Number of outputs is zero." << std::endl;
02121
02122 exit(1);
02123 }
02124 else if(i >= outputs_number)
02125 {
02126 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02127 << "double get_output_variable_upper_bound(int) method." << std::endl
02128 << "Index must be less than number of outputs." << std::endl;
02129
02130 exit(1);
02131 }
02132
02133 #endif
02134
02135 return(output_variables_upper_bound[i]);
02136 }
02137
02138
02139
02140
02147
02148 Vector< Vector<double>* > MultilayerPerceptron::get_output_variables_bounds(void)
02149 {
02150 Vector< Vector<double>* > output_variables_bounds(2);
02151
02152 output_variables_bounds[0] = &output_variables_lower_bound;
02153 output_variables_bounds[1] = &output_variables_upper_bound;
02154
02155 return(output_variables_bounds);
02156 }
02157
02158
02159
02160
02173
02174 Vector< Vector<double>* > MultilayerPerceptron::get_variables_statistics(void)
02175 {
02176 Vector< Vector<double>* > variables_statistics(8);
02177
02178 variables_statistics[0] = &input_variables_mean;
02179 variables_statistics[1] = &output_variables_mean;
02180
02181 variables_statistics[2] = &input_variables_standard_deviation;
02182 variables_statistics[3] = &output_variables_standard_deviation;
02183
02184 variables_statistics[4] = &input_variables_minimum;
02185 variables_statistics[5] = &output_variables_minimum;
02186
02187 variables_statistics[6] = &input_variables_maximum;
02188 variables_statistics[7] = &output_variables_maximum;
02189
02190 return(variables_statistics);
02191 }
02192
02193
02194
02195
02199
02200 Vector<double>& MultilayerPerceptron::get_independent_parameters(void)
02201 {
02202 return(independent_parameters);
02203 }
02204
02205
02206
02207
02212
02213 double MultilayerPerceptron::get_independent_parameter(int independent_parameter_index)
02214 {
02215
02216
02217 #ifdef _DEBUG
02218
02219 int independent_parameters_number = get_independent_parameters_number();
02220
02221 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
02222 {
02223 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02224 << "double get_independent_parameter(int) method." << std::endl
02225 << "Index of independent parameter must be equal or greater than zero and less than "
02226 << "number of independent parameters." << std::endl;
02227
02228 exit(1);
02229 }
02230
02231 #endif
02232
02233 return(independent_parameters[independent_parameter_index]);
02234 }
02235
02236
02237
02238
02240
02241 MultilayerPerceptron::ScalingMethod& MultilayerPerceptron::get_independent_parameters_scaling_method(void)
02242 {
02243 return(independent_parameters_scaling_method);
02244 }
02245
02246
02247
02248
02250
02251 std::string MultilayerPerceptron::get_independent_parameters_scaling_method_name(void)
02252 {
02253 if(independent_parameters_scaling_method == None)
02254 {
02255 return("None");
02256 }
02257 else if(independent_parameters_scaling_method == MeanStandardDeviation)
02258 {
02259 return("MeanStandardDeviation");
02260 }
02261 else if(independent_parameters_scaling_method == MinimumMaximum)
02262 {
02263 return("MinimumMaximum");
02264 }
02265 else
02266 {
02267 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02268 << "std::string get_independent_parameters_scaling_method(void) method." << std::endl
02269 << "Unknown independent parameters scaling and unscaling method." << std::endl;
02270
02271 exit(1);
02272 }
02273 }
02274
02275
02276
02277
02280
02281 Vector<std::string>& MultilayerPerceptron::get_independent_parameters_name(void)
02282 {
02283 return(independent_parameters_name);
02284 }
02285
02286
02287
02288
02292
02293 std::string& MultilayerPerceptron::get_independent_parameter_name(int independent_parameter_index)
02294 {
02295
02296
02297 #ifdef _DEBUG
02298
02299 int independent_parameters_number = get_independent_parameters_number();
02300
02301 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
02302 {
02303 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02304 << "std::string get_independent_parameters_name(int) method." << std::endl
02305 << "Index of independent parameter must be equal or greater than zero and less than "
02306 << "number of independent parameters." << std::endl;
02307
02308 exit(1);
02309 }
02310
02311 #endif
02312
02313 return(independent_parameters_name[independent_parameter_index]);
02314 }
02315
02316
02317
02318
02321
02322 Vector<std::string>& MultilayerPerceptron::get_independent_parameters_units(void)
02323 {
02324 return(independent_parameters_units);
02325 }
02326
02327
02328
02329
02334
02335 std::string& MultilayerPerceptron::get_independent_parameter_units(int independent_parameter_index)
02336 {
02337
02338
02339 #ifdef _DEBUG
02340
02341 int independent_parameters_number = get_independent_parameters_number();
02342
02343 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
02344 {
02345 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02346 << "std::string get_independent_parameters_units(int) method." << std::endl
02347 << "Index of independent parameter must be equal or greater than zero and less than "
02348 << "number of independent parameters." << std::endl;
02349
02350 exit(1);
02351 }
02352
02353 #endif
02354
02355 return(independent_parameters_units[independent_parameter_index]);
02356 }
02357
02358
02359
02360
02363
02364 Vector<std::string>& MultilayerPerceptron::get_independent_parameters_description(void)
02365 {
02366 return(independent_parameters_description);
02367 }
02368
02369
02370
02371
02376
02377 std::string& MultilayerPerceptron::get_independent_parameter_description(int independent_parameter_index)
02378 {
02379
02380
02381 #ifdef _DEBUG
02382
02383 int independent_parameters_number = get_independent_parameters_number();
02384
02385 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
02386 {
02387 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02388 << "std::string get_independent_parameters_description(int) method." << std::endl
02389 << "Index of independent parameter must be equal or greater than zero and less than "
02390 << "number of independent parameters." << std::endl;
02391
02392 exit(1);
02393 }
02394
02395 #endif
02396
02397 return(independent_parameters_description[independent_parameter_index]);
02398 }
02399
02400
02401
02402
02411
02412 Vector< Vector<std::string>* > MultilayerPerceptron::get_independent_parameters_information(void)
02413 {
02414 Vector< Vector<std::string>* > independent_parameters_information(6);
02415
02416 independent_parameters_information[0] = &independent_parameters_name;
02417 independent_parameters_information[2] = &independent_parameters_units;
02418 independent_parameters_information[4] = &independent_parameters_description;
02419
02420 return(independent_parameters_information);
02421 }
02422
02423
02424
02425
02429
02430 Vector<double>& MultilayerPerceptron::get_independent_parameters_mean(void)
02431 {
02432 return(independent_parameters_mean);
02433 }
02434
02435
02436
02437
02441
02442 Vector<double>& MultilayerPerceptron::get_independent_parameters_standard_deviation(void)
02443 {
02444 return(independent_parameters_standard_deviation);
02445 }
02446
02447
02448
02449
02456
02457 Vector< Vector<double>* > MultilayerPerceptron::get_independent_parameters_mean_standard_deviation(void)
02458 {
02459 Vector< Vector<double>* > independent_parameters_mean_standard_deviation(2);
02460
02461 independent_parameters_mean_standard_deviation[0] = &independent_parameters_mean;
02462 independent_parameters_mean_standard_deviation[1] = &independent_parameters_standard_deviation;
02463
02464 return(independent_parameters_mean_standard_deviation);
02465 }
02466
02467
02468
02469
02475
02476 double MultilayerPerceptron::get_independent_parameter_mean(int independent_parameter_index)
02477 {
02478
02479
02480 #ifdef _DEBUG
02481
02482 int independent_parameters_number = get_independent_parameters_number();
02483
02484 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
02485 {
02486 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02487 << "double get_independent_parameters_mean(int) method." << std::endl
02488 << "Index of independent parameter must be equal or greater than zero and less than "
02489 << "number of independent parameters." << std::endl;
02490
02491 exit(1);
02492 }
02493
02494 #endif
02495
02496 return(independent_parameters_mean[independent_parameter_index]);
02497 }
02498
02499
02500
02506
02507 double MultilayerPerceptron::get_independent_parameter_standard_deviation(int independent_parameter_index)
02508 {
02509
02510
02511 #ifdef _DEBUG
02512
02513 int independent_parameters_number = get_independent_parameters_number();
02514
02515 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
02516 {
02517 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02518 << "double get_independent_parameters_standard_deviation(int) method." << std::endl
02519 << "Index of independent parameter must be equal or greater than zero and less than "
02520 << "number of independent parameters." << std::endl;
02521
02522 exit(1);
02523 }
02524
02525 #endif
02526
02527 return(independent_parameters_standard_deviation[independent_parameter_index]);
02528 }
02529
02530
02531
02532
02533
02537
02538 Vector<double>& MultilayerPerceptron::get_independent_parameters_minimum(void)
02539 {
02540 return(independent_parameters_minimum);
02541 }
02542
02543
02544
02545
02549
02550 Vector<double>& MultilayerPerceptron::get_independent_parameters_maximum(void)
02551 {
02552 return(independent_parameters_maximum);
02553 }
02554
02555
02556
02557
02564
02565 Vector< Vector<double>* > MultilayerPerceptron::get_independent_parameters_minimum_maximum(void)
02566 {
02567 Vector< Vector<double>* > independent_parameters_minimum_maximum(2);
02568
02569 independent_parameters_minimum_maximum[0] = &independent_parameters_minimum;
02570 independent_parameters_minimum_maximum[1] = &independent_parameters_maximum;
02571
02572 return(independent_parameters_minimum_maximum);
02573 }
02574
02575
02576
02577
02583
02584 double MultilayerPerceptron::get_independent_parameter_minimum(int independent_parameter_index)
02585 {
02586
02587
02588 #ifdef _DEBUG
02589
02590 int independent_parameters_number = get_independent_parameters_number();
02591
02592 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
02593 {
02594 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02595 << "double get_independent_parameters_minimum(int) method." << std::endl
02596 << "Index of independent parameter must be equal or greater than zero and less than "
02597 << "number of independent parameters." << std::endl;
02598
02599 exit(1);
02600 }
02601
02602 #endif
02603
02604 return(independent_parameters_minimum[independent_parameter_index]);
02605 }
02606
02607
02608
02609
02615
02616 double MultilayerPerceptron::get_independent_parameter_maximum(int independent_parameter_index)
02617 {
02618
02619
02620 #ifdef _DEBUG
02621
02622 int independent_parameters_number = get_independent_parameters_number();
02623
02624 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
02625 {
02626 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02627 << "double get_independent_parameters_maximum(int) method." << std::endl
02628 << "Index of independent parameter must be equal or greater than zero and less than "
02629 << "number of independent parameters." << std::endl;
02630
02631 exit(1);
02632 }
02633
02634 #endif
02635
02636 return(independent_parameters_maximum[independent_parameter_index]);
02637 }
02638
02639
02640
02641
02645
02646 Vector<double>& MultilayerPerceptron::get_independent_parameters_lower_bound(void)
02647 {
02648 return(independent_parameters_lower_bound);
02649 }
02650
02651
02652
02653
02657
02658 Vector<double>& MultilayerPerceptron::get_independent_parameters_upper_bound(void)
02659 {
02660 return(independent_parameters_upper_bound);
02661 }
02662
02663
02664
02665
02672
02673 Vector< Vector<double>* > MultilayerPerceptron::get_independent_parameters_bounds(void)
02674 {
02675 Vector< Vector<double>* > independent_parameters_bounds(2);
02676
02677 independent_parameters_bounds[0] = &independent_parameters_lower_bound;
02678 independent_parameters_bounds[1] = &independent_parameters_upper_bound;
02679
02680 return(independent_parameters_bounds);
02681 }
02682
02683
02684
02685
02690
02691 double MultilayerPerceptron::get_independent_parameter_lower_bound(int independent_parameter_index)
02692 {
02693
02694
02695 #ifdef _DEBUG
02696
02697 int independent_parameters_number = get_independent_parameters_number();
02698
02699 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
02700 {
02701 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02702 << "double get_independent_parameters_lower_bound(int) method." << std::endl
02703 << "Index of independent parameter must be equal or greater than zero and less than "
02704 << "number of independent parameters." << std::endl;
02705
02706 exit(1);
02707 }
02708
02709 #endif
02710
02711 return(independent_parameters_lower_bound[independent_parameter_index]);
02712 }
02713
02714
02715
02716
02722
02723 double MultilayerPerceptron::get_independent_parameter_upper_bound(int independent_parameter_index)
02724 {
02725
02726
02727 #ifdef _DEBUG
02728
02729 int independent_parameters_number = get_independent_parameters_number();
02730
02731 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
02732 {
02733 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02734 << "double get_independent_parameters_upper_bound(int) method." << std::endl
02735 << "Index of independent parameter must be equal or greater than zero and less than "
02736 << "number of independent parameters." << std::endl;
02737
02738 exit(1);
02739 }
02740
02741 #endif
02742
02743 return(independent_parameters_upper_bound[independent_parameter_index]);
02744 }
02745
02746
02747
02748
02751
02752 Vector<double> MultilayerPerceptron::get_parameters(void)
02753 {
02754 int independent_parameters_number = get_independent_parameters_number();
02755
02756 if((inputs_number != 0 && outputs_number != 0) && independent_parameters_number == 0)
02757 {
02758 return(get_neural_parameters());
02759 }
02760 else if((inputs_number == 0 && outputs_number == 0) && independent_parameters_number != 0)
02761 {
02762 return(scale_independent_parameters());
02763 }
02764 else if((inputs_number != 0 && outputs_number != 0) && independent_parameters_number != 0)
02765 {
02766
02767 Vector<double> neural_parameters = get_neural_parameters();
02768
02769 Vector<double> scaled_independent_parameters = scale_independent_parameters();
02770
02771 return(neural_parameters.assemble(scaled_independent_parameters));
02772 }
02773 else
02774 {
02775
02776 Vector<double> parameters;
02777
02778 return(parameters);
02779 }
02780 }
02781
02782
02783
02784
02787
02788 MultilayerPerceptron::NumericalDifferentiationMethod& MultilayerPerceptron::get_numerical_differentiation_method(void)
02789 {
02790 return(numerical_differentiation_method);
02791 }
02792
02793
02794
02795
02796 std::string MultilayerPerceptron::get_numerical_differentiation_method_name(void)
02797 {
02798 switch(numerical_differentiation_method)
02799 {
02800 case MultilayerPerceptron::ForwardDifferences:
02801 {
02802 return("ForwardDifferences");
02803 }
02804 break;
02805
02806 case MultilayerPerceptron::CentralDifferences:
02807 {
02808 return("CentralDifferences");
02809 }
02810 break;
02811
02812 default:
02813 {
02814 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02815 << "std::string get_numerical_differentiation_method(void) method." << std::endl
02816 << "Unknown numerical differentiation method." << std::endl;
02817
02818 exit(1);
02819 }
02820 break;
02821 }
02822 }
02823
02824
02825
02826
02827 MultilayerPerceptron::NumericalEpsilonMethod& MultilayerPerceptron::get_numerical_epsilon_method(void)
02828 {
02829 return(numerical_epsilon_method);
02830 }
02831
02832
02833
02834
02835 std::string MultilayerPerceptron::get_numerical_epsilon_method_name(void)
02836 {
02837 if(numerical_epsilon_method == Absolute)
02838 {
02839 return("Absolute");
02840 }
02841 else if(numerical_epsilon_method == Relative)
02842 {
02843 return("Relative");
02844 }
02845 else
02846 {
02847 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
02848 << "std::string get_numerical_epsilon_method(void) method." << std::endl
02849 << "Unknown numerical epsilon method." << std::endl;
02850
02851 exit(1);
02852 }
02853 }
02854
02855
02856
02857
02860
02861 double MultilayerPerceptron::get_numerical_epsilon(void)
02862 {
02863 return(numerical_epsilon);
02864 }
02865
02866
02867
02868
02869 Vector< Vector<double>* > MultilayerPerceptron::get_independent_parameters_statistics(void)
02870 {
02871 Vector< Vector<double>* > independent_parameters_statistics(4);
02872
02873 independent_parameters_statistics[0] = &independent_parameters_mean;
02874 independent_parameters_statistics[1] = &independent_parameters_standard_deviation;
02875
02876 independent_parameters_statistics[4] = &independent_parameters_minimum;
02877 independent_parameters_statistics[5] = &independent_parameters_minimum;
02878
02879 return(independent_parameters_statistics);
02880 }
02881
02882
02883
02884
02887
02888 Vector<double> MultilayerPerceptron::scale_independent_parameters(void)
02889 {
02890 int independent_parameters_number = get_independent_parameters_number();
02891
02892 switch(independent_parameters_scaling_method)
02893 {
02894 case None:
02895 {
02896 return(independent_parameters);
02897 }
02898 break;
02899
02900 case MeanStandardDeviation:
02901 {
02902 Vector<double> scaled_independent_parameters(independent_parameters_number);
02903
02904 for(int i = 0; i < independent_parameters_number; i++)
02905 {
02906 if(independent_parameters_standard_deviation[i] < 1.0e-99)
02907 {
02908 if(display)
02909 {
02910 std::cout << "Flood Warning: MultilayerPerceptron class." << std::endl
02911 << "Vector<double> scale_independent_parameters(void) method." << std::endl
02912 << "Standard deviation of independent parameter " << i << " is zero." << std::endl
02913 << "That independent won't be unscaled." << std::endl;
02914 }
02915
02916 scaled_independent_parameters[i] = independent_parameters[i];
02917 }
02918 else
02919 {
02920 scaled_independent_parameters[i]
02921 = (independent_parameters[i] - independent_parameters_mean[i])
02922 /independent_parameters_standard_deviation[i];
02923 }
02924 }
02925
02926 return(scaled_independent_parameters);
02927 }
02928 break;
02929
02930 case MinimumMaximum:
02931 {
02932 Vector<double> scaled_independent_parameters(independent_parameters_number);
02933
02934 for(int i = 0; i < independent_parameters_number; i++)
02935 {
02936 if(independent_parameters_maximum[i]-independent_parameters_minimum[i] < 1.0e-99)
02937 {
02938 if(display)
02939 {
02940 std::cout << "Flood Warning: MultilayerPerceptron class." << std::endl
02941 << "Vector<double> scale_independent_parameters(void) method." << std::endl
02942 << "Maximum and minimum of independent parameter " << i << " are equal." << std::endl
02943 << "That independent parameter won't be scaled." << std::endl;
02944 }
02945
02946 scaled_independent_parameters[i] = independent_parameters[i];
02947 }
02948 else
02949 {
02950 scaled_independent_parameters[i]
02951 = 2.0*(independent_parameters[i] - independent_parameters_minimum[i])
02952 /(independent_parameters_maximum[i]-independent_parameters_minimum[i])-1.0;
02953 }
02954 }
02955
02956 return(scaled_independent_parameters);
02957 }
02958 break;
02959
02960 default:
02961 {
02962 std::cerr << "Flood Error: MultilayerPerceptron class" << std::endl
02963 << "Vector<double> scale_independent_parameters(void) method." << std::endl
02964 << "Unknown independent parameters scaling and unscaling method." << std::endl;
02965
02966 exit(1);
02967 }
02968 break;
02969 }
02970 }
02971
02972
02973
02974
02977
02978 void MultilayerPerceptron::unscale_independent_parameters(const Vector<double>& scaled_independent_parameters)
02979 {
02980 int independent_parameters_number = get_independent_parameters_number();
02981
02982 switch(independent_parameters_scaling_method)
02983 {
02984 case None:
02985 {
02986 independent_parameters = scaled_independent_parameters;
02987 }
02988 break;
02989
02990 case MeanStandardDeviation:
02991 {
02992 for(int i = 0; i < independent_parameters_number; i++)
02993 {
02994 if(independent_parameters_standard_deviation[i] < 1e-99)
02995 {
02996 if(display)
02997 {
02998 std::cout << "Flood Warning: MultilayerPerceptron class" << std::endl
02999 << "void unscale_independent_parameters(void) method." << std::endl
03000 << "Standard deviation of independent parameter " << i << " is zero." << std::endl
03001 << "That independent parameter won't be scaled." << std::endl;
03002 }
03003
03004 independent_parameters[i] = scaled_independent_parameters[i];
03005 }
03006 else
03007 {
03008 independent_parameters[i] = independent_parameters_mean[i]
03009 + scaled_independent_parameters[i]*independent_parameters_standard_deviation[i];
03010 }
03011 }
03012 }
03013 break;
03014
03015 case MinimumMaximum:
03016 {
03017 for(int i = 0; i < independent_parameters_number; i++)
03018 {
03019 if(independent_parameters_maximum[i]-independent_parameters_minimum[i] < 1e-99)
03020 {
03021 if(display)
03022 {
03023 std::cout << "Flood Warning: MultilayerPerceptron class" << std::endl
03024 << "void unscale_independent_parameters(void) method." << std::endl
03025 << "Maximum and minimum of independent parameter " << i << " are equal." << std::endl
03026 << "That independent parameter won't be scaled." << std::endl;
03027 }
03028
03029 independent_parameters[i] = scaled_independent_parameters[i];
03030 }
03031 else
03032 {
03033 independent_parameters[i] = 0.5*(scaled_independent_parameters[i] + 1.0)
03034 *(independent_parameters_maximum[i]-independent_parameters_minimum[i])
03035 + independent_parameters_minimum[i];
03036 }
03037 }
03038 }
03039 break;
03040
03041 default:
03042 {
03043 std::cerr << "Flood Error: MultilayerPerceptron class" << std::endl
03044 << "void unscale_independent_parameters(void) method." << std::endl
03045 << "Unknown scaling and unscaling method." << std::endl;
03046
03047 exit(1);
03048 }
03049 break;
03050 }
03051
03052 bound_independent_parameters();
03053 }
03054
03055
03056
03057
03059
03060 double MultilayerPerceptron::calculate_neural_parameters_norm(void)
03061 {
03062 double neural_parameters_norm;
03063
03064 Vector<double> neural_parameters = get_neural_parameters();
03065
03066 neural_parameters_norm = neural_parameters.calculate_norm();
03067
03068 return(neural_parameters_norm);
03069 }
03070
03071
03072
03073
03075
03076 double MultilayerPerceptron::calculate_independent_parameters_norm(void)
03077 {
03078 double independent_parameters_norm = independent_parameters.calculate_norm();
03079
03080
03081
03082 return(independent_parameters_norm);
03083 }
03084
03085
03086
03087
03089
03090 double MultilayerPerceptron::calculate_parameters_norm(void)
03091 {
03092 Vector<double> parameters = get_parameters();
03093
03094 double parameters_norm = parameters.calculate_norm();
03095
03096 return(parameters_norm);
03097 }
03098
03099
03100
03101
03103
03104 void MultilayerPerceptron::bound_independent_parameters(void)
03105 {
03106 int independent_parameters_number = get_independent_parameters_number();
03107
03108 int independent_parameters_lower_bound_size = independent_parameters_lower_bound.get_size();
03109 int independent_parameters_upper_bound_size = independent_parameters_upper_bound.get_size();
03110
03111 if(independent_parameters_lower_bound_size == independent_parameters_number && independent_parameters_upper_bound_size == independent_parameters_number)
03112 {
03113 independent_parameters.apply_lower_upper_bounds(independent_parameters_lower_bound, independent_parameters_upper_bound);
03114 }
03115 else if(independent_parameters_lower_bound_size == independent_parameters_number)
03116 {
03117 independent_parameters.apply_lower_bound(independent_parameters_lower_bound);
03118 }
03119 else if(independent_parameters_upper_bound_size == independent_parameters_number)
03120 {
03121 independent_parameters.apply_upper_bound(independent_parameters_upper_bound);
03122 }
03123 }
03124
03125
03126
03127
03130
03131 void MultilayerPerceptron::bound_independent_parameter(int independent_parameter_index)
03132 {
03133 if(independent_parameters_lower_bound != 0 && independent_parameters_upper_bound != 0)
03134 {
03135 if(independent_parameters[independent_parameter_index]
03136 < independent_parameters_lower_bound[independent_parameter_index])
03137 {
03138 independent_parameters[independent_parameter_index]
03139 = independent_parameters_lower_bound[independent_parameter_index];
03140 }
03141 else if(independent_parameters[independent_parameter_index]
03142 > independent_parameters_upper_bound[independent_parameter_index])
03143 {
03144 independent_parameters[independent_parameter_index]
03145 = independent_parameters_upper_bound[independent_parameter_index];
03146 }
03147 }
03148 else if(independent_parameters_lower_bound != 0)
03149 {
03150 if(independent_parameters[independent_parameter_index]
03151 < independent_parameters_lower_bound[independent_parameter_index])
03152 {
03153 independent_parameters[independent_parameter_index]
03154 = independent_parameters_lower_bound[independent_parameter_index];
03155 }
03156 }
03157 else if(independent_parameters_upper_bound != 0)
03158 {
03159 if(independent_parameters[independent_parameter_index]
03160 > independent_parameters_upper_bound[independent_parameter_index])
03161 {
03162 independent_parameters[independent_parameter_index]
03163 = independent_parameters_upper_bound[independent_parameter_index];
03164 }
03165 }
03166 }
03167
03168
03169
03170
03173
03174 bool MultilayerPerceptron::get_display_range_out_warning(void)
03175 {
03176 return(display_range_out_warning);
03177 }
03178
03179
03180
03181
03184
03185 bool MultilayerPerceptron::get_display(void)
03186 {
03187 return(display);
03188 }
03189
03190
03191
03192
03193 void MultilayerPerceptron::set(void)
03194 {
03195 Vector<int> new_hidden_layers_size;
03196 set_network_architecture(0, new_hidden_layers_size, 0);
03197
03198 set_independent_parameters_number(0);
03199
03200 set_default();
03201 }
03202
03203
03204
03205
03206 void MultilayerPerceptron::set(int new_inputs_number, const Vector<int>& new_hidden_layers_size, int new_outputs_number)
03207 {
03208 set_network_architecture(new_inputs_number, new_hidden_layers_size, new_outputs_number);
03209
03210 set_independent_parameters_number(0);
03211
03212 set_default();
03213 }
03214
03215
03216
03217
03218 void MultilayerPerceptron::set(int new_inputs_number, int new_hidden_layer_size, int new_outputs_number)
03219 {
03220 Vector<int> new_hidden_layers_size(1, new_hidden_layer_size);
03221
03222 set_network_architecture(new_inputs_number, new_hidden_layers_size, new_outputs_number);
03223
03224 set_independent_parameters_number(0);
03225
03226 set_default();
03227 }
03228
03229
03230
03231
03232 void MultilayerPerceptron::set(int new_independent_parameters_number)
03233 {
03234 Vector<int> new_hidden_layers_size;
03235 set_network_architecture(0, new_hidden_layers_size, 0);
03236
03237 set_independent_parameters_number(new_independent_parameters_number);
03238
03239 set_default();
03240 }
03241
03242
03243
03244
03245 void MultilayerPerceptron::set(const char*)
03246 {
03247 }
03248
03249
03250
03251
03252 void MultilayerPerceptron::set(const MultilayerPerceptron&)
03253 {
03254 }
03255
03256
03257
03258
03259 void MultilayerPerceptron::set_default(void)
03260 {
03261
03262
03263 inputs_scaling_method = None;
03264 outputs_unscaling_method = None;
03265
03266 independent_parameters_scaling_method = None;
03267
03268
03269
03270 numerical_differentiation_method = MultilayerPerceptron::CentralDifferences;
03271 numerical_epsilon_method = MultilayerPerceptron::Relative;
03272 numerical_epsilon = 1.0e-6;
03273
03274
03275
03276 display = true;
03277 display_range_out_warning = false;
03278 }
03279
03280
03281
03282
03290
03291 void MultilayerPerceptron::set_network_architecture
03292 (int new_inputs_number, const Vector<int>& new_hidden_layers_size, int new_outputs_number)
03293 {
03294
03295
03296 #ifdef _DEBUG
03297
03298 if(!(new_inputs_number == 0 && new_hidden_layers_size == 0 && new_outputs_number == 0))
03299 {
03300 if(new_inputs_number == 0)
03301 {
03302 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
03303 << "void set_network_architecture(int, const Vector<int>&, int) method." << std::endl
03304 << "Number of inputs must be greater than zero." << std::endl;
03305
03306 exit(1);
03307 }
03308
03309 int size = new_hidden_layers_size.get_size();
03310
03311 for(int i = 0; i < size; i++)
03312 {
03313 if(new_hidden_layers_size == 0)
03314 {
03315 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
03316 << "void set_network_architecture(int, const Vector<int>&, int) method." << std::endl
03317 << "Sizo of hidden layer " << i << " must be greater than zero." << std::endl;
03318
03319 exit(1);
03320 }
03321 }
03322
03323 if(new_outputs_number == 0)
03324 {
03325 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
03326 << "void set_network_architecture(int, const Vector<int>&, int) method." << std::endl
03327 << "Number of outputs must be greater than zero." << std::endl;
03328
03329 exit(1);
03330 }
03331 }
03332
03333 #endif
03334
03335
03336
03337 inputs_number = new_inputs_number;
03338 hidden_layers_size = new_hidden_layers_size;
03339 outputs_number = new_outputs_number;
03340
03341
03342
03343 int hidden_layers_number = get_hidden_layers_number();
03344
03345 hidden_layers.set(hidden_layers_number);
03346
03347 for(int i = 0; i < hidden_layers_number; i++)
03348 {
03349 hidden_layers[i].set(hidden_layers_size[i]);
03350
03351 for(int j = 0; j < hidden_layers_size[i]; j++)
03352 {
03353 if(i == 0)
03354 {
03355 hidden_layers[i][j].set_inputs_number(inputs_number);
03356 }
03357 else
03358 {
03359 hidden_layers[i][j].set_inputs_number(hidden_layers_size[i-1]);
03360 }
03361 }
03362 }
03363
03364
03365
03366 output_layer.set(outputs_number);
03367
03368 for(int i = 0; i < outputs_number; i++)
03369 {
03370 output_layer[i].set_inputs_number(hidden_layers_size[hidden_layers_number-1]);
03371 }
03372
03373
03374
03375 hidden_layers_activation_function.set_size(hidden_layers_number);
03376
03377 Vector<MultilayerPerceptron::LayerActivationFunction> new_hidden_layers_activation_function(hidden_layers_number, HyperbolicTangent);
03378
03379 set_hidden_layers_activation_function(new_hidden_layers_activation_function);
03380
03381 set_output_layer_activation_function(Linear);
03382 }
03383
03384
03385
03386
03393
03394 void MultilayerPerceptron::set_network_architecture
03395 (int new_inputs_number, int new_hidden_layer_size, int new_outputs_number)
03396 {
03397 Vector<int> new_hidden_layers_size(1, new_hidden_layer_size);
03398
03399 set_network_architecture(new_inputs_number, new_hidden_layers_size, new_outputs_number);
03400 }
03401
03402
03403
03404
03410
03411 void MultilayerPerceptron::set_hidden_layers_size(const Vector<int>& new_hidden_layers_size)
03412 {
03413
03414
03415 set_network_architecture(inputs_number, new_hidden_layers_size, outputs_number);
03416 }
03417
03418
03419
03420
03425
03426 void MultilayerPerceptron::set_hidden_layer_size(int new_hidden_layer_size)
03427 {
03428 Vector<int> new_hidden_layers_size(1, new_hidden_layer_size);
03429
03430 set_network_architecture(inputs_number, hidden_layers_size, outputs_number);
03431 }
03432
03433
03434
03435
03439
03440 void MultilayerPerceptron::set_neural_parameters(const Vector<double>& new_neural_parameters)
03441 {
03442
03443
03444 #ifdef _DEBUG
03445
03446 int size = new_neural_parameters.get_size();
03447
03448 int neural_parameters_number = get_neural_parameters_number();
03449
03450 if(size != neural_parameters_number)
03451 {
03452 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
03453 << "void set_neural_parameters(const Vector<double>&) method." << std::endl
03454 << "Size must be equal to number of biases and synaptic weights." << std::endl;
03455
03456 exit(1);
03457 }
03458
03459 #endif
03460
03461 int hidden_layers_number = get_hidden_layers_number();
03462
03463 double bias;
03464 Vector<double> synaptic_weights;
03465
03466
03467
03468 int index = 0;
03469
03470 for(int i = 0; i < hidden_layers_number; i++)
03471 {
03472 if(i == 0)
03473 {
03474 synaptic_weights.set(inputs_number);
03475 }
03476 else
03477 {
03478 synaptic_weights.set(hidden_layers_size[i-1]);
03479 }
03480
03481 for(int j = 0; j < hidden_layers_size[i]; j++)
03482 {
03483
03484
03485 bias = new_neural_parameters[index];
03486 hidden_layers[i][j].set_bias(bias);
03487 index++;
03488
03489
03490
03491 synaptic_weights = new_neural_parameters.extract(index, hidden_layers[i][j].get_inputs_number());
03492 hidden_layers[i][j].set_synaptic_weights(synaptic_weights);
03493 index += hidden_layers[i][j].get_inputs_number();
03494 }
03495 }
03496
03497
03498
03499 for(int i = 0; i < outputs_number; i++)
03500 {
03501 synaptic_weights.set(hidden_layers_size[hidden_layers_number-1]);
03502
03503
03504
03505 bias = new_neural_parameters[index];
03506 output_layer[i].set_bias(bias);
03507 index++;
03508
03509
03510
03511 synaptic_weights = new_neural_parameters.extract(index, output_layer[i].get_inputs_number());
03512 output_layer[i].set_synaptic_weights(synaptic_weights);
03513 index += output_layer[i].get_inputs_number();
03514 }
03515 }
03516
03517
03518
03519
03526
03527 void MultilayerPerceptron::set_hidden_layers_biases(const Vector< Vector<double> >& new_hidden_layers_biases)
03528 {
03529 int hidden_layers_number = get_hidden_layers_number();
03530
03531
03532
03533 #ifdef _DEBUG
03534
03535 int size = new_hidden_layers_biases.get_size();
03536
03537 if(size != hidden_layers_number)
03538 {
03539 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
03540 << "void set_hidden_layers_biases(const Vector< Vector<double> >&) method." << std::endl
03541 << "Size must be equal to number of hidden layers." << std::endl;
03542
03543 exit(1);
03544 }
03545
03546 #endif
03547
03548 for(int i = 0; i < hidden_layers_number; i++)
03549 {
03550 for(int j = 0; j < hidden_layers_size[i]; j++)
03551 {
03552 hidden_layers[i][j].set_bias(new_hidden_layers_biases[i][j]);
03553 }
03554 }
03555 }
03556
03557
03558
03559
03564
03565 void MultilayerPerceptron::
03566 set_hidden_layer_biases(int i, const Vector<double>& new_hidden_layer_biases)
03567 {
03568
03569
03570 #ifdef _DEBUG
03571
03572 int hidden_layers_number = get_hidden_layers_number();
03573
03574 if(i < 0 || i >= hidden_layers_number)
03575 {
03576 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
03577 << "void set_hidden_layer_biases(int, const Vector<double>&) method." << std::endl
03578 << "Index of hidden layer must be equal or greater than zero and less than number of hidden layers."
03579 << std::endl;
03580
03581 exit(1);
03582 }
03583
03584 int size = new_hidden_layer_biases.get_size();
03585
03586 int hidden_layer_size = hidden_layers_size[i];
03587
03588 if(size != hidden_layer_size)
03589 {
03590 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
03591 << "void set_hidden_layer_biases(int, const Vector<double>&) method." << std::endl
03592 << "Size must be equal to number of hidden layer inputs." << std::endl;
03593
03594 exit(1);
03595 }
03596
03597 #endif
03598
03599
03600
03601 for(int j = 0; j < hidden_layers_size[i]; j++)
03602 {
03603 hidden_layers[i][j].set_bias(new_hidden_layer_biases[j]);
03604 }
03605 }
03606
03607
03608
03609
03617
03618 void MultilayerPerceptron
03619 ::set_hidden_layers_synaptic_weights(const Vector< Matrix<double> >& new_hidden_layers_synaptic_weights)
03620 {
03621 int hidden_layers_number = get_hidden_layers_number();
03622
03623
03624
03625 #ifdef _DEBUG
03626
03627 int size = new_hidden_layers_synaptic_weights.get_size();
03628
03629 if(size != hidden_layers_number)
03630 {
03631 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
03632 << "void set_hidden_layers_synaptic_weights(const Vector< Matrix<double> >&) method." << std::endl
03633 << "Size must be equal to number of hidden layers." << std::endl;
03634
03635 exit(1);
03636 }
03637
03638 #endif
03639
03640 Vector<double> synaptic_weights;
03641
03642 for(int i = 0; i < hidden_layers_number; i++)
03643 {
03644 for(int j = 0; j < hidden_layers_size[i]; j++)
03645 {
03646 synaptic_weights = new_hidden_layers_synaptic_weights[i].get_row(j);
03647
03648 hidden_layers[i][j].set_synaptic_weights(synaptic_weights);
03649 }
03650 }
03651 }
03652
03653
03654
03655
03663
03664 void MultilayerPerceptron::set_hidden_layer_synaptic_weights
03665 (int i, const Matrix<double>& new_hidden_layer_synaptic_weights)
03666 {
03667
03668
03669 #ifdef _DEBUG
03670
03671 int hidden_layers_number = get_hidden_layers_number();
03672
03673 if(i < 0 || i >= hidden_layers_number)
03674 {
03675 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
03676 << "void set_hidden_layer_synaptic_weights(int, const Matrix<double>&) method." << std::endl
03677 << "Index of hidden layer must be equal or greater than zero and less than number of hidden layers."
03678 << std::endl;
03679
03680 exit(1);
03681 }
03682
03683 #endif
03684
03685 int hidden_layer_size = hidden_layers_size[i];
03686
03687 int hidden_layer_inputs_number = get_hidden_layer_inputs_number(i);
03688
03689 Vector<double> synaptic_weights(hidden_layer_size, hidden_layer_inputs_number);
03690
03691 for(int j = 0; j < hidden_layer_size; j++)
03692 {
03693 synaptic_weights = new_hidden_layer_synaptic_weights.get_row(j);
03694
03695 hidden_layers[i][j].set_synaptic_weights(synaptic_weights);
03696 }
03697 }
03698
03699
03700
03701
03702
03703 void MultilayerPerceptron::set_hidden_layers_parameters(const Vector< Matrix<double> >& new_hidden_layers_parameters)
03704 {
03705 int hidden_layers_number = get_hidden_layers_number();
03706
03707
03708
03709 #ifdef _DEBUG
03710
03711 int size = new_hidden_layers_parameters.get_size();
03712
03713 if(size != hidden_layers_number)
03714 {
03715 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
03716 << "void set_hidden_layers_parameters(const Vector< Matrix<double> >&) method." << std::endl
03717 << "Size must be equal to number of hidden layers." << std::endl;
03718
03719 exit(1);
03720 }
03721
03722 #endif
03723
03724 Vector<double> parameters;
03725
03726 for(int i = 0; i < hidden_layers_number; i++)
03727 {
03728 for(int j = 0; j < hidden_layers_size[i]; j++)
03729 {
03730 parameters = new_hidden_layers_parameters[i].get_row(j);
03731
03732 hidden_layers[i][j].set_parameters(parameters);
03733 }
03734 }
03735 }
03736
03737
03738
03739
03740 void MultilayerPerceptron::set_hidden_layer_parameters(int i, const Matrix<double>& new_hidden_layer_parameters)
03741 {
03742
03743
03744 #ifdef _DEBUG
03745
03746 int hidden_layers_number = get_hidden_layers_number();
03747
03748 if(i < 0)
03749 {
03750 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
03751 << "void set_hidden_layer_parameters(int, const Matrix<double>&) method." << std::endl
03752 << "Index of hidden layer must be equal or greater than zero." << std::endl;
03753
03754 exit(1);
03755 }
03756 if(i >= hidden_layers_number)
03757 {
03758 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
03759 << "void set_hidden_layer_parameters(int, const Matrix<double>&) method." << std::endl
03760 << "Index of hidden layer must be less than number of hidden layers." << std::endl;
03761
03762 exit(1);
03763 }
03764
03765 #endif
03766
03767 int hidden_layer_size = hidden_layers_size[i];
03768 int hidden_layer_parameters_number = get_hidden_layer_parameters_number(i);
03769
03770 Vector<double> parameters(hidden_layer_size, hidden_layer_parameters_number);
03771
03772 for(int j = 0; j < hidden_layer_size; j++)
03773 {
03774 parameters = new_hidden_layer_parameters.get_row(j);
03775
03776 hidden_layers[i][j].set_parameters(parameters);
03777 }
03778 }
03779
03780
03781
03782
03786
03787 void MultilayerPerceptron::set_output_layer_biases(const Vector<double>& new_output_layer_biases)
03788 {
03789
03790
03791 #ifdef _DEBUG
03792
03793 int size = new_output_layer_biases.get_size();
03794
03795 if(size != outputs_number)
03796 {
03797 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
03798 << "void set_output_layer_biases(const Vector<double>&) method." << std::endl
03799 << "Size must be equal to number of outputs." << std::endl;
03800
03801 exit(1);
03802 }
03803
03804 #endif
03805
03806 for(int i = 0; i < outputs_number; i++)
03807 {
03808 output_layer[i].set_bias(new_output_layer_biases[i]);
03809 }
03810 }
03811
03812
03813
03814
03821
03822 void MultilayerPerceptron::
03823 set_output_layer_synaptic_weights(const Matrix<double>& new_output_layer_synaptic_weights)
03824 {
03825 int hidden_layers_number = get_hidden_layers_number();
03826
03827 int output_layer_inputs_number = hidden_layers_size[hidden_layers_number-1];
03828
03829
03830
03831 #ifdef _DEBUG
03832
03833 int rows_number = new_output_layer_synaptic_weights.get_rows_number();
03834 int columns_number = new_output_layer_synaptic_weights.get_columns_number();
03835
03836 Matrix<double> output_layer_synaptic_weights(outputs_number, output_layer_inputs_number);
03837
03838 if(rows_number != outputs_number || columns_number != output_layer_inputs_number)
03839 {
03840 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
03841 << "void set_hidden_layer_synaptic_weights(int, const Vector<double>&) method." << std::endl
03842 << "Index of hidden layer must be equal or greater than zero and less than number of hidden layers."
03843 << std::endl;
03844
03845 exit(1);
03846 }
03847
03848 #endif
03849
03850 Vector<double> synaptic_weights(output_layer_inputs_number);
03851
03852 for(int i = 0; i < outputs_number; i++)
03853 {
03854 synaptic_weights = new_output_layer_synaptic_weights.get_row(i);
03855
03856 output_layer[i].set_synaptic_weights(synaptic_weights);
03857 }
03858 }
03859
03860
03861
03862
03868
03869 void MultilayerPerceptron::set_hidden_layers_activation_function
03870 (const Vector<MultilayerPerceptron::LayerActivationFunction>& new_hidden_layers_activation_function)
03871 {
03872 int hidden_layers_number = get_hidden_layers_number();
03873
03874
03875
03876 #ifdef _DEBUG
03877
03878 int new_hidden_layers_activation_function_size = new_hidden_layers_activation_function.get_size();
03879
03880 if(new_hidden_layers_activation_function_size != hidden_layers_number)
03881 {
03882 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
03883 << "void set_hidden_layers_activation_function(const Vector<LayerActivationFunction>&) method." << std::endl
03884 << "Size of activation function of hidden layers must be equal to number of hidden layers."
03885 << std::endl;
03886
03887 exit(1);
03888 }
03889
03890 #endif
03891
03892 for(int i = 0; i < hidden_layers_number; i++)
03893 {
03894 set_hidden_layer_activation_function(i, new_hidden_layers_activation_function[i]);
03895 }
03896 }
03897
03898
03899
03900
03905
03906 void MultilayerPerceptron::set_hidden_layer_activation_function
03907 (int i, const MultilayerPerceptron::LayerActivationFunction& new_hidden_layer_activation_function)
03908 {
03909 hidden_layers_activation_function[i] = new_hidden_layer_activation_function;
03910
03911 switch(hidden_layers_activation_function[i])
03912 {
03913 case Logistic:
03914 {
03915 for(int j = 0; j < hidden_layers_size[i]; j++)
03916 {
03917 hidden_layers[i][j].set_activation_function(Perceptron::Logistic);
03918 }
03919 }
03920 break;
03921
03922 case HyperbolicTangent:
03923 {
03924 for(int j = 0; j < hidden_layers_size[i]; j++)
03925 {
03926 hidden_layers[i][j].set_activation_function(Perceptron::HyperbolicTangent);
03927 }
03928 }
03929 break;
03930
03931 case Threshold:
03932 {
03933 for(int j = 0; j < hidden_layers_size[i]; j++)
03934 {
03935 hidden_layers[i][j].set_activation_function(Perceptron::Threshold);
03936 }
03937 }
03938 break;
03939
03940 case SymmetricThreshold:
03941 {
03942 for(int j = 0; j < hidden_layers_size[i]; j++)
03943 {
03944 hidden_layers[i][j].set_activation_function(Perceptron::SymmetricThreshold);
03945 }
03946 }
03947 break;
03948
03949 case Linear:
03950 {
03951 for(int j = 0; j < hidden_layers_size[i]; j++)
03952 {
03953 hidden_layers[i][j].set_activation_function(Perceptron::Linear);
03954 }
03955 }
03956 break;
03957 }
03958 }
03959
03960
03961
03962
03966
03967 void MultilayerPerceptron::set_output_layer_activation_function
03968 (const MultilayerPerceptron::LayerActivationFunction& new_output_layer_activation_function)
03969 {
03970 output_layer_activation_function = new_output_layer_activation_function;
03971
03972 switch(output_layer_activation_function)
03973 {
03974 case Logistic:
03975 {
03976 for(int i = 0; i < outputs_number; i++)
03977 {
03978 output_layer[i].set_activation_function(Perceptron::Logistic);
03979 }
03980 }
03981 break;
03982
03983 case HyperbolicTangent:
03984 {
03985 for(int i = 0; i < outputs_number; i++)
03986 {
03987 output_layer[i].set_activation_function(Perceptron::HyperbolicTangent);
03988 }
03989 }
03990 break;
03991
03992 case Threshold:
03993 {
03994 for(int i = 0; i < outputs_number; i++)
03995 {
03996 output_layer[i].set_activation_function(Perceptron::Threshold);
03997 }
03998 }
03999 break;
04000
04001 case SymmetricThreshold:
04002 {
04003 for(int i = 0; i < outputs_number; i++)
04004 {
04005 output_layer[i].set_activation_function(Perceptron::SymmetricThreshold);
04006 }
04007 }
04008 break;
04009
04010 case Linear:
04011 {
04012 for(int i = 0; i < outputs_number; i++)
04013 {
04014 output_layer[i].set_activation_function(Perceptron::Linear);
04015 }
04016 }
04017 break;
04018 }
04019 }
04020
04021
04022
04023
04024 void MultilayerPerceptron::
04025 set_hidden_layers_activation_function(const Vector<std::string>& new_hidden_layers_activation_function)
04026 {
04027 int hidden_layers_number = get_hidden_layers_number();
04028
04029 for(int i = 0; i < hidden_layers_number; i++)
04030 {
04031 set_hidden_layer_activation_function(i, new_hidden_layers_activation_function[i]);
04032 }
04033 }
04034
04035
04036
04037
04038 void MultilayerPerceptron::set_hidden_layer_activation_function(int hidden_layer_index, const std::string& new_hidden_layer_activation_function)
04039 {
04040 if(new_hidden_layer_activation_function == "Logistic")
04041 {
04042 hidden_layers_activation_function[hidden_layer_index] = MultilayerPerceptron::Logistic;
04043 }
04044 else if(new_hidden_layer_activation_function == "HyperbolicTangent")
04045 {
04046 hidden_layers_activation_function[hidden_layer_index] = MultilayerPerceptron::HyperbolicTangent;
04047 }
04048 else if(new_hidden_layer_activation_function == "Threshold")
04049 {
04050 hidden_layers_activation_function[hidden_layer_index] = MultilayerPerceptron::Threshold;
04051 }
04052 else if(new_hidden_layer_activation_function == "SymmetricThreshold")
04053 {
04054 hidden_layers_activation_function[hidden_layer_index] = MultilayerPerceptron::SymmetricThreshold;
04055 }
04056 else if(new_hidden_layer_activation_function == "Linear")
04057 {
04058 hidden_layers_activation_function[hidden_layer_index] = MultilayerPerceptron::Linear;
04059 }
04060 else
04061 {
04062 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04063 << "void set_hidden_layer_activation_function(int, const std::string&) method." << std::endl
04064 << "Unknown hidden layer activation function: " << new_hidden_layer_activation_function << "." <<std::endl;
04065
04066 exit(1);
04067 }
04068 }
04069
04070
04071
04072
04073 void MultilayerPerceptron::set_output_layer_activation_function(const std::string& new_output_layer_activation_function)
04074 {
04075 if(new_output_layer_activation_function == "Logistic")
04076 {
04077 output_layer_activation_function = MultilayerPerceptron::Logistic;
04078 }
04079 else if(new_output_layer_activation_function == "HyperbolicTangent")
04080 {
04081 output_layer_activation_function = MultilayerPerceptron::HyperbolicTangent;
04082 }
04083 else if(new_output_layer_activation_function == "Threshold")
04084 {
04085 output_layer_activation_function = MultilayerPerceptron::Threshold;
04086 }
04087 else if(new_output_layer_activation_function == "SymmetricThreshold")
04088 {
04089 output_layer_activation_function = MultilayerPerceptron::SymmetricThreshold;
04090 }
04091 else if(new_output_layer_activation_function == "Linear")
04092 {
04093 output_layer_activation_function = MultilayerPerceptron::Linear;
04094 }
04095 else
04096 {
04097 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04098 << "void set_output_layer_activation_function(const std::string&) method." << std::endl
04099 << "Unknown output layer activation function: " << new_output_layer_activation_function << "." <<std::endl;
04100
04101 exit(1);
04102 }
04103 }
04104
04105
04106
04107
04111
04112 void MultilayerPerceptron::set_inputs_scaling_method
04113 (const MultilayerPerceptron::ScalingMethod& new_inputs_scaling_method)
04114 {
04115 inputs_scaling_method = new_inputs_scaling_method;
04116 }
04117
04118
04119
04120
04124
04125 void MultilayerPerceptron::set_outputs_unscaling_method
04126 (const MultilayerPerceptron::ScalingMethod& new_outputs_unscaling_method)
04127 {
04128 outputs_unscaling_method = new_outputs_unscaling_method;
04129 }
04130
04131
04132
04133
04134 void MultilayerPerceptron::set_variables_scaling_method(const ScalingMethod& new_variables_scaling_method)
04135 {
04136 inputs_scaling_method = new_variables_scaling_method;
04137 outputs_unscaling_method = new_variables_scaling_method;
04138 }
04139
04140
04141
04142
04143 void MultilayerPerceptron::set_inputs_scaling_method(const std::string& new_inputs_scaling_method)
04144 {
04145 if(new_inputs_scaling_method == "None")
04146 {
04147 inputs_scaling_method = MultilayerPerceptron::None;
04148 }
04149 else if(new_inputs_scaling_method == "MeanStandardDeviation")
04150 {
04151 inputs_scaling_method = MultilayerPerceptron::MeanStandardDeviation;
04152 }
04153 else if(new_inputs_scaling_method == "MinimumMaximum")
04154 {
04155 inputs_scaling_method = MultilayerPerceptron::MinimumMaximum;
04156 }
04157 else
04158 {
04159 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04160 << "void set_inputs_scaling_method(const std::string&) method." << std::endl
04161 << "Unknown inputs scaling method: " << new_inputs_scaling_method << "." <<std::endl;
04162
04163 exit(1);
04164 }
04165 }
04166
04167
04168
04169
04170 void MultilayerPerceptron::set_outputs_unscaling_method(const std::string& new_outputs_unscaling_method)
04171 {
04172 if(new_outputs_unscaling_method == "None")
04173 {
04174 outputs_unscaling_method = MultilayerPerceptron::None;
04175 }
04176 else if(new_outputs_unscaling_method == "MeanStandardDeviation")
04177 {
04178 outputs_unscaling_method = MultilayerPerceptron::MeanStandardDeviation;
04179 }
04180 else if(new_outputs_unscaling_method == "MinimumMaximum")
04181 {
04182 outputs_unscaling_method = MultilayerPerceptron::MinimumMaximum;
04183 }
04184 else
04185 {
04186 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04187 << "void set_outputs_unscaling_method(const std::string&) method." << std::endl
04188 << "Unknown outputs unscaling method: " << new_outputs_unscaling_method << "." <<std::endl;
04189
04190 exit(1);
04191 }
04192 }
04193
04194
04195
04196
04201
04202 void MultilayerPerceptron::set_input_variables_name(const Vector<std::string>& new_input_variables_name)
04203 {
04204
04205
04206 #ifdef _DEBUG
04207
04208 int size = new_input_variables_name.get_size();
04209
04210 if(size != 0 && size != inputs_number)
04211 {
04212 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04213 << "void set_input_variables_name(const Vector<std::string>&) method." << std::endl
04214 << "Size of name of input variables vector must be either zero or number of inputs." << std::endl;
04215
04216 exit(1);
04217 }
04218
04219 #endif
04220
04221
04222
04223 input_variables_name = new_input_variables_name;
04224 }
04225
04226
04227
04228
04234
04235 void MultilayerPerceptron::
04236 set_input_variable_name(int input_variable_index, const std::string& new_input_variable_name)
04237 {
04238
04239
04240 #ifdef _DEBUG
04241
04242 if(input_variable_index < 0 || input_variable_index >= inputs_number)
04243 {
04244 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04245 << "void set_input_variable_name(int, const std::string&) method." << std::endl
04246 << "Index of input variable must be equal or greater than zero or less than number of inputs."
04247 << std::endl;
04248
04249 exit(1);
04250 }
04251
04252 #endif
04253
04254 if(input_variables_name.get_size() != inputs_number)
04255 {
04256 input_variables_name.set(inputs_number);
04257 }
04258
04259
04260
04261 input_variables_name[input_variable_index] = new_input_variable_name;
04262 }
04263
04264
04265
04266
04271
04272 void MultilayerPerceptron::set_output_variables_name(const Vector<std::string>& new_output_variables_name)
04273 {
04274
04275
04276 #ifdef _DEBUG
04277
04278 int size = new_output_variables_name.get_size();
04279
04280 if(size != 0 && size != outputs_number)
04281 {
04282 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04283 << "void set_output_variables_name(const Vector<std::string>&) method." << std::endl
04284 << "Size of name of output variables vector must be equal to number of outputs." << std::endl;
04285
04286 exit(1);
04287 }
04288
04289 #endif
04290
04291
04292
04293 output_variables_name = new_output_variables_name;
04294 }
04295
04296
04297
04298
04304
04305 void MultilayerPerceptron::set_output_variable_name(int output_layer_index, const std::string& new_output_variable_name)
04306 {
04307
04308
04309 #ifdef _DEBUG
04310
04311 if(output_layer_index < 0 || output_layer_index >= outputs_number)
04312 {
04313 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04314 << "void set_output_variable_name(int, const std::string&) method." << std::endl
04315 << "Index of output varaible must be equal or greater than zero less than number of outputs."
04316 << std::endl;
04317
04318 exit(1);
04319 }
04320
04321 #endif
04322
04323 if(output_variables_name.get_size() != outputs_number)
04324 {
04325 output_variables_name.set(outputs_number);
04326 }
04327
04328
04329
04330 output_variables_name[output_layer_index] = new_output_variable_name;
04331 }
04332
04333
04334
04335
04340
04341 void MultilayerPerceptron::set_input_variables_units(const Vector<std::string>& new_input_variables_units)
04342 {
04343
04344
04345 #ifdef _DEBUG
04346
04347 int size = new_input_variables_units.get_size();
04348
04349 if(size != 0 && size != inputs_number)
04350 {
04351 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04352 << "void set_input_variables_units(const Vector<std::string>&) method." << std::endl
04353 << "Size must be either zero or number of inputs." << std::endl;
04354
04355 exit(1);
04356 }
04357
04358 #endif
04359
04360
04361
04362 input_variables_units = new_input_variables_units;
04363 }
04364
04365
04366
04367
04373
04374 void MultilayerPerceptron::set_input_variable_units(int input_variable_index, const std::string& new_input_variable_units)
04375 {
04376
04377
04378 #ifdef _DEBUG
04379
04380 if(input_variable_index < 0 || input_variable_index >= inputs_number)
04381 {
04382 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04383 << "void set_input_variable_units(int, const std::string&) method." << std::endl
04384 << "Index of input variable must be equal or greater than zero and less than number of inputs."
04385 << std::endl;
04386
04387 exit(1);
04388 }
04389
04390 #endif
04391
04392 if(input_variables_units.get_size() != inputs_number)
04393 {
04394 input_variables_units.set(inputs_number);
04395 }
04396
04397
04398
04399 input_variables_units[input_variable_index] = new_input_variable_units;
04400 }
04401
04402
04403
04404
04409
04410 void MultilayerPerceptron::set_output_variables_units(const Vector<std::string>& new_output_variables_units)
04411 {
04412
04413
04414 #ifdef _DEBUG
04415
04416 int size = new_output_variables_units.get_size();
04417
04418 if(size != 0 && size != outputs_number)
04419 {
04420 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04421 << "void set_output_variables_units(const Vector<std::string>&) method." << std::endl
04422 << "Size must be either zero or number of outputs." << std::endl;
04423
04424 exit(1);
04425 }
04426
04427 #endif
04428
04429
04430
04431 output_variables_units = new_output_variables_units;
04432 }
04433
04434
04435
04436
04442
04443 void MultilayerPerceptron
04444 ::set_output_variable_units(int output_variable_index, const std::string& new_output_variable_units)
04445 {
04446
04447
04448 #ifdef _DEBUG
04449
04450 if(output_variable_index < 0 || output_variable_index >= outputs_number)
04451 {
04452 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04453 << "void set_output_variable_units(int, const std::string&) method." << std::endl
04454 << "Index of output variable must be equal or greater than zero and less than number of outputs."
04455 << std::endl;
04456
04457 exit(1);
04458 }
04459
04460 #endif
04461
04462 if(output_variables_units.get_size() != outputs_number)
04463 {
04464 output_variables_units.set(outputs_number);
04465 }
04466
04467
04468
04469 output_variables_units[output_variable_index] = new_output_variable_units;
04470 }
04471
04472
04473
04474
04479
04480 void MultilayerPerceptron::set_input_variables_description(const Vector<std::string>& new_input_variables_description)
04481 {
04482
04483
04484 #ifdef _DEBUG
04485
04486 int size = new_input_variables_description.get_size();
04487
04488 if(size != 0 && size != inputs_number)
04489 {
04490 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04491 << "void set_input_variables_description(const Vector<std::string>&) method." << std::endl
04492 << "Size must be either zero or number of inputs." << std::endl;
04493
04494 exit(1);
04495 }
04496
04497 #endif
04498
04499
04500
04501 input_variables_description = new_input_variables_description;
04502 }
04503
04504
04505
04506
04512
04513 void MultilayerPerceptron
04514 ::set_input_variable_description(int input_variable_index, const std::string& new_input_variable_description)
04515 {
04516
04517
04518 #ifdef _DEBUG
04519
04520 if(input_variable_index < 0 || input_variable_index >= inputs_number)
04521 {
04522 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04523 << "void set_input_variable_description(int, const std::string&) method." << std::endl
04524 << "Index of input variable must be equal or greater than zero and less than number of inputs."
04525 << std::endl;
04526
04527 exit(1);
04528 }
04529
04530 #endif
04531
04532 if(input_variables_description.get_size() != inputs_number)
04533 {
04534 input_variables_description.set(inputs_number);
04535 }
04536
04537
04538
04539 input_variables_description[input_variable_index] = new_input_variable_description;
04540 }
04541
04542
04543
04544
04549
04550 void MultilayerPerceptron::set_output_variables_description(const Vector<std::string>& new_output_variables_description)
04551 {
04552
04553
04554 #ifdef _DEBUG
04555
04556 int size = new_output_variables_description.get_size();
04557
04558 if(size != 0 && size != outputs_number)
04559 {
04560 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04561 << "void set_output_variables_description(const Vector<std::string>&) method." << std::endl
04562 << "Size must be either zero or number of outputs." << std::endl;
04563
04564 exit(1);
04565 }
04566
04567 #endif
04568
04569
04570
04571 output_variables_description = new_output_variables_description;
04572 }
04573
04574
04575
04576
04582
04583 void MultilayerPerceptron
04584 ::set_output_variable_description(int output_variable_index, const std::string& new_output_variable_description)
04585 {
04586
04587
04588 #ifdef _DEBUG
04589
04590 if(output_variable_index < 0 || output_variable_index >= outputs_number)
04591 {
04592 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04593 << "void set_output_variable_description(int, const std::string&) method." << std::endl
04594 << "Index of output variable must be equal or greater than zero and less than number of outputs."
04595 << std::endl;
04596
04597 exit(1);
04598 }
04599
04600 #endif
04601
04602 if(output_variables_description.get_size() != outputs_number)
04603 {
04604 output_variables_description.set(outputs_number);
04605 }
04606
04607
04608
04609 output_variables_description[output_variable_index] = new_output_variable_description;
04610 }
04611
04612
04613
04614
04628
04629 void MultilayerPerceptron::set_variables_information(const Vector< Vector<std::string> >& new_variables_information)
04630 {
04631
04632
04633 #ifdef _DEBUG
04634
04635 int size = new_variables_information.get_size();
04636
04637 if(size != 6)
04638 {
04639 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04640 << "void set_variables_information(const Vector< Vector<std::string> >&) method." << std::endl
04641 << "Size of vector must be 6." << std::endl;
04642
04643 exit(1);
04644 }
04645
04646 for(int i = 0; i < 3; i++)
04647 {
04648 size = new_variables_information[i].get_size();
04649
04650 if(size != 0 && size != inputs_number)
04651 {
04652 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04653 << "void set_variables_information(const Vector< Vector<std::string> >&) method." << std::endl
04654 << "Size of element " << i << " must be equal to number of inputs." << std::endl;
04655
04656 exit(1);
04657 }
04658 }
04659
04660 for(int i = 3; i < 6; i++)
04661 {
04662 size = new_variables_information[i].get_size();
04663
04664 if(size != 0 && size != outputs_number)
04665 {
04666 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04667 << "void set_variables_information(const Vector< Vector<std::string> >&) method." << std::endl
04668 << "Size of element " << i << " must be equal to number of outputs." << std::endl;
04669
04670 exit(1);
04671 }
04672 }
04673
04674 #endif
04675
04676
04677
04678 set_input_variables_name(new_variables_information[0]);
04679 set_input_variables_units(new_variables_information[1]);
04680 set_input_variables_description(new_variables_information[2]);
04681
04682 set_output_variables_name(new_variables_information[3]);
04683 set_output_variables_units(new_variables_information[4]);
04684 set_output_variables_description(new_variables_information[5]);
04685 }
04686
04687
04688
04689
04695
04696 void MultilayerPerceptron::set_input_variables_mean(const Vector<double>& new_input_variables_mean)
04697 {
04698
04699
04700 #ifdef _DEBUG
04701
04702 if(new_input_variables_mean.get_size() != inputs_number)
04703 {
04704 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04705 << "void set_input_variables_mean(const Vector<double>&) method." << std::endl
04706 << "Size must be equal to number of inputs." << std::endl;
04707
04708 exit(1);
04709 }
04710
04711 #endif
04712
04713
04714
04715 input_variables_mean = new_input_variables_mean;
04716 }
04717
04718
04719
04720
04727
04728 void MultilayerPerceptron::set_input_variable_mean(int input_variable_index, double new_input_variable_mean)
04729 {
04730
04731
04732 #ifdef _DEBUG
04733
04734 if(input_variable_index < 0 || input_variable_index >= inputs_number)
04735 {
04736 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04737 << "void set_input_variable_mean(int, double) method." << std::endl
04738 << "Index of input variable must be equal or greater than zero or less than number of inputs."
04739 << std::endl;
04740
04741 exit(1);
04742 }
04743
04744 #endif
04745
04746
04747
04748 if(input_variables_mean.get_size() != inputs_number)
04749 {
04750 input_variables_mean.set(inputs_number, 0.0);
04751 }
04752
04753
04754
04755 input_variables_mean[input_variable_index] = new_input_variable_mean;
04756 }
04757
04758
04759
04760
04766
04767 void MultilayerPerceptron::set_output_variables_mean(const Vector<double>& new_output_variables_mean)
04768 {
04769
04770
04771 #ifdef _DEBUG
04772
04773 if(new_output_variables_mean.get_size() != outputs_number)
04774 {
04775 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04776 << "void set_output_variables_mean(const Vector<double>&) method." << std::endl
04777 << "Size must be equal to number of outputs." << std::endl;
04778
04779 exit(1);
04780 }
04781
04782 #endif
04783
04784
04785
04786 output_variables_mean = new_output_variables_mean;
04787 }
04788
04789
04790
04791
04798
04799 void MultilayerPerceptron::set_output_variable_mean(int output_variable_index, double new_output_variable_mean)
04800 {
04801
04802
04803 #ifdef _DEBUG
04804
04805 if(output_variable_index < 0 || output_variable_index >= outputs_number)
04806 {
04807 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04808 << "void set_output_variable_mean(int, double) method." << std::endl
04809 << "Index must be less than number of outputs." << std::endl;
04810
04811 exit(1);
04812 }
04813
04814 #endif
04815
04816
04817
04818 if(output_variables_mean.get_size() != outputs_number)
04819 {
04820 output_variables_mean.set(outputs_number, 0.0);
04821 }
04822
04823
04824
04825 output_variables_mean[output_variable_index] = new_output_variable_mean;
04826 }
04827
04828
04829
04830
04837
04838 void MultilayerPerceptron
04839 ::set_input_variables_standard_deviation(const Vector<double>& new_input_variables_standard_deviation)
04840 {
04841
04842
04843 #ifdef _DEBUG
04844
04845 if(new_input_variables_standard_deviation.get_size() != inputs_number)
04846 {
04847 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04848 << "void set_input_variables_standard_deviation(const Vector<double>&) method." << std::endl
04849 << "Size must be equal to number of inputs." << std::endl;
04850
04851 exit(1);
04852 }
04853
04854 #endif
04855
04856
04857
04858 input_variables_standard_deviation = new_input_variables_standard_deviation;
04859 }
04860
04861
04862
04863
04870
04871 void MultilayerPerceptron
04872 ::set_input_variable_standard_deviation(int input_variable_index, double new_input_variable_standard_deviation)
04873 {
04874
04875
04876 #ifdef _DEBUG
04877
04878 if(input_variable_index < 0 || input_variable_index >= inputs_number)
04879 {
04880 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04881 << "void set_input_variable_standard_deviation(int, double) method." << std::endl
04882 << "Index of input variable must be equal or greater than zero and less than number of inputs."
04883 << std::endl;
04884
04885 exit(1);
04886 }
04887
04888 #endif
04889
04890
04891
04892 if(input_variables_standard_deviation.get_size() != inputs_number)
04893 {
04894 input_variables_standard_deviation.set(inputs_number, 1.0);
04895 }
04896
04897
04898
04899 input_variables_standard_deviation[input_variable_index] = new_input_variable_standard_deviation;
04900 }
04901
04902
04903
04904
04911
04912 void MultilayerPerceptron
04913 ::set_output_variables_standard_deviation(const Vector<double>& new_output_variables_standard_deviation)
04914 {
04915
04916
04917 #ifdef _DEBUG
04918
04919 if(new_output_variables_standard_deviation.get_size() != outputs_number)
04920 {
04921 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04922 << "void set_output_variables_standard_deviation(const Vector<double>&) method." << std::endl
04923 << "Size must be equal to number of outputs." << std::endl;
04924
04925 exit(1);
04926 }
04927
04928 #endif
04929
04930
04931
04932 output_variables_standard_deviation = new_output_variables_standard_deviation;
04933 }
04934
04935
04936
04937
04944
04945 void MultilayerPerceptron
04946 ::set_output_variable_standard_deviation(int output_variable_index, double new_output_variable_standard_deviation)
04947 {
04948
04949
04950 #ifdef _DEBUG
04951
04952 if(output_variable_index < 0 || output_variable_index >= outputs_number)
04953 {
04954 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04955 << "void set_output_variable_standard_deviation(int, double) method." << std::endl
04956 << "Index must be equal or greater than zero and less than number of outputs." << std::endl;
04957
04958 exit(1);
04959 }
04960
04961 #endif
04962
04963
04964
04965 if(output_variables_standard_deviation.get_size() != outputs_number)
04966 {
04967 output_variables_standard_deviation.set(outputs_number, 1.0);
04968 }
04969
04970
04971
04972 output_variables_standard_deviation[output_variable_index] = new_output_variable_standard_deviation;
04973 }
04974
04975
04976
04977
04987
04988 void MultilayerPerceptron::set_input_variables_mean_standard_deviation
04989 (const Vector< Vector<double> >& new_input_variables_mean_standard_deviation)
04990 {
04991
04992
04993 #ifdef _DEBUG
04994
04995 if(new_input_variables_mean_standard_deviation.get_size() != 2)
04996 {
04997 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
04998 << "void set_input_variables_mean_standard_deviation(const Vector< Vector<double> >&) method." << std::endl
04999 << "Size of vector must be 2." << std::endl;
05000
05001 exit(1);
05002 }
05003 else if(new_input_variables_mean_standard_deviation[0].get_size() != inputs_number
05004 && new_input_variables_mean_standard_deviation[1].get_size() != inputs_number)
05005 {
05006 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05007 << "void set_input_variables_mean_standard_deviation(const Vector< Vector<double> >&) method." << std::endl
05008 << "Number of columns must be equal to number of inputs." << std::endl;
05009
05010 exit(1);
05011 }
05012
05013
05014
05015 for(int i = 0; i < inputs_number; i++)
05016 {
05017 if(new_input_variables_mean_standard_deviation[1][i] < 1.0e-99)
05018 {
05019 std::cerr << "Flood Error: MultilayerPerceptron class: " << std::endl
05020 << "void set_input_variables_mean_standard_deviation(const Vector< Vector<double> >&) method."
05021 << std::endl
05022 << "Standard deviation of input variable "<< i << " is zero." << std::endl;
05023
05024 exit(1);
05025 }
05026 }
05027
05028 #endif
05029
05030
05031
05032 input_variables_mean = new_input_variables_mean_standard_deviation[0];
05033 input_variables_standard_deviation = new_input_variables_mean_standard_deviation[1];
05034 }
05035
05036
05037
05038
05046
05047 void MultilayerPerceptron::set_output_variables_mean_standard_deviation
05048 (const Vector< Vector<double> >& new_output_variables_mean_standard_deviation)
05049 {
05050
05051
05052 #ifdef _DEBUG
05053
05054 if(new_output_variables_mean_standard_deviation.get_size() != 2)
05055 {
05056 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05057 << "void set_output_variables_mean_standard_deviation(const Vector< Vector<double> >&) method."
05058 << std::endl
05059 << "Size of vector must be 2." << std::endl;
05060
05061 exit(1);
05062 }
05063 else if(new_output_variables_mean_standard_deviation[0].get_size() != outputs_number
05064 && new_output_variables_mean_standard_deviation[1].get_size() != outputs_number)
05065 {
05066 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05067 << "void set_output_variables_mean_standard_deviation(const Vector< Vector<double> >&) method."
05068 << std::endl
05069 << "Number of columns must be equal to number of outputs." << std::endl;
05070
05071 exit(1);
05072 }
05073
05074 #endif
05075
05076
05077
05078 output_variables_mean = new_output_variables_mean_standard_deviation[0];
05079 output_variables_standard_deviation = new_output_variables_mean_standard_deviation[1];
05080 }
05081
05082
05083
05084
05089
05090 void MultilayerPerceptron::set_input_variables_minimum(const Vector<double>& new_input_variables_minimum)
05091 {
05092
05093
05094 #ifdef _DEBUG
05095
05096 if(new_input_variables_minimum.get_size() != inputs_number)
05097 {
05098 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05099 << "void set_input_variables_minimum(const Vector<double>&) method." << std::endl
05100 << "Size must be equal to number of inputs." << std::endl;
05101
05102 exit(1);
05103 }
05104
05105 #endif
05106
05107
05108
05109 input_variables_minimum = new_input_variables_minimum;
05110 }
05111
05112
05113
05114
05120
05121 void MultilayerPerceptron::set_input_variable_minimum(int input_variable_index, double new_input_variable_minimum)
05122 {
05123
05124
05125 #ifdef _DEBUG
05126
05127 if(input_variable_index < 0 || input_variable_index >= inputs_number)
05128 {
05129 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05130 << "void set_input_variable_minimum(int, double) method." << std::endl
05131 << "Index of input variable must be equal or greater than zero and less than number of inputs."
05132 << std::endl;
05133
05134 exit(1);
05135 }
05136
05137 #endif
05138
05139 if(input_variables_minimum.get_size() == 0)
05140 {
05141 input_variables_minimum.set(inputs_number, -1.0);
05142 }
05143
05144
05145
05146 input_variables_minimum[input_variable_index] = new_input_variable_minimum;
05147 }
05148
05149
05150
05151
05157
05158 void MultilayerPerceptron::set_output_variables_minimum(const Vector<double>& new_output_variables_minimum)
05159 {
05160
05161
05162 #ifdef _DEBUG
05163
05164 if(new_output_variables_minimum.get_size() != outputs_number)
05165 {
05166 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05167 << "void set_output_variables_minimum(const Vector<double>&) method." << std::endl
05168 << "Size must be equal to number of outputs." << std::endl;
05169
05170 exit(1);
05171 }
05172
05173 #endif
05174
05175
05176
05177 output_variables_minimum = new_output_variables_minimum;
05178 }
05179
05180
05181
05182
05189
05190 void MultilayerPerceptron::set_output_variable_minimum(int output_variable_index, double new_output_variable_minimum)
05191 {
05192
05193
05194 #ifdef _DEBUG
05195
05196 if(output_variable_index < 0 || output_variable_index >= outputs_number)
05197 {
05198 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05199 << "void set_output_variable_minimum(int, double) method." << std::endl
05200 << "Index of output variable must be equal or greater than zero and less than number of outputs."
05201 << std::endl;
05202
05203 exit(1);
05204 }
05205
05206 #endif
05207
05208 if(output_variables_minimum.get_size() == 0)
05209 {
05210 output_variables_minimum.set(outputs_number, -1.0);
05211 }
05212
05213
05214
05215 output_variables_minimum[output_variable_index] = new_output_variable_minimum;
05216 }
05217
05218
05219
05220
05225
05226 void MultilayerPerceptron::set_input_variables_maximum(const Vector<double>& new_input_variables_maximum)
05227 {
05228
05229
05230 #ifdef _DEBUG
05231
05232 if(new_input_variables_maximum.get_size() != inputs_number)
05233 {
05234 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05235 << "void set_input_variables_maximum(const Vector<double>&) method." << std::endl
05236 << "Size must be equal to number of inputs." << std::endl;
05237
05238 exit(1);
05239 }
05240
05241 #endif
05242
05243
05244
05245 input_variables_maximum = new_input_variables_maximum;
05246 }
05247
05248
05249
05250
05256
05257 void MultilayerPerceptron::set_input_variable_maximum(int input_variable_index, double new_input_variable_maximum)
05258 {
05259
05260
05261 #ifdef _DEBUG
05262
05263 if(input_variable_index < 0 || input_variable_index >= inputs_number)
05264 {
05265 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05266 << "void set_input_variable_maximum(int, double) method." << std::endl
05267 << "Index of input variable must be equal or greater than zero and less than number of inputs."
05268 << std::endl;
05269
05270 exit(1);
05271 }
05272
05273 #endif
05274
05275 if(input_variables_maximum.get_size() == 0)
05276 {
05277 input_variables_maximum.set(inputs_number, 1.0);
05278 }
05279
05280
05281
05282 input_variables_maximum[input_variable_index] = new_input_variable_maximum;
05283 }
05284
05285
05286
05287
05293
05294 void MultilayerPerceptron::set_output_variables_maximum(const Vector<double>& new_output_variables_maximum)
05295 {
05296
05297
05298 #ifdef _DEBUG
05299
05300 if(new_output_variables_maximum.get_size() != outputs_number)
05301 {
05302 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05303 << "void set_output_variables_maximum(const Vector<double>&) method." << std::endl
05304 << "Size must be equal to number of outputs." << std::endl;
05305
05306 exit(1);
05307 }
05308
05309 #endif
05310
05311
05312
05313 output_variables_maximum = new_output_variables_maximum;
05314 }
05315
05316
05317
05318
05325
05326 void MultilayerPerceptron::set_output_variable_maximum(int output_variable_index, double new_output_variable_maximum)
05327 {
05328
05329
05330 #ifdef _DEBUG
05331
05332 if(output_variable_index < 0 || output_variable_index >= outputs_number)
05333 {
05334 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05335 << "void new_output_variable_maximum(int, double) method." << std::endl
05336 << "Index must be equal or greater than zero and less than number of outputs." << std::endl;
05337
05338 exit(1);
05339 }
05340
05341 #endif
05342
05343 if(output_variables_maximum.get_size() == 0)
05344 {
05345 output_variables_maximum.set(outputs_number, 1.0);
05346 }
05347
05348
05349
05350 output_variables_maximum[output_variable_index] = new_output_variable_maximum;
05351 }
05352
05353
05354
05355
05364
05365 void MultilayerPerceptron
05366 ::set_input_variables_minimum_maximum(const Vector< Vector<double> >& new_input_variables_minimum_maximum)
05367 {
05368
05369
05370 #ifdef _DEBUG
05371
05372 if(new_input_variables_minimum_maximum.get_size() != 2)
05373 {
05374 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05375 << "void set_input_variables_minimum_maximum(const Vector< Vector<double> >&) method." << std::endl
05376 << "Number of rows must be 2." << std::endl;
05377
05378 exit(1);
05379 }
05380 else if(new_input_variables_minimum_maximum[0].get_size() != inputs_number
05381 && new_input_variables_minimum_maximum[1].get_size() != inputs_number)
05382 {
05383 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05384 << "void set_input_variables_minimum_maximum(const Vector< Vector<double> >&) method." << std::endl
05385 << "Number of columns must be equal to number of inputs." << std::endl;
05386
05387 exit(1);
05388 }
05389
05390
05391
05392 for(int i = 0; i < inputs_number; i++)
05393 {
05394 if(new_input_variables_minimum_maximum[0][i] >= new_input_variables_minimum_maximum[1][i])
05395 {
05396 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05397 << "void set_input_variables_minimum_maximum(const Vector< Vector<double> >&) method." << std::endl
05398 << "Minimum of input variable " << i << " is equal or greater than maximum of that variable."
05399 << std::endl;
05400
05401 exit(1);
05402 }
05403 }
05404
05405 #endif
05406
05407
05408
05409 input_variables_minimum = new_input_variables_minimum_maximum[0];
05410 input_variables_maximum = new_input_variables_minimum_maximum[1];
05411 }
05412
05413
05414
05415
05424
05425 void MultilayerPerceptron
05426 ::set_output_variables_minimum_maximum(const Vector< Vector<double> >& new_output_variables_minimum_maximum)
05427 {
05428
05429
05430 #ifdef _DEBUG
05431
05432 if(new_output_variables_minimum_maximum.get_size() != 2)
05433 {
05434 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05435 << "void set_output_variables_minimum_maximum(const Vector< Vector<double> >&) method." << std::endl
05436 << "Number of rows must be 2." << std::endl;
05437
05438 exit(1);
05439 }
05440 else if(new_output_variables_minimum_maximum[0].get_size() != outputs_number
05441 && new_output_variables_minimum_maximum[1].get_size() != outputs_number)
05442 {
05443 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05444 << "void set_output_variables_minimum_maximum(const Vector< Vector<double> >&) method." << std::endl
05445 << "Number of columns must be equal to number of outputs." << std::endl;
05446
05447 exit(1);
05448 }
05449
05450
05451
05452 for(int i = 0; i < outputs_number; i++)
05453 {
05454 if(new_output_variables_minimum_maximum[0][i] > new_output_variables_minimum_maximum[1][i])
05455 {
05456 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05457 << "void set_output_variables_minimum_maximum(const Vector< Vector<double> >&) method." << std::endl
05458 << "Minimum of output variable " << i << " is greater than maximum of that variable."
05459 << std::endl;
05460
05461 exit(1);
05462 }
05463 }
05464
05465 #endif
05466
05467
05468
05469 output_variables_minimum = new_output_variables_minimum_maximum[0];
05470 output_variables_maximum = new_output_variables_minimum_maximum[0];
05471 }
05472
05473
05474
05475
05480
05481 void MultilayerPerceptron::set_output_variables_lower_bound(const Vector<double>& new_output_variables_lower_bound)
05482 {
05483
05484
05485 #ifdef _DEBUG
05486
05487 if(new_output_variables_lower_bound.get_size() != outputs_number)
05488 {
05489 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05490 << "void set_output_variables_lower_bound(const Vector<double>&) method." << std::endl
05491 << "Size must be equal to number of outputs." << std::endl;
05492
05493 exit(1);
05494 }
05495
05496 #endif
05497
05498
05499
05500 output_variables_lower_bound = new_output_variables_lower_bound;
05501 }
05502
05503
05504
05505
05511
05512 void MultilayerPerceptron
05513 ::set_output_variable_lower_bound(int output_variable_index, double new_output_variable_lower_bound)
05514 {
05515
05516
05517 #ifdef _DEBUG
05518
05519 if(output_variable_index < 0 || output_variable_index >= outputs_number)
05520 {
05521 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05522 << "void set_output_variable_lower_bound(int, double) method." << std::endl
05523 << "Index of output variable must be greater than zero and less than number of outputs."
05524 << std::endl;
05525
05526 exit(1);
05527 }
05528
05529 #endif
05530
05531 if(output_variables_lower_bound.get_size() != outputs_number)
05532 {
05533 output_variables_lower_bound.set(outputs_number, -1.0e99);
05534 }
05535
05536
05537
05538 output_variables_lower_bound[output_variable_index] = new_output_variable_lower_bound;
05539 }
05540
05541
05542
05543
05548
05549 void MultilayerPerceptron::set_output_variables_upper_bound(const Vector<double>& new_output_variables_upper_bound)
05550 {
05551
05552
05553 #ifdef _DEBUG
05554
05555 if(new_output_variables_upper_bound.get_size() != outputs_number)
05556 {
05557 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05558 << "void set_output_variables_upper_bound(const Vector<double>&) method." << std::endl
05559 << "Size must be equal to number of outputs." << std::endl;
05560
05561 exit(1);
05562 }
05563
05564 #endif
05565
05566
05567
05568 output_variables_upper_bound = new_output_variables_upper_bound;
05569 }
05570
05571
05572
05573
05579
05580 void MultilayerPerceptron
05581 ::set_output_variable_upper_bound(int output_variable_index, double new_output_variable_upper_bound)
05582 {
05583
05584
05585 #ifdef _DEBUG
05586
05587 if(output_variable_index < 0 || output_variable_index >= outputs_number)
05588 {
05589 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05590 << "void set_output_variable_upper_bound(int, double) method." << std::endl
05591 << "Index of output variable must be equal or greater than zero or less than number of outputs."
05592 << std::endl;
05593
05594 exit(1);
05595 }
05596
05597 #endif
05598
05599 if(output_variables_upper_bound.get_size() != outputs_number)
05600 {
05601 output_variables_upper_bound.set(outputs_number, 1.0e99);
05602 }
05603
05604
05605
05606 output_variables_upper_bound[output_variable_index] = new_output_variable_upper_bound;
05607 }
05608
05609
05610
05611
05621
05622 void MultilayerPerceptron
05623 ::set_output_variables_bounds(const Vector< Vector<double> >& new_output_variables_bounds)
05624 {
05625
05626
05627 #ifdef _DEBUG
05628
05629 int size = new_output_variables_bounds.get_size();
05630
05631 if(size != 2)
05632 {
05633 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05634 << "void set_output_variables_bounds(const Vector< Vector<double> >&) method." << std::endl
05635 << "Number of rows must be 2." << std::endl;
05636
05637 exit(1);
05638 }
05639 else if(new_output_variables_bounds[0].get_size() != outputs_number
05640 && new_output_variables_bounds[1].get_size() != outputs_number)
05641 {
05642 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05643 << "void set_output_variables_bounds(const Vector< Vector<double> >&) method." << std::endl
05644 << "Number of columns must be equal to number of outputs." << std::endl;
05645
05646 exit(1);
05647 }
05648
05649 #endif
05650
05651
05652
05653 output_variables_lower_bound = new_output_variables_bounds[0];
05654 output_variables_upper_bound = new_output_variables_bounds[1];
05655 }
05656
05657
05658
05659
05675
05676 void MultilayerPerceptron::set_variables_statistics(const Vector< Vector<double> >& new_variables_statistics)
05677 {
05678
05679
05680 #ifdef _DEBUG
05681
05682 int size = new_variables_statistics.get_size();
05683
05684 if(size != 8)
05685 {
05686 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05687 << "void set_variables_statistics(const Vector< Vector<double> >&) method." << std::endl
05688 << "Size must be 10." << std::endl;
05689
05690 exit(1);
05691 }
05692
05693 for(int i = 0; i < 4; i++)
05694 {
05695 size = new_variables_statistics[i].get_size();
05696
05697 if(size != inputs_number)
05698 {
05699 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05700 << "void set_variables_statistics(const Vector< Vector<double> >&) method." << std::endl
05701 << "Size of element " << i << " must be equal to number of inputs." << std::endl;
05702
05703 exit(1);
05704 }
05705 }
05706
05707 for(int i = 4; i < 8; i++)
05708 {
05709 size = new_variables_statistics[i].get_size();
05710
05711 if(size != outputs_number)
05712 {
05713 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05714 << "void set_variables_statistics(const Vector< Vector<double> >&) method." << std::endl
05715 << "Size of element " << i << " must be equal to number of outputs." << std::endl;
05716
05717 exit(1);
05718 }
05719 }
05720
05721 #endif
05722
05723
05724
05725 set_input_variables_mean(new_variables_statistics[0]);
05726 set_input_variables_standard_deviation(new_variables_statistics[1]);
05727 set_input_variables_minimum(new_variables_statistics[2]);
05728 set_input_variables_maximum(new_variables_statistics[3]);
05729
05730 set_output_variables_mean(new_variables_statistics[4]);
05731 set_output_variables_standard_deviation(new_variables_statistics[5]);
05732 set_output_variables_minimum(new_variables_statistics[6]);
05733 set_output_variables_maximum(new_variables_statistics[7]);
05734 }
05735
05736
05737
05738
05743
05744 void MultilayerPerceptron::set_numerical_differentiation_method
05745 (const MultilayerPerceptron::NumericalDifferentiationMethod& new_numerical_differentiation_method)
05746 {
05747 numerical_differentiation_method = new_numerical_differentiation_method;
05748 }
05749
05750
05751
05752
05753 void MultilayerPerceptron::set_numerical_differentiation_method(const std::string& new_numerical_differentiation_method)
05754 {
05755 if(new_numerical_differentiation_method == "ForwardDifferences")
05756 {
05757 numerical_differentiation_method = ForwardDifferences;
05758 }
05759 else if(new_numerical_differentiation_method == "CentralDifferences")
05760 {
05761 numerical_differentiation_method = CentralDifferences;
05762 }
05763 else
05764 {
05765 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05766 << "void set_numerical_differentiation_method(const std::string&) method." << std::endl
05767 << "Unknown numerical differentiation method: " << new_numerical_differentiation_method << "." <<std::endl;
05768
05769 exit(1);
05770 }
05771 }
05772
05773
05774
05775
05776 void MultilayerPerceptron::set_numerical_epsilon_method
05777 (const MultilayerPerceptron::NumericalEpsilonMethod& new_numerical_epsilon_method)
05778 {
05779 numerical_epsilon_method = new_numerical_epsilon_method;
05780 }
05781
05782
05783
05784
05785 void MultilayerPerceptron::set_numerical_epsilon_method(const std::string& new_numerical_epsilon_method)
05786 {
05787 if(new_numerical_epsilon_method == "Absolute")
05788 {
05789 numerical_epsilon_method = Absolute;
05790 }
05791 else if(new_numerical_epsilon_method == "Relative")
05792 {
05793 numerical_epsilon_method = Relative;
05794 }
05795 else
05796 {
05797 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05798 << "void set_numerical_epsilon_method(const std::string&) method." << std::endl
05799 << "Unknown numerical epsilon method: " << new_numerical_epsilon_method << "." <<std::endl;
05800
05801 exit(1);
05802 }
05803 }
05804
05805
05806
05807
05812
05813 void MultilayerPerceptron::set_numerical_epsilon(double new_numerical_epsilon)
05814 {
05815
05816
05817 #ifdef _DEBUG
05818
05819 if(new_numerical_epsilon <= 0.0)
05820 {
05821 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05822 << "void set_numerical_epsilon(double) method." << std::endl
05823 << "Epsilon must be greater than zero." << std::endl;
05824
05825 exit(1);
05826 }
05827
05828 #endif
05829
05830
05831
05832 numerical_epsilon = new_numerical_epsilon;
05833 }
05834
05835
05836
05837
05843
05844 void MultilayerPerceptron::set_independent_parameters_number(int new_independent_parameters_number)
05845 {
05846
05847
05848 #ifdef _DEBUG
05849
05850 if(new_independent_parameters_number < 0)
05851 {
05852 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05853 << "void set_independent_parameters_number(double) method." << std::endl
05854 << "Number of independent parameters must be equal or greater than zero." << std::endl;
05855
05856 exit(1);
05857 }
05858
05859 #endif
05860
05861
05862
05863 independent_parameters.set_size(new_independent_parameters_number);
05864
05865
05866
05867 initialize_independent_parameters_normal(0.0, 1.0);
05868
05869
05870
05871 bound_independent_parameters();
05872 }
05873
05874
05875
05876
05880
05881 void MultilayerPerceptron::set_independent_parameters(const Vector<double>& new_independent_parameters)
05882 {
05883
05884
05885 #ifdef _DEBUG
05886
05887 int independent_parameters_number = get_independent_parameters_number();
05888
05889 if(new_independent_parameters.get_size() != independent_parameters_number)
05890 {
05891 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05892 << "void set_independent_parameters(const Vector<double>&) method." << std::endl
05893 << "Independent parameters vector size must be equal to number of independent parameters."
05894 << std::endl;
05895
05896 exit(1);
05897 }
05898
05899 #endif
05900
05901
05902
05903 independent_parameters = new_independent_parameters;
05904
05905
05906
05907 bound_independent_parameters();
05908 }
05909
05910
05911
05912
05917
05918 void MultilayerPerceptron
05919 ::set_independent_parameter(int independent_parameter_index, double new_independent_parameter)
05920 {
05921
05922
05923 #ifdef _DEBUG
05924
05925 int independent_parameters_number = get_independent_parameters_number();
05926
05927 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
05928 {
05929 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05930 << "void set_independent_parameter(int, double) method." << std::endl
05931 << "Index must be equal or greater than zero and less than number of independent parameters."
05932 << std::endl;
05933
05934 exit(1);
05935 }
05936
05937 #endif
05938
05939
05940
05941 independent_parameters[independent_parameter_index] = new_independent_parameter;
05942
05943 bound_independent_parameter(independent_parameter_index);
05944 }
05945
05946
05947
05948
05952
05953 void MultilayerPerceptron::set_independent_parameters_scaling_method
05954 (const MultilayerPerceptron::ScalingMethod& new_independent_parameters_scaling_method)
05955 {
05956 independent_parameters_scaling_method = new_independent_parameters_scaling_method;
05957 }
05958
05959
05960
05961
05962 void MultilayerPerceptron::set_independent_parameters_scaling_method
05963 (const std::string& new_independent_parameters_scaling_method)
05964 {
05965 if(new_independent_parameters_scaling_method == "None")
05966 {
05967 independent_parameters_scaling_method = MultilayerPerceptron::None;
05968 }
05969 else if(new_independent_parameters_scaling_method == "MeanStandardDeviation")
05970 {
05971 independent_parameters_scaling_method = MultilayerPerceptron::MeanStandardDeviation;
05972 }
05973 else if(new_independent_parameters_scaling_method == "MinimumMaximum")
05974 {
05975 independent_parameters_scaling_method = MultilayerPerceptron::MinimumMaximum;
05976 }
05977 else
05978 {
05979 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
05980 << "void set_independent_parameters_scaling_method(const std::string&) method." << std::endl
05981 << "Unknown independent parameters scaling method: " << new_independent_parameters_scaling_method << "." <<std::endl;
05982
05983 exit(1);
05984 }
05985 }
05986
05987
05988
05989
05994
05995 void MultilayerPerceptron::set_independent_parameters_name(const Vector<std::string>& new_independent_parameters_name)
05996 {
05997
05998
05999 #ifdef _DEBUG
06000
06001 int independent_parameters_number = get_independent_parameters_number();
06002
06003 if(new_independent_parameters_name.get_size() != independent_parameters_number)
06004 {
06005 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06006 << "void set_independent_parameters_name(const Vector<std::string>&) method." << std::endl
06007 << "Name of independent parameters vector size must be equal to number of independent parameters."
06008 << std::endl;
06009
06010 exit(1);
06011 }
06012
06013 #endif
06014
06015
06016
06017 independent_parameters_name = new_independent_parameters_name;
06018 }
06019
06020
06021
06022
06028
06029 void MultilayerPerceptron
06030 ::set_independent_parameter_name(int independent_parameter_index, const std::string& new_independent_parameter_name)
06031 {
06032
06033
06034 int independent_parameters_number = get_independent_parameters_number();
06035
06036 #ifdef _DEBUG
06037
06038 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
06039 {
06040 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06041 << "void set_independent_parameter_name(int, const std::string&) method." << std::endl
06042 << "Index must be equal or greater than zero and less than number of independent parameters."
06043 << std::endl;
06044
06045 exit(1);
06046 }
06047
06048 #endif
06049
06050 if(independent_parameters_name.get_size() != independent_parameters_number)
06051 {
06052 independent_parameters_name.set(independent_parameters_number);
06053 }
06054
06055
06056
06057 independent_parameters_name[independent_parameter_index] = new_independent_parameter_name;
06058 }
06059
06060
06061
06066
06067 void MultilayerPerceptron::set_independent_parameters_units(const Vector<std::string>& new_independent_parameters_units)
06068 {
06069
06070
06071 #ifdef _DEBUG
06072
06073 int independent_parameters_number = get_independent_parameters_number();
06074
06075 if(new_independent_parameters_units.get_size() != independent_parameters_number)
06076 {
06077 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06078 << "void set_independent_parameters_units(const Vector<std::string>&) method." << std::endl
06079 << "Size must be equal to number of independent parameters." << std::endl;
06080
06081 exit(1);
06082 }
06083
06084 #endif
06085
06086
06087
06088 independent_parameters_units = new_independent_parameters_units;
06089 }
06090
06091
06092
06093
06099
06100 void MultilayerPerceptron
06101 ::set_independent_parameter_units(int independent_parameter_index, const std::string& new_independent_parameter_units)
06102 {
06103 int independent_parameters_number = get_independent_parameters_number();
06104
06105
06106
06107 #ifdef _DEBUG
06108
06109 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
06110 {
06111 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06112 << "void set_independent_parameter_units(int, const std::string&) method." << std::endl
06113 << "Index of independent parameter must be equal or greater than zero and less than "
06114 << "number of independent parameters." << std::endl;
06115
06116 exit(1);
06117 }
06118
06119 #endif
06120
06121 if(independent_parameters_units.get_size() == 0)
06122 {
06123 independent_parameters_units.set(independent_parameters_number);
06124 }
06125
06126
06127
06128 independent_parameters_units[independent_parameter_index] = new_independent_parameter_units;
06129 }
06130
06131
06132
06133
06139
06140 void MultilayerPerceptron
06141 ::set_independent_parameters_description(const Vector<std::string>& new_independent_parameters_description)
06142 {
06143
06144
06145 #ifdef _DEBUG
06146
06147 int independent_parameters_number = get_independent_parameters_number();
06148
06149 if(new_independent_parameters_description.get_size() != independent_parameters_number)
06150 {
06151 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06152 << "void set_independent_parameters_description(const Vector<std::string>&) method." << std::endl
06153 << "Size must be equal to number of independent parameters." << std::endl;
06154
06155 exit(1);
06156 }
06157
06158 #endif
06159
06160
06161
06162 independent_parameters_description = new_independent_parameters_description;
06163 }
06164
06165
06166
06167
06173
06174 void MultilayerPerceptron::set_independent_parameter_description
06175 (int independent_parameter_index, const std::string& new_independent_parameter_description)
06176 {
06177 int independent_parameters_number = get_independent_parameters_number();
06178
06179
06180
06181 #ifdef _DEBUG
06182
06183 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
06184 {
06185 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06186 << "void set_independent_parameter_description(int, const std::string&) method." << std::endl
06187 << "Index of independent parameter must be equal or greater than zero and less than "
06188 << "number of independent parameters."
06189 << std::endl;
06190
06191 exit(1);
06192 }
06193
06194 #endif
06195
06196 if(independent_parameters_description.get_size() == 0)
06197 {
06198 independent_parameters_description.set(independent_parameters_number);
06199 }
06200
06201
06202
06203 independent_parameters_description[independent_parameter_index] = new_independent_parameter_description;
06204 }
06205
06206
06207
06208
06215
06216 void MultilayerPerceptron::set_independent_parameters_mean(const Vector<double>& new_independent_parameters_mean)
06217 {
06218
06219
06220 #ifdef _DEBUG
06221
06222 int independent_parameters_number = get_independent_parameters_number();
06223
06224 if(new_independent_parameters_mean.get_size() != independent_parameters_number)
06225 {
06226 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06227 << "void set_independent_parameters_mean(const Vector<double>&) method." << std::endl
06228 << "Size must be equal to number of independent parameters." << std::endl;
06229
06230 exit(1);
06231 }
06232
06233 #endif
06234
06235
06236
06237 independent_parameters_mean = new_independent_parameters_mean;
06238 }
06239
06240
06241
06242
06249
06250 void MultilayerPerceptron
06251 ::set_independent_parameter_mean(int independent_parameter_index, double new_independent_parameter_mean)
06252 {
06253 int independent_parameters_number = get_independent_parameters_number();
06254
06255
06256
06257 #ifdef _DEBUG
06258
06259 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
06260 {
06261 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06262 << "void set_independent_parameter_mean(int, double) method." << std::endl
06263 << "Index of independent parameter must be equal or greater than zero and less than "
06264 << "number of independent parameters." << std::endl;
06265
06266 exit(1);
06267 }
06268
06269 #endif
06270
06271
06272
06273 int size = independent_parameters_mean.get_size();
06274
06275 if(size != independent_parameters_number)
06276 {
06277 independent_parameters_mean.set(independent_parameters_number, 0.0);
06278 }
06279
06280
06281
06282 independent_parameters_mean[independent_parameter_index] = new_independent_parameter_mean;
06283 }
06284
06285
06286
06287
06294
06295 void MultilayerPerceptron
06296 ::set_independent_parameters_standard_deviation(const Vector<double>& new_independent_parameters_standard_deviation)
06297 {
06298
06299
06300 #ifdef _DEBUG
06301
06302 int independent_parameters_number = get_independent_parameters_number();
06303
06304 if(new_independent_parameters_standard_deviation.get_size() != independent_parameters_number)
06305 {
06306 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06307 << "void set_independent_parameters_standard_deviation(const Vector<double>&) method." << std::endl
06308 << "Size must be equal to number of independent parameters." << std::endl;
06309
06310 exit(1);
06311 }
06312
06313 #endif
06314
06315
06316
06317 independent_parameters_standard_deviation = new_independent_parameters_standard_deviation;
06318 }
06319
06320
06321
06322
06329
06330 void MultilayerPerceptron::set_independent_parameter_standard_deviation
06331 (int independent_parameter_index, double new_independent_parameter_standard_deviation)
06332 {
06333 int independent_parameters_number = get_independent_parameters_number();
06334
06335
06336
06337 #ifdef _DEBUG
06338
06339 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
06340 {
06341 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06342 << "void set_independent_parameter_standard_deviation(int, double) method." << std::endl
06343 << "Index of independent parameter must be equal or greater than zero and less than "
06344 << "number of independent parameters."
06345 << std::endl;
06346
06347 exit(1);
06348 }
06349
06350 #endif
06351
06352
06353
06354 int size = independent_parameters_standard_deviation.get_size();
06355
06356 if(size != independent_parameters_number)
06357 {
06358 independent_parameters_standard_deviation.set(independent_parameters_number, 1.0);
06359 }
06360
06361
06362
06363 independent_parameters_standard_deviation[independent_parameter_index] = new_independent_parameter_standard_deviation;
06364 }
06365
06366
06367
06368
06378
06379 void MultilayerPerceptron::set_independent_parameters_mean_standard_deviation
06380 (const Vector< Vector<double> >& new_independent_parameters_mean_standard_deviation)
06381 {
06382
06383
06384 #ifdef _DEBUG
06385
06386 int independent_parameters_number = get_independent_parameters_number();
06387
06388 int size = new_independent_parameters_mean_standard_deviation.get_size();
06389
06390 if(size != 2)
06391 {
06392 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06393 << "void set_independent_parameters_mean_standard_deviation(const Vector< Vector<double> >& ) method."
06394 << std::endl
06395 << "Number of rows must be 2." << std::endl;
06396
06397 exit(1);
06398 }
06399 else if(new_independent_parameters_mean_standard_deviation[0].get_size() != independent_parameters_number
06400 && new_independent_parameters_mean_standard_deviation[1].get_size() != independent_parameters_number)
06401 {
06402 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06403 << "void set_independent_parameters_mean_standard_deviation(const Vector< Vector<double> >& ) method."
06404 << std::endl
06405 << "Number of columns must be equal to number of independent parameters." << std::endl;
06406
06407 exit(1);
06408 }
06409
06410
06411
06412 if(display)
06413 {
06414 for(int i = 0; i < independent_parameters_number; i++)
06415 {
06416 if(new_independent_parameters_mean_standard_deviation[1][i] < 1.0e-99)
06417 {
06418 std::cerr << "Flood Warning: MultilayerPerceptron class: " << std::endl
06419 << "void set_independent_parameters_mean_standard_deviation(const Vector< Vector<double> >& ) method." << std::endl
06420 << "Standard deviation of independent parameter " << i << " is zero." << std::endl;
06421 }
06422 }
06423 }
06424
06425 #endif
06426
06427
06428
06429 independent_parameters_mean = new_independent_parameters_mean_standard_deviation[0];
06430 independent_parameters_standard_deviation = new_independent_parameters_mean_standard_deviation[1];
06431 }
06432
06433
06434
06435
06442
06443 void MultilayerPerceptron::set_independent_parameters_minimum(const Vector<double>& new_independent_parameters_minimum)
06444 {
06445
06446
06447 #ifdef _DEBUG
06448
06449 int independent_parameters_number = get_independent_parameters_number();
06450
06451 if(new_independent_parameters_minimum.get_size() != independent_parameters_number)
06452 {
06453 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06454 << "void set_independent_parameters_minimum(const Vector<double>&) method." << std::endl
06455 << "Size must be equal to number of independent parameters." << std::endl;
06456
06457 exit(1);
06458 }
06459
06460 #endif
06461
06462
06463
06464 independent_parameters_minimum = new_independent_parameters_minimum;
06465 }
06466
06467
06468
06469
06476
06477 void MultilayerPerceptron
06478 ::set_independent_parameter_minimum(int independent_parameter_index, double new_independent_parameter_minimum)
06479 {
06480 int independent_parameters_number = get_independent_parameters_number();
06481
06482
06483
06484 #ifdef _DEBUG
06485
06486 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
06487 {
06488 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06489 << "void set_independent_parameters_minimum(int, double) method." << std::endl
06490 << "Index of independent parameter must be equal or greater than zero and less than "
06491 << "number of independent parameters." << std::endl;
06492
06493 exit(1);
06494 }
06495
06496 #endif
06497
06498 if(independent_parameters_minimum.get_size() != independent_parameters_number)
06499 {
06500 independent_parameters_minimum.set(independent_parameters_number, -1.0);
06501 }
06502
06503
06504
06505 independent_parameters_minimum[independent_parameter_index] = new_independent_parameter_minimum;
06506 }
06507
06508
06509
06510
06517
06518 void MultilayerPerceptron::set_independent_parameters_maximum(const Vector<double>& new_independent_parameters_maximum)
06519 {
06520
06521
06522
06523 #ifdef _DEBUG
06524
06525 int independent_parameters_number = get_independent_parameters_number();
06526
06527 if(new_independent_parameters_maximum.get_size() != independent_parameters_number)
06528 {
06529 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06530 << "void set_independent_parameters_maximum(const Vector<double>&) method." << std::endl
06531 << "Size must be equal to number of independent parameters." << std::endl;
06532
06533 exit(1);
06534 }
06535
06536 #endif
06537
06538
06539
06540 independent_parameters_maximum = new_independent_parameters_maximum;
06541 }
06542
06543
06544
06545
06552
06553 void MultilayerPerceptron::set_independent_parameter_maximum(int independent_parameter_index, double new_independent_parameter_maximum)
06554 {
06555 int independent_parameters_number = get_independent_parameters_number();
06556
06557
06558
06559 #ifdef _DEBUG
06560
06561 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
06562 {
06563 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06564 << "void set_independent_parameters_maximum(int, double) method." << std::endl
06565 << "Index of independent parameter must be equal or greater than zero and less than "
06566 << "number of independent parameters." << std::endl;
06567
06568 exit(1);
06569 }
06570
06571 #endif
06572
06573
06574
06575 if(independent_parameters_maximum.get_size() != independent_parameters_number)
06576 {
06577 independent_parameters_maximum.set(independent_parameters_number, 1.0);
06578 }
06579
06580
06581
06582 independent_parameters_maximum[independent_parameter_index] = new_independent_parameter_maximum;
06583 }
06584
06585
06586
06587
06596
06597 void MultilayerPerceptron
06598 ::set_independent_parameters_minimum_maximum(const Vector< Vector<double> >& new_independent_parameters_minimum_maximum)
06599 {
06600
06601
06602 #ifdef _DEBUG
06603
06604 int independent_parameters_number = get_independent_parameters_number();
06605
06606 if(new_independent_parameters_minimum_maximum.get_size() != 2)
06607 {
06608 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06609 << "void set_independent_parameters_minimum_maximum(const Vector< Vector<double> >&) method." << std::endl
06610 << "Number of rows must be 2." << std::endl;
06611
06612 exit(1);
06613 }
06614 else if(new_independent_parameters_minimum_maximum[0].get_size() != independent_parameters_number
06615 && new_independent_parameters_minimum_maximum[1].get_size() != independent_parameters_number)
06616 {
06617 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06618 << "void set_independent_parameters_minimum_maximum(const Vector< Vector<double> >&) method." << std::endl
06619 << "Number of columns must be equal to number of independent parameters." << std::endl;
06620
06621 exit(1);
06622 }
06623
06624
06625
06626 for(int i = 0; i < independent_parameters_number; i++)
06627 {
06628 if(new_independent_parameters_minimum_maximum[0][i] >= new_independent_parameters_minimum_maximum[1][i])
06629 {
06630 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06631 << "void set_independent_parameters_minimum_maximum(const Vector< Vector<double> >&) method."
06632 << std::endl
06633 << "Minimum of independent parameter "<< i << " is equal or greater than maximum of that parameter."
06634 << std::endl;
06635
06636 exit(1);
06637 }
06638 }
06639
06640 #endif
06641
06642
06643
06644 independent_parameters_minimum = new_independent_parameters_minimum_maximum[0];
06645 independent_parameters_maximum = new_independent_parameters_minimum_maximum[1];
06646 }
06647
06648
06649
06650
06657
06658 void MultilayerPerceptron
06659 ::set_independent_parameters_lower_bound(const Vector<double>& new_independent_parameters_lower_bound)
06660 {
06661
06662
06663 #ifdef _DEBUG
06664
06665 int independent_parameters_number = get_independent_parameters_number();
06666
06667 if(new_independent_parameters_lower_bound.get_size() != independent_parameters_number)
06668 {
06669 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06670 << "void set_independent_parameters_lower_bound(const Vector<double>&) method." << std::endl
06671 << "Size must be equal to number of independent parameters." << std::endl;
06672
06673 exit(1);
06674 }
06675
06676 #endif
06677
06678
06679
06680 independent_parameters_lower_bound = new_independent_parameters_lower_bound;
06681 }
06682
06683
06684
06685
06692
06693 void MultilayerPerceptron
06694 ::set_independent_parameter_lower_bound(int independent_parameter_index, double new_independent_parameter_lower_bound)
06695 {
06696 int independent_parameters_number = get_independent_parameters_number();
06697
06698
06699
06700 #ifdef _DEBUG
06701
06702 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
06703 {
06704 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06705 << "void set_independent_parameter_lower_bound(int, double) method." << std::endl
06706 << "Index of independent parameter must be equal or greater than zero and less than "
06707 << "number of independent parameters." << std::endl;
06708
06709 exit(1);
06710 }
06711
06712 #endif
06713
06714
06715
06716 if(independent_parameters_lower_bound.get_size() != independent_parameters_number)
06717 {
06718 independent_parameters_lower_bound.set(independent_parameters_number, -1.0e99);
06719 }
06720
06721
06722
06723 independent_parameters_lower_bound[independent_parameter_index] = new_independent_parameter_lower_bound;
06724 }
06725
06726
06727
06728
06735
06736 void MultilayerPerceptron
06737 ::set_independent_parameters_upper_bound(const Vector<double>& new_independent_parameters_upper_bound)
06738 {
06739
06740
06741 #ifdef _DEBUG
06742
06743 int independent_parameters_number = get_independent_parameters_number();
06744
06745 if(new_independent_parameters_upper_bound.get_size() != independent_parameters_number)
06746 {
06747 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06748 << "void set_independent_parameters_upper_bound(const Vector<double>&) method." << std::endl
06749 << "Size must be equal to number of independent parameters." << std::endl;
06750
06751 exit(1);
06752 }
06753
06754 #endif
06755
06756
06757
06758 independent_parameters_upper_bound = new_independent_parameters_upper_bound;
06759 }
06760
06761
06762
06763
06770
06771 void MultilayerPerceptron::set_independent_parameter_upper_bound
06772 (int independent_parameter_index, double new_independent_parameter_upper_bound)
06773 {
06774 int independent_parameters_number = get_independent_parameters_number();
06775
06776
06777
06778 #ifdef _DEBUG
06779
06780 if(independent_parameter_index < 0 || independent_parameter_index >= independent_parameters_number)
06781 {
06782 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06783 << "void set_independent_parameter_upper_bound(int, double) method." << std::endl
06784 << "Index of independent parameter must be equal or greater than zero and less than number of independent parameters." << std::endl;
06785
06786 exit(1);
06787 }
06788
06789 #endif
06790
06791
06792
06793 if(independent_parameters_upper_bound.get_size() != independent_parameters_number)
06794 {
06795 independent_parameters_upper_bound.set(independent_parameters_number, 1.0e99);
06796 }
06797
06798
06799
06800 independent_parameters_upper_bound[independent_parameter_index] = new_independent_parameter_upper_bound;
06801 }
06802
06803
06804
06805
06815
06816 void MultilayerPerceptron::set_independent_parameters_bounds
06817 (const Vector< Vector<double> >& new_independent_parameters_bounds)
06818 {
06819
06820
06821 #ifdef _DEBUG
06822
06823 int independent_parameters_number = get_independent_parameters_number();
06824
06825 if(new_independent_parameters_bounds.get_size() != 2)
06826 {
06827 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06828 << "void set_independent_parameters_bounds(const Vector< Vector<double> >&) method."
06829 << std::endl
06830 << "Number of rows must be 2." << std::endl;
06831
06832 exit(1);
06833 }
06834 else if(new_independent_parameters_bounds[0].get_size() != independent_parameters_number
06835 && new_independent_parameters_bounds[1].get_size() != independent_parameters_number)
06836 {
06837 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06838 << "void set_independent_parameters_bounds(const Vector< Vector<double> >&) method."
06839 << std::endl
06840 << "Number of columns must be equal to number of independent parameters." << std::endl;
06841
06842 exit(1);
06843 }
06844
06845 #endif
06846
06847
06848
06849 independent_parameters_lower_bound = new_independent_parameters_bounds[0];
06850 independent_parameters_upper_bound = new_independent_parameters_bounds[1];
06851 }
06852
06853
06854
06867
06868 void MultilayerPerceptron
06869 ::set_independent_parameters_statistics(const Vector< Vector<double> >& new_independent_parameters_statistics)
06870 {
06871
06872
06873 #ifdef _DEBUG
06874
06875 if(new_independent_parameters_statistics.get_size() != 4)
06876 {
06877 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06878 << "void set_independent_parameters_statistics(const Vector< Vector<double> >&) method." << std::endl
06879 << "Size must be 6." << std::endl;
06880
06881 exit(1);
06882 }
06883
06884 #endif
06885
06886
06887
06888 set_independent_parameters_mean(new_independent_parameters_statistics[0]);
06889 set_independent_parameters_standard_deviation(new_independent_parameters_statistics[1]);
06890
06891 set_independent_parameters_minimum(new_independent_parameters_statistics[2]);
06892 set_independent_parameters_maximum(new_independent_parameters_statistics[3]);
06893 }
06894
06895
06896
06897
06901
06902 void MultilayerPerceptron::set_parameters(const Vector<double>& new_parameters)
06903 {
06904
06905
06906 int independent_parameters_number = get_independent_parameters_number();
06907
06908 #ifdef _DEBUG
06909
06910 int size = new_parameters.get_size();
06911
06912 int parameters_number = get_parameters_number();
06913
06914 if(size != parameters_number)
06915 {
06916 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
06917 << "void set_parameters(const Vector<double>&) method." << std::endl
06918 << "Size must be equal to number of parameters." << std::endl;
06919
06920 exit(1);
06921 }
06922
06923 #endif
06924
06925 if((inputs_number != 0 && outputs_number != 0) && independent_parameters_number == 0)
06926 {
06927
06928 set_neural_parameters(new_parameters);
06929 }
06930 else if((inputs_number == 0 && outputs_number == 0) && independent_parameters_number != 0)
06931 {
06932
06933 unscale_independent_parameters(new_parameters);
06934 }
06935 else if((inputs_number != 0 && outputs_number != 0) && independent_parameters_number != 0)
06936 {
06937
06938
06939
06940 int neural_parameters_number = get_neural_parameters_number();
06941
06942 Vector<double> neural_parameters = new_parameters.extract(0, neural_parameters_number);
06943
06944 set_neural_parameters(neural_parameters);
06945
06946
06947
06948 Vector<double> scaled_independent_parameters
06949 = new_parameters.extract(neural_parameters_number, independent_parameters_number);
06950
06951 unscale_independent_parameters(scaled_independent_parameters);
06952 }
06953 else
06954 {
06955 return;
06956 }
06957 }
06958
06959
06960
06961
06966
06967 void MultilayerPerceptron::set_scaling_method
06968 (const MultilayerPerceptron::ScalingMethod& new_scaling_method)
06969 {
06970 inputs_scaling_method = new_scaling_method;
06971 outputs_unscaling_method = new_scaling_method;
06972 independent_parameters_scaling_method = new_scaling_method;
06973 }
06974
06975
06976
06977
06983
06984 void MultilayerPerceptron::set_display(bool new_display)
06985 {
06986 display = new_display;
06987 }
06988
06989
06990
06991
06997
06998 void MultilayerPerceptron::set_display_range_out_warning(bool new_display_range_out_warning)
06999 {
07000 display_range_out_warning = new_display_range_out_warning;
07001 }
07002
07003
07004
07005
07008
07009 void MultilayerPerceptron::initialize_biases(double value)
07010 {
07011
07012
07013 int hidden_layers_number = get_hidden_layers_number();
07014
07015 for(int i = 0; i < hidden_layers_number; i++)
07016 {
07017 for(int j = 0; j < hidden_layers_size[i]; j++)
07018 {
07019 hidden_layers[i][j].initialize_bias(value);
07020 }
07021 }
07022
07023
07024
07025 for(int i = 0; i < outputs_number; i++)
07026 {
07027 output_layer[i].initialize_bias(value);
07028 }
07029 }
07030
07031
07032
07033
07036
07037 void MultilayerPerceptron::initialize_synaptic_weights(double value)
07038 {
07039
07040
07041 int hidden_layers_number = get_hidden_layers_number();
07042
07043 for(int i = 0; i < hidden_layers_number; i++)
07044 {
07045 for(int j = 0; j < hidden_layers_size[i]; j++)
07046 {
07047 hidden_layers[i][j].initialize_synaptic_weights(value);
07048 }
07049 }
07050
07051
07052
07053 for(int i = 0; i < outputs_number; i++)
07054 {
07055 output_layer[i].initialize_synaptic_weights(value);
07056 }
07057 }
07058
07059
07060
07061
07064
07065 void MultilayerPerceptron::initialize_neural_parameters(double value)
07066 {
07067 int neural_parameters_number = get_neural_parameters_number();
07068
07069 Vector<double> neural_parameters(neural_parameters_number, value);
07070
07071 set_neural_parameters(neural_parameters);
07072 }
07073
07074
07075
07076
07079
07080 void MultilayerPerceptron::initialize_neural_parameters_uniform(void)
07081 {
07082 int neural_parameters_number = get_neural_parameters_number();
07083
07084 Vector<double> neural_parameters(neural_parameters_number);
07085
07086 neural_parameters.initialize_uniform();
07087
07088 set_neural_parameters(neural_parameters);
07089 }
07090
07091
07092
07093
07098
07099 void MultilayerPerceptron::initialize_neural_parameters_uniform(double minimum, double maximum)
07100 {
07101 int neural_parameters_number = get_neural_parameters_number();
07102
07103 Vector<double> neural_parameters(neural_parameters_number);
07104
07105 neural_parameters.initialize_uniform(minimum, maximum);
07106
07107 set_neural_parameters(neural_parameters);
07108 }
07109
07110
07111
07112
07117
07118 void MultilayerPerceptron
07119 ::initialize_neural_parameters_uniform(const Vector<double>& minimum, const Vector<double>& maximum)
07120 {
07121 int neural_parameters_number = get_neural_parameters_number();
07122
07123 Vector<double> neural_parameters(neural_parameters_number);
07124
07125 neural_parameters.initialize_uniform(minimum, maximum);
07126
07127 set_neural_parameters(neural_parameters);
07128 }
07129
07130
07131
07132
07139
07140 void MultilayerPerceptron::initialize_neural_parameters_uniform(const Vector< Vector<double> >& minimum_maximum)
07141 {
07142 int neural_parameters_number = get_neural_parameters_number();
07143
07144 Vector<double> neural_parameters(neural_parameters_number);
07145
07146 neural_parameters.initialize_uniform(minimum_maximum[0], minimum_maximum[1]);
07147
07148 set_neural_parameters(neural_parameters);
07149 }
07150
07151
07152
07153
07156
07157 void MultilayerPerceptron::initialize_neural_parameters_normal(void)
07158 {
07159 int neural_parameters_number = get_neural_parameters_number();
07160
07161 Vector<double> neural_parameters(neural_parameters_number);
07162
07163 neural_parameters.initialize_normal();
07164
07165 set_neural_parameters(neural_parameters);
07166 }
07167
07168
07169
07170
07175
07176 void MultilayerPerceptron
07177 ::initialize_neural_parameters_normal(double mean, double standard_deviation)
07178 {
07179 int neural_parameters_number = get_neural_parameters_number();
07180
07181 Vector<double> neural_parameters(neural_parameters_number);
07182
07183 neural_parameters.initialize_normal(mean, standard_deviation);
07184
07185 set_neural_parameters(neural_parameters);
07186 }
07187
07188
07189
07190
07195
07196 void MultilayerPerceptron
07197 ::initialize_neural_parameters_normal(const Vector<double>& mean, const Vector<double>& standard_deviation)
07198 {
07199 int neural_parameters_number = get_neural_parameters_number();
07200
07201 Vector<double> neural_parameters(neural_parameters_number);
07202
07203 neural_parameters.initialize_normal(mean, standard_deviation);
07204
07205 set_neural_parameters(neural_parameters);
07206 }
07207
07208
07209
07210
07217
07218 void MultilayerPerceptron::initialize_neural_parameters_normal(const Vector< Vector<double> >& mean_standard_deviation)
07219 {
07220 int neural_parameters_number = get_neural_parameters_number();
07221
07222 Vector<double> neural_parameters(neural_parameters_number);
07223
07224 neural_parameters.initialize_normal(mean_standard_deviation[0], mean_standard_deviation[1]);
07225
07226 set_neural_parameters(neural_parameters);
07227 }
07228
07229
07230
07231
07234
07235 void MultilayerPerceptron::initialize_independent_parameters(double value)
07236 {
07237 independent_parameters.initialize(value);
07238
07239 bound_independent_parameters();
07240 }
07241
07242
07243
07244
07247
07248 void MultilayerPerceptron::initialize_independent_parameters_uniform(void)
07249 {
07250 independent_parameters.initialize_uniform();
07251
07252 bound_independent_parameters();
07253 }
07254
07255
07256
07257
07262
07263 void MultilayerPerceptron::initialize_independent_parameters_uniform(double minimum, double maximum)
07264 {
07265 independent_parameters.initialize_uniform(minimum, maximum);
07266
07267 bound_independent_parameters();
07268 }
07269
07270
07271
07272
07277
07278 void MultilayerPerceptron
07279 ::initialize_independent_parameters_uniform(const Vector<double>& minimum, const Vector<double>& maximum)
07280 {
07281 independent_parameters.initialize_uniform(minimum, maximum);
07282
07283 bound_independent_parameters();
07284 }
07285
07286
07287
07288
07295
07296 void MultilayerPerceptron::initialize_independent_parameters_uniform(const Vector< Vector<double> >& minimum_maximum)
07297 {
07298 independent_parameters.initialize_uniform(minimum_maximum[0], minimum_maximum[1]);
07299
07300 bound_independent_parameters();
07301 }
07302
07303
07304
07305
07308
07309 void MultilayerPerceptron::initialize_independent_parameters_normal(void)
07310 {
07311 independent_parameters.initialize_normal();
07312
07313 bound_independent_parameters();
07314 }
07315
07316
07317
07318
07323
07324 void MultilayerPerceptron::initialize_independent_parameters_normal(double mean, double standard_deviation)
07325 {
07326 independent_parameters.initialize_normal(mean, standard_deviation);
07327
07328 bound_independent_parameters();
07329 }
07330
07331
07332
07333
07338
07339 void MultilayerPerceptron::initialize_independent_parameters_normal
07340 (const Vector<double>& mean, const Vector<double>& standard_deviation)
07341 {
07342 independent_parameters.initialize_normal(mean, standard_deviation);
07343
07344 bound_independent_parameters();
07345 }
07346
07347
07348
07349
07356
07357 void MultilayerPerceptron::initialize_independent_parameters_normal(const Vector< Vector<double> >& mean_standard_deviation)
07358 {
07359 independent_parameters.initialize_normal(mean_standard_deviation[0], mean_standard_deviation[1]);
07360
07361 bound_independent_parameters();
07362 }
07363
07364
07365
07366
07368
07369 void MultilayerPerceptron::initialize_parameters(double value)
07370 {
07371 initialize_neural_parameters(value);
07372 initialize_independent_parameters(value);
07373 }
07374
07375
07376
07377
07380
07381 void MultilayerPerceptron::initialize_parameters_uniform(void)
07382 {
07383 initialize_neural_parameters_uniform();
07384 initialize_independent_parameters_uniform();
07385 }
07386
07387
07388
07389
07394
07395 void MultilayerPerceptron::initialize_parameters_uniform(double minimum, double maximum)
07396 {
07397 initialize_neural_parameters_uniform(minimum, maximum);
07398 initialize_independent_parameters_uniform(minimum, maximum);
07399 }
07400
07401
07402
07403
07409
07410 void MultilayerPerceptron::initialize_parameters_uniform(const Vector<double>& minimum, const Vector<double>& maximum)
07411 {
07412 initialize_neural_parameters_uniform(minimum, maximum);
07413 initialize_independent_parameters_uniform(minimum, maximum);
07414 }
07415
07416
07417
07418
07425
07426 void MultilayerPerceptron::initialize_parameters_uniform(const Vector< Vector<double> >& minimum_maximum)
07427 {
07428 initialize_neural_parameters_uniform(minimum_maximum);
07429 initialize_independent_parameters_uniform(minimum_maximum);
07430 }
07431
07432
07433
07434
07437
07438 void MultilayerPerceptron::initialize_parameters_normal(void)
07439 {
07440 initialize_neural_parameters_normal();
07441 initialize_independent_parameters_normal();
07442 }
07443
07444
07445
07446
07452
07453 void MultilayerPerceptron::initialize_parameters_normal(double mean, double standard_deviation)
07454 {
07455 initialize_neural_parameters_normal(mean, standard_deviation);
07456 initialize_independent_parameters_normal(mean, standard_deviation);
07457 }
07458
07459
07460
07461
07467
07468 void MultilayerPerceptron
07469 ::initialize_parameters_normal(const Vector<double>& mean, const Vector<double>& standard_deviation)
07470 {
07471 initialize_neural_parameters_normal(mean, standard_deviation);
07472 initialize_independent_parameters_normal(mean, standard_deviation);
07473 }
07474
07475
07476
07477
07485
07486 void MultilayerPerceptron::initialize_parameters_normal(const Vector< Vector<double> >& mean_standard_deviation)
07487 {
07488 initialize_neural_parameters_normal(mean_standard_deviation);
07489 initialize_independent_parameters_normal(mean_standard_deviation);
07490 }
07491
07492
07493
07494
07500
07501 void MultilayerPerceptron::check_input_range(const Vector<double>& input)
07502 {
07503
07504
07505 #ifdef _DEBUG
07506
07507 int size = input.get_size();
07508
07509 if(size != inputs_number)
07510 {
07511 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07512 << "void check_input_range(const Vector<double>&) method." << std::endl
07513 << "Size of input must be equal to number of inputs." << std::endl;
07514
07515 exit(1);
07516 }
07517
07518 #endif
07519
07520
07521
07522 if(display_range_out_warning)
07523 {
07524 for(int i = 0; i < inputs_number; i++)
07525 {
07526 if(input[i] < input_variables_minimum[i])
07527 {
07528 std::cout << "Flood Warning: MultilayerPerceptron class." << std::endl
07529 << "void check_input_range(const Vector<double>&) method." << std::endl
07530 << "Input variable " << i << " is less than minimum." << std::endl;
07531 }
07532 if(input[i] > input_variables_maximum[i])
07533 {
07534 std::cout << "Flood Warning: MultilayerPerceptron class." << std::endl
07535 << "void check_input_range(const Vector<double>&) method." << std::endl
07536 << "Input variable " << i << " is greater than maximum." << std::endl;
07537 }
07538 }
07539 }
07540 }
07541
07542
07543
07544
07547
07548 Vector<double> MultilayerPerceptron::calculate_scaled_input(const Vector<double>& input)
07549 {
07550
07551
07552 #ifdef _DEBUG
07553
07554 int size = input.get_size();
07555
07556 if(size != inputs_number)
07557 {
07558 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07559 << "Vector<double> calculate_scaled_input(const Vector<double>&) method." << std::endl
07560 << "Size of input must be equal to number of inputs." << std::endl;
07561
07562 exit(1);
07563 }
07564
07565 #endif
07566
07567
07568
07569 switch(inputs_scaling_method)
07570 {
07571 case None:
07572 {
07573 return(input);
07574 }
07575 break;
07576
07577 case MeanStandardDeviation:
07578 {
07579
07580
07581 #ifdef _DEBUG
07582
07583 int mean_size = input_variables_mean.get_size();
07584
07585 if(mean_size != inputs_number)
07586 {
07587 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07588 << "Vector<double> calculate_scaled_input(const Vector<double>&) method." << std::endl
07589 << "Size of mean of input variables must be equal to number of inputs."
07590 << std::endl;
07591
07592 exit(1);
07593 }
07594
07595 int standard_deviation_size = input_variables_standard_deviation.get_size();
07596
07597 if(standard_deviation_size != inputs_number)
07598 {
07599 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07600 << "Vector<double> calculate_scaled_input(const Vector<double>&) method." << std::endl
07601 << "Size of standard deviation of input variables must be equal to number of inputs."
07602 << std::endl;
07603
07604 exit(1);
07605 }
07606
07607 #endif
07608
07609 Vector<double> scaled_input(inputs_number);
07610
07611 for(int i = 0; i < inputs_number; i++)
07612 {
07613 if(input_variables_standard_deviation[i] < 1e-99)
07614 {
07615 if(display)
07616 {
07617 std::cout << "Flood Warning: MultilayerPerceptron class." << std::endl
07618 << "Vector<double> calculate_scaled_input(Vector<double>&) method." << std::endl
07619 << "Standard deviation of input variable " << i << " is zero." << std::endl
07620 << "Those inputs won't be scaled." << std::endl;
07621 }
07622
07623 scaled_input[i] = input[i];
07624 }
07625 else
07626 {
07627 scaled_input[i] = (input[i] - input_variables_mean[i])/input_variables_standard_deviation[i];
07628 }
07629 }
07630
07631 return(scaled_input);
07632 }
07633 break;
07634
07635 case MinimumMaximum:
07636 {
07637
07638
07639 #ifdef _DEBUG
07640
07641 int minimum_size = input_variables_minimum.get_size();
07642 int maximum_size = input_variables_maximum.get_size();
07643
07644 if(minimum_size != inputs_number || maximum_size != inputs_number)
07645 {
07646 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07647 << "Vector<double> calculate_scaled_input(const Vector<double>&) method." << std::endl
07648 << "Size of minimum and maximum of input variables must be equal to number of inputs."
07649 << std::endl;
07650
07651 exit(1);
07652 }
07653
07654 #endif
07655
07656
07657 Vector<double> scaled_input(inputs_number);
07658
07659 for(int i = 0; i < inputs_number; i++)
07660 {
07661 if(input_variables_maximum[i]-input_variables_minimum[i] < 1e-99)
07662 {
07663 if(display)
07664 {
07665 std::cout << "Flood Warning: MultilayerPerceptron class" << std::endl
07666 << "Vector<double> calculate_scaled_input(Vector<double>&) method." << std::endl
07667 << "Minimum and maximum values of input variable " << i << " are equal." << std::endl
07668 << "Those inputs won't be scaled." << std::endl;
07669 }
07670
07671 scaled_input[i] = input[i];
07672 }
07673 else
07674 {
07675 scaled_input[i] = 2.0*(input[i] - input_variables_minimum[i])
07676 /(input_variables_maximum[i]-input_variables_minimum[i]) - 1.0;
07677 }
07678 }
07679
07680 return(scaled_input);
07681 }
07682 break;
07683
07684 default:
07685 {
07686 std::cerr << "Flood Warning: MultilayerPerceptron class" << std::endl
07687 << "Vector<double> calculate_scaled_input(Vector<double>&) method." << std::endl
07688 << "Unknown scaling and unscaling method." << std::endl;
07689
07690 exit(1);
07691 }
07692 break;
07693 }
07694 }
07695
07696
07697
07698
07702
07703 Vector<double> MultilayerPerceptron::calculate_hidden_layer_combination
07704 (int i, const Vector<double>& hidden_layer_input)
07705 {
07706
07707
07708 #ifdef _DEBUG
07709
07710 int size = hidden_layer_input.get_size();
07711
07712 int hidden_layer_inputs_number = get_hidden_layer_inputs_number(i);
07713
07714 if(size != hidden_layer_inputs_number)
07715 {
07716 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07717 << "Vector<double> calculate_hidden_layer_combination(int, const Vector<double>&) method." << std::endl
07718 << "Size of input to hidden layer must be equal to number of layer inputs." << std::endl;
07719
07720 exit(1);
07721 }
07722
07723 #endif
07724
07725
07726
07727 Vector<double> hidden_layer_combination(hidden_layers_size[i]);
07728
07729 for(int j = 0; j < hidden_layers_size[i]; j++)
07730 {
07731 hidden_layer_combination[j] = hidden_layers[i][j].calculate_combination(hidden_layer_input);
07732 }
07733
07734 return(hidden_layer_combination);
07735 }
07736
07737
07738
07739
07743
07744 Vector<double> MultilayerPerceptron::
07745 calculate_hidden_layer_activation(int i, const Vector<double>& hidden_layer_combination)
07746 {
07747
07748
07749 #ifdef _DEBUG
07750
07751 if(i < 0)
07752 {
07753 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07754 << "Vector<double> calculate_hidden_layer_activation(int, const Vector<double>&) method." << std::endl
07755 << "Index of hidden layer must be equal or greater than zero." << std::endl;
07756
07757 exit(1);
07758 }
07759
07760 int hidden_layers_number = get_hidden_layers_number();
07761
07762 if(i >= hidden_layers_number)
07763 {
07764 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07765 << "Vector<double> calculate_hidden_layer_activation(int, const Vector<double>&) method." << std::endl
07766 << "Index of hidden layer must be less than number of hidden layers." << std::endl;
07767
07768 exit(1);
07769 }
07770
07771 int size = hidden_layer_combination.get_size();
07772
07773 if(size != hidden_layers_size[i])
07774 {
07775 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07776 << "Vector<double> calculate_hidden_layer_activation(int, const Vector<double>&) method." << std::endl
07777 << "Size must be equal to number of hidden neurons." << std::endl;
07778
07779 exit(1);
07780 }
07781
07782 #endif
07783
07784
07785
07786 Vector<double> hidden_layer_output(hidden_layers_size[i]);
07787
07788 for(int j = 0; j < hidden_layers_size[i]; j++)
07789 {
07790 hidden_layer_output[j] = hidden_layers[i][j].calculate_activation(hidden_layer_combination[j]);
07791 }
07792
07793 return(hidden_layer_output);
07794 }
07795
07796
07797
07798
07803
07804 Vector<double> MultilayerPerceptron::calculate_hidden_layer_activation_derivative
07805 (int i, const Vector<double>& hidden_layer_combination)
07806 {
07807
07808
07809 #ifdef _DEBUG
07810
07811 if(i < 0)
07812 {
07813 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07814 << "Vector<double> calculate_hidden_layer_activation_derivative(int, const Vector<double>&) method."
07815 << std::endl
07816 << "Index of hidden layer must be equal or greater than zero." << std::endl;
07817
07818 exit(1);
07819 }
07820
07821 int hidden_layers_number = get_hidden_layers_number();
07822
07823 if(i >= hidden_layers_number)
07824 {
07825 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07826 << "Vector<double> calculate_hidden_layer_activation_derivative(int, const Vector<double>&) method." << std::endl
07827 << "Index of hidden layer must be less than number of hidden layers." << std::endl;
07828
07829 exit(1);
07830 }
07831
07832 int size = hidden_layer_combination.get_size();
07833
07834 if(size != hidden_layers_size[i])
07835 {
07836 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07837 << "Vector<double> calculate_hidden_layer_activation_derivative(int, const Vector<double>&) method." << std::endl
07838 << "Size must be equal to number of hidden neurons." << std::endl;
07839
07840 exit(1);
07841 }
07842
07843 #endif
07844
07845
07846
07847 Vector<double> hidden_layer_activation_derivative(hidden_layers_size[i]);
07848
07849
07850
07851 for(int j = 0; j < hidden_layers_size[i]; j++)
07852 {
07853 hidden_layer_activation_derivative[j]
07854 = hidden_layers[i][j].calculate_activation_derivative(hidden_layer_combination[j]);
07855 }
07856
07857 return(hidden_layer_activation_derivative);
07858 }
07859
07860
07861
07862
07867
07868 Vector<double> MultilayerPerceptron::calculate_hidden_layer_activation_second_derivative
07869 (int i, const Vector<double>& hidden_layer_combination)
07870 {
07871
07872
07873 #ifdef _DEBUG
07874
07875 if(i < 0)
07876 {
07877 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07878 << "Vector<double> calculate_hidden_layer_activation_second_derivative(int, const Vector<double>&) method."
07879 << std::endl
07880 << "Index of hidden layer must be equal or greater than zero." << std::endl;
07881
07882 exit(1);
07883 }
07884
07885 int hidden_layers_number = get_hidden_layers_number();
07886
07887 if(i >= hidden_layers_number)
07888 {
07889 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07890 << "Vector<double> calculate_hidden_layer_activation_second_derivative(int, const Vector<double>&) method."
07891 << std::endl
07892 << "Index of hidden layer must be less than number of hidden layers." << std::endl;
07893
07894 exit(1);
07895 }
07896
07897 int size = hidden_layer_combination.get_size();
07898
07899 if(size != hidden_layers_size[i])
07900 {
07901 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07902 << "Vector<double> calculate_hidden_layer_activation_second_derivative(int, const Vector<double>&) method."
07903 << std::endl
07904 << "Size must be equal to number of hidden neurons." << std::endl;
07905
07906 exit(1);
07907 }
07908
07909 #endif
07910
07911
07912
07913 Vector<double> hidden_layer_activation_second_derivative(hidden_layers_size[i]);
07914
07915
07916
07917 for(int j = 0; j < hidden_layers_size[i]; j++)
07918 {
07919 hidden_layer_activation_second_derivative[j]
07920 = hidden_layers[i][j].calculate_activation_second_derivative(hidden_layer_combination[j]);
07921 }
07922
07923 return(hidden_layer_activation_second_derivative);
07924 }
07925
07926
07927
07928
07932
07933 Vector<double> MultilayerPerceptron::
07934 calculate_hidden_layer_output(int i, const Vector<double>& hidden_layer_input)
07935 {
07936
07937
07938 #ifdef _DEBUG
07939
07940 if(i < 0)
07941 {
07942 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07943 << "Vector<double> calculate_hidden_layer_output(int, const Vector<double>&) method." << std::endl
07944 << "Index of hidden layer must be equal or greater than zero." << std::endl;
07945
07946 exit(1);
07947 }
07948
07949 int hidden_layers_number = get_hidden_layers_number();
07950
07951 if(i >= hidden_layers_number)
07952 {
07953 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07954 << "Vector<double> calculate_hidden_layer_output(int, const Vector<double>&) method."
07955 << std::endl
07956 << "Index of hidden layer must be less than number of hidden layers." << std::endl;
07957
07958 exit(1);
07959 }
07960
07961 int size = hidden_layer_input.get_size();
07962
07963 int hidden_layer_inputs_number = get_hidden_layer_inputs_number(i);
07964
07965 if(size != hidden_layer_inputs_number)
07966 {
07967 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
07968 << "Vector<double> calculate_hidden_layer_output(int, const Vector<double>&) method."
07969 << std::endl
07970 << "Size must be equal to number of inputs to hidden layer." << std::endl;
07971
07972 exit(1);
07973 }
07974
07975 #endif
07976
07977 return(calculate_hidden_layer_activation(i, calculate_hidden_layer_combination(i, hidden_layer_input)));
07978 }
07979
07980
07981
07982
07986
07987 Vector<double> MultilayerPerceptron::calculate_output_layer_combination(const Vector<double>& last_hidden_layer_output)
07988 {
07989
07990
07991 #ifdef _DEBUG
07992
07993 int size = last_hidden_layer_output.get_size();
07994
07995 int hidden_layers_number = get_hidden_layers_number();
07996
07997 if(size != hidden_layers_size[hidden_layers_number-1])
07998 {
07999 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
08000 << "Vector<double> calculate_output_layer_combination(const Vector<double>&) method." << std::endl
08001 << "Size must be equal to number of neurons of last hidden layer." << std::endl;
08002
08003 exit(1);
08004 }
08005
08006 #endif
08007
08008
08009
08010 Vector<double> output_layer_combination(outputs_number);
08011
08012
08013
08014 for(int i = 0; i < outputs_number; i++)
08015 {
08016 output_layer_combination[i] = output_layer[i].calculate_combination(last_hidden_layer_output);
08017 }
08018
08019 return(output_layer_combination);
08020 }
08021
08022
08023
08024
08027
08028 Vector<double> MultilayerPerceptron::calculate_output_layer_activation(const Vector<double>& output_layer_combination)
08029 {
08030
08031
08032 #ifdef _DEBUG
08033
08034 int size = output_layer_combination.get_size();
08035
08036 if(size != outputs_number)
08037 {
08038 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
08039 << "Vector<double> calculate_output_layer_activation(const Vector<double>&) method." << std::endl
08040 << "Size must be equal to number of outputs." << std::endl;
08041
08042 exit(1);
08043 }
08044
08045 #endif
08046
08047
08048
08049 Vector<double> output_layer_output(outputs_number, 0.0);
08050
08051 for(int i = 0; i < outputs_number; i++)
08052 {
08053 output_layer_output[i] = output_layer[i].calculate_activation(output_layer_combination[i]);
08054 }
08055
08056 return(output_layer_output);
08057 }
08058
08059
08060
08061
08065
08066 Vector<double> MultilayerPerceptron::
08067 calculate_output_layer_activation_derivative(const Vector<double>& output_layer_combination)
08068 {
08069
08070
08071 #ifdef _DEBUG
08072
08073 int size = output_layer_combination.get_size();
08074
08075 if(size != outputs_number)
08076 {
08077 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
08078 << "Vector<double> calculate_output_layer_activation_derivative(const Vector<double>&) method." << std::endl
08079 << "Size must be equal to number of outputs." << std::endl;
08080
08081 exit(1);
08082 }
08083
08084 #endif
08085
08086
08087
08088 Vector<double> output_layer_activation_derivative(outputs_number);
08089
08090 for(int i = 0; i < outputs_number; i++)
08091 {
08092 output_layer_activation_derivative[i]
08093 = output_layer[i].calculate_activation_derivative(output_layer_combination[i]);
08094 }
08095
08096 return(output_layer_activation_derivative);
08097 }
08098
08099
08100
08101
08105
08106 Vector<double> MultilayerPerceptron::
08107 calculate_output_layer_activation_second_derivative(const Vector<double>& output_layer_combination)
08108 {
08109
08110
08111 #ifdef _DEBUG
08112
08113 int size = output_layer_combination.get_size();
08114
08115 if(size != outputs_number)
08116 {
08117 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
08118 << "Vector<double> calculate_output_layer_activation_second_derivative(const Vector<double>&) method." << std::endl
08119 << "Size must be equal to number of outputs." << std::endl;
08120
08121 exit(1);
08122 }
08123
08124 #endif
08125
08126
08127
08128 Vector<double> output_layer_activation_second_derivative(outputs_number);
08129
08130 for(int i = 0; i < outputs_number; i++)
08131 {
08132 output_layer_activation_second_derivative[i]
08133 = output_layer[i].calculate_activation_second_derivative(output_layer_combination[i]);
08134 }
08135
08136 return(output_layer_activation_second_derivative);
08137 }
08138
08139
08140
08141
08142 Vector<double> MultilayerPerceptron::
08143 calculate_output_layer_output(const Vector<double>& output_layer_input)
08144 {
08145
08146
08147 #ifdef _DEBUG
08148
08149 int size = output_layer_input.get_size();
08150
08151 int output_layer_inputs_number = get_output_layer_inputs_number();
08152
08153 if(size != output_layer_inputs_number)
08154 {
08155 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
08156 << "Vector<double> calculate_output_layer_output(const Vector<double>&) method."
08157 << std::endl
08158 << "Size must be equal to number of inputs to output layer." << std::endl;
08159
08160 exit(1);
08161 }
08162
08163 #endif
08164
08165 return(calculate_output_layer_activation(calculate_output_layer_combination(output_layer_input)));
08166 }
08167
08168
08169
08170
08173
08174 Vector<double> MultilayerPerceptron::calculate_unscaled_output(const Vector<double>& scaled_output)
08175 {
08176
08177
08178 #ifdef _DEBUG
08179
08180 int size = scaled_output.get_size();
08181
08182 if(size != outputs_number)
08183 {
08184 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
08185 << "Vector<double> calculate_unscaled_output(const Vector<double>&) method." << std::endl
08186 << "Size must be equal to number of outputs." << std::endl;
08187
08188 exit(1);
08189 }
08190
08191 #endif
08192
08193
08194
08195 switch(outputs_unscaling_method)
08196 {
08197 case None:
08198 {
08199 return(scaled_output);
08200 }
08201 break;
08202
08203 case MeanStandardDeviation:
08204 {
08205 Vector<double> unscaled_output(outputs_number);
08206
08207 for(int i = 0; i < outputs_number; i++)
08208 {
08209 if(output_variables_standard_deviation[i] < 1e-99)
08210 {
08211 if(display)
08212 {
08213 std::cout << "Flood Warning: MultilayerPerceptron class." << std::endl
08214 << "void calculate_unscaled_output(Vector<double>&) method." << std::endl
08215 << "Standard deviation of output variable " << i << " is zero." << std::endl
08216 << "Those outputs won't be unscaled." << std::endl;
08217 }
08218
08219 unscaled_output[i] = scaled_output[i];
08220 }
08221 else
08222 {
08223 unscaled_output[i] = scaled_output[i]*output_variables_standard_deviation[i] + output_variables_mean[i];
08224 }
08225 }
08226
08227 return(unscaled_output);
08228
08229 }
08230 break;
08231
08232 case MinimumMaximum:
08233 {
08234 Vector<double> unscaled_output(outputs_number);
08235
08236 for(int i = 0; i < outputs_number; i++)
08237 {
08238 if(output_variables_maximum[i]-output_variables_minimum[i] < 1e-99)
08239 {
08240 if(display)
08241 {
08242 std::cout << "Flood Warning: MultilayerPerceptron class." << std::endl
08243 << "void calculate_unscaled_output(Vector<double>&) method." << std::endl
08244 << "Minimum and maximum values of output variable " << i << " are equal." << std::endl
08245 << "Those outputs won't be unscaled."
08246 << std::endl;
08247 }
08248
08249 unscaled_output[i] = scaled_output[i];
08250 }
08251 else
08252 {
08253 unscaled_output[i] = 0.5*(scaled_output[i] + 1.0)*(output_variables_maximum[i]-output_variables_minimum[i])
08254 + output_variables_minimum[i];
08255 }
08256 }
08257
08258 return(unscaled_output);
08259
08260 }
08261 break;
08262
08263 default:
08264 {
08265 std::cerr << "Flood Warning: MultilayerPerceptron class." << std::endl
08266 << "void calculate_unscaled_output(Vector<double>&) method." << std::endl
08267 << "Unknown scaling and unscaling method." << std::endl;
08268
08269 exit(1);
08270 }
08271 break;
08272
08273 }
08274 }
08275
08276
08277
08278
08282
08283 Vector<double> MultilayerPerceptron::calculate_bounded_output(const Vector<double>& unbounded_output)
08284 {
08285
08286
08287 #ifdef _DEBUG
08288
08289 int size = unbounded_output.get_size();
08290
08291 if(size != outputs_number)
08292 {
08293 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
08294 << "Vector<double> calculate_bounded_output(const Vector<double>&) method." << std::endl
08295 << "Size must be equal to number of outputs." << std::endl;
08296
08297 exit(1);
08298 }
08299
08300 #endif
08301
08302
08303
08304 if(output_variables_lower_bound.get_size() != outputs_number && output_variables_upper_bound.get_size() != outputs_number)
08305 {
08306 return(unbounded_output);
08307 }
08308 else if(output_variables_lower_bound.get_size() == outputs_number && output_variables_upper_bound.get_size() == outputs_number)
08309 {
08310 return(unbounded_output.calculate_lower_upper_bounded(output_variables_lower_bound, output_variables_upper_bound));
08311 }
08312 else if(output_variables_lower_bound.get_size() == outputs_number)
08313 {
08314 return(unbounded_output.calculate_lower_bounded(output_variables_lower_bound));
08315 }
08316 else if(output_variables_upper_bound.get_size() == outputs_number)
08317 {
08318 return(unbounded_output.calculate_upper_bounded(output_variables_upper_bound));
08319 }
08320 else
08321 {
08322 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
08323 << "Vector<double> calculate_bounded_output(const Vector<double>&) method." << std::endl
08324 << "Unknown lower and upper bounds case." << std::endl;
08325
08326 exit(1);
08327 }
08328 }
08329
08330
08331
08332
08347
08348 Vector<double> MultilayerPerceptron::calculate_output(const Vector<double>& input)
08349 {
08350
08351
08352 #ifdef _DEBUG
08353
08354 int size = input.get_size();
08355
08356 if(size != inputs_number)
08357 {
08358 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
08359 << "Vector<double> calculate_output(const Vector<double>&) method." << std::endl
08360 << "Size must be equal to number of inputs." << std::endl;
08361
08362 exit(1);
08363 }
08364
08365 #endif
08366
08367
08368
08369 int hidden_layers_number = get_hidden_layers_number();
08370
08371 check_input_range(input);
08372
08373 Vector<double> scaled_input = calculate_scaled_input(input);
08374
08375 Vector< Vector<double> > forward_propagation = calculate_forward_propagation(scaled_input);
08376
08377 Vector<double> unscaled_output = calculate_unscaled_output(forward_propagation[hidden_layers_number]);
08378
08379 Vector<double> bounded_output = calculate_bounded_output(unscaled_output);
08380
08381 return(bounded_output);
08382 }
08383
08384
08385
08386
08391
08392 Matrix<double> MultilayerPerceptron::calculate_output_matrix(const Matrix<double>& input_matrix)
08393 {
08394
08395
08396 #ifdef _DEBUG
08397
08398 int columns_number = input_matrix.get_columns_number();
08399
08400 if(columns_number != inputs_number)
08401 {
08402 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
08403 << "Matrix<double> calculate_output_matrix(const Matrix<double>&) method." << std::endl
08404 << "Number of columns must be equal to number of inputs." << std::endl;
08405
08406 exit(1);
08407 }
08408
08409 #endif
08410
08411 int input_vectors_number = input_matrix.get_rows_number();
08412
08413 Matrix<double> output_matrix(input_vectors_number, outputs_number);
08414
08415 Vector<double> input(inputs_number);
08416 Vector<double> output(outputs_number);
08417
08418 for(int i = 0; i < input_vectors_number; i++)
08419 {
08420 input = input_matrix.get_row(i);
08421 output = calculate_output(input);
08422 output_matrix.set_row(i, output);
08423 }
08424
08425 return(output_matrix);
08426 }
08427
08428
08429
08430
08440
08441 Vector< Vector<double> > MultilayerPerceptron::calculate_forward_propagation(const Vector<double>& input)
08442 {
08443
08444
08445 #ifdef _DEBUG
08446
08447 int input_size = input.get_size();
08448
08449 if(input_size != inputs_number)
08450 {
08451 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
08452 << "Vector< Vector<double> > calculate_forward_propagation(const Vector<double>&) method." << std::endl
08453 << "Size must be equal to number of inputs." << std::endl;
08454
08455 exit(1);
08456 }
08457
08458 #endif
08459
08460 int hidden_layers_number = get_hidden_layers_number();
08461
08462 int size = hidden_layers_number + 1;
08463
08464 Vector< Vector<double> > forward_propagation(size);
08465
08466
08467
08468 forward_propagation[0] = calculate_hidden_layer_output(0, input);
08469
08470 for(int h = 1; h < hidden_layers_number; h++)
08471 {
08472 forward_propagation[h] = calculate_hidden_layer_output(h, forward_propagation[h-1]);
08473 }
08474
08475
08476
08477 forward_propagation[size-1] = calculate_output_layer_output(forward_propagation[hidden_layers_number-1]);
08478
08479 return(forward_propagation);
08480 }
08481
08482
08483
08484
08495
08496 Vector< Vector<double> > MultilayerPerceptron::calculate_forward_propagation_derivative(const Vector<double>& input)
08497 {
08498
08499
08500 #ifdef _DEBUG
08501
08502 int input_size = input.get_size();
08503
08504 if(input_size != inputs_number)
08505 {
08506 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
08507 << "Vector< Vector<double> > calculate_forward_propagation_derivative(const Vector<double>&) method." << std::endl
08508 << "Size must be equal to number of inputs." << std::endl;
08509
08510 exit(1);
08511 }
08512
08513 #endif
08514
08515 int hidden_layers_number = get_hidden_layers_number();
08516
08517 int size = 2*hidden_layers_number + 2;
08518
08519 Vector< Vector<double> > forward_propagation_derivative(size);
08520
08521
08522
08523 Vector< Vector<double> > hidden_layers_combination(hidden_layers_number);
08524 Vector< Vector<double> > hidden_layers_activation(hidden_layers_number);
08525 Vector< Vector<double> > hidden_layers_activation_derivative(hidden_layers_number);
08526
08527 hidden_layers_combination[0] = calculate_hidden_layer_combination(0, input);
08528
08529 hidden_layers_activation[0] = calculate_hidden_layer_activation(0, hidden_layers_combination[0]);
08530
08531 hidden_layers_activation_derivative[0] = calculate_hidden_layer_activation_derivative(0, hidden_layers_combination[0]);
08532
08533 for(int h = 1; h < hidden_layers_number; h++)
08534 {
08535 hidden_layers_combination[h] = calculate_hidden_layer_combination(h, hidden_layers_activation[h-1]);
08536
08537 hidden_layers_activation[h] = calculate_hidden_layer_activation(h, hidden_layers_combination[h]);
08538
08539 hidden_layers_activation_derivative[h]
08540 = calculate_hidden_layer_activation_derivative(h, hidden_layers_combination[h]);
08541 }
08542
08543
08544
08545 Vector<double> output_layer_combination
08546 = calculate_output_layer_combination(hidden_layers_activation[hidden_layers_number-1]);
08547
08548 Vector<double> output_layer_activation = calculate_output_layer_activation(output_layer_combination);
08549
08550 Vector<double> output_layer_activation_derivative
08551 = calculate_output_layer_activation_derivative(output_layer_combination);
08552
08553
08554
08555 int index = 0;
08556
08557 for(int h = 0; h < hidden_layers_number; h++)
08558 {
08559 forward_propagation_derivative[index] = hidden_layers_activation[h];
08560 index++;
08561
08562 forward_propagation_derivative[index] = hidden_layers_activation_derivative[h];
08563 index++;
08564 }
08565
08566 forward_propagation_derivative[index] = output_layer_activation;
08567 index++;
08568
08569 forward_propagation_derivative[index] = output_layer_activation_derivative;
08570 index++;
08571
08572 return(forward_propagation_derivative);
08573 }
08574
08575
08576
08577
08590
08591 Vector< Vector<double> > MultilayerPerceptron::
08592 calculate_forward_propagation_second_derivative(const Vector<double>& input)
08593 {
08594
08595
08596 #ifdef _DEBUG
08597
08598 int input_size = input.get_size();
08599
08600 if(input_size != inputs_number)
08601 {
08602 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
08603 << "Vector< Vector<double> > calculate_forward_propagation_second_derivative(const Vector<double>&) method." << std::endl
08604 << "Size must be equal to number of inputs." << std::endl;
08605
08606 exit(1);
08607 }
08608
08609 #endif
08610
08611 int hidden_layers_number = get_hidden_layers_number();
08612
08613 int size = 3*hidden_layers_number + 3;
08614
08615 Vector< Vector<double> > forward_propagation_second_derivative(size);
08616
08617
08618
08619 Vector< Vector<double> > hidden_layers_combination(hidden_layers_number);
08620 Vector< Vector<double> > hidden_layers_activation(hidden_layers_number);
08621 Vector< Vector<double> > hidden_layers_activation_derivative(hidden_layers_number);
08622 Vector< Vector<double> > hidden_layers_activation_second_derivative(hidden_layers_number);
08623
08624 hidden_layers_combination[0] = calculate_hidden_layer_combination(0, input);
08625
08626 hidden_layers_activation[0] = calculate_hidden_layer_activation(0, hidden_layers_combination[0]);
08627
08628 hidden_layers_activation_derivative[0]
08629 = calculate_hidden_layer_activation_derivative(0, hidden_layers_combination[0]);
08630
08631 hidden_layers_activation_second_derivative[0]
08632 = calculate_hidden_layer_activation_second_derivative(0, hidden_layers_combination[0]);
08633
08634 for(int h = 1; h < hidden_layers_number; h++)
08635 {
08636 hidden_layers_combination[h] = calculate_hidden_layer_combination(h, hidden_layers_activation[h-1]);
08637
08638 hidden_layers_activation[h] = calculate_hidden_layer_activation(h, hidden_layers_combination[h]);
08639
08640 hidden_layers_activation_derivative[h]
08641 = calculate_hidden_layer_activation_derivative(h, hidden_layers_combination[h]);
08642
08643 hidden_layers_activation_second_derivative[h]
08644 = calculate_hidden_layer_activation_second_derivative(h, hidden_layers_combination[h]);
08645 }
08646
08647
08648
08649 Vector<double> output_layer_combination
08650 = calculate_output_layer_combination(hidden_layers_activation[hidden_layers_number-1]);
08651
08652 Vector<double> output_layer_activation = calculate_output_layer_activation(output_layer_combination);
08653
08654 Vector<double> output_layer_activation_derivative
08655 = calculate_output_layer_activation_derivative(output_layer_combination);
08656
08657 Vector<double> output_layer_activation_second_derivative
08658 = calculate_output_layer_activation_second_derivative(output_layer_combination);
08659
08660
08661
08662 int index = 0;
08663
08664 for(int h = 0; h < hidden_layers_number; h++)
08665 {
08666 forward_propagation_second_derivative[index] = hidden_layers_activation[h];
08667 index++;
08668
08669 forward_propagation_second_derivative[index] = hidden_layers_activation_derivative[h];
08670 index++;
08671
08672 forward_propagation_second_derivative[index] = hidden_layers_activation_second_derivative[h];
08673 index++;
08674 }
08675
08676 forward_propagation_second_derivative[index] = output_layer_activation;
08677 index++;
08678
08679 forward_propagation_second_derivative[index] = output_layer_activation_derivative;
08680 index++;
08681
08682 forward_propagation_second_derivative[index] = output_layer_activation_second_derivative;
08683 index++;
08684
08685 return(forward_propagation_second_derivative);
08686 }
08687
08688
08689
08690
08694
08695 Matrix<double> MultilayerPerceptron::calculate_Jacobian_forward_differences(const Vector<double>& input)
08696 {
08697 int hidden_layers_number = get_hidden_layers_number();
08698
08699 Matrix<double> Jacobian(outputs_number, inputs_number);
08700
08701
08702
08703 check_input_range(input);
08704
08705
08706
08707 Vector<double> scaled_input = calculate_scaled_input(input);
08708
08709
08710
08711 Vector< Vector<double> > forward_propagation = calculate_forward_propagation(scaled_input);
08712
08713
08714
08715 Vector<double> scaled_input_forward(scaled_input);
08716
08717 Vector< Vector<double> > forward_propagation_forward(hidden_layers_number);
08718
08719 for(int i = 0; i < inputs_number; i++)
08720 {
08721
08722
08723 scaled_input_forward[i] += numerical_epsilon;
08724
08725
08726
08727 forward_propagation_forward = calculate_forward_propagation(scaled_input_forward);
08728
08729
08730
08731 for(int k = 0; k < outputs_number; k++)
08732 {
08733 Jacobian[k][i] = (forward_propagation_forward[hidden_layers_number][k] - forward_propagation[hidden_layers_number][k])/numerical_epsilon;
08734 }
08735
08736
08737
08738 scaled_input_forward[i] -= numerical_epsilon;
08739 }
08740
08741 return(Jacobian);
08742 }
08743
08744
08745
08746
08750
08751 Matrix<double> MultilayerPerceptron::calculate_Jacobian_central_differences(const Vector<double>& input)
08752 {
08753 Matrix<double> Jacobian(outputs_number, inputs_number);
08754
08755
08756
08757 check_input_range(input);
08758
08759 int hidden_layers_number = get_hidden_layers_number();
08760
08761
08762
08763 Vector<double> scaled_input = calculate_scaled_input(input);
08764
08765 Vector<double> scaled_input_forward(scaled_input);
08766 Vector<double> scaled_input_backward(scaled_input);
08767
08768 Vector< Vector<double> > forward_propagation_forward(hidden_layers_number);
08769 Vector< Vector<double> > forward_propagation_backward(hidden_layers_number);
08770
08771 for(int i = 0; i < inputs_number; i++)
08772 {
08773
08774
08775 scaled_input_forward[i] += numerical_epsilon;
08776 scaled_input_backward[i] -= numerical_epsilon;
08777
08778
08779
08780 forward_propagation_forward = calculate_forward_propagation(scaled_input_forward);
08781 forward_propagation_backward = calculate_forward_propagation(scaled_input_backward);
08782
08783
08784
08785 for(int k = 0; k < outputs_number; k++)
08786 {
08787 Jacobian[k][i] = (forward_propagation_forward[hidden_layers_number][k] - forward_propagation_backward[hidden_layers_number][k])/(2.0*numerical_epsilon);
08788 }
08789
08790
08791
08792 scaled_input_forward[i] -= numerical_epsilon;
08793 scaled_input_backward[i] += numerical_epsilon;
08794 }
08795
08796 return(Jacobian);
08797 }
08798
08799
08800
08801
08805
08806 Matrix<double> MultilayerPerceptron::calculate_Jacobian(const Vector<double>& input)
08807 {
08808 #ifdef _DEBUG
08809
08810 int size = input.get_size();
08811
08812 if(size != inputs_number)
08813 {
08814 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
08815 << "void calculate_Jacobian(const Vector<double>&) method." << std::endl
08816 << "Size must be equal to number of inputs." << std::endl;
08817
08818 exit(1);
08819 }
08820
08821 #endif
08822
08823 check_input_range(input);
08824
08825 int hidden_layers_number = get_hidden_layers_number();
08826
08827 Vector< Vector<double> > hidden_layers_output(hidden_layers_number);
08828 hidden_layers_output[0] = calculate_hidden_layer_output(0, input);
08829
08830 Vector< Matrix<double> > hidden_layers_Jacobian(hidden_layers_number);
08831 hidden_layers_Jacobian[0] = calculate_hidden_layer_Jacobian(0, input);
08832
08833 for(int i = 1; i < hidden_layers_number; i++)
08834 {
08835 hidden_layers_output[i]
08836 = calculate_hidden_layer_output(i, hidden_layers_output[i-1]);
08837
08838 hidden_layers_Jacobian[i] = calculate_hidden_layer_Jacobian(i, hidden_layers_output[i-1]);
08839 }
08840
08841 Matrix<double> output_layer_Jacobian
08842 = calculate_output_layer_Jacobian(hidden_layers_output[hidden_layers_number-1]);
08843
08844
08845
08846 Matrix<double> Jacobian = output_layer_Jacobian.dot(hidden_layers_Jacobian[hidden_layers_number-1]);
08847
08848 for(int i = hidden_layers_number-2; i > -1; i--)
08849 {
08850 Jacobian = Jacobian.dot(hidden_layers_Jacobian[i]);
08851 }
08852
08853 return(Jacobian);
08854 }
08855
08856
08857
08858
08862
08863 Matrix<double> MultilayerPerceptron::calculate_Jacobian_numerical_differentiation(const Vector<double>& input)
08864 {
08865 Matrix<double> Jacobian(outputs_number, inputs_number);
08866
08867 switch(numerical_differentiation_method)
08868 {
08869 case ForwardDifferences:
08870 {
08871 Jacobian = calculate_Jacobian_forward_differences(input);
08872 }
08873 break;
08874
08875 case CentralDifferences:
08876 {
08877 Jacobian = calculate_Jacobian_central_differences(input);
08878 }
08879 break;
08880
08881 default:
08882 {
08883 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
08884 << "Matrix<double> calculate_Jacobian_numerical_differentiation(const Vector<double>&) method." << std::endl
08885 << "Unknown numerical differentiation method." << std::endl;
08886
08887 exit(1);
08888 }
08889 break;
08890
08891 }
08892
08893 return(Jacobian);
08894 }
08895
08896
08897
08898
08902 Matrix<double> MultilayerPerceptron::calculate_sensitivity(const Vector<double>& input)
08903 {
08904 #ifdef _DEBUG
08905
08906 int size = input.get_size();
08907
08908 if(size != inputs_number)
08909 {
08910 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
08911 << "void calculate_sensitivity(Vector<double>&) method." << std::endl
08912 << "Size must be equal to number of inputs." << std::endl;
08913
08914 exit(1);
08915 }
08916
08917 #endif
08918
08919 return(calculate_sensitivity_numerical_differentiation(input));
08920 }
08921
08922
08923
08924
08928
08929 Matrix<double> MultilayerPerceptron::calculate_sensitivity_forward_differences(const Vector<double>& input)
08930 {
08931 int hidden_layers_number = get_hidden_layers_number();
08932
08933 Vector<double> neural_parameters = get_neural_parameters();
08934 int neural_parameters_number = get_neural_parameters_number();
08935
08936 Matrix<double> sensitivity(outputs_number, neural_parameters_number);
08937
08938
08939
08940 check_input_range(input);
08941
08942 Vector<double> scaled_input = calculate_scaled_input(input);
08943
08944
08945
08946 Vector< Vector<double> > forward_propagation = calculate_forward_propagation(scaled_input);
08947
08948
08949
08950 Vector<double> neural_parameters_forward(neural_parameters);
08951
08952 Vector< Vector<double> > forward_propagation_forward(hidden_layers_number);
08953
08954 double actual_epsilon;
08955
08956 for(int i = 0; i < neural_parameters_number; i++)
08957 {
08958 actual_epsilon = calculate_actual_epsilon(neural_parameters[i]);
08959
08960
08961
08962 neural_parameters_forward[i] += actual_epsilon;
08963 set_neural_parameters(neural_parameters_forward);
08964
08965
08966
08967 forward_propagation_forward = calculate_forward_propagation(scaled_input);
08968
08969
08970
08971 for(int k = 0; k < outputs_number; k++)
08972 {
08973 sensitivity[k][i] = (forward_propagation_forward[hidden_layers_number][k] - forward_propagation[hidden_layers_number][k])/actual_epsilon;
08974 }
08975
08976
08977
08978 neural_parameters_forward[i] -= actual_epsilon;
08979 set_neural_parameters(neural_parameters_forward);
08980 }
08981
08982 return(sensitivity);
08983 }
08984
08985
08986
08987
08991
08992 Matrix<double> MultilayerPerceptron::calculate_sensitivity_central_differences(const Vector<double>& input)
08993 {
08994
08995
08996 check_input_range(input);
08997
08998 int neural_parameters_number = get_neural_parameters_number();
08999
09000 Matrix<double> sensitivity(outputs_number, neural_parameters_number);
09001
09002 int hidden_layers_number = get_hidden_layers_number();
09003
09004 Vector<double> scaled_input = calculate_scaled_input(input);
09005
09006 Vector<double> neural_parameters = get_neural_parameters();
09007
09008 Vector<double> neural_parameters_forward(neural_parameters);
09009 Vector<double> neural_parameters_backward(neural_parameters);
09010
09011 Vector< Vector<double> > hidden_layers_output_forward(hidden_layers_number);
09012 Vector< Vector<double> > hidden_layers_output_backward(hidden_layers_number);
09013
09014 Vector<double> output_layer_output_forward(outputs_number);
09015 Vector<double> output_layer_output_backward(outputs_number);
09016
09017 double actual_epsilon;
09018
09019 for(int i = 0; i < neural_parameters_number; i++)
09020 {
09021 actual_epsilon = calculate_actual_epsilon(neural_parameters[i]);
09022
09023
09024
09025 neural_parameters_forward[i] += actual_epsilon;
09026 set_neural_parameters(neural_parameters_forward);
09027
09028
09029
09030 hidden_layers_output_forward[0] = calculate_hidden_layer_output(0, input);
09031
09032 for(int j = 1; j < hidden_layers_number; j++)
09033
09034 {
09035 hidden_layers_output_forward[j]
09036 = calculate_hidden_layer_output(j, hidden_layers_output_forward[j-1]);
09037 }
09038
09039 output_layer_output_forward
09040 = calculate_output_layer_output(hidden_layers_output_forward[hidden_layers_number-1]);
09041
09042
09043
09044 neural_parameters_forward[i] -= actual_epsilon;
09045 set_neural_parameters(neural_parameters_forward);
09046
09047
09048
09049 neural_parameters_backward[i] -= actual_epsilon;
09050 set_neural_parameters(neural_parameters_backward);
09051
09052
09053
09054 hidden_layers_output_backward[0] = calculate_hidden_layer_output(0, input);
09055
09056 for(int j = 1; j < hidden_layers_number; j++)
09057 {
09058 hidden_layers_output_backward[j]
09059 = calculate_hidden_layer_output(j, hidden_layers_output_backward[j-1]);
09060 }
09061
09062 output_layer_output_backward
09063 = calculate_output_layer_output(hidden_layers_output_backward[hidden_layers_number-1]);
09064
09065
09066
09067 neural_parameters_backward[i] += actual_epsilon;
09068 set_neural_parameters(neural_parameters_backward);
09069
09070
09071
09072 for(int k = 0; k < outputs_number; k++)
09073 {
09074 sensitivity[k][i] = (output_layer_output_forward[k] - output_layer_output_backward[k])/(2.0*actual_epsilon);
09075 }
09076 }
09077
09078 return(sensitivity);
09079 }
09080
09081
09082
09083
09087
09088 Matrix<double> MultilayerPerceptron::calculate_sensitivity_numerical_differentiation(const Vector<double>& input)
09089 {
09090 int neural_parameters_number = get_neural_parameters_number();
09091
09092 Matrix<double> sensitivity(outputs_number, neural_parameters_number);
09093
09094 switch(numerical_differentiation_method)
09095 {
09096 case ForwardDifferences:
09097 {
09098 return(calculate_sensitivity_forward_differences(input));
09099 }
09100 break;
09101
09102 case CentralDifferences:
09103 {
09104 return(calculate_sensitivity_central_differences(input));
09105 }
09106 break;
09107
09108 default:
09109 {
09110 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09111 << "Matrix<double> calculate_sensitivity_numerical_differentiation(const Vector<double>&) method." << std::endl
09112 << "Unknown numerical differentiation method." << std::endl;
09113
09114 exit(1);
09115 }
09116 break;
09117 }
09118 }
09119
09120
09121
09122
09123
09127
09128 std::string MultilayerPerceptron::to_XML(bool show_declaration)
09129 {
09130 std::stringstream buffer;
09131
09132
09133
09134 if(show_declaration)
09135 {
09136 buffer << "<Flood version='3.0' class='MultilayerPerceptron'>" << std::endl;
09137 }
09138
09139
09140
09141 int hidden_layers_number = get_hidden_layers_number();
09142
09143 buffer << "<InputsNumber>" << std::endl
09144 << inputs_number << std::endl
09145 << "</InputsNumber>" << std::endl
09146 << "<HiddenLayersNumber>" << std::endl
09147 << hidden_layers_number << std::endl
09148 << "</HiddenLayersNumber>" << std::endl
09149 << "<HiddenLayersSize>" << std::endl
09150 << hidden_layers_size << std::endl
09151 << "</HiddenLayersSize>" << std::endl
09152 << "<OutputsNumber>" << std::endl
09153 << outputs_number << std::endl
09154 << "</OutputsNumber>" << std::endl;
09155
09156 int independent_parameters_number = get_independent_parameters_number();
09157
09158 buffer << "<IndependentParametersNumber>" << std::endl
09159 << independent_parameters_number << std::endl
09160 << "</IndependentParametersNumber>" << std::endl;
09161
09162
09163 if(inputs_number != 0 && hidden_layers_number != 0 && outputs_number != 0)
09164 {
09165
09166
09167 buffer << "<HiddenLayersActivationFunction>" << std::endl;
09168
09169 for(int i = 0; i < hidden_layers_number; i++)
09170 {
09171 buffer << get_hidden_layer_activation_function_name(i) << std::endl;
09172 }
09173
09174 buffer << "</HiddenLayersActivationFunction>" << std::endl;
09175
09176
09177
09178 buffer << "<OutputLayerActivationFunction>" << std::endl
09179 << get_output_layer_activation_function_name() << std::endl
09180 << "</OutputLayerActivationFunction>" << std::endl;
09181
09182
09183
09184 buffer << "<InputsScalingMethod>" << std::endl
09185 << get_inputs_scaling_method_name() << std::endl
09186 << "</InputsScalingMethod>" << std::endl;
09187
09188
09189
09190 buffer << "<OutputsUnscalingMethod>" << std::endl
09191 << get_outputs_unscaling_method_name() << std::endl
09192 << "</OutputsUnscalingMethod>" << std::endl;
09193
09194
09195
09196 Vector<double> neural_parameters = get_neural_parameters();
09197
09198 buffer << "<NeuralParameters>" << std::endl
09199 << neural_parameters << std::endl
09200 << "</NeuralParameters>" << std::endl;
09201
09202
09203
09204 if(input_variables_name.get_size() != 0)
09205 {
09206 buffer << "<InputVariablesName>" << std::endl;
09207
09208 for(int i = 0; i < inputs_number; i++)
09209 {
09210 buffer << "<Name>" << std::endl
09211 << input_variables_name[i] << std::endl
09212 << "</Name>" << std::endl;
09213 }
09214
09215 buffer << "</InputVariablesName>" << std::endl;
09216 }
09217
09218
09219
09220 if(output_variables_name.get_size() != 0)
09221 {
09222 buffer << "<OutputVariablesName>" << std::endl;
09223
09224 for(int i = 0; i < outputs_number; i++)
09225 {
09226 buffer << "<Name>" << std::endl
09227 << output_variables_name[i] << std::endl
09228 << "</Name>" << std::endl;
09229 }
09230
09231 buffer << "</OutputVariablesName>" << std::endl;
09232 }
09233
09234
09235
09236 if(input_variables_units.get_size() != 0)
09237 {
09238 buffer << "<InputVariablesUnits>" << std::endl;
09239
09240 for(int i = 0; i < inputs_number; i++)
09241 {
09242 buffer << "<Units>" << std::endl
09243 << input_variables_units[i] << std::endl
09244 << "</Units>" << std::endl;
09245 }
09246
09247 buffer << "</InputVariablesUnits>" << std::endl;
09248 }
09249
09250
09251
09252 if(output_variables_units.get_size() != 0)
09253 {
09254 buffer << "<OutputVariablesUnits>" << std::endl;
09255
09256 for(int i = 0; i < outputs_number; i++)
09257 {
09258 buffer << "<Units>" << std::endl
09259 << output_variables_units[i] << std::endl
09260 << "</Units>" << std::endl;
09261 }
09262
09263 buffer << "</OutputVariablesUnits>" << std::endl;
09264 }
09265
09266
09267
09268 if(input_variables_description.get_size() != 0)
09269 {
09270 buffer << "<InputVariablesDescription>" << std::endl;
09271
09272 for(int i = 0; i < inputs_number; i++)
09273 {
09274 buffer << "<Description>" << std::endl
09275 << input_variables_description[i] << std::endl
09276 << "</Description>" << std::endl;
09277 }
09278
09279 buffer << "</InputVariablesDescription>" << std::endl;
09280 }
09281
09282
09283
09284 if(output_variables_description.get_size() != 0)
09285 {
09286 buffer << "<OutputVariablesDescription>" << std::endl;
09287
09288 for(int i = 0; i < outputs_number; i++)
09289 {
09290 buffer << "<Description>" << std::endl
09291 << output_variables_description[i] << std::endl
09292 << "</Description>" << std::endl;
09293 }
09294
09295 buffer << "</OutputVariablesDescription>" << std::endl;
09296 }
09297
09298
09299
09300 if(input_variables_mean.get_size() != 0)
09301 {
09302 buffer << "<InputVariablesMean>" << std::endl
09303 << input_variables_mean << std::endl
09304 << "</InputVariablesMean>" << std::endl;
09305 }
09306
09307
09308
09309 if(input_variables_standard_deviation.get_size() != 0)
09310 {
09311 buffer << "<InputVariablesStandardDeviation>" << std::endl
09312 << input_variables_standard_deviation << std::endl
09313 << "</InputVariablesStandardDeviation>" << std::endl;
09314 }
09315
09316
09317
09318 if(output_variables_mean.get_size() != 0)
09319 {
09320 buffer << "<OutputVariablesMean>" << std::endl
09321 << output_variables_mean << std::endl
09322 << "</OutputVariablesMean>" << std::endl;
09323 }
09324
09325
09326
09327 if(output_variables_standard_deviation.get_size() != 0)
09328 {
09329 buffer << "<OutputVariablesStandardDeviation>" << std::endl
09330 << output_variables_standard_deviation << std::endl
09331 << "</OutputVariablesStandardDeviation>" << std::endl;
09332 }
09333
09334
09335
09336 if(input_variables_minimum.get_size() != 0)
09337 {
09338 buffer << "<InputVariablesMinimum>" << std::endl
09339 << input_variables_minimum << std::endl
09340 << "</InputVariablesMinimum>" << std::endl;
09341 }
09342
09343
09344
09345 if(input_variables_maximum.get_size() != 0)
09346 {
09347 buffer << "<InputVariablesMaximum>" << std::endl
09348 << input_variables_maximum << std::endl
09349 << "</InputVariablesMaximum>" << std::endl;
09350 }
09351
09352
09353
09354 if(output_variables_minimum.get_size() != 0)
09355 {
09356 buffer << "<OutputVariablesMinimum>" << std::endl
09357 << output_variables_minimum << std::endl
09358 << "</OutputVariablesMinimum>" << std::endl;
09359 }
09360
09361
09362
09363 if(output_variables_maximum.get_size() != 0)
09364 {
09365 buffer << "<OutputVariablesMaximum>" << std::endl
09366 << output_variables_maximum << std::endl
09367 << "</OutputVariablesMaximum>" << std::endl;
09368 }
09369
09370
09371
09372 if(output_variables_lower_bound.get_size() != 0)
09373 {
09374 buffer << "<OutputVariablesLowerBound>" << std::endl
09375 << output_variables_lower_bound << std::endl
09376 << "</OutputVariablesLowerBound>" << std::endl;
09377 }
09378
09379
09380
09381 if(output_variables_upper_bound.get_size() != 0)
09382 {
09383 buffer << "<OutputVariablesUpperBound>" << std::endl
09384 << output_variables_upper_bound << std::endl
09385 << "</OutputVariablesUpperBound>" << std::endl;
09386 }
09387 }
09388
09389
09390
09391 if(independent_parameters_number > 0)
09392 {
09393
09394
09395 if(independent_parameters_name.get_size() != 0)
09396 {
09397 buffer << "<IndependentParametersName>" << std::endl;
09398
09399 for(int i = 0; i < independent_parameters_number; i++)
09400 {
09401 buffer << "<Name>" << std::endl
09402 << independent_parameters_name[i] << std::endl
09403 << "</Name>" << std::endl;
09404 }
09405
09406 buffer << "</IndependentParametersName>" << std::endl;
09407 }
09408
09409
09410
09411 if(independent_parameters_units.get_size() != 0)
09412 {
09413 buffer << "<IndependentParametersUnits>" << std::endl;
09414
09415 for(int i = 0; i < independent_parameters_number; i++)
09416 {
09417 buffer << "<Units>" << std::endl
09418 << independent_parameters_units[i] << std::endl
09419 << "</Units>" << std::endl;
09420 }
09421
09422 buffer << "</IndependentParametersUnits>" << std::endl;
09423 }
09424
09425
09426
09427 if(independent_parameters_description.get_size() != 0)
09428 {
09429 buffer << "<IndependentParametersDescription>" << std::endl;
09430
09431 for(int i = 0; i < independent_parameters_number; i++)
09432 {
09433 buffer << "<Description>" << std::endl
09434 << independent_parameters_description[i] << std::endl
09435 << "</Description>" << std::endl;
09436 }
09437
09438 buffer << "</IndependentParametersDescription>" << std::endl;
09439 }
09440
09441
09442
09443 if(independent_parameters_mean.get_size() != 0)
09444 {
09445 buffer << "<IndependentParametersMean>" << std::endl
09446 << independent_parameters_mean << std::endl
09447 << "</IndependentParametersMean>" << std::endl;
09448 }
09449
09450
09451
09452 if(independent_parameters_standard_deviation.get_size() != 0)
09453 {
09454 buffer << "<IndependentParametersStandardDeviation>" << std::endl
09455 << independent_parameters_standard_deviation << std::endl
09456 << "</IndependentParametersStandardDeviation>" << std::endl;
09457 }
09458
09459
09460
09461 if(independent_parameters_minimum.get_size() != 0)
09462 {
09463 buffer << "<IndependentParametersMinimum>" << std::endl
09464 << independent_parameters_minimum << std::endl
09465 << "</IndependentParametersMinimum>" << std::endl;
09466 }
09467
09468
09469
09470 if(independent_parameters_maximum.get_size() != 0)
09471 {
09472 buffer << "<IndependentParametersMaximum>" << std::endl
09473 << independent_parameters_maximum << std::endl
09474 << "</IndependentParametersMaximum>" << std::endl;
09475 }
09476
09477
09478
09479 if(independent_parameters_lower_bound.get_size() != 0)
09480 {
09481 buffer << "<IndependentParametersLowerBound>" << std::endl
09482 << independent_parameters_lower_bound << std::endl
09483 << "<IndependentParametersLowerBound>" << std::endl;
09484 }
09485
09486
09487
09488 if(independent_parameters_upper_bound.get_size() != 0)
09489 {
09490 buffer << "<IndependentParametersUpperBound>" << std::endl
09491 << independent_parameters_upper_bound << std::endl
09492 << "</IndependentParametersUpperBound>" << std::endl;
09493 }
09494
09495
09496
09497 buffer << "<IndependentParametersScalingMethod>" << std::endl
09498 << get_independent_parameters_scaling_method_name() << std::endl
09499 << "</IndependentParametersScalingMethod>" << std::endl;
09500
09501
09502
09503 buffer << "<IndependentParameters>" << std::endl
09504 << independent_parameters << std::endl
09505 << "</IndependentParameters>" << std::endl;
09506 }
09507
09508
09509
09510 buffer << "<NumericalDifferentiationMethod>" << std::endl
09511 << get_numerical_differentiation_method_name() << std::endl
09512 << "</NumericalDifferentiationMethod>" << std::endl;
09513
09514
09515
09516 buffer << "<NumericalEpsilonMethod>" << std::endl
09517 << get_numerical_epsilon_method_name() << std::endl
09518 << "</NumericalEpsilonMethod>" << std::endl;
09519
09520
09521
09522 buffer << "<NumericalEpsilon>" << std::endl
09523 << numerical_epsilon << std::endl
09524 << "</NumericalEpsilon>" << std::endl;
09525
09526
09527
09528 buffer << "<Display>" << std::endl
09529 << display << std::endl
09530 << "</Display>" << std::endl;
09531
09532
09533
09534 buffer << "<DisplayRangeOutWarning>" << std::endl
09535 << display_range_out_warning << std::endl
09536 << "</DisplayRangeOutWarning>" << std::endl;
09537
09538 return(buffer.str());
09539 }
09540
09541
09542
09543
09545
09546 void MultilayerPerceptron::print(void)
09547 {
09548 if(display)
09549 {
09550 std::cout << to_XML(true);
09551 }
09552 }
09553
09554
09555
09556
09559
09560 void MultilayerPerceptron::save(const char* filename)
09561 {
09562 std::fstream file;
09563
09564 file.open(filename, std::ios::out);
09565
09566 if(!file.is_open())
09567 {
09568 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09569 << "void save(const char*) method." << std::endl
09570 << "Cannot open multilayer perceptron XML-type file." << std::endl;
09571
09572 exit(1);
09573 }
09574
09575
09576
09577 file << to_XML(true);
09578
09579
09580
09581 file.close();
09582 }
09583
09584
09585
09586
09590
09591 void MultilayerPerceptron::load(const char* filename)
09592 {
09593 std::fstream file;
09594
09595 file.open(filename, std::ios::in);
09596
09597 if(!file.is_open())
09598 {
09599 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09600 << "void load(const char*) method." << std::endl
09601 << "Cannot open multilayer perceptron XML-type file." << std::endl;
09602
09603 exit(1);
09604 }
09605
09606 set();
09607
09608 std::string line;
09609 std::string word;
09610
09611
09612
09613 getline(file, line);
09614
09615 if(line != "<Flood version='3.0' class='MultilayerPerceptron'>")
09616 {
09617
09618
09619
09620
09621
09622 }
09623
09624
09625
09626 file >> word;
09627
09628 if(word != "<InputsNumber>")
09629 {
09630 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09631 << "void load(const char*) method." << std::endl
09632 << "Unknown inputs number begin tag: " << line << std::endl;
09633
09634 exit(1);
09635 }
09636
09637 int new_inputs_number;
09638
09639 file >> new_inputs_number;
09640
09641 file >> word;
09642
09643 if(word != "</InputsNumber>")
09644 {
09645 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09646 << "void load(const char*) method." << std::endl
09647 << "Unknown inputs number end tag: " << line << std::endl;
09648
09649 exit(1);
09650 }
09651
09652
09653
09654 file >> word;
09655
09656 if(word != "<HiddenLayersNumber>")
09657 {
09658 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09659 << "void load(const char*) method." << std::endl
09660 << "Unknown hidden layers number begin tag: " << word << std::endl;
09661
09662 exit(1);
09663 }
09664
09665 int new_hidden_layers_number;
09666 file >> new_hidden_layers_number;
09667
09668 file >> word;
09669
09670 if(word != "</HiddenLayersNumber>")
09671 {
09672 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09673 << "void load(const char*) method." << std::endl
09674 << "Unknown hidden layers number end tag: " << word << std::endl;
09675
09676 exit(1);
09677 }
09678
09679
09680
09681 file >> word;
09682
09683 if(word != "<HiddenLayersSize>")
09684 {
09685 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09686 << "void load(const char*) method." << std::endl
09687 << "Unknown hidden layers size begin tag: " << word << std::endl;
09688
09689 exit(1);
09690 }
09691
09692 Vector<int> new_hidden_layers_size(new_hidden_layers_number);
09693 file >> new_hidden_layers_size;
09694
09695 file >> word;
09696
09697 if(word != "</HiddenLayersSize>")
09698 {
09699 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09700 << "void load(const char*) method." << std::endl
09701 << "Unknown hidden layers size end tag: " << word << std::endl;
09702
09703 exit(1);
09704 }
09705
09706
09707
09708 file >> word;
09709
09710 if(word != "<OutputsNumber>")
09711 {
09712 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09713 << "void load(const char*) method." << std::endl
09714 << "Unknown outputs number begin tag: " << word << std::endl;
09715
09716 exit(1);
09717 }
09718
09719 int new_outputs_number;
09720 file >> new_outputs_number;
09721
09722 file >> word;
09723
09724 if(word != "</OutputsNumber>")
09725 {
09726 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09727 << "void load(const char*) method." << std::endl
09728 << "Unknown outputs number end tag: " << word << std::endl;
09729
09730 exit(1);
09731 }
09732
09733 set_network_architecture(new_inputs_number, new_hidden_layers_size, new_outputs_number);
09734
09735
09736
09737 file >> word;
09738
09739 if(word != "<IndependentParametersNumber>")
09740 {
09741 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09742 << "void load(const char*) method." << std::endl
09743 << "Unknown independent parameters number begin tag: " << word << std::endl;
09744
09745 exit(1);
09746 }
09747
09748 int new_independent_parameters_number;
09749 file >> new_independent_parameters_number;
09750
09751 file >> word;
09752
09753 if(word != "</IndependentParametersNumber>")
09754 {
09755 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09756 << "void load(const char*) method." << std::endl
09757 << "Unknown independent parameters number end tag: " << word << std::endl;
09758
09759 exit(1);
09760 }
09761
09762 set_independent_parameters_number(new_independent_parameters_number);
09763
09764 while(!file.eof())
09765 {
09766 file >> word;
09767
09768 if(word == "<HiddenLayersActivationFunction>")
09769 {
09770 Vector<std::string> new_hidden_layers_activation_function(new_hidden_layers_number);
09771 file >> new_hidden_layers_activation_function;
09772 set_hidden_layers_activation_function(new_hidden_layers_activation_function);
09773
09774 file >> word;
09775
09776 if(word != "</HiddenLayersActivationFunction>")
09777 {
09778 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09779 << "void load(const char*) method." << std::endl
09780 << "Unknown hidden layers activation function end tag: " << word << std::endl;
09781
09782 exit(1);
09783 }
09784 }
09785 else if(word == "<OutputLayerActivationFunction>")
09786 {
09787 std::string new_output_layer_activation_function;
09788 file >> new_output_layer_activation_function;
09789 set_output_layer_activation_function(new_output_layer_activation_function);
09790
09791 file >> word;
09792
09793 if(word != "</OutputLayerActivationFunction>")
09794 {
09795 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09796 << "void load(const char*) method." << std::endl
09797 << "Unknown output layer activation function end tag: " << word << std::endl;
09798
09799 exit(1);
09800 }
09801 }
09802 else if(word == "<InputsScalingMethod>")
09803 {
09804 std::string new_inputs_scaling_method;
09805 file >> new_inputs_scaling_method;
09806 set_inputs_scaling_method(new_inputs_scaling_method);
09807
09808 file >> word;
09809
09810 if(word != "</InputsScalingMethod>")
09811 {
09812 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09813 << "void load(const char*) method." << std::endl
09814 << "Unknown inputs scaling method end tag: " << word << std::endl;
09815
09816 exit(1);
09817 }
09818 }
09819 else if(word == "<OutputsUnscalingMethod>")
09820 {
09821 std::string new_outputs_unscaling_method;
09822 file >> new_outputs_unscaling_method;
09823 set_outputs_unscaling_method(new_outputs_unscaling_method);
09824
09825 file >> word;
09826
09827 if(word != "</OutputsUnscalingMethod>")
09828 {
09829 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09830 << "void load(const char*) method." << std::endl
09831 << "Unknown outputs unscaling method end tag: " << word << std::endl;
09832
09833 exit(1);
09834 }
09835 }
09836 else if(word == "<NeuralParameters>")
09837 {
09838 int neural_parameters_number = get_neural_parameters_number();
09839
09840 Vector<double> new_neural_parameters(neural_parameters_number);
09841 file >> new_neural_parameters;
09842
09843 set_neural_parameters(new_neural_parameters);
09844
09845 file >> word;
09846
09847 if(word != "</NeuralParameters>")
09848 {
09849 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09850 << "void load(const char*) method." << std::endl
09851 << "Unknown neural parameters end tag: " << word << std::endl;
09852
09853 exit(1);
09854 }
09855 }
09856 else if(word == "<InputVariablesName>")
09857 {
09858 for(int i = 0; i < inputs_number; i++)
09859 {
09860 file >> word;
09861
09862 if(word != "<Name>")
09863 {
09864 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09865 << "void load(const char*) method." << std::endl
09866 << "Unknown name begin tag: " << word << std::endl;
09867
09868 exit(1);
09869 }
09870
09871 getline(file, line);
09872 getline(file, line);
09873
09874 set_input_variable_name(i, line);
09875
09876 file >> word;
09877
09878 if(word != "</Name>")
09879 {
09880 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09881 << "void load(const char*) method." << std::endl
09882 << "Unknown name end tag: " << word << std::endl;
09883
09884 exit(1);
09885 }
09886 }
09887
09888 file >> word;
09889
09890 if(word != "</InputVariablesName>")
09891 {
09892 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09893 << "void load(const char*) method." << std::endl
09894 << "Unknown input variables name end tag: " << word << std::endl;
09895
09896 exit(1);
09897 }
09898 }
09899 else if(word == "<OutputVariablesName>")
09900 {
09901 for(int i = 0; i < outputs_number; i++)
09902 {
09903 file >> word;
09904
09905 if(word != "<Name>")
09906 {
09907 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09908 << "void load(const char*) method." << std::endl
09909 << "Unknown name begin tag: " << word << std::endl;
09910
09911 exit(1);
09912 }
09913
09914 getline(file, line);
09915 getline(file, line);
09916
09917 set_output_variable_name(i, line);
09918
09919 file >> word;
09920
09921 if(word != "</Name>")
09922 {
09923 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09924 << "void load(const char*) method." << std::endl
09925 << "Unknown name end tag: " << word << std::endl;
09926
09927 exit(1);
09928 }
09929 }
09930
09931 file >> word;
09932
09933 if(word != "</OutputVariablesName>")
09934 {
09935 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09936 << "void load(const char*) method." << std::endl
09937 << "Unknown output variables name end tag: " << word << std::endl;
09938
09939 exit(1);
09940 }
09941 }
09942 else if(word == "<InputVariablesUnits>")
09943 {
09944 for(int i = 0; i < inputs_number; i++)
09945 {
09946 file >> word;
09947
09948 if(word != "<Units>")
09949 {
09950 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09951 << "void load(const char*) method." << std::endl
09952 << "Unknown units begin tag: " << word << std::endl;
09953
09954 exit(1);
09955 }
09956
09957 getline(file, line);
09958 getline(file, line);
09959
09960 set_input_variable_units(i, line);
09961
09962 file >> word;
09963
09964 if(word != "</Units>")
09965 {
09966 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09967 << "void load(const char*) method." << std::endl
09968 << "Unknown units end tag: " << word << std::endl;
09969
09970 exit(1);
09971 }
09972 }
09973
09974 file >> word;
09975 if(word != "</InputVariablesUnits>")
09976 {
09977 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09978 << "void load(const char*) method." << std::endl
09979 << "Unknown input variables units end tag: " << word << std::endl;
09980
09981 exit(1);
09982 }
09983 }
09984 else if(word == "<OutputVariablesUnits>")
09985 {
09986 for(int i = 0; i < outputs_number; i++)
09987 {
09988 file >> word;
09989
09990 if(word != "<Units>")
09991 {
09992 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
09993 << "void load(const char*) method." << std::endl
09994 << "Unknown units begin tag: " << word << std::endl;
09995
09996 exit(1);
09997 }
09998
09999 getline(file, line);
10000 getline(file, line);
10001
10002 set_output_variable_units(i, line);
10003
10004 file >> word;
10005
10006 if(word != "</Units>")
10007 {
10008 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10009 << "void load(const char*) method." << std::endl
10010 << "Unknown units end tag: " << word << std::endl;
10011
10012 exit(1);
10013 }
10014 }
10015
10016 file >> word;
10017
10018 if(word != "</OutputVariablesUnits>")
10019 {
10020 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10021 << "void load(const char*) method." << std::endl
10022 << "Unknown output variables units end tag: " << word << std::endl;
10023
10024 exit(1);
10025 }
10026 }
10027 else if(word == "<InputVariablesDescription>")
10028 {
10029 for(int i = 0; i < inputs_number; i++)
10030 {
10031 file >> word;
10032
10033 if(word != "<Description>")
10034 {
10035 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10036 << "void load(const char*) method." << std::endl
10037 << "Unknown description begin tag: " << word << std::endl;
10038
10039 exit(1);
10040 }
10041
10042 getline(file, line);
10043 getline(file, line);
10044
10045 set_input_variable_description(i, line);
10046
10047 file >> word;
10048
10049 if(word != "</Description>")
10050 {
10051 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10052 << "void load(const char*) method." << std::endl
10053 << "Unknown description end tag: " << word << std::endl;
10054
10055 exit(1);
10056 }
10057 }
10058
10059 file >> word;
10060
10061 if(word != "</InputVariablesDescription>")
10062 {
10063 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10064 << "void load(const char*) method." << std::endl
10065 << "Unknown input variables description end tag: " << word << std::endl;
10066
10067 exit(1);
10068 }
10069 }
10070 else if(word == "<OutputVariablesDescription>")
10071 {
10072 for(int i = 0; i < outputs_number; i++)
10073 {
10074 file >> word;
10075
10076 if(word != "<Description>")
10077 {
10078 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10079 << "void load(const char*) method." << std::endl
10080 << "Unknown description begin tag: " << word << std::endl;
10081
10082 exit(1);
10083 }
10084
10085 getline(file, line);
10086 getline(file, line);
10087
10088 set_output_variable_description(i, line);
10089
10090 file >> word;
10091
10092 if(word != "</Description>")
10093 {
10094 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10095 << "void load(const char*) method." << std::endl
10096 << "Unknown description end tag: " << word << std::endl;
10097
10098 exit(1);
10099 }
10100 }
10101
10102 file >> word;
10103
10104 if(word != "</OutputVariablesDescription>")
10105 {
10106 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10107 << "void load(const char*) method." << std::endl
10108 << "Unknown output variables description end tag: " << word << std::endl;
10109
10110 exit(1);
10111 }
10112 }
10113 else if(word == "<InputVariablesMean>")
10114 {
10115 Vector<double> new_input_variables_mean(inputs_number);
10116 file >> new_input_variables_mean;
10117
10118 file >> word;
10119
10120 if(word != "</InputVariablesMean>")
10121 {
10122 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10123 << "void load(const char*) method." << std::endl
10124 << "Unknown input variables mean end tag: " << word << std::endl;
10125
10126 exit(1);
10127 }
10128
10129 set_input_variables_mean(new_input_variables_mean);
10130 }
10131 else if(word == "<InputVariablesStandardDeviation>")
10132 {
10133 Vector<double> new_input_variables_standard_deviation(inputs_number);
10134 file >> new_input_variables_standard_deviation;
10135
10136 file >> word;
10137
10138 if(word != "</InputVariablesStandardDeviation>")
10139 {
10140 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10141 << "void load(const char*) method." << std::endl
10142 << "Unknown input variables standard deviation end tag: " << word << std::endl;
10143
10144 exit(1);
10145 }
10146
10147 set_input_variables_standard_deviation(new_input_variables_standard_deviation);
10148 }
10149 else if(word == "<OutputVariablesMean>")
10150 {
10151 Vector<double> new_output_variables_mean(outputs_number);
10152 file >> new_output_variables_mean;
10153
10154 file >> word;
10155
10156 if(word != "</OutputVariablesMean>")
10157 {
10158 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10159 << "void load(const char*) method." << std::endl
10160 << "Unknown output variables mean end tag: " << word << std::endl;
10161
10162 exit(1);
10163 }
10164
10165 set_output_variables_mean(new_output_variables_mean);
10166 }
10167 else if(word == "<OutputVariablesStandardDeviation>")
10168 {
10169 Vector<double> new_output_variables_standard_deviation(outputs_number);
10170 file >> new_output_variables_standard_deviation;
10171
10172 file >> word;
10173
10174 if(word != "</OutputVariablesStandardDeviation>")
10175 {
10176 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10177 << "void load(const char*) method." << std::endl
10178 << "Unknown output variables standard deviation end tag: " << word << std::endl;
10179
10180 exit(1);
10181 }
10182
10183 set_output_variables_standard_deviation(new_output_variables_standard_deviation);
10184 }
10185 else if(word == "<InputVariablesMinimum>")
10186 {
10187 Vector<double> new_input_variables_minimum(inputs_number);
10188 file >> new_input_variables_minimum;
10189
10190 file >> word;
10191
10192 if(word != "</InputVariablesMinimum>")
10193 {
10194 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10195 << "void load(const char*) method." << std::endl
10196 << "Unknown input variables minimum end tag: " << word << std::endl;
10197
10198 exit(1);
10199 }
10200
10201 set_input_variables_minimum(new_input_variables_minimum);
10202 }
10203 else if(word == "<InputVariablesMaximum>")
10204 {
10205 Vector<double> new_input_variables_maximum(inputs_number);
10206 file >> new_input_variables_maximum;
10207
10208 file >> word;
10209
10210 if(word != "</InputVariablesMaximum>")
10211 {
10212 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10213 << "void load(const char*) method." << std::endl
10214 << "Unknown input variables maximum end tag: " << word << std::endl;
10215
10216 exit(1);
10217 }
10218
10219 set_input_variables_maximum(new_input_variables_maximum);
10220 }
10221 else if(word == "<OutputVariablesMinimum>")
10222 {
10223 Vector<double> new_output_variables_minimum(outputs_number);
10224 file >> new_output_variables_minimum;
10225
10226 file >> word;
10227
10228 if(word != "</OutputVariablesMinimum>")
10229 {
10230 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10231 << "void load(const char*) method." << std::endl
10232 << "Unknown output variables minimum end tag: " << word << std::endl;
10233
10234 exit(1);
10235 }
10236
10237 set_output_variables_minimum(new_output_variables_minimum);
10238 }
10239 else if(word == "<OutputVariablesMaximum>")
10240 {
10241 Vector<double> new_output_variables_maximum(outputs_number);
10242 file >> new_output_variables_maximum;
10243
10244 file >> word;
10245
10246 if(word != "</OutputVariablesMaximum>")
10247 {
10248 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10249 << "void load(const char*) method." << std::endl
10250 << "Unknown output variables maximum end tag: " << word << std::endl;
10251
10252 exit(1);
10253 }
10254
10255 set_output_variables_maximum(new_output_variables_maximum);
10256 }
10257 else if(word == "<OutputVariablesLowerBound>")
10258 {
10259 Vector<double> new_output_variables_lower_bound(outputs_number);
10260 file >> new_output_variables_lower_bound;
10261
10262 file >> word;
10263
10264 if(word != "</OutputVariablesLowerBound>")
10265 {
10266 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10267 << "void load(const char*) method." << std::endl
10268 << "Unknown output variables lower bound end tag: " << word << std::endl;
10269
10270 exit(1);
10271 }
10272
10273 set_output_variables_lower_bound(new_output_variables_lower_bound);
10274 }
10275 else if(word == "<OutputVariablesUpperBound>")
10276 {
10277 Vector<double> new_output_variables_upper_bound(outputs_number);
10278 file >> new_output_variables_upper_bound;
10279
10280 file >> word;
10281
10282 if(word != "</OutputVariablesUpperBound>")
10283 {
10284 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10285 << "void load(const char*) method." << std::endl
10286 << "Unknown output variables upper bound end tag: " << word << std::endl;
10287
10288 exit(1);
10289 }
10290
10291 set_output_variables_upper_bound(new_output_variables_upper_bound);
10292 }
10293 else if(word == "<IndependentParametersName>")
10294 {
10295 for(int i = 0; i < new_independent_parameters_number; i++)
10296 {
10297 file >> word;
10298
10299 if(word != "<Name>")
10300 {
10301 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10302 << "void load(const char*) method." << std::endl
10303 << "Unknown name begin tag: " << word << std::endl;
10304
10305 exit(1);
10306 }
10307
10308 getline(file, line);
10309 getline(file, line);
10310
10311 set_independent_parameter_name(i, line);
10312
10313 file >> word;
10314
10315 if(word != "</Name>")
10316 {
10317 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10318 << "void load(const char*) method." << std::endl
10319 << "Unknown name end tag: " << word << std::endl;
10320
10321 exit(1);
10322 }
10323 }
10324 }
10325 else if(word == "<IndependentParametersUnits>")
10326 {
10327 for(int i = 0; i < new_independent_parameters_number; i++)
10328 {
10329 file >> word;
10330
10331 if(word != "<Units>")
10332 {
10333 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10334 << "void load(const char*) method." << std::endl
10335 << "Unknown units begin tag: " << word << std::endl;
10336
10337 exit(1);
10338 }
10339
10340 getline(file, line);
10341 getline(file, line);
10342
10343 set_independent_parameter_units(i, line);
10344
10345 file >> word;
10346
10347 if(word != "</Units>")
10348 {
10349 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10350 << "void load(const char*) method." << std::endl
10351 << "Unknown units end tag: " << word << std::endl;
10352
10353 exit(1);
10354 }
10355 }
10356 }
10357 else if(word == "<IndependentParametersDescription>")
10358 {
10359 for(int i = 0; i < new_independent_parameters_number; i++)
10360 {
10361 file >> word;
10362
10363 if(word != "<Description>")
10364 {
10365 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10366 << "void load(const char*) method." << std::endl
10367 << "Unknown description begin tag: " << word << std::endl;
10368
10369 exit(1);
10370 }
10371
10372 getline(file, line);
10373 getline(file, line);
10374
10375 set_independent_parameter_description(i, line);
10376
10377 file >> word;
10378
10379 if(word != "</Description>")
10380 {
10381 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10382 << "void load(const char*) method." << std::endl
10383 << "Unknown description end tag: " << word << std::endl;
10384
10385 exit(1);
10386 }
10387 }
10388 }
10389 else if(word == "<IndependentParametersMean>")
10390 {
10391 Vector<double> new_independent_parameters_mean(new_independent_parameters_number);
10392
10393 file >> new_independent_parameters_mean;
10394
10395 set_independent_parameters_mean(new_independent_parameters_mean);
10396
10397 file >> word;
10398
10399 if(word != "</IndependentParametersMean>")
10400 {
10401 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10402 << "void load(const char*) method." << std::endl
10403 << "Unknown independent parameters mean end tag: " << word << std::endl;
10404
10405 exit(1);
10406 }
10407 }
10408 else if(word == "<IndependentParametersStandardDeviation>")
10409 {
10410 Vector<double> new_independent_parameters_standard_deviation(new_independent_parameters_number);
10411
10412 file >> new_independent_parameters_standard_deviation;
10413
10414 set_independent_parameters_standard_deviation(new_independent_parameters_standard_deviation);
10415
10416 file >> word;
10417
10418 if(word != "</IndependentParametersStandardDeviation>")
10419 {
10420 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10421 << "void load(const char*) method." << std::endl
10422 << "Unknown independent parameters standard deviation end tag: " << word << std::endl;
10423
10424 exit(1);
10425 }
10426 }
10427 else if(word == "<IndependentParametersMinimum>")
10428 {
10429 Vector<double> new_independent_parameters_minimum(new_independent_parameters_number);
10430
10431 file >> new_independent_parameters_minimum;
10432
10433 set_independent_parameters_minimum(new_independent_parameters_minimum);
10434
10435 file >> word;
10436
10437 if(word != "</IndependentParametersMinimum>")
10438 {
10439 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10440 << "void load(const char*) method." << std::endl
10441 << "Unknown independent parameters minimum end tag: " << word << std::endl;
10442
10443 exit(1);
10444 }
10445 }
10446 else if(word == "<IndependentParametersMaximum>")
10447 {
10448 Vector<double> new_independent_parameters_maximum(new_independent_parameters_number);
10449
10450 file >> new_independent_parameters_maximum;
10451
10452 set_independent_parameters_maximum(new_independent_parameters_maximum);
10453
10454 file >> word;
10455
10456 if(word != "</IndependentParametersMaximum>")
10457 {
10458 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10459 << "void load(const char*) method." << std::endl
10460 << "Unknown independent parameters maximum end tag: " << word << std::endl;
10461
10462 exit(1);
10463 }
10464 }
10465 else if(word == "<NumericalDifferentiationMethod>")
10466 {
10467 file >> word;
10468
10469 set_numerical_differentiation_method(word);
10470
10471 file >> word;
10472
10473 if(word != "</NumericalDifferentiationMethod>")
10474 {
10475 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10476 << "void load(const char*) method." << std::endl
10477 << "Unknown numerical differentiation method end tag: " << word << std::endl;
10478
10479 exit(1);
10480 }
10481 }
10482 else if(word == "<NumericalEpsilonMethod>")
10483 {
10484 file >> word;
10485
10486 set_numerical_epsilon_method(word);
10487
10488 file >> word;
10489
10490 if(word != "</NumericalEpsilonMethod>")
10491 {
10492 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10493 << "void load(const char*) method." << std::endl
10494 << "Unknown numerical epsilon method end tag: " << word << std::endl;
10495
10496 exit(1);
10497 }
10498 }
10499 else if(word == "<NumericalEpsilon>")
10500 {
10501 double new_numerical_epsilon;
10502
10503 file >> new_numerical_epsilon;
10504
10505 set_numerical_epsilon(new_numerical_epsilon);
10506
10507 file >> word;
10508
10509 if(word != "</NumericalEpsilon>")
10510 {
10511 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10512 << "void load(const char*) method." << std::endl
10513 << "Unknown numerical epsilon end tag: " << word << std::endl;
10514
10515 exit(1);
10516 }
10517 }
10518 else if(word == "<DisplayRangeOutWarning>")
10519 {
10520 file >> display_range_out_warning;
10521
10522 file >> word;
10523
10524 if(word != "</DisplayRangeOutWarning>")
10525 {
10526 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10527 << "void load(const char*) method." << std::endl
10528 << "Unknown display range out warning end tag: " << word << std::endl;
10529
10530 exit(1);
10531 }
10532 }
10533 else if(word == "<Display>")
10534 {
10535 file >> display;
10536
10537 file >> word;
10538
10539 if(word != "</Display>")
10540 {
10541 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10542 << "void load(const char*) method." << std::endl
10543 << "Unknown display end tag: " << word << std::endl;
10544
10545 exit(1);
10546 }
10547 }
10548 }
10549 }
10550
10551
10552
10553
10556
10557 std::string MultilayerPerceptron::get_expression(void)
10558 {
10559 std::stringstream expression;
10560
10561 expression << "<Flood version='3.0' class='MultilayerPerceptron' content='Expression'>" << std::endl;
10562
10563
10564
10565 expression << "<InputsScaling>" << std::endl;
10566 expression << get_inputs_scaling_expression();
10567 expression << "</InputsScaling>" << std::endl;
10568
10569
10570
10571 expression << "<ForwardPropagation>" << std::endl;
10572 expression << get_forward_propagation_expression();
10573 expression << "</ForwardPropagation>" << std::endl;
10574
10575
10576
10577
10578 expression << "<OutputsUnscaling>" << std::endl;
10579 expression << get_outputs_unscaling_expression();
10580 expression << "</OutputsUnscaling>" << std::endl;
10581
10582 return(expression.str());
10583 }
10584
10585
10586
10587
10590
10591 Vector<std::string> MultilayerPerceptron::get_inputs_name_expression(void)
10592 {
10593 Vector<std::string> inputs_name_expression(inputs_number);
10594
10595 if(input_variables_name.get_size() != inputs_number)
10596 {
10597 for(int i = 0; i < inputs_number; i++)
10598 {
10599 std::stringstream buffer;
10600 buffer << "x" << i+1;
10601 inputs_name_expression[i] = buffer.str();
10602 }
10603 }
10604 else
10605 {
10606 inputs_name_expression = input_variables_name;
10607 }
10608
10609 return(inputs_name_expression);
10610 }
10611
10612
10613
10614
10617
10618 Vector<std::string> MultilayerPerceptron::get_scaled_inputs_name_expression(void)
10619 {
10620 Vector<std::string> scaled_inputs_name_expression(inputs_number);
10621
10622 Vector<std::string> inputs_name_expression = get_inputs_name_expression();
10623
10624 for(int i = 0; i < inputs_number; i++)
10625 {
10626 std::stringstream buffer;
10627 buffer << "scaled_" << inputs_name_expression[i];
10628 scaled_inputs_name_expression[i] = buffer.str();
10629 }
10630
10631 return(scaled_inputs_name_expression);
10632 }
10633
10634
10635
10636
10640
10641 Vector<std::string> MultilayerPerceptron::get_hidden_layer_inputs_name_expression(int index)
10642 {
10643 if(index == 0)
10644 {
10645 return(get_scaled_inputs_name_expression());
10646 }
10647 else
10648 {
10649 return(get_hidden_layer_outputs_name_expression(index-1));
10650 }
10651 }
10652
10653
10654
10655
10659
10660 Vector<std::string> MultilayerPerceptron::get_hidden_layer_outputs_name_expression(int index)
10661 {
10662 int hidden_layer_size = hidden_layers_size[index];
10663
10664 Vector<std::string> hidden_layer_outputs_name_expression(hidden_layer_size);
10665
10666 for(int i = 0; i < hidden_layer_size; i++)
10667 {
10668 std::stringstream buffer;
10669 buffer << "y" << index+1 << i+1;
10670 hidden_layer_outputs_name_expression[i] = buffer.str();
10671 }
10672
10673 return(hidden_layer_outputs_name_expression);
10674 }
10675
10676
10677
10678
10681
10682 Vector<std::string> MultilayerPerceptron::get_output_layer_inputs_name_expression(void)
10683 {
10684 int hidden_layers_number = get_hidden_layers_number();
10685
10686 return(get_hidden_layer_outputs_name_expression(hidden_layers_number-1));
10687 }
10688
10689
10690
10691
10694
10695 Vector<std::string> MultilayerPerceptron::get_scaled_outputs_name_expression(void)
10696 {
10697 Vector<std::string> scaled_outputs_name_expression(outputs_number);
10698
10699 Vector<std::string> outputs_name_expression = get_outputs_name_expression();
10700
10701 for(int i = 0; i < outputs_number; i++)
10702 {
10703 std::stringstream buffer;
10704 buffer << "scaled_" << outputs_name_expression[i];
10705 scaled_outputs_name_expression[i] = buffer.str();
10706 }
10707
10708 return(scaled_outputs_name_expression);
10709 }
10710
10711
10712
10713
10716
10717 Vector<std::string> MultilayerPerceptron::get_outputs_name_expression(void)
10718 {
10719 Vector<std::string> outputs_name_expression(outputs_number);
10720
10721 if(output_variables_name.get_size() != outputs_number)
10722 {
10723 for(int i = 0; i < outputs_number; i++)
10724 {
10725 std::stringstream buffer;
10726 buffer << "y" << i+1;
10727 outputs_name_expression[i] = buffer.str();
10728 }
10729 }
10730 else
10731 {
10732 outputs_name_expression = output_variables_name;
10733 }
10734
10735 return(outputs_name_expression);
10736 }
10737
10738
10739
10740
10743
10744 std::string MultilayerPerceptron::get_hidden_layer_activation_function_expression(int i)
10745 {
10746 switch(hidden_layers_activation_function[i])
10747 {
10748 case Logistic:
10749 {
10750 return("logistic");
10751 }
10752 break;
10753
10754 case HyperbolicTangent:
10755 {
10756 return("tanh");
10757 }
10758 break;
10759
10760 case Threshold:
10761 {
10762 return("threshold");
10763 }
10764 break;
10765
10766 case SymmetricThreshold:
10767 {
10768 return("symmetric_threshold");
10769 }
10770 break;
10771
10772 case Linear:
10773 {
10774 return("");
10775 }
10776 break;
10777
10778 default:
10779 {
10780 std::cerr << "Flood Error: MultilayerPerceptron class" << std::endl
10781 << "std::string get_hidden_layer_activation_function_expression(int) method." << std::endl
10782 << "Unknown hidden layer activation function." << std::endl;
10783
10784 exit(1);
10785 }
10786 break;
10787 }
10788 }
10789
10790
10791
10792
10795
10796 std::string MultilayerPerceptron::get_hidden_layer_expression(int i)
10797 {
10798 std::stringstream hidden_layer_expression;
10799
10800 int hidden_layer_outputs_number = hidden_layers_size[i];
10801 int hidden_layer_inputs_number = get_hidden_layer_inputs_number(i);
10802
10803 Vector<std::string> hidden_layer_inputs_name_expression = get_hidden_layer_inputs_name_expression(i);
10804 Vector<std::string> hidden_layer_outputs_name_expression = get_hidden_layer_outputs_name_expression(i);
10805
10806 std::string hidden_layer_activation_function_expression = get_hidden_layer_activation_function_expression(i);
10807
10808 double bias;
10809 double synaptic_weight;
10810
10811
10812
10813 for(int j = 0; j < hidden_layer_outputs_number; j++)
10814 {
10815 hidden_layer_expression << hidden_layer_outputs_name_expression[j] << "=";
10816
10817 bias = hidden_layers[i][j].get_bias();
10818
10819 hidden_layer_expression << hidden_layer_activation_function_expression << "(";
10820
10821 hidden_layer_expression << bias;
10822
10823 for(int k = 0; k < hidden_layer_inputs_number; k++)
10824 {
10825 synaptic_weight = hidden_layers[i][j].get_synaptic_weight(k);
10826
10827 if(synaptic_weight >= 0)
10828 {
10829 hidden_layer_expression << "+";
10830 }
10831
10832 hidden_layer_expression << synaptic_weight << "*" << hidden_layer_inputs_name_expression[k];
10833 }
10834
10835 hidden_layer_expression << ");" << std::endl;
10836 }
10837
10838 return(hidden_layer_expression.str());
10839 }
10840
10841
10842
10843
10845
10846 std::string MultilayerPerceptron::get_output_layer_expression(void)
10847 {
10848 std::stringstream output_layer_expression;
10849
10850 Vector<std::string> output_layer_inputs_name_expression = get_output_layer_inputs_name_expression();
10851
10852 std::string output_layer_activation_function_expression = get_output_layer_activation_function_expression();
10853
10854 Vector<std::string> scaled_outputs_name_expression = get_scaled_outputs_name_expression();
10855
10856 int output_layer_inputs_number = get_output_layer_inputs_number();
10857
10858 double bias;
10859 double synaptic_weight;
10860
10861 for(int i = 0; i < outputs_number; i++)
10862 {
10863 output_layer_expression << scaled_outputs_name_expression[i];
10864
10865 output_layer_expression << "=";
10866
10867 output_layer_expression << output_layer_activation_function_expression;
10868
10869 output_layer_expression << "(";
10870
10871 bias = output_layer[i].get_bias();
10872
10873 output_layer_expression << bias;
10874
10875 for(int j = 0; j < output_layer_inputs_number; j++)
10876 {
10877 synaptic_weight = output_layer[i].get_synaptic_weight(j);
10878
10879 if(synaptic_weight >= 0.0)
10880 {
10881 output_layer_expression << "+";
10882 }
10883
10884 output_layer_expression << synaptic_weight << "*" << output_layer_inputs_name_expression[j];
10885 }
10886
10887 output_layer_expression << ");" << std::endl;
10888 }
10889
10890 return(output_layer_expression.str());
10891 }
10892
10893
10894
10895
10897
10898 std::string MultilayerPerceptron::get_forward_propagation_expression(void)
10899 {
10900 std::stringstream forward_propagation_expression;
10901
10902 int hidden_layers_number = get_hidden_layers_number();
10903
10904 for(int i = 0; i< hidden_layers_number; i++)
10905 {
10906 forward_propagation_expression << get_hidden_layer_expression(i);
10907 }
10908
10909 forward_propagation_expression << get_output_layer_expression();
10910
10911 return(forward_propagation_expression.str());
10912 }
10913
10914
10915
10916
10918
10919 std::string MultilayerPerceptron::get_inputs_scaling_expression(void)
10920 {
10921 std::stringstream inputs_scaling_expression;
10922
10923
10924
10925 Vector<std::string> inputs_name_expression = get_inputs_name_expression();
10926
10927
10928
10929 Vector<std::string> scaled_inputs_name_expression = get_scaled_inputs_name_expression();
10930
10931
10932
10933 switch(inputs_scaling_method)
10934 {
10935 case None:
10936 {
10937
10938 }
10939 break;
10940
10941 case MeanStandardDeviation:
10942 {
10943
10944
10945 for(int i = 0; i < inputs_number; i++)
10946 {
10947 inputs_scaling_expression
10948 << scaled_inputs_name_expression[i] << "=(" << inputs_name_expression[i] << "-" << input_variables_mean[i]
10949 << ")/" << input_variables_standard_deviation[i] << ";" << std::endl;
10950 }
10951 }
10952 break;
10953
10954 case MinimumMaximum:
10955 {
10956
10957
10958 for(int i = 0; i < inputs_number; i++)
10959 {
10960 inputs_scaling_expression
10961 << scaled_inputs_name_expression[i] << "=2*(" << inputs_name_expression[i] << "-" << input_variables_minimum[i]
10962 << ")/(" << input_variables_maximum[i] << "-" << input_variables_minimum[i] << ")-1;" << std::endl;
10963 }
10964 }
10965 break;
10966
10967 default:
10968 {
10969 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
10970 << "std::string get_inputs_scaling_expression(void) method." << std::endl
10971 << "Unknown scaling and unscaling method." << std::endl;
10972
10973 exit(1);
10974 }
10975 break;
10976 }
10977
10978 return(inputs_scaling_expression.str());
10979 }
10980
10981
10982
10983
10985
10986 std::string MultilayerPerceptron::get_outputs_unscaling_expression(void)
10987 {
10988 std::stringstream outputs_unscaling_expression;
10989
10990
10991
10992 Vector<std::string> scaled_outputs_name_expression = get_scaled_outputs_name_expression();
10993
10994
10995
10996 Vector<std::string> outputs_name_expression = get_outputs_name_expression();
10997
10998
10999
11000 switch(outputs_unscaling_method)
11001 {
11002 case None:
11003 {
11004
11005 }
11006 break;
11007
11008 case MeanStandardDeviation:
11009 {
11010
11011
11012 for(int i = 0; i < outputs_number; i++)
11013 {
11014 outputs_unscaling_expression << outputs_name_expression[i] << "=" << output_variables_mean[i] << "+"
11015 << output_variables_standard_deviation[i] << "*" << scaled_outputs_name_expression[i] << ";" << std::endl;
11016 }
11017 }
11018 break;
11019
11020 case MinimumMaximum:
11021 {
11022
11023
11024 for(int i = 0; i < outputs_number; i++)
11025 {
11026 outputs_unscaling_expression << outputs_name_expression[i] << "=0.5*(" << outputs_name_expression[i] << "+1.0)*("
11027 << output_variables_maximum[i] << "-" << output_variables_minimum[i] << ")+"
11028 << output_variables_minimum[i] << ";" << std::endl;
11029 }
11030 }
11031 break;
11032 }
11033
11034 return(outputs_unscaling_expression.str());
11035 }
11036
11037
11038
11039
11041
11042 std::string MultilayerPerceptron::get_output_layer_activation_function_expression(void)
11043 {
11044 switch(output_layer_activation_function)
11045 {
11046 case Logistic:
11047 {
11048 return("logistic");
11049 }
11050 break;
11051
11052 case HyperbolicTangent:
11053 {
11054 return("tanh");
11055 }
11056 break;
11057
11058 case Threshold:
11059 {
11060 return("threshold");
11061 }
11062 break;
11063
11064 case SymmetricThreshold:
11065 {
11066 return("symmetric_threshold");
11067 }
11068 break;
11069
11070 case Linear:
11071 {
11072 return("");
11073 }
11074 break;
11075
11076 default:
11077 {
11078 std::cerr << "Flood Error: MultilayerPerceptron class" << std::endl
11079 << "std::string get_output_layer_activation_function_expression(void) method." << std::endl
11080 << "Unknown hidden layer activation function." << std::endl;
11081
11082 exit(1);
11083 }
11084 break;
11085 }
11086 }
11087
11088
11089
11090
11093
11094 void MultilayerPerceptron::save_expression(const char* filename)
11095 {
11096 std::fstream file;
11097
11098 file.precision(6);
11099
11100 file.open(filename, std::ios::out);
11101
11102 if(!file.is_open())
11103 {
11104 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
11105 << "void save_expression(const char*) method." << std::endl
11106 << "Cannot open expression XML-type file." << std::endl;
11107
11108 exit(1);
11109 }
11110
11111 file << get_expression();
11112
11113 file.close();
11114 }
11115
11116
11117
11118
11122
11123 void MultilayerPerceptron::save_Hinton_diagram(const char* filename)
11124 {
11125 std::fstream file;
11126
11127 file.open(filename, std::ios::in);
11128
11129 if(!file.is_open())
11130 {
11131 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
11132 << "void save_Hinton_diagramData(const char*) method." << std::endl
11133 << "Cannot open Hinton Diagram XML-type file." << std::endl;
11134
11135 exit(1);
11136 }
11137
11138
11139
11140 file << "% Flood Neural Network. Hinton Diagram Data." << std::endl;
11141
11142
11143
11144 int hidden_layers_number = get_hidden_layers_number();
11145
11146 Vector< Matrix<double> > hidden_layers_parameters = get_hidden_layers_parameters();
11147
11148 for(int i = 0; i < hidden_layers_number; i++)
11149 {
11150 file << "% Hidden layer " << i << "." << std::endl;
11151
11152 file << hidden_layers_parameters[i] << std::endl;
11153 }
11154
11155
11156
11157 Matrix<double> output_layer_parameters = get_output_layer_parameters();
11158
11159 file << "% Output layer." << std::endl;
11160
11161 file << output_layer_parameters << std::endl;
11162
11163 file.close();
11164 }
11165
11166
11167
11168
11173
11174 double MultilayerPerceptron::calculate_actual_epsilon(double parameter)
11175 {
11176 double actual_epsilon;
11177
11178
11179
11180 switch(numerical_epsilon_method)
11181 {
11182 case Absolute:
11183 {
11184 actual_epsilon = numerical_epsilon;
11185 }
11186 break;
11187
11188 case Relative:
11189 {
11190 if(fabs(parameter) < 1.0)
11191 {
11192 actual_epsilon = numerical_epsilon;
11193 }
11194 else
11195 {
11196 actual_epsilon = numerical_epsilon*fabs(parameter);
11197 }
11198 }
11199 break;
11200
11201 default:
11202 {
11203 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
11204 << "double calculate_actual_epsilon(double) method." << std::endl
11205 << "Unknown numerical epsilon method." << std::endl;
11206
11207 exit(1);
11208 }
11209 break;
11210 }
11211
11212 return(actual_epsilon);
11213 }
11214
11215
11216
11217
11224
11225 Matrix<double> MultilayerPerceptron::calculate_hidden_layer_Jacobian
11226 (int index, const Vector<double>& hidden_layer_input)
11227 {
11228
11229
11230 #ifdef _DEBUG
11231
11232 int hidden_layers_number = get_hidden_layers_number();
11233
11234 if(index < 0)
11235 {
11236 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
11237 << "void calculate_hidden_layer_Jacobian(int, const Vector<double>&) method." << std::endl
11238 << "Index of hidden layer must be equal or greater than zero." << std::endl;
11239
11240 exit(1);
11241 }
11242
11243 if(index >= hidden_layers_number)
11244 {
11245 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
11246 << "void calculate_hidden_layer_Jacobian(int, const Vector<double>&) method." << std::endl
11247 << "Index of hidden layer must be less than number of hidden layers." << std::endl;
11248
11249 exit(1);
11250 }
11251
11252 #endif
11253
11254
11255
11256 int hidden_layer_inputs_number = get_hidden_layer_inputs_number(index);
11257
11258 #ifdef _DEBUG
11259
11260 int size = hidden_layer_input.get_size();
11261
11262 if(size != hidden_layer_inputs_number)
11263 {
11264 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
11265 << "void calculate_hidden_layer_Jacobian(int, const Vector<double>&) method." << std::endl
11266 << "Size must be equal to number of inputs of hidden layer." << std::endl;
11267
11268 exit(1);
11269 }
11270
11271 #endif
11272
11273 int hidden_layer_size = hidden_layers_size[index];
11274
11275 Matrix<double> hidden_layer_Jacobian(hidden_layer_size, hidden_layer_inputs_number);
11276
11277 Vector<double> hidden_layer_combination = calculate_hidden_layer_combination(index, hidden_layer_input);
11278
11279 Vector<double> hidden_layer_activation_derivative
11280 = calculate_hidden_layer_activation_derivative(index, hidden_layer_combination);
11281
11282 for(int i = 0; i < hidden_layer_size; i++)
11283 {
11284 for(int j = 0; j < hidden_layer_inputs_number; j++)
11285 {
11286 hidden_layer_Jacobian[i][j]
11287 = hidden_layer_activation_derivative[i]*hidden_layers[index][i].get_synaptic_weight(j);
11288 }
11289 }
11290
11291 return(hidden_layer_Jacobian);
11292 }
11293
11294
11295
11296
11303
11304 Matrix<double> MultilayerPerceptron::calculate_output_layer_Jacobian(const Vector<double>& output_layer_input)
11305 {
11306 int hidden_layers_number = get_hidden_layers_number();
11307
11308 int output_layer_inputs_number = hidden_layers_size[hidden_layers_number-1];
11309
11310 #ifdef _DEBUG
11311
11312 int size = output_layer_input.get_size();
11313
11314 if(size != output_layer_inputs_number)
11315 {
11316 std::cerr << "Flood Error: MultilayerPerceptron class." << std::endl
11317 << "void calculate_output_layer_Jacobian(const Vector<double>&) method." << std::endl
11318 << "Size must be equal to number of inputs of output layer." << std::endl;
11319
11320 exit(1);
11321 }
11322
11323 #endif
11324
11325 Vector<double> output_layer_combination = calculate_output_layer_combination(output_layer_input);
11326
11327 Vector<double> output_layer_activation_derivative
11328 = calculate_output_layer_activation_derivative(output_layer_combination);
11329
11330 Matrix<double> output_layer_Jacobian(outputs_number, output_layer_inputs_number);
11331
11332 for(int i = 0; i < outputs_number; i++)
11333 {
11334 for(int j = 0; j < output_layer_inputs_number; j++)
11335 {
11336 output_layer_Jacobian[i][j]
11337 = output_layer_activation_derivative[i]*output_layer[i].get_synaptic_weight(j);
11338 }
11339 }
11340
11341 return(output_layer_Jacobian);
11342 }
11343
11344
11345
11346
11350
11351 int MultilayerPerceptron::get_parameters_number(void)
11352 {
11353 int neural_parameters_number = get_neural_parameters_number();
11354 int independent_parameters_number = get_independent_parameters_number();
11355
11356 int parameters_number = neural_parameters_number + independent_parameters_number;
11357
11358 return(parameters_number);
11359 }
11360
11361 }
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379