From 653e98e029a0d0f110b0ac599e50406060bb0f87 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 16 Dec 2023 10:21:16 -0800 Subject: Decouple activations from linear layer. --- src/lib/test/train_xor_test.c | 55 +++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 20 deletions(-) (limited to 'src/lib/test/train_xor_test.c') diff --git a/src/lib/test/train_xor_test.c b/src/lib/test/train_xor_test.c index 6ddc6e0..78695a3 100644 --- a/src/lib/test/train_xor_test.c +++ b/src/lib/test/train_xor_test.c @@ -1,9 +1,9 @@ #include -#include -#include #include "activation.h" #include "neuralnet_impl.h" +#include +#include #include "test.h" #include "test_util.h" @@ -11,18 +11,27 @@ #include TEST_CASE(neuralnet_train_xor_test) { - const int num_layers = 2; - const int layer_sizes[] = { 2, 2, 1 }; - const nnActivation layer_activations[] = { nnRelu, nnIdentity }; + 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(num_layers, layer_sizes, layer_activations); + 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. }; +#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); @@ -30,31 +39,37 @@ TEST_CASE(neuralnet_train_xor_test) { nnMatrixInit(&targets_matrix, targets); nnTrainingParams params = { - .learning_rate = 0.1, - .max_iterations = 500, - .seed = 0, - .weight_init = nnWeightInit01, - .debug = false, + .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 +#define M 4 - nnQueryObject* query = nnMakeQueryObject(net, /*num_inputs=*/M); + nnQueryObject* query = nnMakeQueryObject(net, M); - const R test_inputs[M][2] = { { 0., 0. }, { 1., 0. }, { 0., 1. }, { 1., 1. } }; + 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. }; + 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]); + 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); -- cgit v1.2.3