-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathkeras_model_test.cc
165 lines (148 loc) · 4.8 KB
/
keras_model_test.cc
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "test/benchmark.h"
#include "test/conv_2x2.h"
#include "test/conv_3x3.h"
#include "test/conv_3x3x3.h"
#include "test/conv_hard_sigmoid_2x2.h"
#include "test/conv_sigmoid_2x2.h"
#include "test/conv_softplus_2x2.h"
#include "test/dense_10x1.h"
#include "test/dense_10x10.h"
#include "test/dense_10x10x10.h"
#include "test/dense_1x1.h"
#include "test/dense_2x2.h"
#include "test/dense_relu_10.h"
#include "test/dense_tanh_10.h"
#include "test/elu_10.h"
#include "test/embedding_64.h"
#include "test/lstm_simple_7x20.h"
#include "test/lstm_simple_stacked_16x9.h"
#include "test/lstm_stacked_64x83.h"
#include "test/maxpool2d_1x1.h"
#include "test/maxpool2d_2x2.h"
#include "test/maxpool2d_3x2x2.h"
#include "test/maxpool2d_3x3x3.h"
#include "test/relu_10.h"
#include "src/model.h"
using namespace keras2cpp;
namespace test {
inline void basics() noexcept {
{
const int i = 3;
const int j = 5;
const int k = 10;
Tensor t {i, j, k};
float c = 1.f;
for (size_t ii = 0; ii < i; ++ii)
for (size_t jj = 0; jj < j; ++jj)
for (size_t kk = 0; kk < k; ++kk) {
t(ii, jj, kk) = c;
c += 1.f;
}
c = 1.f;
size_t cc = 0;
for (size_t ii = 0; ii < i; ++ii)
for (size_t jj = 0; jj < j; ++jj)
for (size_t kk = 0; kk < k; ++kk) {
kassert_eq(t(ii, jj, kk), c, 1e-9);
kassert_eq(t.data_[cc], c, 1e-9);
c += 1.f;
++cc;
}
}
{
const size_t i = 2;
const size_t j = 3;
const size_t k = 4;
const size_t l = 5;
Tensor t {i, j, k, l};
float c = 1.f;
for (size_t ii = 0; ii < i; ++ii)
for (size_t jj = 0; jj < j; ++jj)
for (size_t kk = 0; kk < k; ++kk)
for (size_t ll = 0; ll < l; ++ll) {
t(ii, jj, kk, ll) = c;
c += 1.f;
}
c = 1.f;
size_t cc = 0;
for (size_t ii = 0; ii < i; ++ii)
for (size_t jj = 0; jj < j; ++jj)
for (size_t kk = 0; kk < k; ++kk)
for (size_t ll = 0; ll < l; ++ll) {
kassert_eq(t(ii, jj, kk, ll), c, 1e-9);
kassert_eq(t.data_[cc], c, 1e-9);
c += 1.f;
++cc;
}
}
{
Tensor a {2, 2};
Tensor b {2, 2};
a.data_ = {1.0, 2.0, 3.0, 5.0};
b.data_ = {2.0, 5.0, 4.0, 1.0};
Tensor result = a + b;
kassert(result.data_ == std::vector<float>({3.0, 7.0, 7.0, 6.0}));
}
{
Tensor a {2, 2};
Tensor b {2, 2};
a.data_ = {1.0, 2.0, 3.0, 5.0};
b.data_ = {2.0, 5.0, 4.0, 1.0};
Tensor result = a * b;
kassert(result.data_ == std::vector<float>({2.0, 10.0, 12.0, 5.0}));
}
{
Tensor a {1, 2};
Tensor b {1, 2};
a.data_ = {1.0, 2.0};
b.data_ = {2.0, 5.0};
Tensor result = a.dot(b);
kassert(result.data_ == std::vector<float>({12.0}));
}
{
Tensor a {2, 1};
Tensor b {2, 1};
a.data_ = {1.0, 2.0};
b.data_ = {2.0, 5.0};
Tensor result = a.dot(b);
kassert(result.data_ == std::vector<float>({2.0, 5.0, 4.0, 10.0}));
}
}
}
int main() {
test::basics();
test::dense_1x1();
test::dense_10x1();
test::dense_2x2();
test::dense_10x10();
test::dense_10x10x10();
test::conv_2x2();
test::conv_3x3();
test::conv_3x3x3();
test::elu_10();
test::relu_10();
test::dense_relu_10();
test::dense_tanh_10();
test::conv_softplus_2x2();
test::conv_hard_sigmoid_2x2();
test::conv_sigmoid_2x2();
test::maxpool2d_1x1();
test::maxpool2d_2x2();
test::maxpool2d_3x2x2();
test::maxpool2d_3x3x3();
test::lstm_simple_7x20();
test::lstm_simple_stacked_16x9();
test::lstm_stacked_64x83();
test::embedding_64();
const size_t n = 10; // Run benchmark "n" times.
double total_load_time = 0.0;
double total_apply_time = 0.0;
for (size_t i = 0; i < n; ++i) {
auto [load_time, apply_time] = test::benchmark();
total_load_time += load_time;
total_apply_time += apply_time;
}
printf("Benchmark network loads in %fs\n", total_load_time / n);
printf("Benchmark network runs in %fs\n", total_apply_time / n);
return 0;
}