Newer
Older
/**
* DESCRIPTION OF THE CLASS
*
* @author David Vojtek
* @date 2018
*/
kra568
committed
#define BOOST_TEST_MODULE ErrorFunctions_test
#include "boost_unit_tests_preamble.h"
kra568
committed
#include "../ErrorFunction/ErrorFunctions.h"
Martin Beseda
committed
using namespace lib4neuro;
/**
* Boost testing suite for testing ErrorFunction.h
* doesn't test inherited methods
*/
Martin Beseda
committed
BOOST_AUTO_TEST_SUITE(ErrorFunctions_test);
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
BOOST_AUTO_TEST_CASE(ErrorFunction_MSE_Construction_Test) {
NeuralNetwork network;
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
std::vector<double> inp, out;
for (int i = 0; i < 3; i++) {
inp.push_back(i);
out.push_back(i + 4);
}
data_vec.emplace_back(std::make_pair(inp, out));
DataSet dataSet(&data_vec);
BOOST_CHECK_NO_THROW(MSE mse(&network, &dataSet));
}
BOOST_AUTO_TEST_CASE(ErrorFunction_MSE_Eval_Test) {
Neuron *n1 = new NeuronLinear();
Neuron *n2 = new NeuronLinear();
NeuralNetwork network;
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
std::vector<double> inp, out;
for (int i = 0; i < 1; i++) {
inp.push_back(i);
out.push_back(i + 4);
}
data_vec.emplace_back(std::make_pair(inp, out));
network.add_neuron(n1);
network.add_neuron(n2);
network.add_connection_simple(0, 1, SIMPLE_CONNECTION_TYPE::UNITARY_WEIGHT, 2.5);
network.randomize_weights();
std::vector<size_t> net_input_neurons_indices(1);
std::vector<size_t> net_output_neurons_indices(1);
net_input_neurons_indices[0] = 0;
net_output_neurons_indices[0] = 1;
network.specify_input_neurons(net_input_neurons_indices);
network.specify_output_neurons(net_output_neurons_indices);
DataSet dataSet(&data_vec);
std::vector<double> weights;
weights.push_back(1);
MSE mse(&network, &dataSet);
BOOST_CHECK_EQUAL(9, mse.eval(&weights));
}
BOOST_AUTO_TEST_CASE(ErrorFunction_MSE_Get_dimension_test) {
Neuron *n1 = new NeuronLinear();
Neuron *n2 = new NeuronLinear();
NeuralNetwork network;
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
std::vector<double> inp, out;
for (int i = 0; i < 1; i++) {
inp.push_back(i);
out.push_back(i + 4);
}
data_vec.emplace_back(std::make_pair(inp, out));
network.add_neuron(n1);
network.add_neuron(n2);
network.add_connection_simple(0, 1, SIMPLE_CONNECTION_TYPE::UNITARY_WEIGHT, 2.5);
network.randomize_weights();
std::vector<size_t> net_input_neurons_indices(1);
std::vector<size_t> net_output_neurons_indices(1);
net_input_neurons_indices[0] = 0;
net_output_neurons_indices[0] = 1;
network.specify_input_neurons(net_input_neurons_indices);
network.specify_output_neurons(net_output_neurons_indices);
DataSet dataSet(&data_vec);
MSE mse(&network, &dataSet);
BOOST_CHECK_EQUAL(2, mse.get_dimension());
}
BOOST_AUTO_TEST_CASE(ErrorFunction_MSE_SUM_Construction_Test) {
BOOST_CHECK_NO_THROW(ErrorSum mse_sum);
}
BOOST_AUTO_TEST_CASE(ErrorFunction_MSE_SUM_Add_Error_Function_Test) {
NeuralNetwork network;
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
std::vector<double> inp, out;
for (int i = 0; i < 3; i++) {
inp.push_back(i);
out.push_back(i + 4);
}
data_vec.emplace_back(std::make_pair(inp, out));
DataSet dataSet(&data_vec);
ErrorFunction *f = new MSE(&network, &dataSet);
ErrorSum mse_sum;
BOOST_CHECK_NO_THROW(mse_sum.add_error_function(f));
}
BOOST_AUTO_TEST_CASE(ErrorFunction_MSE_SUM_Eval_Test) {
ErrorSum mse_sum;
Neuron *n1 = new NeuronLinear();
Neuron *n2 = new NeuronLinear();
NeuralNetwork network;
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
std::vector<double> inp, out;
for (int i = 0; i < 1; i++) {
inp.push_back(i);
out.push_back(i + 4);
}
data_vec.emplace_back(std::make_pair(inp, out));
network.add_neuron(n1);
network.add_neuron(n2);
network.add_connection_simple(0, 1, SIMPLE_CONNECTION_TYPE::UNITARY_WEIGHT, 2.5);
network.randomize_weights();
std::vector<size_t> net_input_neurons_indices(1);
std::vector<size_t> net_output_neurons_indices(1);
net_input_neurons_indices[0] = 0;
net_output_neurons_indices[0] = 1;
network.specify_input_neurons(net_input_neurons_indices);
network.specify_output_neurons(net_output_neurons_indices);
DataSet dataSet(&data_vec);
ErrorFunction *f = new MSE(&network, &dataSet);
mse_sum.add_error_function(f);
std::vector<double> weights;
weights.push_back(1);
BOOST_CHECK_EQUAL(9, mse_sum.eval(&weights));
}
BOOST_AUTO_TEST_CASE(ErrorFunction_MSE_SUM_Get_Dimension_test) {
ErrorSum mse_sum;
BOOST_CHECK_EQUAL(0, mse_sum.get_dimension());
Neuron *n1 = new NeuronLinear();
Neuron *n2 = new NeuronLinear();
NeuralNetwork network;
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_vec;
std::vector<double> inp, out;
for (int i = 0; i < 1; i++) {
inp.push_back(i);
out.push_back(i + 4);
}
data_vec.emplace_back(std::make_pair(inp, out));
network.add_neuron(n1);
network.add_neuron(n2);
network.add_connection_simple(0, 1, SIMPLE_CONNECTION_TYPE::UNITARY_WEIGHT, 2.5);
network.randomize_weights();
std::vector<size_t> net_input_neurons_indices(1);
std::vector<size_t> net_output_neurons_indices(1);
net_input_neurons_indices[0] = 0;
net_output_neurons_indices[0] = 1;
network.specify_input_neurons(net_input_neurons_indices);
network.specify_output_neurons(net_output_neurons_indices);
DataSet dataSet(&data_vec);
ErrorFunction *f = new MSE(&network, &dataSet);
mse_sum.add_error_function(f);
BOOST_CHECK_EQUAL(2, mse_sum.get_dimension());
}
BOOST_AUTO_TEST_SUITE_END()