MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
MomentumTests.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
33 
34 
35 
36 
38 // Momentum
40 
41 
46 TEST_F(Sphere1DLandscape, Momentum_Convergence) {
47  // Optimization function - 1d momentum with learning rate = 0.1 (and default 0.9 momentum).
48  mic::neural_nets::optimization::Momentum<double> opt(x->rows(), x->cols());
49 
50  // Simulate a simple gradient descent.
51  size_t iteration = 0;
52  double abs_diff = 1.0;
53  while (abs_diff > eps) {
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  double 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 
74 TEST_F(Sphere20DLandscape, Momentum_Convergence) {
75  // Optimization function - 20d momentum with learning rate = 0.1 (and default 0.9 momentum).
76  mic::neural_nets::optimization::Momentum<double> opt(x->rows(), x->cols());
77 
78  // Simulate a simple gradient descent.
79  size_t iteration = 0;
80  double abs_diff = 1.0;
81  while (abs_diff > eps) {
82  mic::types::MatrixPtr<double> dx = fun.calculateGradient(x);
83  opt.update(x, dx, 0.01);
84 
85  // Check whether value of the function is finite.
86  double value = fun.calculateValue(x);
87  ASSERT_EQ(true, std::isfinite(value)) << " at iteration i=" << iteration;
88 
89  // Calculate diff - std:abs!!
90  abs_diff = std::abs(value - fun.minValue());
91  iteration++;
92  }//: while
93  ASSERT_GE(eps, std::abs(fun.calculateValue(x) - fun.minValue()));
94  std::cout << " -> Converged after " << iteration << " iterations\n";
95 }
96 
97 
102 TEST_F(Beale2DLandscape, Momentum_Convergence) {
103  // Optimization function - 2d momentum with learning rate = 0.1 (and default 0.9 momentum).
104  mic::neural_nets::optimization::Momentum<double> opt(x->rows(), x->cols());
105 
106  // Simulate a simple gradient descent.
107  size_t iteration = 0;
108  double abs_diff = 1.0;
109  while (abs_diff > eps) {
110  mic::types::MatrixPtr<double> dx = fun.calculateGradient(x);
111  opt.update(x, dx, 0.01);
112 
113  // Check whether value of the function is finite.
114  double value = fun.calculateValue(x);
115  ASSERT_EQ(true, std::isfinite(value)) << " at iteration i=" << iteration;
116 
117  // Calculate diff - std:abs!!
118  abs_diff = std::abs(value - fun.minValue());
119  iteration++;
120  }//: while
121  ASSERT_GE(eps, std::abs(fun.calculateValue(x) - fun.minValue()));
122  std::cout << " -> Converged after " << iteration << " iterations\n";
123 }
124 
125 
130 TEST_F(Rosenbrock2DLandscape, Momentum_Convergence) {
131  // Optimization function - 2d momentum with learning rate = 0.00001 (and default 0.9 momentum).
132  mic::neural_nets::optimization::Momentum<double> opt(x->rows(), x->cols());
133 
134  // Simulate a simple gradient descent.
135  size_t iteration = 0;
136  double abs_diff = 1.0;
137  while (abs_diff > eps) {
138  mic::types::MatrixPtr<double> dx = fun.calculateGradient(x);
139  opt.update(x, dx, 0.00001);
140 
141  // Check whether value of the function is finite.
142  double value = fun.calculateValue(x);
143  ASSERT_EQ(true, std::isfinite(value)) << " at iteration i=" << iteration;
144 
145  // Calculate diff - std:abs!!
146  abs_diff = std::abs(value - fun.minValue());
147  iteration++;
148  }//: while
149  ASSERT_GE(eps, std::abs(fun.calculateValue(x) - fun.minValue()));
150  std::cout << " -> Converged after " << iteration << " iterations\n";
151 }
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).
Update in the direction of gradient descent - with momentum.
Definition: Momentum.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)
TEST_F(Sphere1DLandscape, Momentum_Convergence)