00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "RootMeanSquaredError.h"
00019
00020
00021
00022 #include <string>
00023 #include <sstream>
00024 #include <iostream>
00025 #include <fstream>
00026 #include <limits>
00027 #include <cmath>
00028
00029 namespace Flood
00030 {
00031
00032
00033
00037
00038 RootMeanSquaredError::RootMeanSquaredError(void) : ObjectiveFunctional()
00039 {
00040 input_target_data_set_pointer = NULL;
00041 }
00042
00043
00044
00045
00050
00051 RootMeanSquaredError::RootMeanSquaredError(MultilayerPerceptron* new_multilayer_perceptron_pointer)
00052 : ObjectiveFunctional(new_multilayer_perceptron_pointer)
00053 {
00054 input_target_data_set_pointer = NULL;
00055 }
00056
00057
00058
00059
00064
00065 RootMeanSquaredError::RootMeanSquaredError(InputTargetDataSet* new_input_target_data_set_pointer)
00066 : ObjectiveFunctional()
00067 {
00068 input_target_data_set_pointer = new_input_target_data_set_pointer;
00069 }
00070
00071
00072
00073
00079
00080 RootMeanSquaredError::RootMeanSquaredError(MultilayerPerceptron* new_multilayer_perceptron_pointer,
00081 InputTargetDataSet* new_input_target_data_set_pointer)
00082 : ObjectiveFunctional(new_multilayer_perceptron_pointer)
00083 {
00084 input_target_data_set_pointer = new_input_target_data_set_pointer;
00085 }
00086
00087
00088
00089
00091
00092 RootMeanSquaredError::~RootMeanSquaredError(void)
00093 {
00094 }
00095
00096
00097
00098
00099
00100
00103
00104 void RootMeanSquaredError::set(void)
00105 {
00106 multilayer_perceptron_pointer = NULL;
00107 input_target_data_set_pointer = NULL;
00108 set_default();
00109 }
00110
00111
00112
00113
00117
00118 void RootMeanSquaredError::set(MultilayerPerceptron* new_multilayer_perceptron_pointer)
00119 {
00120 multilayer_perceptron_pointer = new_multilayer_perceptron_pointer;
00121 input_target_data_set_pointer = NULL;
00122 set_default();
00123 }
00124
00125
00126
00127
00131
00132 void RootMeanSquaredError::set(InputTargetDataSet* new_input_target_data_set_pointer)
00133 {
00134 multilayer_perceptron_pointer = NULL;
00135 input_target_data_set_pointer = new_input_target_data_set_pointer;
00136 set_default();
00137 }
00138
00139
00140
00141
00146
00147 void RootMeanSquaredError::set(MultilayerPerceptron* new_multilayer_perceptron_pointer, InputTargetDataSet* new_input_target_data_set_pointer)
00148 {
00149 multilayer_perceptron_pointer = new_multilayer_perceptron_pointer;
00150 input_target_data_set_pointer = new_input_target_data_set_pointer;
00151 set_default();
00152 }
00153
00154
00155
00156
00159
00160 void RootMeanSquaredError
00161 ::set_input_target_data_set_pointer(InputTargetDataSet* new_input_target_data_set_pointer)
00162 {
00163 input_target_data_set_pointer = new_input_target_data_set_pointer;
00164 }
00165
00166
00167
00168
00171
00172 double RootMeanSquaredError::calculate_objective(void)
00173 {
00174 int inputs_number = multilayer_perceptron_pointer->get_inputs_number();
00175 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
00176
00177
00178
00179 #ifdef _DEBUG
00180
00181 if(multilayer_perceptron_pointer == NULL)
00182 {
00183 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00184 << "double calculate_objective(void) method." << std::endl
00185 << "Pointer to multilayer perceptron object cannot be NULL." << std::endl;
00186
00187 exit(1);
00188 }
00189 else if(input_target_data_set_pointer == NULL)
00190 {
00191 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00192 << "double calculate_objective(void) method." << std::endl
00193 << "Pointer to input-target data set object cannot be NULL." << std::endl;
00194
00195 exit(1);
00196 }
00197
00198 int input_variables_number = input_target_data_set_pointer->get_input_variables_number();
00199 int target_variables_number = input_target_data_set_pointer->get_target_variables_number();
00200
00201 if(inputs_number != input_variables_number || outputs_number != target_variables_number)
00202 {
00203 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00204 << "double calculate_objective(void) method." << std::endl
00205 << "Number of inputs and outputs in multilayer perceptron must be equal to "
00206 << "number of input and output variables in input-target data set." << std::endl;
00207
00208 exit(1);
00209 }
00210
00211 #endif
00212
00213 Vector<double> input(inputs_number);
00214 Vector<double> output(outputs_number);
00215 Vector<double> target(outputs_number);
00216 Vector<double> instance_error(outputs_number);
00217
00218 int training_instances_number = input_target_data_set_pointer->get_training_instances_number();
00219
00220 double training_error = 0.0;
00221
00222 for(int i = 0; i < training_instances_number; i++)
00223 {
00224
00225
00226 input = input_target_data_set_pointer->get_training_input_instance(i);
00227
00228
00229
00230 output = multilayer_perceptron_pointer->calculate_output(input);
00231
00232
00233
00234 target = input_target_data_set_pointer->get_training_target_instance(i);
00235
00236
00237
00238 instance_error = output - target;
00239
00240
00241
00242 training_error += instance_error.dot(instance_error);
00243 }
00244
00245 return(sqrt(training_error/(double)training_instances_number));
00246 }
00247
00248
00249
00250
00252
00253 Vector<double> RootMeanSquaredError::calculate_objective_gradient(void)
00254 {
00255
00256
00257 #ifdef _DEBUG
00258
00259 if(multilayer_perceptron_pointer == NULL)
00260 {
00261 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00262 << "double calculate_objective_gradient(void) method." << std::endl
00263 << "Pointer to multilayer perceptron object cannot be NULL." << std::endl;
00264
00265 exit(1);
00266 }
00267 else if(input_target_data_set_pointer == NULL)
00268 {
00269 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00270 << "double calculate_objective_gradient(void) method." << std::endl
00271 << "Pointer to input-target data set object cannot be NULL." << std::endl;
00272
00273 exit(1);
00274 }
00275
00276 #endif
00277
00278
00279
00280 int hidden_layers_number = multilayer_perceptron_pointer->get_hidden_layers_number();
00281
00282 Vector<int> hidden_layers_size = multilayer_perceptron_pointer->get_hidden_layers_size();
00283
00284 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
00285
00286 int forward_propagation_derivative_size = 2*hidden_layers_number + 2;
00287
00288 Vector< Vector<double> > forward_propagation_derivative(forward_propagation_derivative_size);
00289
00290
00291
00292 int training_instances_number = input_target_data_set_pointer->get_training_instances_number();
00293
00294 int input_variables_number = input_target_data_set_pointer->get_input_variables_number();
00295 int target_variables_number = input_target_data_set_pointer->get_target_variables_number(); Vector<double> training_input_instance(input_variables_number);
00296 Vector<double> training_target_instance(target_variables_number);
00297
00298
00299
00300 Vector<double> output_errors(outputs_number);
00301 Vector< Vector<double> > hidden_errors(hidden_layers_number);
00302
00303 for(int h = 0; h < hidden_layers_number; h++)
00304 {
00305 hidden_errors[h].set_size(hidden_layers_size[h]);
00306 }
00307
00308
00309
00310 int hidden_layers_parameters_number = multilayer_perceptron_pointer->get_hidden_layers_parameters_number();
00311 int output_layer_parameters_number = multilayer_perceptron_pointer->get_output_layer_parameters_number();
00312
00313 Vector<double> hidden_layers_error_gradient(hidden_layers_parameters_number, 0.0);
00314 Vector<double> output_layer_error_gradient(output_layer_parameters_number, 0.0);
00315
00316
00317
00318 for(int i = 0; i < training_instances_number; i++)
00319 {
00320 training_input_instance = input_target_data_set_pointer->get_training_input_instance(i);
00321
00322 forward_propagation_derivative = multilayer_perceptron_pointer->calculate_forward_propagation_derivative(training_input_instance);
00323
00324 training_target_instance = input_target_data_set_pointer->get_training_target_instance(i);
00325
00326 output_errors = calculate_output_errors(forward_propagation_derivative, training_target_instance);
00327
00328 hidden_errors = calculate_hidden_errors(forward_propagation_derivative, output_errors);
00329
00330 hidden_layers_error_gradient += calculate_hidden_layers_error_gradient(training_input_instance, forward_propagation_derivative, hidden_errors);
00331
00332 output_layer_error_gradient += calculate_output_layer_error_gradient(forward_propagation_derivative, output_errors);
00333 }
00334
00335 return(hidden_layers_error_gradient.assemble(output_layer_error_gradient));
00336 }
00337
00338
00339
00340
00344
00345 Vector<double> RootMeanSquaredError::calculate_output_errors
00346 (const Vector< Vector<double> >& forward_propagation_derivative, const Vector<double>& target)
00347 {
00348 int forward_propagation_derivative_size = forward_propagation_derivative.get_size();
00349 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
00350
00351
00352
00353 #ifdef _DEBUG
00354
00355 int hidden_layers_number = multilayer_perceptron_pointer->get_hidden_layers_number();
00356
00357 if(forward_propagation_derivative_size != 2*hidden_layers_number+2)
00358 {
00359 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00360 << "double calculate_output_errors(void) method." << std::endl
00361 << "Size of forward propagation derivative vector must be equal to 2*hidden_layers_number+2."
00362 << std::endl;
00363
00364 exit(1);
00365 }
00366
00367 int output_layer_output_size = forward_propagation_derivative[forward_propagation_derivative_size-2].get_size();
00368
00369 if(output_layer_output_size != outputs_number)
00370 {
00371 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00372 << "double calculate_output_errors(void) method." << std::endl
00373 << "Size of output layer output ("<< output_layer_output_size << ") must be equal to "
00374 << "number of outputs (" << outputs_number << ")." << std::endl;
00375
00376 exit(1);
00377 }
00378
00379 int output_layer_output_derivative_size
00380 = forward_propagation_derivative[forward_propagation_derivative_size-1].get_size();
00381
00382 if(output_layer_output_derivative_size != outputs_number)
00383 {
00384 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00385 << "double calculate_output_errors(void) method." << std::endl
00386 << "Size of output layer output derivative (" << output_layer_output_derivative_size << ")must be equal to " << "number of outputs (" << outputs_number << ")." << std::endl;
00387
00388 exit(1);
00389 }
00390
00391 int target_size = target.get_size();
00392
00393 if(target_size != outputs_number)
00394 {
00395 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00396 << "double calculate_output_errors(void) method." << std::endl
00397 << "Size of target must be equal to number of outputs." << std::endl;
00398
00399 exit(1);
00400 }
00401
00402 #endif
00403
00404 double evaluation = calculate_evaluation();
00405
00406 if(evaluation < 1.0e-99)
00407 {
00408 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00409 << "double calculate_output_errors(void) method." << std::endl
00410 << "Evaluation of root mean squared error is zero." << std::endl;
00411
00412 exit(1);
00413 }
00414
00415 Vector<double> output_layer_output_derivative = forward_propagation_derivative[forward_propagation_derivative_size-1];
00416 Vector<double> output_layer_output = forward_propagation_derivative[forward_propagation_derivative_size-2];
00417
00418 int training_instances_number = input_target_data_set_pointer->get_training_instances_number();
00419
00420 Vector<double> output_errors(outputs_number);
00421
00422 Vector<double> error(outputs_number);
00423
00424 MultilayerPerceptron::ScalingMethod outputs_unscaling_method = multilayer_perceptron_pointer->get_outputs_unscaling_method();
00425
00426 switch(outputs_unscaling_method)
00427 {
00428 case MultilayerPerceptron::None:
00429 {
00430 error = output_layer_output-target;
00431
00432 output_errors = output_layer_output_derivative*error/(evaluation*training_instances_number);
00433 }
00434 break;
00435
00436 case MultilayerPerceptron::MeanStandardDeviation:
00437 {
00438 Vector<double> output_variables_standard_deviation
00439 = multilayer_perceptron_pointer->get_output_variables_standard_deviation();
00440
00441 output_errors = output_layer_output_derivative*2.0*error*output_variables_standard_deviation/(double)training_instances_number;
00442 }
00443 break;
00444
00445 case MultilayerPerceptron::MinimumMaximum:
00446 {
00447 Vector<double> output_variables_minimum = multilayer_perceptron_pointer->get_output_variables_minimum();
00448 Vector<double> output_variables_maximum = multilayer_perceptron_pointer->get_output_variables_maximum();
00449
00450 Vector<double> output_variables_range = output_variables_maximum-output_variables_minimum;
00451
00452 output_errors = output_layer_output_derivative*2.0*(output_layer_output-target)*0.5*output_variables_range/(double)training_instances_number;
00453 }
00454 break;
00455
00456 default:
00457 {
00458 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00459 << "double calculate_output_errors(void) method." << std::endl
00460 << "Unknown outputs unscaling method." << std::endl;
00461
00462 exit(1);
00463 }
00464 break;
00465
00466 }
00467
00468 return(output_errors);
00469 }
00470
00471
00472
00473
00477
00478 Vector< Vector<double> > RootMeanSquaredError::calculate_hidden_errors
00479 (const Vector< Vector<double> >& forward_propagation_derivative, const Vector<double>& output_errors)
00480 {
00481 int hidden_layers_number = multilayer_perceptron_pointer->get_hidden_layers_number();
00482 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
00483
00484
00485
00486 #ifdef _DEBUG
00487
00488 int forward_propagation_derivative_size = forward_propagation_derivative.get_size();
00489
00490 if(forward_propagation_derivative_size != 2*hidden_layers_number+2)
00491 {
00492 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00493 << "Vector< Vector<double> > calculate_hidden_errors(const Vector< Vector<double> >&, const Vector<double>&) method." << std::endl
00494 << "Size of forward propagation derivative vector must be equal to 2*hidden_layers_number+2."
00495 << std::endl;
00496
00497 exit(1);
00498 }
00499
00500 int output_layer_output_size = forward_propagation_derivative[forward_propagation_derivative_size-2].get_size();
00501
00502 if(output_layer_output_size != outputs_number)
00503 {
00504 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00505 << "Vector< Vector<double> > calculate_hidden_errors(const Vector< Vector<double> >&, const Vector<double>&) method." << std::endl
00506 << "Size of output layer output ("<< output_layer_output_size << ") must be equal to "
00507 << "number of outputs (" << outputs_number << ")." << std::endl;
00508
00509 exit(1);
00510 }
00511
00512 int output_layer_output_derivative_size
00513 = forward_propagation_derivative[forward_propagation_derivative_size-1].get_size();
00514
00515 if(output_layer_output_derivative_size != outputs_number)
00516 {
00517 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00518 << "Vector< Vector<double> > calculate_hidden_errors(const Vector< Vector<double> >&, const Vector<double>&) method." << std::endl
00519 << "Size of output layer output derivative (" << output_layer_output_derivative_size << ")must be equal to "
00520 << "number of outputs (" << outputs_number << ")." << std::endl;
00521
00522 exit(1);
00523 }
00524
00525 int output_errors_size = output_errors.get_size();
00526
00527 if(output_errors_size != outputs_number)
00528 {
00529 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00530 << "Vector< Vector<double> > calculate_hidden_errors(const Vector< Vector<double> >&, const Vector<double>&) method." << std::endl
00531 << "Size of target must be equal to number of outputs." << std::endl;
00532
00533 exit(1);
00534 }
00535
00536 #endif
00537
00538
00539
00540 Vector<int> hidden_layers_size = multilayer_perceptron_pointer->get_hidden_layers_size();
00541
00542 Vector< Vector<double> > hidden_errors(hidden_layers_number);
00543
00544 for(int h = 0; h < hidden_layers_number; h++)
00545 {
00546 hidden_errors[h].set_size(hidden_layers_size[h]);
00547 }
00548
00549
00550
00551 Vector<Perceptron>& output_layer = multilayer_perceptron_pointer->get_output_layer();
00552 Vector< Vector<Perceptron> >& hidden_layers = multilayer_perceptron_pointer->get_hidden_layers();
00553
00554 Vector< Vector<double> > hidden_layers_output_derivative(hidden_layers_number);
00555
00556 for(int i = 0; i < hidden_layers_number; i++)
00557 {
00558 hidden_layers_output_derivative[i] = forward_propagation_derivative[1+2*i];
00559 }
00560
00561 Vector<double> synaptic_weights;
00562
00563 double sum;
00564
00565
00566
00567 for(int j = 0; j < hidden_layers_size[hidden_layers_number-1]; j++)
00568 {
00569 sum = 0.0;
00570
00571 for(int k = 0; k < outputs_number; k++)
00572 {
00573 synaptic_weights = output_layer[k].get_synaptic_weights();
00574
00575 sum += (synaptic_weights[j])*output_errors[k];
00576 }
00577
00578 hidden_errors[hidden_layers_number-1][j] = hidden_layers_output_derivative[hidden_layers_number-1][j]*sum;
00579 }
00580
00581
00582
00583 for(int h = hidden_layers_number-2; h >= 0; h--)
00584 {
00585 for(int j = 0; j < hidden_layers_size[h]; j++)
00586 {
00587 sum = 0.0;
00588
00589 for(int k = 0; k < hidden_layers_size[h+1]; k++)
00590 {
00591 synaptic_weights = hidden_layers[h+1][k].get_synaptic_weights();
00592
00593 sum += (synaptic_weights[j])*hidden_errors[h+1][k];
00594 }
00595
00596 hidden_errors[h][j] = hidden_layers_output_derivative[h][j]*sum;
00597 }
00598 }
00599
00600 return(hidden_errors);
00601 }
00602
00603
00604
00605
00606
00611
00612 Vector<double> RootMeanSquaredError::calculate_hidden_layers_error_gradient
00613 (const Vector<double>& input,
00614 const Vector< Vector<double> >& forward_propagation_derivative,
00615 const Vector< Vector<double> >& hidden_errors)
00616 {
00617 int inputs_number = multilayer_perceptron_pointer->get_inputs_number();
00618 int hidden_layers_number = multilayer_perceptron_pointer->get_hidden_layers_number();
00619
00620
00621
00622 #ifdef _DEBUG
00623
00624 int input_size = input.get_size();
00625
00626 if(input_size != inputs_number)
00627 {
00628 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00629 << "Vector< Vector<double> > calculate_hidden_layers_error_gradient(const Vector< Vector<double> >&, const Vector<double>&, const Vector<double>&) method." << std::endl
00630 << "Size of input (" << input_size << ") must be equal to inputs number (" << inputs_number << ")."
00631 << std::endl;
00632
00633 exit(1);
00634 }
00635
00636
00637 int forward_propagation_derivative_size = forward_propagation_derivative.get_size();
00638
00639 if(forward_propagation_derivative_size != 2*hidden_layers_number+2)
00640 {
00641 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00642 << "Vector< Vector<double> > calculate_hidden_layers_error_gradient(const Vector< Vector<double> >&, const Vector<double>&, const Vector<double>&) method." << std::endl
00643 << "Size of forward propagation derivative (" << forward_propagation_derivative_size << ") must be equal to 2*hidden_layers_number+2 (" << 2*hidden_layers_number+2 << ")."
00644 << std::endl;
00645
00646 exit(1);
00647 }
00648
00649 int hidden_errors_size = hidden_errors.get_size();
00650
00651 if(hidden_errors_size != hidden_layers_number)
00652 {
00653 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00654 << "Vector< Vector<double> > calculate_hidden_layers_error_gradient(const Vector< Vector<double> >&, const Vector<double>&) method." << std::endl
00655 << "Size of output errors ("<< hidden_errors_size << ") must be equal to number of hidden layers (" << hidden_layers_number << ")." << std::endl;
00656
00657 exit(1);
00658 }
00659
00660 #endif
00661
00662
00663
00664 Vector<int> hidden_layers_size = multilayer_perceptron_pointer->get_hidden_layers_size();
00665
00666 int hidden_layers_parameters_number = multilayer_perceptron_pointer->get_hidden_layers_parameters_number();
00667
00668 Vector< Vector<Perceptron> >& hidden_layers = multilayer_perceptron_pointer->get_hidden_layers();
00669
00670 Vector<double> synaptic_weights;
00671
00672 Vector< Vector<double> > hidden_layers_output(hidden_layers_number);
00673
00674 for(int i = 0; i < hidden_layers_number; i++)
00675 {
00676 hidden_layers_output[i] = forward_propagation_derivative[2*i];
00677 }
00678
00679 int index = 0;
00680
00681 Vector<double> hidden_layers_error_gradient(hidden_layers_parameters_number, 0.0);
00682
00683
00684
00685 for(int j = 0; j < hidden_layers_size[0]; j++)
00686 {
00687
00688
00689 hidden_layers_error_gradient[index] += hidden_errors[0][j];
00690 index++;
00691
00692
00693
00694 synaptic_weights = hidden_layers[0][j].get_synaptic_weights();
00695
00696 for(int k = 0; k < inputs_number; k++)
00697 {
00698 hidden_layers_error_gradient[index] += hidden_errors[0][j]*input[k];
00699 index++;
00700 }
00701 }
00702
00703
00704
00705 for(int h = 1; h < hidden_layers_number; h++)
00706 {
00707 for(int j = 0; j < hidden_layers_size[h]; j++)
00708 {
00709
00710
00711 hidden_layers_error_gradient[index] += hidden_errors[h][j];
00712 index++;
00713
00714
00715
00716 synaptic_weights = hidden_layers[h][j].get_synaptic_weights();
00717
00718 for(int k = 0; k < hidden_layers_size[h-1]; k++)
00719 {
00720 hidden_layers_error_gradient[index] += hidden_errors[h][j]*hidden_layers_output[h-1][k];
00721 index++;
00722 }
00723 }
00724 }
00725
00726 return(hidden_layers_error_gradient);
00727 }
00728
00729
00730
00731
00735
00736 Vector<double> RootMeanSquaredError::calculate_output_layer_error_gradient
00737 (const Vector< Vector<double> >& forward_propagation_derivative, const Vector<double>& output_errors)
00738 {
00739 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
00740 int hidden_layers_number = multilayer_perceptron_pointer->get_hidden_layers_number();
00741
00742
00743
00744 #ifdef _DEBUG
00745
00746 int forward_propagation_derivative_size = forward_propagation_derivative.get_size();
00747
00748 if(forward_propagation_derivative_size != 2*hidden_layers_number+2)
00749 {
00750 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00751 << "Vector< Vector<double> > calculate_output_layer_error_gradient(const Vector< Vector<double> >&, const Vector<double>&) method." << std::endl
00752 << "Size of forward propagation derivative (" << forward_propagation_derivative_size << ") must be equal to 2*hidden_layers_number+2 (" << 2*hidden_layers_number+2 << ")."
00753 << std::endl;
00754
00755 exit(1);
00756 }
00757
00758 int output_errors_size = output_errors.get_size();
00759
00760 if(output_errors_size != outputs_number)
00761 {
00762 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00763 << "Vector< Vector<double> > calculate_output_layer_error_gradient(const Vector< Vector<double> >&, const Vector<double>&) method." << std::endl
00764 << "Size of output errors ("<< output_errors_size << ") must be equal to number of outputs (" << outputs_number << ")." << std::endl;
00765
00766 exit(1);
00767 }
00768
00769 #endif
00770
00771
00772
00773 Vector<int> hidden_layers_size = multilayer_perceptron_pointer->get_hidden_layers_size();
00774
00775 int output_layer_parameters_number = multilayer_perceptron_pointer->get_output_layer_parameters_number();
00776
00777 Vector< Vector<double> > hidden_layers_output(hidden_layers_number);
00778
00779 for(int i = 0; i < hidden_layers_number; i++)
00780 {
00781 hidden_layers_output[i] = forward_propagation_derivative[2*i];
00782 }
00783
00784
00785
00786 Vector<double> output_layer_error_gradient(output_layer_parameters_number, 0.0);
00787
00788 int index = 0;
00789
00790 for(int j = 0; j < outputs_number; j++)
00791 {
00792
00793
00794 output_layer_error_gradient[index] += output_errors[j];
00795 index++;
00796
00797
00798
00799 for(int k = 0; k < hidden_layers_size[hidden_layers_number-1]; k++)
00800 {
00801 output_layer_error_gradient[index] = hidden_layers_output[hidden_layers_number-1][k]*output_errors[j];
00802 index++;
00803 }
00804 }
00805
00806 return(output_layer_error_gradient);
00807 }
00808
00809
00810
00811
00814
00815 double RootMeanSquaredError::calculate_validation_error(void)
00816 {
00817
00818
00819 #ifdef _DEBUG
00820
00821 if(multilayer_perceptron_pointer == NULL)
00822 {
00823 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00824 << "double calculate_validation_error(void) method." << std::endl
00825 << "Pointer to multilayer perceptron object cannot be NULL." << std::endl;
00826
00827 exit(1);
00828 }
00829 else if(input_target_data_set_pointer == NULL)
00830 {
00831 std::cerr << "Flood Error: RootMeanSquaredError class." << std::endl
00832 << "double calculate_validation_error(void) method." << std::endl
00833 << "Pointer to input-target data set object cannot be NULL." << std::endl;
00834
00835 exit(1);
00836 }
00837
00838 #endif
00839
00840 int inputs_number = multilayer_perceptron_pointer->get_inputs_number();
00841 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
00842
00843 #ifdef _DEBUG
00844
00845 int input_variables_number = input_target_data_set_pointer->get_input_variables_number();
00846 int target_variables_number = input_target_data_set_pointer->get_target_variables_number();
00847
00848 if(inputs_number != input_variables_number || outputs_number != target_variables_number)
00849 {
00850 std::cout << "Flood Error: RootMeanSquaredError class." << std::endl
00851 << "double calculate_validation_error(void) method." << std::endl
00852 << "Number of inputs and outputs in multilayer perceptron must be equal to "
00853 << "number of input and output variables in input-target data set." << std::endl;
00854
00855 exit(1);
00856 }
00857
00858 #endif
00859
00860 int validation_instances_number = input_target_data_set_pointer->get_validation_instances_number();
00861
00862 if(validation_instances_number == 0)
00863 {
00864 return(0.0);
00865 }
00866
00867 Vector<double> input(inputs_number);
00868 Vector<double> output(outputs_number);
00869 Vector<double> target(outputs_number);
00870
00871 double validation_error = 0.0;
00872
00873 for(int i = 0; i < validation_instances_number; i++)
00874 {
00875
00876
00877 input = input_target_data_set_pointer->get_validation_input_instance(i);
00878
00879
00880
00881 output = multilayer_perceptron_pointer->calculate_output(input);
00882
00883
00884
00885 target = input_target_data_set_pointer->get_validation_target_instance(i);
00886
00887
00888
00889 validation_error += (output - target).dot(output - target);
00890 }
00891
00892 return(sqrt(validation_error/(double)validation_instances_number));
00893 }
00894
00895 }
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912