MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
AdamIDTests.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/AdamID.hpp>
33 
34 
35 
40 TEST_F(Sphere1DLandscape, AdamID_Convergence) {
41  // Optimization function - 1d momentum with learning rate = 0.1 (and default 0.9 momentum).
42  mic::neural_nets::optimization::AdamID<double> opt(x->rows(), x->cols());
43 
44  // Simulate a simple gradient descent.
45  size_t iteration = 0;
46  double abs_diff = 1.0;
47  while (abs_diff > eps) {
48  mic::types::MatrixPtr<double> dx = fun.calculateGradient(x);
49  opt.update(x, dx, 0.001);
50 
51  // errorCheck whether value of the function is finite.
52  double value = fun.calculateValue(x);
53  ASSERT_EQ(true, std::isfinite(value)) << " at iteration i=" << iteration;
54 
55  // Calculate diff - std:abs!!
56  abs_diff = std::abs(value - fun.minValue());
57  iteration++;
58  }//: while
59  ASSERT_GE(eps, std::abs(fun.calculateValue(x) - fun.minValue()));
60  std::cout << " -> Converged after " << iteration << " iterations\n";
61 }
62 
63 
68 TEST_F(Sphere20DLandscape, AdamID_Convergence) {
69  // Optimization function - 20d momentum with learning rate = 0.1 (and default 0.9 momentum).
70  mic::neural_nets::optimization::AdamID<double> opt(x->rows(), x->cols());
71 
72  // Simulate a simple gradient descent.
73  size_t iteration = 0;
74  double abs_diff = 1.0;
75  while (abs_diff > eps) {
76  mic::types::MatrixPtr<double> dx = fun.calculateGradient(x);
77  opt.update(x, dx, 0.001);
78 
79  // Check whether value of the function is finite.
80  double value = fun.calculateValue(x);
81  ASSERT_EQ(true, std::isfinite(value)) << " at iteration i=" << iteration;
82 
83  // Calculate diff - std:abs!!
84  abs_diff = std::abs(value - fun.minValue());
85  iteration++;
86  }//: while
87  ASSERT_GE(eps, std::abs(fun.calculateValue(x) - fun.minValue()));
88  std::cout << " -> Converged after " << iteration << " iterations\n";
89 }
90 
95 TEST_F(Beale2DLandscape, AdamID_Convergence) {
96  // Optimization function.
97  mic::neural_nets::optimization::AdamID<double> opt(x->rows(), x->cols());
98 
99  // Simulate a simple gradient descent.
100  size_t iteration = 0;
101  double abs_diff = 1.0;
102  while (abs_diff > eps) {
103  mic::types::MatrixPtr<double> dx = fun.calculateGradient(x);
104  opt.update(x, dx, 0.001);
105 
106  // Check whether value of the function is finite.
107  double value = fun.calculateValue(x);
108  ASSERT_EQ(true, std::isfinite(value)) << " at iteration i=" << iteration;
109 
110  // Calculate diff - std:abs!!
111  abs_diff = std::abs(value - fun.minValue());
112  iteration++;
113  }//: while
114  ASSERT_GE(eps, std::abs(fun.calculateValue(x) - fun.minValue()));
115  std::cout << " -> Converged after " << iteration << " iterations\n";
116 }
117 
118 
123 TEST_F(Rosenbrock2DLandscape, AdamID_Convergence) {
124  // Optimization function.
125  mic::neural_nets::optimization::AdamID<double> opt(x->rows(), x->cols());
126 
127  // Simulate a simple gradient descent.
128  size_t iteration = 0;
129  double abs_diff = 1.0;
130  while (abs_diff > eps) {
131  mic::types::MatrixPtr<double> dx = fun.calculateGradient(x);
132  opt.update(x, dx, 0.001);
133 
134  // Check whether value of the function is finite.
135  double value = fun.calculateValue(x);
136  ASSERT_EQ(true, std::isfinite(value)) << " at iteration i=" << iteration;
137 
138  // Calculate diff - std:abs!!
139  abs_diff = std::abs(value - fun.minValue());
140  iteration++;
141  }//: while
142  ASSERT_GE(eps, std::abs(fun.calculateValue(x) - fun.minValue()));
143  std::cout << " -> Converged after " << iteration << " iterations\n";
144 }
Test fixture - artificial landscape - sphere function 20D (square function).
Test fixture - artificial landscape - Rosenbrock function 2D.
Test fixture - artificial landscape - sphere function 1D (square function).
TEST_F(Sphere1DLandscape, AdamID_Convergence)
Definition: AdamIDTests.cpp:40
AdamID - ADAM with integral and derivative coefficients.
Definition: AdamID.hpp:39
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)