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
|
#include <neuralnet/train.h>
#include "activation.h"
#include "neuralnet_impl.h"
#include <neuralnet/matrix.h>
#include <neuralnet/neuralnet.h>
#include "test.h"
#include "test_util.h"
#include <assert.h>
TEST_CASE(neuralnet_train_xor_test) {
const int num_layers = 3;
const int input_size = 2;
const nnLayer layers[] = {
{.type = nnLinear, .linear = {.input_size = 2, .output_size = 2}},
{.type = nnRelu},
{.type = nnLinear, .linear = {.input_size = 2, .output_size = 1}}
};
nnNeuralNetwork* net = nnMakeNet(layers, num_layers, input_size);
assert(net);
// Train.
#define N 4
const R inputs[N][2] = {
{0., 0.},
{0., 1.},
{1., 0.},
{1., 1.}
};
const R targets[N] = {0., 1., 1., 0.};
nnMatrix inputs_matrix = nnMatrixMake(N, 2);
nnMatrix targets_matrix = nnMatrixMake(N, 1);
nnMatrixInit(&inputs_matrix, (const R*)inputs);
nnMatrixInit(&targets_matrix, targets);
nnTrainingParams params = {
.learning_rate = 0.1,
.max_iterations = 500,
.seed = 0,
.weight_init = nnWeightInit01,
.debug = false,
};
nnTrain(net, &inputs_matrix, &targets_matrix, ¶ms);
// Test.
#define M 4
nnQueryObject* query = nnMakeQueryObject(net, M);
const R test_inputs[M][2] = {
{0., 0.},
{1., 0.},
{0., 1.},
{1., 1.}
};
nnMatrix test_inputs_matrix = nnMatrixMake(M, 2);
nnMatrixInit(&test_inputs_matrix, (const R*)test_inputs);
nnQuery(net, query, &test_inputs_matrix);
const R expected_outputs[M] = {0., 1., 1., 0.};
for (int i = 0; i < M; ++i) {
const R test_output = nnMatrixAt(nnNetOutputs(query), i, 0);
printf(
"\nInput: (%f, %f), Output: %f, Expected: %f\n", test_inputs[i][0],
test_inputs[i][1], test_output, expected_outputs[i]);
}
for (int i = 0; i < M; ++i) {
const R test_output = nnMatrixAt(nnNetOutputs(query), i, 0);
TEST_TRUE(double_eq(test_output, expected_outputs[i], OUTPUT_EPS));
}
nnDeleteQueryObject(&query);
nnDeleteNet(&net);
}
|