MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
LossTests.cpp
Go to the documentation of this file.
1 
25 #include "LossTests.hpp"
26 #include "LossTypes.hpp"
27 
28 
32 TEST_F(Vectors4x1Float, SquaredErrorLoss) {
33  // Loss function.
35 
36  ASSERT_EQ(loss.calculateLoss(target_y, predicted_y), (float)2.0);
37 }
38 
42 TEST_F(Vectors4x1Float, SquaredErrorMSELoss) {
43  // Loss function.
45 
46  ASSERT_EQ(loss.calculateMeanLoss(target_y, predicted_y), (float)2.0);
47 }
48 
52 TEST_F(Vectors4x1Float, SquaredErrorGradient) {
53  // Loss function.
55  mic::types::MatrixPtr<float> dy = loss.calculateGradient(target_y, predicted_y);
56 
57  for (size_t i=0; i<(size_t)dy->size(); i++){
58  ASSERT_EQ((*dy)[i], -1.0) << "Gradient error at position i=" << i << " (*dy)[i] is " << (*dy)[i] << " whereas -1.0 is expected";
59  }
60 }
61 
65 TEST_F(Vectors4x1Float2, SquaredErrorLoss) {
66  // Loss function.
68  float eps = 1e-5;
69 
70  float l1 = loss.calculateLoss(target_y, predicted_y1);
71  EXPECT_LE(fabs(l1-0.0225), eps);
72 
73  float l2 = loss.calculateLoss(target_y, predicted_y2);
74  EXPECT_LE(fabs(l2-0.0225), eps);
75 }
76 
80 TEST_F(Vectors4x1Float2, SquaredErrorMSELoss) {
81  // Loss function.
83  float eps = 1e-5;
84 
85  float l1 = loss.calculateMeanLoss(target_y, predicted_y1);
86  EXPECT_LE(fabs(l1-0.0225), eps);
87 
88  float l2 = loss.calculateMeanLoss(target_y, predicted_y2);
89  EXPECT_LE(fabs(l2-0.0225), eps);
90 }
91 
95 TEST_F(Vectors4x1Float2, SquaredErrorGradient) {
96  // Loss function.
98  float eps = 1e-5;
99 
100  mic::types::MatrixPtr<float> dy1 = loss.calculateGradient(target_y, predicted_y1);
101  EXPECT_LE(fabs((*dy1)[0] + 0.15), eps) << "Gradient error at position i=0";
102  EXPECT_LE(fabs((*dy1)[1] - 0.15), eps) << "Gradient error at position i=1";
103  EXPECT_LE(fabs((*dy1)[2] - 0.0), eps) << "Gradient error at position i=2";
104  EXPECT_LE(fabs((*dy1)[3] - 0.0), eps) << "Gradient error at position i=3";
105 
106  mic::types::MatrixPtr<float> dy2 = loss.calculateGradient(target_y, predicted_y2);
107  EXPECT_LE(fabs((*dy2)[0] - 0.0), eps) << "Gradient error at position i=0";
108  EXPECT_LE(fabs((*dy2)[1] - 0.0), eps) << "Gradient error at position i=1";
109  EXPECT_LE(fabs((*dy2)[2] + 0.15), eps) << "Gradient error at position i=2";
110  EXPECT_LE(fabs((*dy2)[3] - 0.15), eps) << "Gradient error at position i=3";
111 }
112 
113 
114 
118 TEST_F(Vectors4x1Float2, CrossEntropyLoss) {
119  // Loss function.
121  float eps = 1e-5;
122 
123  float l1 = loss.calculateLoss(target_y, predicted_y1);
124  EXPECT_LE(fabs(l1 - 2.0), eps);
125 
126  float l2 = loss.calculateLoss(target_y, predicted_y2);
127  EXPECT_LE(fabs(l2 - 2.02193), eps);
128 }
129 
133 TEST_F(Vectors4x1Float2, CrossEntropyACELoss) {
134  // Loss function.
136  float eps = 1e-5;
137 
138  float l1 = loss.calculateMeanLoss(target_y, predicted_y1);
139  EXPECT_LE(fabs(l1 - 2.0), eps);
140 
141  float l2 = loss.calculateMeanLoss(target_y, predicted_y2);
142  EXPECT_LE(fabs(l2 - 2.02193), eps);
143 }
144 
145 
149 TEST_F(Vectors4x1Float2, CrossEntropyGradient) {
150  // Loss function.
152  double eps = 1e-5;
153 
154  mic::types::MatrixPtr<float> dy1 = loss.calculateGradient(target_y, predicted_y1);
155  EXPECT_LE(fabs((*dy1)[0] + 0.15), eps) << "Gradient error at position i=0";
156  EXPECT_LE(fabs((*dy1)[1] - 0.15), eps) << "Gradient error at position i=1";
157  EXPECT_LE(fabs((*dy1)[2] + 0.0), eps) << "Gradient error at position i=2";
158  EXPECT_LE(fabs((*dy1)[3] + 0.0), eps) << "Gradient error at position i=3";
159 
160  mic::types::MatrixPtr<float> dy2 = loss.calculateGradient(target_y, predicted_y2);
161  EXPECT_LE(fabs((*dy2)[0] + 0.0), eps) << "Gradient error at position i=0";
162  EXPECT_LE(fabs((*dy2)[1] + 0.0), eps) << "Gradient error at position i=1";
163  EXPECT_LE(fabs((*dy2)[2] + 0.15), eps) << "Gradient error at position i=2";
164  EXPECT_LE(fabs((*dy2)[3] - 0.15), eps) << "Gradient error at position i=3";
165 }
166 
167 
171 TEST_F(Vectors3x2Float, SquaredErrorLoss) {
172  // Loss function.
174  float eps = 1e-5;
175 
176  float l1 = loss.calculateLoss(target_y, predicted_y);
177  EXPECT_LE(fabs(l1 - 0.145), eps);
178 }
179 
183 TEST_F(Vectors3x2Float, SquaredErrorACELoss) {
184  // Loss function.
186  float eps = 1e-5;
187 
188  float l1 = loss.calculateMeanLoss(target_y, predicted_y);
189  EXPECT_LE(fabs(l1 - 0.0725), eps);
190 }
191 
192 
196 TEST_F(Vectors3x2Float, SquaredErrorGradient) {
197  // Loss function.
199  double eps = 1e-5;
200 
201  mic::types::MatrixPtr<float> dy = loss.calculateGradient(target_y, predicted_y);
202 
203  EXPECT_LE(fabs((*dy)(0,0) - 0.1), eps) << "Gradient error at position (0,0)";
204  EXPECT_LE(fabs((*dy)(0,1) + 0.0), eps) << "Gradient error at position (0,1)";
205  EXPECT_LE(fabs((*dy)(1,0) - 0.1), eps) << "Gradient error at position (1,0)";
206  EXPECT_LE(fabs((*dy)(1,1) + 0.1), eps) << "Gradient error at position (1,1)";
207  EXPECT_LE(fabs((*dy)(2,0) + 0.1), eps) << "Gradient error at position (2,0)";
208  EXPECT_LE(fabs((*dy)(2,1) - 0.5), eps) << "Gradient error at position (2,1)";
209 }
210 
211 
212 
216 TEST_F(Vectors3x2Float, CrossEntropyLoss) {
217  // Loss function.
219  float eps = 1e-5;
220 
221  float l1 = loss.calculateLoss(target_y, predicted_y);
222  EXPECT_LE(fabs(l1 - 2.42782), eps);
223 
224 }
225 
229 TEST_F(Vectors3x2Float, CrossEntropyACELoss) {
230  // Loss function.
232  float eps = 1e-5;
233 
234  float l1 = loss.calculateMeanLoss(target_y, predicted_y);
235  EXPECT_LE(fabs(l1 - 1.21391), eps);
236 }
237 
241 TEST_F(Vectors3x2Float, CrossEntropyGradient) {
242  // Loss function.
244  double eps = 1e-5;
245 
246  mic::types::MatrixPtr<float> dy = loss.calculateGradient(target_y, predicted_y);
247 
248  EXPECT_LE(fabs((*dy)(0,0) - 0.1), eps) << "Gradient error at position (0,0)";
249  EXPECT_LE(fabs((*dy)(0,1) + 0.0), eps) << "Gradient error at position (0,1)";
250  EXPECT_LE(fabs((*dy)(1,0) - 0.1), eps) << "Gradient error at position (1,0)";
251  EXPECT_LE(fabs((*dy)(1,1) + 0.1), eps) << "Gradient error at position (1,1)";
252  EXPECT_LE(fabs((*dy)(2,0) + 0.1), eps) << "Gradient error at position (2,0)";
253  EXPECT_LE(fabs((*dy)(2,1) - 0.5), eps) << "Gradient error at position (2,1)";
254 }
255 
256 
257 
258 int main(int argc, char **argv) {
259  testing::InitGoogleTest(&argc, argv);
260  return RUN_ALL_TESTS();
261 }
Class representing a cross-entropy loss function (classification).
Test Fixture - two predictions of size 4x1, floats.
Definition: LossTests.hpp:67
mic::types::MatrixPtr< dtype > calculateGradient(mic::types::MatrixPtr< dtype > target_y_, mic::types::MatrixPtr< dtype > predicted_y_)
Function calculating gradient - for squared difference (regression).
Test Fixture - two vectors of size 4x1, floats.
Definition: LossTests.hpp:40
mic::types::MatrixPtr< dtype > calculateGradient(mic::types::MatrixPtr< dtype > target_y_, mic::types::MatrixPtr< dtype > predicted_y_)
Gradient calculation for cross-entropy.
dtype calculateLoss(mic::types::MatrixPtr< dtype > target_y_, mic::types::MatrixPtr< dtype > predicted_y_)
Calculates cross entropy(using log) and returns cross-entropy error (CE).
int main(int argc, char **argv)
Definition: LossTests.cpp:258
dtype calculateLoss(mic::types::MatrixPtr< dtype > target_y_, mic::types::MatrixPtr< dtype > predicted_y_)
Function calculates squared difference loss (regression) and returns squared error (SE)...
TEST_F(Vectors4x1Float, SquaredErrorLoss)
Definition: LossTests.cpp:32
Test Fixture - two vectors of size 3x2, floats.
Definition: LossTests.hpp:99
virtual dtype calculateMeanLoss(mic::types::MatrixPtr< dtype > target_y_, mic::types::MatrixPtr< dtype > predicted_y_)
Calculates mean loss (i.e. divides the loss by the size of batch) - ACE for cross-entropy or MSE for ...
Definition: Loss.hpp:51