forked from pgomoluch/simple-nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cxx
158 lines (124 loc) · 4.18 KB
/
test.cxx
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
#include "matrix.h"
#include "legacy/network.h"
#include "network.h"
#include <chrono>
#include <iostream>
using namespace std;
void matrix_test()
{
cout << "Matrix class test" << endl;
double a[] = {
1.0, 2.0,
3.0, 4.0
};
double b[] = {
-1.0, 0.0,
0.0, 0.5
};
Matrix m1(2, 2, a);
cout << m1;
Matrix m2(2, 2, {1.0, 2.0, 3.0});
cout << m2;
Matrix m3(2, 2, b);
Matrix m4(2, 2);
Matrix::multiply(m1, m3, m4);
cout << m4;
Matrix::point_multiply(m1,m2,m4);
cout << m4;
Matrix m5(2, 3, {1,2,3,4,5,6});
cout << "m5\n" << m5 << "m5^T\n" << m5.T();
cout << endl;
}
// Assumes there's a network file 'network.txt', defining a network with two inputs.
// Will create a network file 'network2.txt' in the new file format.
void forward_pass_test()
{
cout << "Forward pass comparison" << endl;
legacy::Network n1("network.txt");
Network n2("network.txt");
const vector<double> sample = {1.0, 2.0};
cout << "Old: " << n1.evaluate(sample) << "\n";
cout << "New: " << n2.evaluate(sample) << "\n";
cout << endl;
}
// Assumes there's a network file 'network.txt', defining a network with two inputs.
// Will create 'network-t1.txt' and 'network-t2.txt', which should be the same.
void train_test()
{
cout << "Training comparison" << endl;
legacy::Network n1("network.txt");
Network n2("network.txt");
const vector<vector<double>> features = {{1.0, 2.0}};
const vector<double> labels = {1.0};
chrono::high_resolution_clock::time_point t1, t2;
t1 = chrono::high_resolution_clock::now();
n1.train(features, labels, 100000, 0.0001);
t2 = chrono::high_resolution_clock::now();
cout << "Old network train time: " << chrono::duration_cast<chrono::milliseconds>(t2-t1).count() << "ms" << endl;
n1.save("network-t1.txt");
t1 = chrono::high_resolution_clock::now();
n2.train(features, labels, 100000, 0.0001);
t2 = chrono::high_resolution_clock::now();
cout << "New network train time: " << chrono::duration_cast<chrono::milliseconds>(t2-t1).count() << "ms" << endl;
n2.save("network-t2.txt");
double result;
t1 = chrono::high_resolution_clock::now();
result = n1.evaluate(features[0]);
t2 = chrono::high_resolution_clock::now();
cout << "Old result: " << result << endl;
cout << "Old network evaluation time: " << chrono::duration_cast<chrono::nanoseconds>(t2-t1).count() << "ns" << endl;
t1 = chrono::high_resolution_clock::now();
result = n2.evaluate(features[0]);
t2 = chrono::high_resolution_clock::now();
cout << "New result: " << result << endl;
cout << "New network evaluation time: " << chrono::duration_cast<chrono::nanoseconds>(t2-t1).count() << "ns" << endl;
}
// Will create 'network_constructor_test.txt'
void network_constructor_test()
{
cout << "Network constructor test\n";
vector<unsigned> shape({3,4,2});
Network nn2(shape, true);
nn2.save("network_constructor_test.txt", true);
cout << endl;
}
// Assumes there's a network file 'network.txt', defining a network with two inputs.
// Will create a network file 'network2.txt' in the new file format.
void new_format_test()
{
cout << "New file format test" << endl;
Network nn("network.txt");
nn.save("network2.txt", true);
Network nn2("network2.txt", true);
const vector<double> sample({1.0, 2.0});
cout << "Original: " << nn.evaluate(sample) << endl;
cout << "Reloaded: " << nn2.evaluate(sample) << endl;
cout << endl;
}
// Assumes there's a network file 'network.txt', defining a network with two inputs
// and two outputs.
void multiple_outputs_test()
{
cout << "Multiple outputs test" << endl;
Network nn("network22.txt", true);
Matrix result(2,1);
const vector<double> sample({1.0, 2.0});
nn.evaluate(sample, result);
cout << "Result:\n" << result << endl;
}
void handler_test()
{
Network nn("nptest.txt", true);
nn.save("nptest2.txt", true);
}
int main()
{
matrix_test();
forward_pass_test();
train_test();
network_constructor_test();
new_format_test();
multiple_outputs_test();
handler_test();
return 0;
}