1 // OpenNN: Open Neural Networks Library
2 // www.opennn.net
3 //
4 // U N S C A L I N G L A Y E R T E S T C L A S S
5 //
6 // Artificial Intelligence Techniques SL
7 // artelnics@artelnics.com
8
9 #include "unscaling_layer_test.h"
10
11
UnscalingLayerTest()12 UnscalingLayerTest::UnscalingLayerTest() : UnitTesting()
13 {
14 }
15
16
~UnscalingLayerTest()17 UnscalingLayerTest::~UnscalingLayerTest()
18 {
19 }
20
21
test_constructor()22 void UnscalingLayerTest::test_constructor()
23 {
24 cout << "test_constructor\n";
25
26 UnscalingLayer ul2(3);
27 UnscalingLayer ul1;
28
29 assert_true(ul1.get_type() == Layer::Unscaling, LOG);
30 assert_true(ul1.get_descriptives().size() == 0, LOG);
31
32 assert_true(ul2.get_descriptives().size() == 3, LOG);
33
34 Tensor<Descriptives, 1> descriptives;
35
36 descriptives.resize(2);
37
38 UnscalingLayer ul3(descriptives);
39
40 assert_true(ul3.get_descriptives().size() == 2, LOG);
41
42 }
43
test_destructor()44 void UnscalingLayerTest::test_destructor()
45 {
46 cout << "test_destructor\n";
47 }
48
test_assignment_operator()49 void UnscalingLayerTest::test_assignment_operator()
50 {
51 cout << "test_assignment_operator\n";
52
53 UnscalingLayer ul_1;
54 UnscalingLayer ul_2 = ul_1;
55
56 assert_true(ul_2.get_inputs_number() == 0, LOG);
57 assert_true(ul_2.get_neurons_number() == 0, LOG);
58 }
59
test_get_dimensions()60 void UnscalingLayerTest::test_get_dimensions()
61 {
62 cout << "test_get_dimensions\n";
63
64 UnscalingLayer ul1;
65
66 ul1.set(1);
67
68 // Test 0
69
70 assert_true(ul1.get_neurons_number() == 1, LOG);
71
72 // // Test 1
73
74 ul1.set(3);
75
76 assert_true(ul1.get_neurons_number() == 3, LOG);
77
78 }
79
test_get_neurons_number()80 void UnscalingLayerTest::test_get_neurons_number()
81 {
82 cout << "test_get_neurons_number\n";
83
84 UnscalingLayer ul1;
85
86 // Test 0
87
88 assert_true(ul1.get_neurons_number() == 0, LOG);
89 assert_true(ul1.get_neurons_number() == ul1.get_inputs_number(), LOG);
90
91 // Test 1
92
93 ul1.set(3);
94
95 Tensor<Descriptives, 1> descriptives(3);
96 ul1.set_descriptives(descriptives);
97
98 assert_true(ul1.get_neurons_number() == 3, LOG);
99 assert_true(ul1.get_neurons_number() == ul1.get_inputs_number(), LOG);
100 }
101
test_get_inputs_number()102 void UnscalingLayerTest::test_get_inputs_number()
103 {
104 cout << "test_get_inputs_number\n";
105
106 UnscalingLayer ul1;
107
108 // Test 0
109
110 assert_true(ul1.get_inputs_number() == 0, LOG);
111 assert_true(ul1.get_inputs_number() == ul1.get_neurons_number(), LOG);
112
113 // Test 1
114
115 ul1.set(3);
116
117 Tensor<Descriptives, 1> descriptives(3);
118 ul1.set_descriptives(descriptives);
119
120 assert_true(ul1.get_inputs_number() == 3, LOG);
121 assert_true(ul1.get_inputs_number() == ul1.get_neurons_number(), LOG);
122 }
123
test_get_descriptives()124 void UnscalingLayerTest::test_get_descriptives()
125 {
126 cout << "test_get_descriptives\n";
127
128 // Test 0
129
130 Tensor<Descriptives, 1> descriptives(1);
131
132 UnscalingLayer ul(descriptives);
133
134 Tensor<Descriptives, 1> get_des = ul.get_descriptives();
135
136 assert_true(get_des.dimension(0) == 1, LOG);
137 assert_true(abs(get_des(0).minimum + 1) < static_cast<type>(1e-3), LOG);
138 assert_true(abs(get_des(0).maximum - 1) < static_cast<type>(1e-3), LOG);
139 assert_true(abs(get_des(0).mean - 0) < static_cast<type>(1e-3), LOG);
140 assert_true(abs(get_des(0).standard_deviation - 1) < static_cast<type>(1e-3), LOG);
141
142 // Test 1
143
144 Descriptives des_0(1,1,1,0);
145 Descriptives des_1(2,2,2,0);
146
147 descriptives.resize(2);
148 descriptives.setValues({des_0,des_1});
149
150 ul.set_descriptives(descriptives);
151
152 Tensor<Descriptives, 1> get_des_1 = ul.get_descriptives();
153
154 assert_true(get_des_1.dimension(0) == 2, LOG);
155 assert_true(abs(get_des_1(1).minimum - 2) < static_cast<type>(1e-3), LOG);
156 assert_true(abs(get_des_1(1).maximum - 2) < static_cast<type>(1e-3), LOG);
157 assert_true(abs(get_des_1(1).mean - 2) < static_cast<type>(1e-3), LOG);
158 assert_true(abs(get_des_1(1).standard_deviation - 0) < static_cast<type>(1e-3), LOG);
159 }
160
test_get_descriptives_matrix()161 void UnscalingLayerTest::test_get_descriptives_matrix()
162 {
163 cout << "test_get_descriptives_matrix\n";
164
165 // Test 0
166
167 Tensor<Descriptives, 1> descriptives(1);
168
169 UnscalingLayer ul(descriptives);
170
171 assert_true(ul.get_descriptives_matrix().size() == 4, LOG);
172 assert_true(abs(ul.get_descriptives_matrix()(0,0) + 1) < static_cast<type>(1e-3), LOG);
173 assert_true(abs(ul.get_descriptives_matrix()(0,1) - 1) < static_cast<type>(1e-3), LOG);
174 assert_true(abs(ul.get_descriptives_matrix()(0,2) - 0) < static_cast<type>(1e-3), LOG);
175 assert_true(abs(ul.get_descriptives_matrix()(0,3) - 1) < static_cast<type>(1e-3), LOG);
176
177 // Test 1
178
179 descriptives.resize(2);
180
181 descriptives(0).minimum = 1;
182 descriptives(0).maximum = 1;
183 descriptives(0).mean = 1;
184 descriptives(0).standard_deviation = 0;
185
186 descriptives(1).minimum = 2;
187 descriptives(1).maximum = 2;
188 descriptives(1).mean = 2;
189 descriptives(1).standard_deviation = 0;
190
191 ul.set_descriptives(descriptives);
192
193 assert_true(ul.get_descriptives_matrix().size() == 8, LOG);
194 assert_true(abs(ul.get_descriptives_matrix()(0,0) - 1) < static_cast<type>(1e-3), LOG);
195 assert_true(abs(ul.get_descriptives_matrix()(0,2) - 1) < static_cast<type>(1e-3), LOG);
196 assert_true(abs(ul.get_descriptives_matrix()(1,1) - 2) < static_cast<type>(1e-3), LOG);
197 assert_true(abs(ul.get_descriptives_matrix()(1,3) - 0) < static_cast<type>(1e-3), LOG);
198 }
199
test_get_minimums()200 void UnscalingLayerTest::test_get_minimums()
201 {
202 cout << "test_get_minimums\n";
203
204 UnscalingLayer ul(2);
205
206 // Test 0
207
208 Tensor<Descriptives, 1> descriptives(2);
209
210 ul.set_descriptives(descriptives);
211
212 assert_true(abs(ul.get_minimums()(0) + 1) < static_cast<type>(1e-3), LOG);
213 assert_true(abs(ul.get_minimums()(1) + 1) < static_cast<type>(1e-3), LOG);
214
215 // Test 1
216
217 descriptives(0).minimum = 1;
218 descriptives(1).minimum = -1;
219
220 ul.set_descriptives(descriptives);
221
222 assert_true(abs(ul.get_minimums()(0) - 1) < static_cast<type>(1e-3), LOG);
223 assert_true(abs(ul.get_minimums()(1) + 1) < static_cast<type>(1e-3), LOG);
224 }
225
test_get_maximums()226 void UnscalingLayerTest::test_get_maximums()
227 {
228 cout << "test_get_maximums\n";
229
230 UnscalingLayer ul(2);
231
232 // Test 0
233
234 Tensor<Descriptives, 1> descriptives(2);
235
236 ul.set_descriptives(descriptives);
237
238 assert_true(abs(ul.get_maximums()(0) - 1) < static_cast<type>(1e-3), LOG);
239 assert_true(abs(ul.get_maximums()(1) - 1) < static_cast<type>(1e-3), LOG);
240
241 // Test 1
242
243 descriptives(0).maximum = 1;
244 descriptives(1).maximum = -1;
245
246 ul.set_descriptives(descriptives);
247
248 assert_true(abs(ul.get_maximums()(0) - 1) < static_cast<type>(1e-3), LOG);
249 assert_true(abs(ul.get_maximums()(1) + 1) < static_cast<type>(1e-3), LOG);
250 }
251
test_write_scaling_methods()252 void UnscalingLayerTest::test_write_scaling_methods()
253 {
254 cout << "test_get_scaling_method_name\n";
255
256 UnscalingLayer usl(1);
257
258 // Test 1
259
260 UnscalingLayer::UnscalingMethod no_unscaling = UnscalingLayer::UnscalingMethod::NoUnscaling;
261
262 UnscalingLayer::UnscalingMethod minimum_maximum = UnscalingLayer::UnscalingMethod::MinimumMaximum;
263
264 UnscalingLayer::UnscalingMethod mean_standard_deviation = UnscalingLayer::UnscalingMethod::MeanStandardDeviation;
265
266 UnscalingLayer::UnscalingMethod logarithmic = UnscalingLayer::UnscalingMethod::Logarithmic;
267
268 usl.set_unscaling_methods(no_unscaling);
269 assert_true(usl.write_unscaling_methods()(0) == "NoUnscaling", LOG);
270
271 usl.set_unscaling_methods(minimum_maximum);
272 assert_true(usl.write_unscaling_methods()(0) == "MinimumMaximum", LOG);
273
274 usl.set_unscaling_methods(mean_standard_deviation);
275 assert_true(usl.write_unscaling_methods()(0) == "MeanStandardDeviation", LOG);
276
277 usl.set_unscaling_methods(logarithmic);
278 assert_true(usl.write_unscaling_methods()(0) == "Logarithmic", LOG);
279
280 // Test 2
281
282 usl.set_unscaling_methods(no_unscaling);
283 assert_true(usl.write_unscaling_method_text()(0) == "no unscaling", LOG);
284
285 usl.set_unscaling_methods(minimum_maximum);
286 assert_true(usl.write_unscaling_method_text()(0) == "minimum and maximum", LOG);
287
288 usl.set_unscaling_methods(mean_standard_deviation);
289 assert_true(usl.write_unscaling_method_text()(0) == "mean and standard deviation", LOG);
290
291 usl.set_unscaling_methods(logarithmic);
292 assert_true(usl.write_unscaling_method_text()(0) == "logarithmic", LOG);
293
294 }
295
test_get_display()296 void UnscalingLayerTest::test_get_display()
297 {
298 cout << "test_get_display\n";
299
300 UnscalingLayer ul;
301
302 assert_true(ul.get_display(), LOG);
303 }
304
test_set()305 void UnscalingLayerTest::test_set()
306 {
307 cout << "test_set\n";
308
309 UnscalingLayer ul;
310
311 // Test 1
312
313 ul.set();
314
315 assert_true(ul.get_descriptives().size() == 0, LOG);
316
317 Tensor<Descriptives, 1> descriptives(4);
318 ul.set_descriptives(descriptives);
319 ul.set();
320
321 assert_true(ul.get_descriptives().size() == 0, LOG);
322
323 // Test 2
324
325 ul.set();
326
327 Index new_neurons_number(0);
328 ul.set(new_neurons_number);
329
330 assert_true(ul.get_descriptives().size()== 0, LOG);
331
332 Index new_inputs_number_ = 4;
333 ul.set(new_inputs_number_);
334
335 assert_true(ul.get_descriptives().size()== 4, LOG);
336 assert_true(ul.get_inputs_number()== ul.get_descriptives().size(), LOG);
337
338 // Test 3
339
340 ul.set();
341
342 Tensor<Descriptives, 1> descriptives_3;
343 ul.set(descriptives_3);
344
345 assert_true(ul.get_descriptives().size()== 0, LOG);
346
347 Tensor<Descriptives, 1> descriptives_3_(4);
348 ul.set(descriptives_3_);
349
350 assert_true(ul.get_descriptives().size()== 4, LOG);
351
352 // Test 4
353
354 ul.set();
355
356 UnscalingLayer ul4;
357 ul4.set(7);
358
359 ul.set(ul4);
360
361 assert_true(ul.get_descriptives().size() == 7, LOG);
362 }
363
test_set_inputs_number()364 void UnscalingLayerTest::test_set_inputs_number()
365 {
366 cout << "test_set_inputs_number\n";
367
368 // UnscalingLayer ul;
369
370 // Index new_inputs_number;
371 // ul.set_inputs_number(new_inputs_number);
372
373 // assert_true(ul.get_descriptives().size()== 0, LOG);
374
375 // Index new_inputs_number_ = 4;
376 // ul.set_inputs_number(new_inputs_number_);
377
378 // assert_true(ul.get_descriptives().size()== 4, LOG);
379 }
380
test_set_neurons_number()381 void UnscalingLayerTest::test_set_neurons_number()
382 {
383 cout << "test_set_inputs_number\n";
384
385 UnscalingLayer ul;
386
387 Index new_inputs_number(0);
388 ul.set_neurons_number(new_inputs_number);
389
390 assert_true(ul.get_descriptives().size()== 0, LOG);
391
392 Index new_inputs_number_ = 4;
393 ul.set_neurons_number(new_inputs_number_);
394
395 assert_true(ul.get_descriptives().size()== 4, LOG);
396 }
397
test_set_default()398 void UnscalingLayerTest::test_set_default()
399 {
400 cout << "test_set_default\n";
401
402 UnscalingLayer ul(1);
403
404 ul.set_default();
405
406 assert_true(ul.get_type() == Layer::Unscaling, LOG);
407 assert_true(ul.get_type() == 7, LOG);
408
409 assert_true(ul.write_unscaling_method_text()(0) == "minimum and maximum", LOG);
410 }
411
test_set_descriptives()412 void UnscalingLayerTest::test_set_descriptives()
413 {
414 cout << "test_set_descriptives\n";
415
416 UnscalingLayer ul;
417
418
419 // Test 0
420
421 Tensor<Descriptives, 1> descriptives(1);
422
423 ul.set_descriptives(descriptives);
424
425 assert_true(abs(ul.get_descriptives_matrix()(0,0) + 1) < static_cast<type>(1e-3), LOG);
426 assert_true(abs(ul.get_descriptives_matrix()(0,1) - 1) < static_cast<type>(1e-3), LOG);
427 assert_true(abs(ul.get_descriptives_matrix()(0,2) - 0) < static_cast<type>(1e-3), LOG);
428 assert_true(abs(ul.get_descriptives_matrix()(0,3) - 1) < static_cast<type>(1e-3), LOG);
429
430 // Test 1
431
432 Descriptives des_0(1,1,1,0);
433 Descriptives des_1(2,2,2,0);
434
435 descriptives.resize(2);
436 descriptives.setValues({des_0,des_1});
437
438 ul.set_descriptives(descriptives);
439
440 assert_true(abs(ul.get_descriptives_matrix()(0,0) - 1) < static_cast<type>(1e-3), LOG);
441 assert_true(abs(ul.get_descriptives_matrix()(0,2) - 1) < static_cast<type>(1e-3), LOG);
442 assert_true(abs(ul.get_descriptives_matrix()(1,1) - 2) < static_cast<type>(1e-3), LOG);
443 assert_true(abs(ul.get_descriptives_matrix()(1,3) - 0) < static_cast<type>(1e-3), LOG);
444 }
445
test_set_descriptives_eigen()446 void UnscalingLayerTest::test_set_descriptives_eigen()
447 {
448 cout << "test_set_descriptives_eigen\n";
449
450 UnscalingLayer ul(1);
451
452 // Test 0
453
454 Tensor<type, 2> descriptives_eigen(1,4);
455
456 ul.set_descriptives_eigen(descriptives_eigen);
457
458 assert_true(abs(ul.get_descriptives_matrix()(0,0) + 0) < static_cast<type>(1e-3), LOG);
459 assert_true(abs(ul.get_descriptives_matrix()(0,1) + 0) < static_cast<type>(1e-3), LOG);
460 assert_true(abs(ul.get_descriptives_matrix()(0,2) + 0) < static_cast<type>(1e-3), LOG);
461 assert_true(abs(ul.get_descriptives_matrix()(0,3) + 0) < static_cast<type>(1e-3), LOG);
462
463 // Test 1
464
465 ScalingLayer sl_(2);
466
467 Tensor<type, 2> descriptives_eigen_(2,4);
468 descriptives_eigen_.setValues({{1,1,1,0},{2,2,2,0}});
469
470 sl_.set_descriptives_eigen(descriptives_eigen_);
471
472 assert_true(abs(sl_.get_descriptives_matrix()(0,0) - 1) < static_cast<type>(1e-3), LOG);
473 assert_true(abs(sl_.get_descriptives_matrix()(0,2) - 1) < static_cast<type>(1e-3), LOG);
474 assert_true(abs(sl_.get_descriptives_matrix()(1,1) - 2) < static_cast<type>(1e-3), LOG);
475 assert_true(abs(sl_.get_descriptives_matrix()(1,3) - 0) < static_cast<type>(1e-3), LOG);
476 }
477
test_set_item_descriptives()478 void UnscalingLayerTest::test_set_item_descriptives()
479 {
480 cout << "test_set_item_descriptives\n";
481
482 ScalingLayer ul(1);
483
484 // Test 0
485
486 Descriptives des;
487
488 ul.set_item_descriptives(0,des);
489
490 assert_true(abs(ul.get_descriptives_matrix()(0,0) + 1) < static_cast<type>(1e-3), LOG);
491 assert_true(abs(ul.get_descriptives_matrix()(0,1) - 1) < static_cast<type>(1e-3), LOG);
492 assert_true(abs(ul.get_descriptives_matrix()(0,2) - 0) < static_cast<type>(1e-3), LOG);
493 assert_true(abs(ul.get_descriptives_matrix()(0,3) - 1) < static_cast<type>(1e-3), LOG);
494
495 UnscalingLayer ul1(2);
496
497 // Test
498
499 Descriptives des_0(1,1,1,0);
500 Descriptives des_1(2,2,2,0);
501
502 ul1.set_item_descriptives(0,des_0);
503 ul1.set_item_descriptives(1,des_1);
504
505 assert_true(abs(ul1.get_descriptives_matrix()(0,0) - 1) < static_cast<type>(1e-3), LOG);
506 assert_true(abs(ul1.get_descriptives_matrix()(0,2) - 1) < static_cast<type>(1e-3), LOG);
507 assert_true(abs(ul1.get_descriptives_matrix()(1,1) - 2) < static_cast<type>(1e-3), LOG);
508 assert_true(abs(ul1.get_descriptives_matrix()(1,3) - 0) < static_cast<type>(1e-3), LOG);
509 }
510
test_set_minimum()511 void UnscalingLayerTest::test_set_minimum()
512 {
513 cout << "test_set_minimum\n";
514
515 UnscalingLayer ul(2);
516
517 // Test 1
518
519 Tensor<Descriptives, 1> descriptives(2);
520
521 ul.set_descriptives(descriptives);
522
523 ul.set_minimum(0, -5);
524 ul.set_minimum(1, -6);
525
526 assert_true(abs(ul.get_descriptives_matrix()(0,0) + 5) < static_cast<type>(1e-3), LOG);
527 assert_true(abs(ul.get_descriptives_matrix()(1,0) + 6) < static_cast<type>(1e-3), LOG);
528 }
529
test_set_maximum()530 void UnscalingLayerTest::test_set_maximum()
531 {
532 cout << "test_set_maximum\n";
533
534 UnscalingLayer ul(2);
535
536 // Test 1
537
538 Tensor<Descriptives, 1> descriptives(2);
539
540 ul.set_descriptives(descriptives);
541
542 ul.set_maximum(0, 5);
543 ul.set_maximum(1, 6);
544
545 assert_true(abs(ul.get_descriptives_matrix()(0,1) - 5) < static_cast<type>(1e-3), LOG);
546 assert_true(abs(ul.get_descriptives_matrix()(1,1) - 6) < static_cast<type>(1e-3), LOG);
547 }
548
test_set_mean()549 void UnscalingLayerTest::test_set_mean()
550 {
551 cout << "test_set_mean\n";
552
553 UnscalingLayer ul(2);
554
555 // Test 1
556
557 Tensor<Descriptives, 1> descriptives(2);
558
559 ul.set_descriptives(descriptives);
560
561 ul.set_mean(0, 5);
562 ul.set_mean(1, 6);
563
564 assert_true(abs(ul.get_descriptives_matrix()(0,2) - 5) < static_cast<type>(1e-3), LOG);
565 assert_true(abs(ul.get_descriptives_matrix()(1,2) - 6) < static_cast<type>(1e-3), LOG);
566 }
567
test_set_standard_deviation()568 void UnscalingLayerTest::test_set_standard_deviation()
569 {
570 cout << "test_set_standard_deviation\n";
571
572 UnscalingLayer ul(2);
573
574 // Test 1
575
576 Tensor<Descriptives, 1> descriptives(2);
577
578 ul.set_descriptives(descriptives);
579
580 ul.set_standard_deviation(0, 5);
581 ul.set_standard_deviation(1, 6);
582
583 assert_true(abs(ul.get_descriptives_matrix()(0,3) - 5) < static_cast<type>(1e-3), LOG);
584 assert_true(abs(ul.get_descriptives_matrix()(1,3) - 6) < static_cast<type>(1e-3), LOG);
585 }
586
test_set_unscaling_method()587 void UnscalingLayerTest::test_set_unscaling_method()
588 {
589 cout << "test_set_unscaling_method\n";
590
591 UnscalingLayer ul(1);
592
593 // Test 1
594
595 ul.set_unscaling_methods(UnscalingLayer::UnscalingMethod::NoUnscaling);
596 assert_true(ul.write_unscaling_method_text()(0) == "no unscaling", LOG);
597
598 ul.set_unscaling_methods(UnscalingLayer::UnscalingMethod::MinimumMaximum);
599 assert_true(ul.write_unscaling_method_text()(0) == "minimum and maximum", LOG);
600
601
602 ul.set_unscaling_methods(UnscalingLayer::UnscalingMethod::MeanStandardDeviation);
603 assert_true(ul.write_unscaling_method_text()(0) == "mean and standard deviation", LOG);
604
605 ul.set_unscaling_methods(UnscalingLayer::UnscalingMethod::Logarithmic);
606 assert_true(ul.write_unscaling_method_text()(0) == "logarithmic", LOG);
607
608 }
609
test_set_display()610 void UnscalingLayerTest::test_set_display()
611 {
612 cout << "test_set_display\n";
613
614 bool display_true = true;
615 bool display_false = false;
616
617 set_display(display_true);
618 assert_true(get_display() == true, LOG);
619
620 set_display(display_false);
621 assert_true(get_display() == false, LOG);
622 }
623
test_is_empty()624 void UnscalingLayerTest::test_is_empty()
625 {
626 cout << "test_is_empty\n";
627
628 UnscalingLayer ul;
629
630 UnscalingLayer ul1(1);
631
632 assert_true(ul.is_empty() == true, LOG);
633 assert_true(ul1.is_empty() == false, LOG);
634 }
635
test_calculate_outputs()636 void UnscalingLayerTest::test_calculate_outputs()
637 {
638 cout << "test_calculate_outputs\n";
639
640 UnscalingLayer unscaling_layer;
641
642 Tensor<type, 2> inputs;
643
644 unscaling_layer.set_display(false);
645
646 // Test 0_0
647
648 unscaling_layer.set(1);
649 unscaling_layer.set_unscaling_methods(UnscalingLayer::NoUnscaling);
650
651 inputs.resize(1,1);
652 Tensor<type, 2> outputs = unscaling_layer.calculate_outputs(inputs);
653 assert_true(outputs.dimension(0) == 1, LOG);
654 assert_true(outputs.dimension(1) == 1, LOG);
655 assert_true(abs(outputs(0) - inputs(0)) < static_cast<type>(1e-3), LOG);
656
657 // Test 0_1
658
659 unscaling_layer.set(3);
660 unscaling_layer.set_unscaling_methods(UnscalingLayer::NoUnscaling);
661
662 inputs.resize(1,3);
663 inputs.setConstant(0);
664 outputs = unscaling_layer.calculate_outputs(inputs);
665
666 assert_true(outputs.dimension(0) == 1, LOG);
667 assert_true(outputs.dimension(1) == 3, LOG);
668 assert_true(abs(outputs(0) - static_cast<type>(0)) < static_cast<type>(1e-3), LOG);
669 assert_true(abs(outputs(1) - static_cast<type>(0)) < static_cast<type>(1e-3), LOG);
670 assert_true(abs(outputs(2) - static_cast<type>(0)) < static_cast<type>(1e-3), LOG);
671
672 // Test 1_0
673
674 unscaling_layer.set(1);
675 unscaling_layer.set_unscaling_methods(UnscalingLayer::MinimumMaximum);
676
677 inputs.resize(1,1);
678 Tensor<type, 2> outputs_1 = unscaling_layer.calculate_outputs(inputs);
679
680 assert_true(outputs_1.dimension(0) - 1 < static_cast<type>(1e-3), LOG);
681 assert_true(outputs_1.dimension(1) - 1 < static_cast<type>(1e-3), LOG);
682 assert_true(abs(outputs_1(0) - inputs(0)) < static_cast<type>(1e-3), LOG);
683
684 // Test 1_1
685
686 unscaling_layer.set(2);
687 unscaling_layer.set_unscaling_methods(UnscalingLayer::MinimumMaximum);
688
689 Tensor<type, 2> minimums_maximums(2,4);
690 minimums_maximums.setValues({{-1000,1000,0,0},{-100,100,0,0}});
691
692 unscaling_layer.set_descriptives_eigen(minimums_maximums);
693 inputs.resize(1,2);
694 inputs.setValues({{0.1f,0}});
695 outputs_1 = unscaling_layer.calculate_outputs(inputs);
696
697 assert_true(outputs_1.dimension(0) - 1 < static_cast<type>(1e-3), LOG);
698 assert_true(outputs_1.dimension(1) - 2 < static_cast<type>(1e-3), LOG);
699 assert_true(abs(outputs_1(0) - static_cast<type>(100)) < static_cast<type>(1e-3), LOG);
700 assert_true(abs(outputs_1(1) - static_cast<type>(0)) < static_cast<type>(1e-3), LOG);
701
702 // Test 2_0
703
704 unscaling_layer.set(1);
705 unscaling_layer.set_unscaling_methods(UnscalingLayer::MeanStandardDeviation);
706
707 inputs.resize(1,1);
708 Tensor<type, 2> outputs_2 = unscaling_layer.calculate_outputs(inputs);
709 assert_true(outputs_2.dimension(0) - 1 < static_cast<type>(1e-3), LOG);
710 assert_true(outputs_2.dimension(1) - 1 < static_cast<type>(1e-3), LOG);
711 assert_true(abs(outputs_2(0) - inputs(0)) < static_cast<type>(1e-3), LOG);
712
713 // Test 2_1
714
715 unscaling_layer.set(2);
716 unscaling_layer.set_unscaling_methods(UnscalingLayer::MeanStandardDeviation);
717
718 Tensor<type, 2> mean_standard_deviation(2,4);
719 mean_standard_deviation.setValues({{-1,1,-1,-2},{-1,1,2,3}});
720
721 unscaling_layer.set_descriptives_eigen(mean_standard_deviation);
722 inputs.resize(1,2);
723 inputs.setValues({{-1,1}});
724 outputs_2 = unscaling_layer.calculate_outputs(inputs);
725
726 assert_true(outputs_2.dimension(0) - 1 < static_cast<type>(1e-3), LOG);
727 assert_true(outputs_2.dimension(1) - 2 < static_cast<type>(1e-3), LOG);
728 assert_true(abs(outputs_2(0) - static_cast<type>(1)) < static_cast<type>(1e-3), LOG);
729 assert_true(abs(outputs_2(1) - static_cast<type>(5)) < static_cast<type>(1e-3), LOG);
730
731 // Test 3_0
732
733 unscaling_layer.set(1);
734 unscaling_layer.set_unscaling_methods(UnscalingLayer::Logarithmic);
735
736 inputs.resize(1,1);
737 Tensor<type, 2> outputs_3 = unscaling_layer.calculate_outputs(inputs);
738 assert_true(outputs_3.dimension(0) - 1 < static_cast<type>(1e-3), LOG);
739 assert_true(outputs_3.dimension(1) - 1 < static_cast<type>(1e-3), LOG);
740
741 assert_true(abs(outputs_3(0) - 1) < static_cast<type>(1e-3), LOG);
742
743 // Test 3_1
744
745 unscaling_layer.set(2);
746 unscaling_layer.set_unscaling_methods(UnscalingLayer::Logarithmic);
747
748 Tensor<type, 2> standard_deviation(2,4);
749 standard_deviation.setValues({{-1,1,-1,2},{-1,1,1,4}});
750
751 unscaling_layer.set_descriptives_eigen(standard_deviation);
752 inputs.resize(1,2);
753 inputs.setConstant(1);
754 outputs_3 = unscaling_layer.calculate_outputs(inputs);
755
756 assert_true(abs(outputs_3.dimension(0) - 1) < static_cast<type>(1e-3), LOG);
757 assert_true(abs(outputs_3.dimension(1) - 2) < static_cast<type>(1e-3), LOG);
758 assert_true(abs(outputs_3(0) - static_cast<type>(2.7182)) < static_cast<type>(1e-3), LOG);
759 assert_true(abs(outputs_3(1) - static_cast<type>(2.7182)) < static_cast<type>(1e-3), LOG);
760 }
761
test_write_expression()762 void UnscalingLayerTest::test_write_expression()
763 {
764 cout << "test_write_expression\n";
765
766 UnscalingLayer ul;
767
768 Tensor<string, 1> inputs_names(1);
769 Tensor<string, 1> outputs_names(1);
770
771 string expression;
772
773 // Test 1
774
775 ul.set(1);
776 ul.set_unscaling_methods(UnscalingLayer::NoUnscaling);
777 inputs_names.setValues({"x"});
778 outputs_names.setValues({"y"});
779
780 expression = ul.write_expression(inputs_names, outputs_names);
781
782 assert_true(expression.empty() == false, LOG);
783 assert_true(expression == "y = x;\n", LOG);
784
785 // Test 2
786
787 ul.set(1);
788 ul.set_unscaling_methods(UnscalingLayer::MinimumMaximum);
789
790 expression = ul.write_expression(inputs_names, outputs_names);
791
792 cout << expression;
793 assert_true(expression.empty() == false, LOG);
794
795 assert_true(expression == "y = x*(1+1)/(1+1)-1+1*(1+1)/(1+1);\n", LOG);
796
797 // Test 3
798
799 ul.set(1);
800 ul.set_unscaling_methods(UnscalingLayer::MeanStandardDeviation);
801
802 expression = ul.write_expression(inputs_names, outputs_names);
803
804 assert_true(expression.empty() == false, LOG);
805 assert_true(expression == "y = -1+0.5*(x+1)*((1)-(-1);\n", LOG);
806
807 // Test 4
808
809 ul.set(1);
810 ul.set_unscaling_methods(UnscalingLayer::Logarithmic);
811
812 expression = ul.write_expression(inputs_names, outputs_names);
813
814 assert_true(expression.empty() == false, LOG);
815 assert_true(expression == "y = -1+0.5*(exp(x)+1)*((1)-(-1));\n", LOG);
816 }
817
818
run_test_case()819 void UnscalingLayerTest::run_test_case()
820 {
821 cout << "Running unscaling layer test case...\n";
822
823 // Constructor and destructor methods
824
825 test_constructor();
826 test_destructor();
827
828
829 // Assignment operators methods
830
831 test_assignment_operator();
832
833
834 // Get methods
835
836 test_get_dimensions();
837
838
839 // Unscaling layer architecture
840
841 test_get_inputs_number();
842 test_get_neurons_number();
843
844
845 // Output variables descriptives
846
847 test_get_minimums();
848 test_get_maximums();
849
850
851 // Variables descriptives
852
853 test_get_descriptives();
854 test_get_descriptives_matrix();
855
856
857 // Display messages
858
859 test_get_display();
860
861
862 // Set methods
863
864 test_set();
865 test_set_inputs_number();
866 test_set_neurons_number();
867 test_set_default();
868
869
870 // Output variables descriptives
871
872 test_set_descriptives();
873 test_set_descriptives_eigen();
874 test_set_item_descriptives();
875 test_set_minimum();
876 test_set_maximum();
877 test_set_mean();
878 test_set_standard_deviation();
879
880
881 // Variables descriptives
882
883 // Variables scaling and unscaling
884
885 test_write_scaling_methods();
886
887
888 // Display messages
889
890 test_set_display();
891
892
893 // Check methods
894
895 test_is_empty();
896
897
898 // Output methods
899
900 test_calculate_outputs();
901
902
903 // Expression methods
904
905 test_write_expression();
906
907
908 cout << "End of unscaling layer test case.\n\n";
909 }
910
911
912
913 // OpenNN: Open Neural Networks Library.
914 // Copyright (C) 2005-2020 Artificial Intelligence Techniques, SL.
915 //
916 // This library is free software; you can redistribute it and/or
917 // modify it under the terms of the GNU Lesser General Public
918 // License as published by the Free Software Foundation; either
919 // version 2.1 of the License, or any later version.
920 //
921 // This library is distributed in the hope that it will be useful,
922 // but WITHOUT ANY WARRANTY; without even the implied warranty of
923 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
924 // Lesser General Public License for more details.
925
926 // You should have received a copy of the GNU Lesser General Public
927 // License along with this library; if not, write to the Free Software
928 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
929