-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_dropoutlayer.cpp
More file actions
86 lines (78 loc) · 2.56 KB
/
Copy pathtest_dropoutlayer.cpp
File metadata and controls
86 lines (78 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <numeric>
#include <vector>
#include "gtest/gtest.h"
#include "layers/DropOutLayer.hpp"
using namespace it_lab_ai;
TEST(DropOutLayer, IncompatibleInput) {
DropOutLayer layer(1);
Shape sh({2, 2});
Tensor input = make_tensor<int>({1, -1, 2, -2}, sh);
Tensor output;
std::vector<Tensor> in{input, input};
std::vector<Tensor> out{output};
ASSERT_ANY_THROW(layer.run(in, out));
}
TEST(DropOutLayer, dropoutlayer_int) {
DropOutLayer layer(1, true);
Shape sh({2, 2});
Tensor input = make_tensor<int>({1, -1, 2, -2}, sh);
Tensor output;
std::vector<Tensor> in{input};
std::vector<Tensor> out{output};
layer.run(in, out);
std::vector<int> vec = *out[0].as<int>();
EXPECT_EQ(vec[0], 0);
EXPECT_EQ(vec[1], 0);
EXPECT_EQ(vec[2], 0);
EXPECT_EQ(vec[3], 0);
}
TEST(DropOutLayer, dropoutlayer_float) {
DropOutLayer layer(0);
Shape sh({2, 2});
Tensor input = make_tensor<float>({1.0F, -1.0F, 2.0F, -2.0F}, sh);
Tensor output;
std::vector<Tensor> in{input};
std::vector<Tensor> out{output};
layer.run(in, out);
std::vector<float> vec = *out[0].as<float>();
EXPECT_NEAR(vec[0], 1, 1e-5);
EXPECT_NEAR(vec[1], -1, 1e-5);
EXPECT_NEAR(vec[2], 2, 1e-5);
EXPECT_NEAR(vec[3], -2, 1e-5);
}
TEST(DropOutLayer, dropoutlayer_float_50proc) {
DropOutLayer layer(0.5, true);
Shape sh({10, 10});
std::vector<float> a(100, static_cast<float>(0.01));
Tensor input = make_tensor<float>(a, sh);
Tensor output;
std::vector<Tensor> in{input};
std::vector<Tensor> out{output};
layer.run(in, out);
std::vector<float> vec = *out[0].as<float>();
EXPECT_NEAR(std::accumulate(vec.begin(), vec.end(), 0.0F), 0.5, 0.2);
}
TEST(DropOutLayer, dropoutlayer_float_30proc) {
DropOutLayer layer(0.3, true);
Shape sh({10, 10});
std::vector<float> a(100, static_cast<float>(0.01));
Tensor input = make_tensor<float>(a, sh);
Tensor output;
std::vector<Tensor> in{input};
std::vector<Tensor> out{output};
layer.run(in, out);
std::vector<float> vec = *out[0].as<float>();
EXPECT_NEAR(std::accumulate(vec.begin(), vec.end(), 0.0F), 0.7, 0.2);
}
TEST(DropOutLayer, dropoutlayer_float_70proc) {
DropOutLayer layer(0.7, true);
Shape sh({10, 10});
std::vector<float> a(100, static_cast<float>(0.01));
Tensor input = make_tensor<float>(a, sh);
Tensor output;
std::vector<Tensor> in{input};
std::vector<Tensor> out{output};
layer.run(in, out);
std::vector<float> vec = *out[0].as<float>();
EXPECT_NEAR(std::accumulate(vec.begin(), vec.end(), 0.0F), 0.3, 0.2);
}