MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
GradPIDTests.cpp
Go to the documentation of this file.
1 
25 #include <gtest/gtest.h>
26 #include <cmath>
27 
29 
30 // Redefine word "public" so every class field/method will be accessible for tests.
31 #define private public
32 #include <optimization/GradPID.hpp>
33 
34 
36 // GradPID
38 
39 
44 TEST_F(Sphere1DLandscape, GradPID_Convergence) {
45  // Optimization function - 1d GradPID (with default values).
46  mic::neural_nets::optimization::GradPID<double> opt(x->rows(), x->cols());
47 
48  // Simulate a simple gradient descent.
49  size_t iteration = 0;
50  double value = fun.calculateValue(x);
51  double abs_diff = 1.0;
52  while (abs_diff > eps) {
53  // Calculate gradient.
54  mic::types::MatrixPtr<double> dx = fun.calculateGradient(x);
55  opt.update(x, dx, 0.1);
56 
57  // Check whether value of the function is finite.
58  value = fun.calculateValue(x);
59  ASSERT_EQ(true, std::isfinite(value)) << " at iteration i=" << iteration;
60 
61  // Calculate diff - std:abs!!
62  abs_diff = std::abs(value - fun.minValue());
63  iteration++;
64  }//: while
65  ASSERT_GE(eps, std::abs(fun.calculateValue(x) - fun.minValue()));
66  std::cout << " -> Converged after " << iteration << " iterations\n";
67 }
68 
69 
70 
75 TEST_F(Sphere20DLandscape, GradPID_Convergence) {
76  for (size_t i=0; i<20; i++)
77  (*x)[i] = i;
78  // Optimization function - 20d GradPID (with default values).
79  mic::neural_nets::optimization::GradPID<double> opt(x->rows(), x->cols());
80 
81  // Simulate a simple gradient descent.
82  size_t iteration = 0;
83  double abs_diff = 1.0;
84  while (abs_diff > eps) {
85  mic::types::MatrixPtr<double> dx = fun.calculateGradient(x);
86  opt.update(x, dx, 0.1);
87 
88  // Check whether value of the function is finite.
89  double value = fun.calculateValue(x);
90  ASSERT_EQ(true, std::isfinite(value)) << " at iteration i=" << iteration;
91 
92  // Calculate diff - std:abs!!
93  abs_diff = std::abs(value - fun.minValue());
94  iteration++;
95 /* if (iteration >135)
96  exit(1);*/
97  }//: while
98  ASSERT_GE(eps, std::abs(fun.calculateValue(x) - fun.minValue()));
99  std::cout << " -> Converged after " << iteration << " iterations\n";
100 }
101 
102 
107 TEST_F(Beale2DLandscape, GradPID_Convergence) {
108  // Optimization function - 2d GradPID.
109  mic::neural_nets::optimization::GradPID<double> opt(x->rows(), x->cols());
110 
111  // Simulate a simple gradient descent.
112  size_t iteration = 0;
113  double abs_diff = 1.0;
114  while (abs_diff > eps) {
115  mic::types::MatrixPtr<double> dx = fun.calculateGradient(x);
116  opt.update(x, dx, 0.01);
117 
118  // Check whether value of the function is finite.
119  double value = fun.calculateValue(x);
120  ASSERT_EQ(true, std::isfinite(value)) << " at iteration i=" << iteration;
121 
122  // Calculate diff - std:abs!!
123  abs_diff = std::abs(value - fun.minValue());
124  iteration++;
125  }//: while
126  ASSERT_GE(eps, std::abs(fun.calculateValue(x) - fun.minValue()));
127  std::cout << " -> Converged after " << iteration << " iterations\n";
128 }
129 
130 
135 TEST_F(Rosenbrock2DLandscape, GradPID_Convergence) {
136  // Optimization function - 2d GradPID.
137  double ni = 0.0001;
138  mic::neural_nets::optimization::GradPID<double> opt(x->rows(), x->cols());
139 
140  // Simulate a simple gradient descent.
141  size_t iteration = 0;
142  double abs_diff = 1.0;
143  while (abs_diff > eps) {
144  mic::types::MatrixPtr<double> dx = fun.calculateGradient(x);
145  opt.update(x, dx, ni);
146 
147  // Check whether value of the function is finite.
148  double value = fun.calculateValue(x);
149  ASSERT_EQ(true, std::isfinite(value)) << " at iteration i=" << iteration << " for ni =" << ni;
150 
151  // Calculate diff - std:abs!!
152  abs_diff = std::abs(value - fun.minValue());
153  iteration++;
154  }//: while
155  ASSERT_GE(eps, std::abs(fun.calculateValue(x) - fun.minValue()));
156  std::cout << " -> Converged after " << iteration << " iterations\n";
157 }
158 
159 
Test fixture - artificial landscape - sphere function 20D (square function).
TEST_F(Sphere1DLandscape, GradPID_Convergence)
Test fixture - artificial landscape - Rosenbrock function 2D.
Test fixture - artificial landscape - sphere function 1D (square function).
GradPID - adaptive gradient descent with proportional, integral and derivative coefficients.
Definition: GradPID.hpp:40
Test fixture - artificial landscape - Beale's function 2D.
virtual void update(mic::types::MatrixPtr< eT > p_, mic::types::MatrixPtr< eT > dp_, eT learning_rate_, eT decay_=0.0)