00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <cmath>
00019 #include <cstdlib>
00020 #include <fstream>
00021 #include <iostream>
00022 #include <string>
00023 #include <sstream>
00024
00025
00026
00027 #include "unscaling_layer.h"
00028
00029 namespace OpenNN
00030 {
00031
00032
00033
00035
00036 UnscalingLayer::UnscalingLayer(void)
00037 {
00038 set();
00039 }
00040
00041
00043
00044 UnscalingLayer::UnscalingLayer(const unsigned int& new_unscaling_neurons_number)
00045 {
00046 set(new_unscaling_neurons_number);
00047 }
00048
00049
00051
00052 UnscalingLayer::UnscalingLayer(const Vector< Vector<double> > & new_statistics)
00053 {
00054 set(new_statistics);
00055 }
00056
00057
00059
00060 UnscalingLayer::UnscalingLayer(TiXmlElement* unscaling_layer_element)
00061 {
00062 set(unscaling_layer_element);
00063 }
00064
00065
00067
00068 UnscalingLayer::UnscalingLayer(const UnscalingLayer& other_unscaling_layer)
00069 {
00070 set(other_unscaling_layer);
00071 }
00072
00073
00074
00075
00077
00078 UnscalingLayer::~UnscalingLayer(void)
00079 {
00080 }
00081
00082
00083
00084
00085
00086
00089
00090 UnscalingLayer& UnscalingLayer::operator = (const UnscalingLayer& other_unscaling_layer)
00091 {
00092 if(this != &other_unscaling_layer)
00093 {
00094 minimums = other_unscaling_layer.minimums;
00095
00096 maximums = other_unscaling_layer.maximums;
00097
00098 means = other_unscaling_layer.means;
00099
00100 standard_deviations = other_unscaling_layer.standard_deviations;
00101
00102 unscaling_method = other_unscaling_layer.unscaling_method;
00103 display = other_unscaling_layer.display;
00104 }
00105
00106 return(*this);
00107 }
00108
00109
00110
00111
00112
00113
00117
00118 bool UnscalingLayer::operator == (const UnscalingLayer& other_unscaling_layer) const
00119 {
00120 if(minimums == other_unscaling_layer.minimums
00121 && maximums == other_unscaling_layer.maximums
00122 && means == other_unscaling_layer.means
00123 && standard_deviations == other_unscaling_layer.standard_deviations
00124 && unscaling_method == other_unscaling_layer.unscaling_method
00125 && display == other_unscaling_layer.display)
00126 {
00127 return(true);
00128 }
00129 else
00130 {
00131 return(false);
00132 }
00133 }
00134
00135
00136
00137
00139
00140 unsigned int UnscalingLayer::count_unscaling_neurons_number(void) const
00141 {
00142 return(minimums.size());
00143 }
00144
00145
00146
00147
00150
00151 const Vector<double>& UnscalingLayer::get_minimums(void) const
00152 {
00153 return(minimums);
00154 }
00155
00156
00157
00158
00161
00162 const double& UnscalingLayer::get_minimum(const unsigned int& i) const
00163 {
00164
00165
00166 #ifdef _DEBUG
00167
00168 std::ostringstream buffer;
00169
00170 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
00171
00172 if(unscaling_neurons_number == 0)
00173 {
00174 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00175 << "const double& get_minimum(const unsigned int&) const method.\n"
00176 << "Number of outputs is zero.\n";
00177
00178 throw std::logic_error(buffer.str());
00179 }
00180 else if(i >= unscaling_neurons_number)
00181 {
00182 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00183 << "double get_minimum(const unsigned int&) const method.\n"
00184 << "Index must be less than number of unscaling neurons.\n";
00185
00186 throw std::logic_error(buffer.str());
00187 }
00188
00189 #endif
00190
00191 return(minimums[i]);
00192 }
00193
00194
00195
00196
00199
00200 const Vector<double>& UnscalingLayer::get_maximums(void) const
00201 {
00202 return(maximums);
00203 }
00204
00205
00206
00207
00210
00211 const double& UnscalingLayer::get_maximum(const unsigned int& i) const
00212 {
00213
00214
00215 #ifdef _DEBUG
00216
00217 std::ostringstream buffer;
00218
00219 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
00220
00221 if(unscaling_neurons_number == 0)
00222 {
00223 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00224 << "const double& get_maximum(const unsigned int&) const method.\n"
00225 << "Number of outputs is zero.\n";
00226
00227 throw std::logic_error(buffer.str());
00228 }
00229 else if(i >= unscaling_neurons_number)
00230 {
00231 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00232 << "const double& get_maximum(const unsigned int&) const method.\n"
00233 << "Index must be less than number of unscaling neurons.\n";
00234
00235 throw std::logic_error(buffer.str());
00236 }
00237
00238 #endif
00239
00240 return(maximums[i]);
00241 }
00242
00243
00244
00245
00248
00249 const Vector<double>& UnscalingLayer::get_means(void) const
00250 {
00251 return(means);
00252 }
00253
00254
00255
00256
00259
00260 const double& UnscalingLayer::get_mean(const unsigned int& index) const
00261 {
00262
00263
00264 #ifdef _DEBUG
00265
00266 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
00267
00268 if(index >= unscaling_neurons_number)
00269 {
00270 std::ostringstream buffer;
00271
00272 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00273 << "const double& get_mean(unsigned int) method const.\n"
00274 << "Index of outputs must be less than number of unscaling neurons.\n";
00275
00276 throw std::logic_error(buffer.str());
00277 }
00278
00279 #endif
00280
00281 return(means[index]);
00282 }
00283
00284
00285
00286
00289
00290 const Vector<double>& UnscalingLayer::get_standard_deviations(void) const
00291 {
00292 return(standard_deviations);
00293 }
00294
00295
00296
00297
00301
00302 const double& UnscalingLayer::get_standard_deviation(const unsigned int& index) const
00303 {
00304
00305
00306 #ifdef _DEBUG
00307
00308 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
00309
00310 if(index >= unscaling_neurons_number)
00311 {
00312 std::ostringstream buffer;
00313
00314 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00315 << "double get_standard_deviation(const unsigned int&) const method.\n"
00316 << "Index of output variable must be less than number opf unscaling neurons.\n";
00317
00318 throw std::logic_error(buffer.str());
00319 }
00320
00321 #endif
00322
00323 return(standard_deviations[index]);
00324 }
00325
00326
00327
00328
00334
00335 Vector< Vector<double>* > UnscalingLayer::get_minimums_maximums(void)
00336 {
00337 Vector< Vector<double>* > minimums_maximums(2);
00338
00339 minimums_maximums[0] = &minimums;
00340 minimums_maximums[1] = &maximums;
00341
00342 return(minimums_maximums);
00343 }
00344
00345
00346
00347
00353
00354 Vector< Vector<double>* > UnscalingLayer::get_means_standard_deviations(void)
00355 {
00356 Vector< Vector<double>* > means_standard_deviations(2);
00357
00358 means_standard_deviations[0] = &means;
00359 means_standard_deviations[1] = &standard_deviations;
00360
00361 return(means_standard_deviations);
00362 }
00363
00364
00365
00366
00375
00376 Vector< Vector<double>* > UnscalingLayer::get_statistics(void)
00377 {
00378 Vector< Vector<double>* > statistics(4);
00379
00380 statistics[0] = &minimums;
00381 statistics[1] = &maximums;
00382 statistics[2] = &means;
00383 statistics[3] = &standard_deviations;
00384
00385 return(statistics);
00386 }
00387
00388
00389
00390
00392
00393 const UnscalingLayer::UnscalingMethod& UnscalingLayer::get_unscaling_method(void) const
00394 {
00395 return(unscaling_method);
00396 }
00397
00398
00399
00400
00402
00403 std::string UnscalingLayer::write_unscaling_method(void) const
00404 {
00405 if(unscaling_method == MeanStandardDeviation)
00406 {
00407 return("MeanStandardDeviation");
00408 }
00409 else if(unscaling_method == MinimumMaximum)
00410 {
00411 return("MinimumMaximum");
00412 }
00413 else
00414 {
00415 std::ostringstream buffer;
00416
00417 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00418 << "std::string write_unscaling_method(void) const method.\n"
00419 << "Unknown outputs unscaling method.\n";
00420
00421 throw std::logic_error(buffer.str());
00422 }
00423 }
00424
00425
00426
00427
00430
00431 const bool& UnscalingLayer::get_display(void) const
00432 {
00433 return(display);
00434 }
00435
00436
00437
00438
00440
00441 void UnscalingLayer::set(void)
00442 {
00443 minimums.set();
00444 maximums.set();
00445 means.set();
00446 standard_deviations.set();
00447
00448 set_default();
00449 }
00450
00451
00452
00453
00456
00457 void UnscalingLayer::set(const unsigned int& new_unscaling_neurons_number)
00458 {
00459 minimums.set(new_unscaling_neurons_number, -1.0);
00460 maximums.set(new_unscaling_neurons_number, 1.0);
00461 means.set(new_unscaling_neurons_number, 0.0);
00462 standard_deviations.set(new_unscaling_neurons_number, 1.0);
00463
00464 set_default();
00465 }
00466
00467
00468
00469
00474
00475 void UnscalingLayer::set(const Vector< Vector<double> >& new_statistics)
00476 {
00477 minimums = new_statistics[0];
00478 maximums = new_statistics[1];
00479 means = new_statistics[2];
00480 standard_deviations = new_statistics[3];
00481
00482 set_default();
00483 }
00484
00485
00486
00487
00490
00491 void UnscalingLayer::set(TiXmlElement* new_unscaling_layer_element)
00492 {
00493 from_XML(new_unscaling_layer_element);
00494 }
00495
00496
00497
00498
00500
00501 void UnscalingLayer::set(const UnscalingLayer& new_unscaling_layer)
00502 {
00503 minimums = new_unscaling_layer.minimums;
00504 maximums = new_unscaling_layer.maximums;
00505 means = new_unscaling_layer.means;
00506 standard_deviations = new_unscaling_layer.standard_deviations;
00507
00508 unscaling_method = new_unscaling_layer.unscaling_method;
00509
00510 display = new_unscaling_layer.display;
00511 }
00512
00513
00514
00515
00525
00526 void UnscalingLayer::set_default(void)
00527 {
00528 minimums.initialize(-1.0);
00529 maximums.initialize(1.0);
00530 means.initialize(0.0);
00531 standard_deviations.initialize(1.0);
00532
00533 set_unscaling_method(MinimumMaximum);
00534
00535 set_display(true);
00536 }
00537
00538
00539
00540
00544
00545 void UnscalingLayer::set_minimums(const Vector<double>& new_minimums)
00546 {
00547
00548
00549 #ifdef _DEBUG
00550
00551 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
00552
00553 if(new_minimums.size() != unscaling_neurons_number)
00554 {
00555 std::ostringstream buffer;
00556
00557 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00558 << "void set_minimums(const Vector<double>&) method.\n"
00559 << "Size must be equal to number of unscaling neurons.\n";
00560
00561 throw std::logic_error(buffer.str());
00562 }
00563
00564 #endif
00565
00566
00567
00568 minimums = new_minimums;
00569 }
00570
00571
00572
00573
00578
00579 void UnscalingLayer::set_minimum(const unsigned int& index, const double& new_minimum)
00580 {
00581 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
00582
00583
00584
00585 #ifdef _DEBUG
00586
00587 if(index >= unscaling_neurons_number)
00588 {
00589 std::ostringstream buffer;
00590
00591 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00592 << "void set_minimum(const unsigned int&, const double&) method.\n"
00593 << "Index of unscaling neuron must be less than number of unscaling neurons.\n";
00594
00595 throw std::logic_error(buffer.str());
00596 }
00597
00598 #endif
00599
00600 if(minimums.size() == 0)
00601 {
00602 minimums.set(unscaling_neurons_number, -1.0);
00603 }
00604
00605
00606
00607 minimums[index] = new_minimum;
00608 }
00609
00610
00611
00612
00616
00617 void UnscalingLayer::set_maximums(const Vector<double>& new_maximums)
00618 {
00619
00620
00621 #ifdef _DEBUG
00622
00623 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
00624
00625 if(new_maximums.size() != unscaling_neurons_number)
00626 {
00627 std::ostringstream buffer;
00628
00629 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00630 << "void set_maximum(const Vector<double>&) method.\n"
00631 << "Size must be equal to number of unscaling neurons.\n";
00632
00633 throw std::logic_error(buffer.str());
00634 }
00635
00636 #endif
00637
00638 maximums = new_maximums;
00639 }
00640
00641
00642
00643
00648
00649 void UnscalingLayer::set_maximum(const unsigned int& index, const double& new_maximum)
00650 {
00651 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
00652
00653
00654
00655 #ifdef _DEBUG
00656
00657 if(index >= unscaling_neurons_number)
00658 {
00659 std::ostringstream buffer;
00660
00661 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00662 << "void new_maximum(const unsigned int&, const double&) method.\n"
00663 << "Index must be less than number of unscaling neurons outputs.\n";
00664
00665 throw std::logic_error(buffer.str());
00666 }
00667
00668 #endif
00669
00670 if(maximums.size() == 0)
00671 {
00672 maximums.set(unscaling_neurons_number, 1.0);
00673 }
00674
00675
00676
00677 maximums[index] = new_maximum;
00678 }
00679
00680
00681
00682
00686
00687 void UnscalingLayer::set_means(const Vector<double>& new_means)
00688 {
00689
00690
00691 #ifdef _DEBUG
00692
00693 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
00694
00695 if(new_means.size() != unscaling_neurons_number)
00696 {
00697 std::ostringstream buffer;
00698
00699 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00700 << "void set_means(const Vector<double>&) method.\n"
00701 << "Size must be equal to number of unscaling neurons.\n";
00702
00703 throw std::logic_error(buffer.str());
00704 }
00705
00706 #endif
00707
00708
00709
00710 means = new_means;
00711 }
00712
00713
00714
00715
00720
00721 void UnscalingLayer::set_mean(const unsigned int& index, const double& new_mean)
00722 {
00723 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
00724
00725
00726
00727 #ifdef _DEBUG
00728
00729 if(index >= unscaling_neurons_number)
00730 {
00731 std::ostringstream buffer;
00732
00733 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00734 << "void set_mean(const unsigned int&, const double&) method.\n"
00735 << "Index must be less than number of unscaling neurons.\n";
00736
00737 throw std::logic_error(buffer.str());
00738 }
00739
00740 #endif
00741
00742
00743
00744 if(means.size() != unscaling_neurons_number)
00745 {
00746 means.set(unscaling_neurons_number, 0.0);
00747 }
00748
00749
00750
00751 means[index] = new_mean;
00752 }
00753
00754
00755
00756
00760
00761 void UnscalingLayer::set_standard_deviations(const Vector<double>& new_standard_deviations)
00762 {
00763
00764
00765 #ifdef _DEBUG
00766
00767 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
00768
00769 if(new_standard_deviations.size() != unscaling_neurons_number)
00770 {
00771 std::ostringstream buffer;
00772
00773 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00774 << "void set_standard_deviations(const Vector<double>&) method.\n"
00775 << "Size must be equal to number of unscaling neurons.\n";
00776
00777 throw std::logic_error(buffer.str());
00778 }
00779
00780 #endif
00781
00782
00783
00784 standard_deviations = new_standard_deviations;
00785 }
00786
00787
00788
00789
00795
00796 void UnscalingLayer::set_standard_deviation(const unsigned int& index, const double& new_standard_deviation)
00797 {
00798 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
00799
00800
00801
00802 #ifdef _DEBUG
00803
00804 if(index >= unscaling_neurons_number)
00805 {
00806 std::ostringstream buffer;
00807
00808 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00809 << "void set_standard_deviation(const unsigned int&, const double&) method.\n"
00810 << "Index must be less than number of unscaling neurons.\n";
00811
00812 throw std::logic_error(buffer.str());
00813 }
00814
00815 #endif
00816
00817
00818
00819 if(standard_deviations.size() != unscaling_neurons_number)
00820 {
00821 standard_deviations.set(unscaling_neurons_number, 1.0);
00822 }
00823
00824
00825
00826 standard_deviations[index] = new_standard_deviation;
00827 }
00828
00829
00830
00831
00838
00839 void UnscalingLayer::set_minimums_maximums(const Vector< Vector<double> >& new_minimums_maximums)
00840 {
00841
00842
00843 #ifdef _DEBUG
00844
00845 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
00846
00847 if(new_minimums_maximums.size() != 2)
00848 {
00849 std::ostringstream buffer;
00850
00851 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00852 << "void set_minimums_maximums(const Vector< Vector<double> >&) method.\n"
00853 << "Number of rows must be 2.\n";
00854
00855 throw std::logic_error(buffer.str());
00856 }
00857 else if(new_minimums_maximums[0].size() != unscaling_neurons_number
00858 && new_minimums_maximums[1].size() != unscaling_neurons_number)
00859 {
00860 std::ostringstream buffer;
00861
00862 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00863 << "void set_minimums_maximums(const Vector< Vector<double> >&) method.\n"
00864 << "Number of columns must be equal to number of unscaling neurons.\n";
00865
00866 throw std::logic_error(buffer.str());
00867 }
00868
00869
00870
00871 for(unsigned int i = 0; i < unscaling_neurons_number; i++)
00872 {
00873 if(new_minimums_maximums[0][i] > new_minimums_maximums[1][i])
00874 {
00875 std::ostringstream buffer;
00876
00877 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00878 << "void set_minimum_maximum(const Vector< Vector<double> >&) method.\n"
00879 << "Minimum of output variable " << i << " is greater than maximums of that variable.\n";
00880
00881 throw std::logic_error(buffer.str());
00882 }
00883 }
00884
00885 #endif
00886
00887
00888
00889 minimums = new_minimums_maximums[0];
00890 maximums = new_minimums_maximums[1];
00891 }
00892
00893
00894
00895
00901
00902 void UnscalingLayer::set_means_standard_deviations(const Vector< Vector<double> >& new_means_standard_deviations)
00903 {
00904
00905
00906 #ifdef _DEBUG
00907
00908 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
00909
00910 if(new_means_standard_deviations.size() != 2)
00911 {
00912 std::ostringstream buffer;
00913
00914 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00915 << "void set_mean_standard_deviation(const Vector< Vector<double> >&) method.\n"
00916 << "Size of vector must be 2.\n";
00917
00918 throw std::logic_error(buffer.str());
00919 }
00920 else if(new_means_standard_deviations[0].size() != unscaling_neurons_number && new_means_standard_deviations[1].size() != unscaling_neurons_number)
00921 {
00922 std::ostringstream buffer;
00923
00924 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00925 << "void set_mean_standard_deviation(const Vector< Vector<double> >&) method.\n"
00926 << "Number of columns must be equal to number of unscaling neurons.\n";
00927
00928 throw std::logic_error(buffer.str());
00929 }
00930
00931 #endif
00932
00933
00934
00935 means = new_means_standard_deviations[0];
00936 standard_deviations = new_means_standard_deviations[1];
00937 }
00938
00939
00940
00941
00955
00956 void UnscalingLayer::set_statistics(const Vector< Vector<double> >& new_statistics)
00957 {
00958
00959
00960 #ifdef _DEBUG
00961
00962 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
00963
00964 const unsigned int new_statistics_size = new_statistics.size();
00965
00966 if(new_statistics_size != 4)
00967 {
00968 std::ostringstream buffer;
00969
00970 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00971 << "void set_statistics(const Vector< Vector<double> >&) method.\n"
00972 << "Size must be 4.\n";
00973
00974 throw std::logic_error(buffer.str());
00975 }
00976
00977 unsigned int size;
00978
00979 for(unsigned int i = 0; i < 4; i++)
00980 {
00981 size = new_statistics[i].size();
00982
00983 if(size != unscaling_neurons_number)
00984 {
00985 std::ostringstream buffer;
00986
00987 buffer << "OpenNN Exception: UnscalingLayer class.\n"
00988 << "void set_statistics(const Vector< Vector<double> >&) method.\n"
00989 << "Size of element " << i << " must be equal to number of unscaling neurons.\n";
00990
00991 throw std::logic_error(buffer.str());
00992 }
00993 }
00994
00995 #endif
00996
00997
00998
00999 set_minimums(new_statistics[0]);
01000 set_maximums(new_statistics[1]);
01001 set_means(new_statistics[2]);
01002 set_standard_deviations(new_statistics[3]);
01003 }
01004
01005
01006
01007
01010
01011 void UnscalingLayer::set_unscaling_method(const UnscalingLayer::UnscalingMethod& new_unscaling_method)
01012 {
01013 unscaling_method = new_unscaling_method;
01014 }
01015
01016
01017
01018
01022
01023 void UnscalingLayer::set_unscaling_method(const std::string& new_unscaling_method)
01024 {
01025 if(new_unscaling_method == "MeanStandardDeviation")
01026 {
01027 set_unscaling_method(MeanStandardDeviation);
01028 }
01029 else if(new_unscaling_method == "MinimumMaximum")
01030 {
01031 set_unscaling_method(MinimumMaximum);
01032 }
01033 else
01034 {
01035 std::ostringstream buffer;
01036
01037 buffer << "OpenNN Exception: UnscalingLayer class.\n"
01038 << "void set_unscaling_method(const std::string&) method.\n"
01039 << "Unknown unscaling method: " << new_unscaling_method << ".\n";
01040
01041 throw std::logic_error(buffer.str());
01042 }
01043 }
01044
01045
01046
01047
01052
01053 void UnscalingLayer::set_display(const bool& new_display)
01054 {
01055 display = new_display;
01056 }
01057
01058
01059
01060
01064
01065 void UnscalingLayer::check_range(const Vector<double>& outputs) const
01066 {
01067 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
01068
01069
01070
01071 #ifdef _DEBUG
01072
01073 const unsigned int size = outputs.size();
01074
01075 if(size != unscaling_neurons_number)
01076 {
01077 std::ostringstream buffer;
01078
01079 buffer << "OpenNN Exception: UnscalingLayer class.\n"
01080 << "void check_range(const Vector<double>&) const method.\n"
01081 << "Size of outputs must be equal to number of unscaling neurons.\n";
01082
01083 throw std::logic_error(buffer.str());
01084 }
01085
01086 #endif
01087
01088
01089
01090 if(display)
01091 {
01092 if(minimums.size() != 0)
01093 {
01094 for(unsigned int i = 0; i < unscaling_neurons_number; i++)
01095 {
01096 if(outputs[i] < minimums[i])
01097 {
01098 std::cout << "OpenNN Warning: UnscalingLayer class.\n"
01099 << "void check_range(const Vector<double>&) const method.\n"
01100 << "Output variable " << i << " is less than outputs.\n";
01101 }
01102 }
01103 }
01104 if(maximums.size() != 0)
01105 {
01106 for(unsigned int i = 0; i < unscaling_neurons_number; i++)
01107 {
01108 if(outputs[i] > maximums[i])
01109 {
01110 std::cout << "OpenNN Warning: UnscalingLayer class.\n"
01111 << "void check_range(const Vector<double>&) const method.\n"
01112 << "Output variable " << i << " is greater than maximum.\n";
01113 }
01114 }
01115 }
01116 }
01117 }
01118
01119
01120
01121
01123
01124 bool UnscalingLayer::is_empty(void) const
01125 {
01126 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
01127
01128 if(unscaling_neurons_number == 0)
01129 {
01130 return(true);
01131 }
01132 else
01133 {
01134 return(false);
01135 }
01136 }
01137
01138
01139
01140
01142
01143 void UnscalingLayer::initialize_random(void)
01144 {
01145
01146 }
01147
01148
01149
01150
01153
01154 Vector<double> UnscalingLayer::calculate_outputs(const Vector<double>& inputs) const
01155 {
01156
01157
01158 #ifdef _DEBUG
01159
01160 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
01161
01162 const unsigned int size = inputs.size();
01163
01164 if(size != unscaling_neurons_number)
01165 {
01166 std::ostringstream buffer;
01167
01168 buffer << "OpenNN Exception: UnscalingLayer class.\n"
01169 << "Vector<double> calculate_outputs(const Vector<double>&) const method.\n"
01170 << "Size must be equal to number of unscaling neurons.\n";
01171
01172 throw std::logic_error(buffer.str());
01173 }
01174
01175 #endif
01176
01177
01178
01179 switch(unscaling_method)
01180 {
01181 case MinimumMaximum:
01182 {
01183 return(calculate_minimum_maximum_output(inputs));
01184 }
01185 break;
01186
01187 case MeanStandardDeviation:
01188 {
01189 return(calculate_mean_standard_deviation_output(inputs));
01190 }
01191 break;
01192
01193 default:
01194 {
01195 std::ostringstream buffer;
01196
01197 buffer << "OpenNN Exception: UnscalingLayer class.\n"
01198 << "Vector<double> calculate_outputs(const Vector<double>&) const method.\n"
01199 << "Unknown unscaling method.\n";
01200
01201 throw std::logic_error(buffer.str());
01202 }
01203 break;
01204
01205 }
01206 }
01207
01208
01209
01210
01213
01214 Vector<double> UnscalingLayer::calculate_derivative(const Vector<double>& inputs) const
01215 {
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235 switch(unscaling_method)
01236 {
01237 case MinimumMaximum:
01238 {
01239 return(calculate_minimum_maximum_derivative(inputs));
01240 }
01241 break;
01242
01243 case MeanStandardDeviation:
01244 {
01245 return(calculate_mean_standard_deviation_derivative(inputs));
01246 }
01247 break;
01248
01249 default:
01250 {
01251 std::ostringstream buffer;
01252
01253 buffer << "OpenNN Exception: UnscalingLayer class.\n"
01254 << "Vector<double> calculate_derivative(const Vector<double>&) const.\n"
01255 << "Unknown scaling and unscaling method.\n";
01256
01257 throw std::logic_error(buffer.str());
01258 }
01259 break;
01260
01261 }
01262 }
01263
01264
01265
01266
01269
01270 Vector<double> UnscalingLayer::calculate_second_derivative(const Vector<double>& inputs) const
01271 {
01272 switch(unscaling_method)
01273 {
01274 case MinimumMaximum:
01275 {
01276 return(calculate_minimum_maximum_second_derivative(inputs));
01277 }
01278 break;
01279
01280 case MeanStandardDeviation:
01281 {
01282 return(calculate_mean_standard_deviation_second_derivative(inputs));
01283 }
01284 break;
01285
01286 default:
01287 {
01288 std::ostringstream buffer;
01289
01290 buffer << "OpenNN Exception: UnscalingLayer class.\n"
01291 << "Vector<double> calculate_second_derivative(const Vector<double>&) const.\n"
01292 << "Unknown scaling and unscaling method.\n";
01293
01294 throw std::logic_error(buffer.str());
01295 }
01296 break;
01297
01298 }
01299 }
01300
01301
01302
01303
01306
01307 Vector<double> UnscalingLayer::calculate_minimum_maximum_output(const Vector<double>& inputs) const
01308 {
01309 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
01310
01311 Vector<double> outputs(unscaling_neurons_number);
01312
01313 for(unsigned int i = 0; i < unscaling_neurons_number; i++)
01314 {
01315 if(maximums[i]-minimums[i] < 1e-99)
01316 {
01317 if(display)
01318 {
01319 std::cout << "OpenNN Warning: UnscalingLayer class.\n"
01320 << "Vector<double> calculate_minimum_maximum_output(Vector<double>&) const method.\n"
01321 << "Minimum and maximum values of output variable " << i << " are equal.\n"
01322 << "Those outputs won't be unscaled.\n";
01323 }
01324
01325 outputs[i] = inputs[i];
01326 }
01327 else
01328 {
01329 outputs[i] = 0.5*(inputs[i] + 1.0)*(maximums[i]-minimums[i]) + minimums[i];
01330 }
01331 }
01332
01333 return(outputs);
01334 }
01335
01336
01337
01338
01341
01342 Vector<double> UnscalingLayer::calculate_minimum_maximum_derivative(const Vector<double>&) const
01343 {
01344 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
01345
01346 Vector<double> derivative(unscaling_neurons_number);
01347
01348 for(unsigned int i = 0; i < unscaling_neurons_number; i++)
01349 {
01350 if(maximums[i]-minimums[i] < 1e-99)
01351 {
01352 if(display)
01353 {
01354 std::cout << "OpenNN Warning: UnscalingLayer class.\n"
01355 << "Vector<double> calculate_minimum_maximum_derivative(const Vector<double>&) const.\n"
01356 << "Minimum and maximum values of output variable " << i << " are equal.\n"
01357 << "Those derivatives won't be unscaled.\n";
01358 }
01359
01360 derivative[i] = 1.0;
01361 }
01362 else
01363 {
01364 derivative[i] = 0.5*(maximums[i]-minimums[i]);
01365 }
01366 }
01367
01368 return(derivative);
01369 }
01370
01371
01372
01373
01376
01377 Vector<double> UnscalingLayer::calculate_minimum_maximum_second_derivative(const Vector<double>&) const
01378 {
01379 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
01380
01381 const Vector<double> unscaled_second_derivative(unscaling_neurons_number, 0.0);
01382
01383 return(unscaled_second_derivative);
01384 }
01385
01386
01387
01388
01391
01392 Vector<double> UnscalingLayer::calculate_mean_standard_deviation_output(const Vector<double>& inputs) const
01393 {
01394 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
01395
01396 Vector<double> outputs(unscaling_neurons_number);
01397
01398 for(unsigned int i = 0; i < unscaling_neurons_number; i++)
01399 {
01400 if(standard_deviations[i] < 1e-99)
01401 {
01402 if(display)
01403 {
01404 std::cout << "OpenNN Warning: UnscalingLayer class.\n"
01405 << "Vector<double> calculate_mean_standard_deviation_output(const Vector<double>&) const method.\n"
01406 << "Standard deviation of output variable " << i << " is zero.\n"
01407 << "Those outputs won't be unscaled.\n";
01408 }
01409
01410 outputs[i] = inputs[i];
01411 }
01412 else
01413 {
01414 outputs[i] = inputs[i]*standard_deviations[i] + means[i];
01415 }
01416 }
01417
01418 return(outputs);
01419 }
01420
01421
01422
01423
01426
01427 Vector<double> UnscalingLayer::calculate_mean_standard_deviation_derivative(const Vector<double>&) const
01428 {
01429 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
01430
01431 Vector<double> unscaled_derivative(unscaling_neurons_number, 0.0);
01432
01433 for(unsigned int i = 0; i < unscaling_neurons_number; i++)
01434 {
01435 if(standard_deviations[i] < 1e-99)
01436 {
01437 if(display)
01438 {
01439 std::cout << "OpenNN Warning: UnscalingLayer class.\n"
01440 << "Vector<double> calculate_derivative(const Vector<double>&) const.\n"
01441 << "Standard deviation of output variable " << i << " is zero.\n"
01442 << "Those derivatives won't be unscaled.\n";
01443 }
01444
01445 unscaled_derivative[i] = 1.0;
01446 }
01447 else
01448 {
01449 unscaled_derivative[i] = standard_deviations[i];
01450 }
01451 }
01452
01453 return(unscaled_derivative);
01454 }
01455
01456
01457
01458
01461
01462 Vector<double> UnscalingLayer::calculate_mean_standard_deviation_second_derivative(const Vector<double>&) const
01463 {
01464 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
01465
01466 const Vector<double> unscaled_second_derivative(unscaling_neurons_number, 0.0);
01467
01468 return(unscaled_second_derivative);
01469 }
01470
01471
01472
01473
01475
01476 Matrix<double> UnscalingLayer::arrange_Jacobian(const Vector<double>& derivatives) const
01477 {
01478 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
01479
01480 Matrix<double> Jacobian(unscaling_neurons_number, unscaling_neurons_number, 0.0);
01481
01482 Jacobian.set_diagonal(derivatives);
01483
01484 return(Jacobian);
01485 }
01486
01487
01488
01489
01491
01492 Vector< Matrix<double> > UnscalingLayer::arrange_Hessian_form(const Vector<double>& second_derivative) const
01493 {
01494 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
01495
01496 Vector< Matrix<double> > Hessian_form(unscaling_neurons_number);
01497
01498 for(unsigned int i = 0; i < unscaling_neurons_number; i++)
01499 {
01500 Hessian_form[i].set(unscaling_neurons_number, unscaling_neurons_number, 0.0);
01501
01502 Hessian_form[i][i][i] = second_derivative[i];
01503 }
01504 return(Hessian_form);
01505 }
01506
01507
01508
01509
01511
01512 std::string UnscalingLayer::to_string(void) const
01513 {
01514 std::ostringstream buffer;
01515
01516 buffer << "Unscaling layer\n"
01517 << "Minimums: " << minimums << "\n"
01518 << "Maximums: " << maximums << "\n"
01519 << "Means: " << means << "\n"
01520 << "Standard deviations: " << standard_deviations << "\n"
01521 << "Unscaling method: " << write_unscaling_method() << "\n"
01522 << "Display: " << display << "\n";
01523
01524 return(buffer.str());
01525 }
01526
01527
01528
01529
01532
01533 TiXmlElement* UnscalingLayer::to_XML(void) const
01534 {
01535 std::ostringstream buffer;
01536
01537 TiXmlElement* unscaling_layer_element = new TiXmlElement("UnscalingLayer");
01538 unscaling_layer_element->SetAttribute("Version", 4);
01539
01540
01541 {
01542 TiXmlElement* element = new TiXmlElement("Minimums");
01543 unscaling_layer_element->LinkEndChild(element);
01544
01545 TiXmlText* text = new TiXmlText(minimums.to_string().c_str());
01546 element->LinkEndChild(text);
01547 }
01548
01549
01550 {
01551 TiXmlElement* element = new TiXmlElement("Maximums");
01552 unscaling_layer_element->LinkEndChild(element);
01553
01554 TiXmlText* text = new TiXmlText(maximums.to_string().c_str());
01555 element->LinkEndChild(text);
01556 }
01557
01558
01559 {
01560 TiXmlElement* element = new TiXmlElement("Means");
01561 unscaling_layer_element->LinkEndChild(element);
01562
01563 TiXmlText* text = new TiXmlText(means.to_string().c_str());
01564 element->LinkEndChild(text);
01565 }
01566
01567
01568 {
01569 TiXmlElement* element = new TiXmlElement("StandardDeviations");
01570 unscaling_layer_element->LinkEndChild(element);
01571
01572 TiXmlText* text = new TiXmlText(standard_deviations.to_string().c_str());
01573 element->LinkEndChild(text);
01574 }
01575
01576
01577 {
01578 TiXmlElement* element = new TiXmlElement("UnscalingMethod");
01579 unscaling_layer_element->LinkEndChild(element);
01580
01581 TiXmlText* text = new TiXmlText(write_unscaling_method().c_str());
01582 element->LinkEndChild(text);
01583 }
01584
01585
01586 {
01587 TiXmlElement* element = new TiXmlElement("Display");
01588 unscaling_layer_element->LinkEndChild(element);
01589
01590 buffer.str("");
01591 buffer << display;
01592
01593 TiXmlText* text = new TiXmlText(buffer.str().c_str());
01594 element->LinkEndChild(text);
01595 }
01596
01597 return(unscaling_layer_element);
01598 }
01599
01600
01601
01602
01605
01606 void UnscalingLayer::from_XML(TiXmlElement* unscaling_layer_element)
01607 {
01608 if(unscaling_layer_element)
01609 {
01610
01611
01612 {
01613 TiXmlElement* unscaling_method_element = unscaling_layer_element->FirstChildElement("UnscalingMethod");
01614
01615 if(unscaling_method_element)
01616 {
01617 std::string new_method = unscaling_method_element->GetText();
01618
01619 try
01620 {
01621 set_unscaling_method(new_method);
01622 }
01623 catch(std::exception& e)
01624 {
01625 std::cout << e.what() << std::endl;
01626 }
01627 }
01628 }
01629
01630
01631 {
01632 TiXmlElement* minimums_element = unscaling_layer_element->FirstChildElement("Minimums");
01633
01634 if(minimums_element)
01635 {
01636 const char* minimums_text = minimums_element->GetText();
01637
01638 if(minimums_text)
01639 {
01640 Vector<double> new_minimums;
01641 new_minimums.parse(minimums_text);
01642
01643 try
01644 {
01645 set_minimums(new_minimums);
01646 }
01647 catch(std::exception& e)
01648 {
01649 std::cout << e.what() << std::endl;
01650 }
01651 }
01652 }
01653 }
01654
01655
01656 {
01657 TiXmlElement* maximums_element = unscaling_layer_element->FirstChildElement("Maximums");
01658
01659 if(maximums_element)
01660 {
01661 const char* maximums_text = maximums_element->GetText();
01662
01663 if(maximums_text)
01664 {
01665 Vector<double> new_maximums;
01666 new_maximums.parse(maximums_text);
01667
01668 try
01669 {
01670 set_maximums(new_maximums);
01671 }
01672 catch(std::exception& e)
01673 {
01674 std::cout << e.what() << std::endl;
01675 }
01676 }
01677 }
01678 }
01679
01680
01681 {
01682 TiXmlElement* means_element = unscaling_layer_element->FirstChildElement("Means");
01683
01684 if(means_element)
01685 {
01686 const char* means_text = means_element->GetText();
01687
01688 if(means_text)
01689 {
01690 Vector<double> new_means;
01691 new_means.parse(means_text);
01692
01693 try
01694 {
01695 set_means(new_means);
01696 }
01697 catch(std::exception& e)
01698 {
01699 std::cout << e.what() << std::endl;
01700 }
01701 }
01702 }
01703 }
01704
01705
01706 {
01707 TiXmlElement* standard_deviations_element = unscaling_layer_element->FirstChildElement("StandardDeviations");
01708
01709 if(standard_deviations_element)
01710 {
01711 const char* standard_deviations_text = standard_deviations_element->GetText();
01712
01713 if(standard_deviations_text)
01714 {
01715 Vector<double> new_standard_deviations;
01716 new_standard_deviations.parse(standard_deviations_text);
01717
01718 try
01719 {
01720 set_standard_deviations(new_standard_deviations);
01721 }
01722 catch(std::exception& e)
01723 {
01724 std::cout << e.what() << std::endl;
01725 }
01726 }
01727 }
01728 }
01729
01730
01731 {
01732 TiXmlElement* display_element = unscaling_layer_element->FirstChildElement("Display");
01733
01734 if(display_element)
01735 {
01736 std::string new_display_string = display_element->GetText();
01737
01738 try
01739 {
01740 set_display(new_display_string != "0");
01741 }
01742 catch(std::exception& e)
01743 {
01744 std::cout << e.what() << std::endl;
01745 }
01746 }
01747 }
01748 }
01749 }
01750
01751
01752
01753
01757
01758 std::string UnscalingLayer::write_minimum_maximum_expression(const Vector<std::string>& inputs_name, const Vector<std::string>& outputs_name) const
01759 {
01760 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
01761
01762 std::ostringstream buffer;
01763
01764 for(unsigned int i = 0; i < unscaling_neurons_number; i++)
01765 {
01766 buffer << outputs_name[i] << "=0.5*(" << inputs_name[i] << "+1.0)*(" << maximums[i] << "-" << minimums[i] << ")+" << minimums[i] << ";\n";
01767 }
01768
01769 return(buffer.str());
01770 }
01771
01772
01773
01774
01778
01779 std::string UnscalingLayer::write_mean_stadard_deviation_expression(const Vector<std::string>& inputs_name, const Vector<std::string>& outputs_name) const
01780 {
01781 const unsigned int unscaling_neurons_number = count_unscaling_neurons_number();
01782
01783 std::ostringstream buffer;
01784
01785 for(unsigned int i = 0; i < unscaling_neurons_number; i++)
01786 {
01787 buffer << outputs_name[i] << "=" << means[i] << "+" << standard_deviations[i] << "*" << inputs_name[i] << ";\n";
01788 }
01789
01790 return(buffer.str());
01791 }
01792
01793
01794
01795
01799
01800 std::string UnscalingLayer::write_expression(const Vector<std::string>& inputs_name, const Vector<std::string>& outputs_name) const
01801 {
01802 switch(unscaling_method)
01803 {
01804 case MinimumMaximum:
01805 {
01806 return(write_minimum_maximum_expression(inputs_name, outputs_name));
01807 }
01808 break;
01809
01810 case MeanStandardDeviation:
01811 {
01812 return(write_mean_stadard_deviation_expression(inputs_name, outputs_name));
01813 }
01814 break;
01815
01816 default:
01817 {
01818 std::ostringstream buffer;
01819
01820 buffer << "OpenNN Exception: UnscalingLayer class.\n"
01821 << "std::string write_expression(const Vector<std::string>&, const Vector<std::string>&) const method.\n"
01822 << "Unknown unscaling method.\n";
01823
01824 throw std::logic_error(buffer.str());
01825 }
01826 break;
01827 }
01828
01829
01830 }
01831
01832 }
01833
01834
01835
01836
01837
01838
01839
01840
01841
01842
01843
01844
01845
01846
01847
01848
01849
01850