forked from libfann/fann
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfann_test.cpp
168 lines (138 loc) · 5.69 KB
/
fann_test.cpp
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
166
167
168
#include <vector>
#include "fann_test.h"
using namespace std;
void FannTest::SetUp() {
//ensure random generator is seeded at a known value to ensure reproducible results
srand(0);
fann_disable_seed_rand();
}
void FannTest::TearDown() {
net.destroy();
data.destroy_train();
}
void FannTest::AssertCreate(neural_net &net, unsigned int numLayers, const unsigned int *layers,
unsigned int neurons, unsigned int connections) {
EXPECT_EQ(numLayers, net.get_num_layers());
EXPECT_EQ(layers[0], net.get_num_input());
EXPECT_EQ(layers[numLayers - 1], net.get_num_output());
unsigned int *layers_res = new unsigned int[numLayers];
net.get_layer_array(layers_res);
for (unsigned int i = 0; i < numLayers; i++) {
EXPECT_EQ(layers[i], layers_res[i]);
}
delete layers_res;
EXPECT_EQ(neurons, net.get_total_neurons());
EXPECT_EQ(connections, net.get_total_connections());
AssertWeights(net, -0.09, 0.09, 0.0);
}
void FannTest::AssertCreateAndCopy(neural_net &net, unsigned int numLayers, const unsigned int *layers, unsigned int neurons,
unsigned int connections) {
AssertCreate(net, numLayers, layers, neurons, connections);
neural_net net_copy(net);
AssertCreate(net_copy, numLayers, layers, neurons, connections);
}
void FannTest::AssertWeights(neural_net &net, fann_type min, fann_type max, fann_type avg) {
connection *connections = new connection[net.get_total_connections()];
net.get_connection_array(connections);
fann_type minWeight = connections[0].weight;
fann_type maxWeight = connections[0].weight;
fann_type totalWeight = 0.0;
for (int i = 1; i < net.get_total_connections(); ++i) {
if (connections[i].weight < minWeight)
minWeight = connections[i].weight;
if (connections[i].weight > maxWeight)
maxWeight = connections[i].weight;
totalWeight += connections[i].weight;
}
EXPECT_NEAR(min, minWeight, 0.05);
EXPECT_NEAR(max, maxWeight, 0.05);
EXPECT_NEAR(avg, totalWeight / (fann_type) net.get_total_connections(), 0.5);
}
TEST_F(FannTest, CreateStandardThreeLayers) {
neural_net net(LAYER, 3, 2, 3, 4);
AssertCreateAndCopy(net, 3, (const unsigned int[]) {2, 3, 4}, 11, 25);
}
TEST_F(FannTest, CreateStandardThreeLayersUsingCreateMethod) {
ASSERT_TRUE(net.create_standard(3, 2, 3, 4));
unsigned int layers[] = {2, 3, 4};
AssertCreateAndCopy(net, 3, layers, 11, 25);
}
TEST_F(FannTest, CreateStandardFourLayersArray) {
unsigned int layers[] = {2, 3, 4, 5};
neural_net net(LAYER, 4, layers);
AssertCreateAndCopy(net, 4, layers, 17, 50);
}
TEST_F(FannTest, CreateStandardFourLayersArrayUsingCreateMethod) {
unsigned int layers[] = {2, 3, 4, 5};
ASSERT_TRUE(net.create_standard_array(4, layers));
AssertCreateAndCopy(net, 4, layers, 17, 50);
}
TEST_F(FannTest, CreateStandardFourLayersVector) {
vector<unsigned int> layers{2, 3, 4, 5};
neural_net net(LAYER, layers.begin(), layers.end());
AssertCreateAndCopy(net, 4, layers.data(), 17, 50);
}
TEST_F(FannTest, CreateSparseFourLayers) {
neural_net net(0.5, 4, 2, 3, 4, 5);
AssertCreateAndCopy(net, 4, (const unsigned int[]){2, 3, 4, 5}, 17, 31);
}
TEST_F(FannTest, CreateSparseFourLayersUsingCreateMethod) {
ASSERT_TRUE(net.create_sparse(0.5f, 4, 2, 3, 4, 5));
AssertCreateAndCopy(net, 4, (const unsigned int[]){2, 3, 4, 5}, 17, 31);
}
TEST_F(FannTest, CreateSparseArrayFourLayers) {
unsigned int layers[] = {2, 3, 4, 5};
neural_net net(0.5f, 4, layers);
AssertCreateAndCopy(net, 4, layers, 17, 31);
}
TEST_F(FannTest, CreateSparseArrayFourLayersUsingCreateMethod) {
unsigned int layers[] = {2, 3, 4, 5};
ASSERT_TRUE(net.create_sparse_array(0.5f, 4, layers));
AssertCreateAndCopy(net, 4, layers, 17, 31);
}
TEST_F(FannTest, CreateSparseArrayWithMinimalConnectivity) {
unsigned int layers[] = {2, 2, 2};
neural_net net(0.01f, 3, layers);
AssertCreateAndCopy(net, 3, layers, 8, 8);
}
TEST_F(FannTest, CreateShortcutFourLayers) {
neural_net net(SHORTCUT, 4, 2, 3, 4, 5);
AssertCreateAndCopy(net, 4, (const unsigned int[]){2, 3, 4, 5}, 15, 83);
EXPECT_EQ(SHORTCUT, net.get_network_type());
}
TEST_F(FannTest, CreateShortcutFourLayersUsingCreateMethod) {
ASSERT_TRUE(net.create_shortcut(4, 2, 3, 4, 5));
AssertCreateAndCopy(net, 4, (const unsigned int[]){2, 3, 4, 5}, 15, 83);
EXPECT_EQ(SHORTCUT, net.get_network_type());
}
TEST_F(FannTest, CreateShortcutArrayFourLayers) {
unsigned int layers[] = {2, 3, 4, 5};
neural_net net(SHORTCUT, 4, layers);
AssertCreateAndCopy(net, 4, layers, 15, 83);
EXPECT_EQ(SHORTCUT, net.get_network_type());
}
TEST_F(FannTest, CreateShortcutArrayFourLayersUsingCreateMethod) {
unsigned int layers[] = {2, 3, 4, 5};
ASSERT_TRUE(net.create_shortcut_array(4, layers));
AssertCreateAndCopy(net, 4, layers, 15, 83);
EXPECT_EQ(SHORTCUT, net.get_network_type());
}
TEST_F(FannTest, CreateFromFile) {
ASSERT_TRUE(net.create_standard(3, 2, 3, 4));
neural_net netToBeSaved(LAYER, 3, 2, 3, 4);
ASSERT_TRUE(netToBeSaved.save("tmpfile"));
neural_net netToBeLoaded("tmpfile");
AssertCreateAndCopy(netToBeLoaded, 3, (const unsigned int[]){2, 3, 4}, 11, 25);
}
TEST_F(FannTest, CreateFromFileUsingCreateMethod) {
ASSERT_TRUE(net.create_standard(3, 2, 3, 4));
neural_net inputNet(LAYER, 3, 2, 3, 4);
ASSERT_TRUE(inputNet.save("tmpfile"));
ASSERT_TRUE(net.create_from_file("tmpfile"));
AssertCreateAndCopy(net, 3, (const unsigned int[]){2, 3, 4}, 11, 25);
}
TEST_F(FannTest, RandomizeWeights) {
neural_net net(LAYER, 2, 20, 10);
net.randomize_weights(-1.0, 1.0);
AssertWeights(net, -1.0, 1.0, 0);
}