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_linear_perceptron_test.c | 44 +++++++++++++++-------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'src/lib/test/train_linear_perceptron_test.c') diff --git a/src/lib/test/train_linear_perceptron_test.c b/src/lib/test/train_linear_perceptron_test.c index 2b1336d..667643b 100644 --- a/src/lib/test/train_linear_perceptron_test.c +++ b/src/lib/test/train_linear_perceptron_test.c @@ -1,9 +1,8 @@ #include +#include "neuralnet_impl.h" #include #include -#include "activation.h" -#include "neuralnet_impl.h" #include "test.h" #include "test_util.h" @@ -11,19 +10,21 @@ #include TEST_CASE(neuralnet_train_linear_perceptron_test) { - const int num_layers = 1; - const int layer_sizes[] = { 1, 1 }; - const nnActivation layer_activations[] = { nnIdentity }; + const int num_layers = 1; + const int input_size = 1; + const nnLayer layers[] = { + {.type = nnLinear, .linear = {.input_size = 1, .output_size = 1}} + }; - nnNeuralNetwork* net = nnMakeNet(num_layers, layer_sizes, layer_activations); + nnNeuralNetwork* net = nnMakeNet(layers, num_layers, input_size); assert(net); - // Train. +// Train. - // Try to learn the Y=X line. - #define N 2 - const R inputs[N] = { 0., 1. }; - const R targets[N] = { 0., 1. }; +// Try to learn the Y=X line. +#define N 2 + const R inputs[N] = {0., 1.}; + const R targets[N] = {0., 1.}; nnMatrix inputs_matrix = nnMatrixMake(N, 1); nnMatrix targets_matrix = nnMatrixMake(N, 1); @@ -31,26 +32,27 @@ TEST_CASE(neuralnet_train_linear_perceptron_test) { nnMatrixInit(&targets_matrix, targets); nnTrainingParams params = { - .learning_rate = 0.7, - .max_iterations = 10, - .seed = 0, - .weight_init = nnWeightInit01, - .debug = false, + .learning_rate = 0.7, + .max_iterations = 10, + .seed = 0, + .weight_init = nnWeightInit01, + .debug = false, }; nnTrain(net, &inputs_matrix, &targets_matrix, ¶ms); - const R weight = nnMatrixAt(&net->weights[0], 0, 0); + const R weight = nnMatrixAt(&net->layers[0].linear.weights, 0, 0); const R expected_weight = 1.0; - printf("\nTrained network weight: %f, Expected: %f\n", weight, expected_weight); + printf( + "\nTrained network weight: %f, Expected: %f\n", weight, expected_weight); TEST_TRUE(double_eq(weight, expected_weight, WEIGHT_EPS)); // Test. - nnQueryObject* query = nnMakeQueryObject(net, /*num_inputs=*/1); + nnQueryObject* query = nnMakeQueryObject(net, 1); - const R test_input[] = { 2.3 }; - R test_output[1]; + const R test_input[] = {2.3}; + R test_output[1]; nnQueryArray(net, query, test_input, test_output); const R expected_output = test_input[0]; -- cgit v1.2.3