From f4427d289254a71a92b9c3f34bd1a3f48f8b9edb Mon Sep 17 00:00:00 2001 From: Jan Uhlmann Date: Sun, 23 Nov 2025 16:38:17 +0000 Subject: [PATCH] unittests bestanden --- neuralNetworkTests.c | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/neuralNetworkTests.c b/neuralNetworkTests.c index fb3fb80..7a97f3c 100644 --- a/neuralNetworkTests.c +++ b/neuralNetworkTests.c @@ -5,29 +5,44 @@ #include "unity.h" #include "neuralNetwork.h" - -static void erzeugeMatrix(FILE *file, const Matrix *m) +static void writeLayer(FILE *file, const Matrix weights, const Matrix biases, unsigned int inputDim) { - fwrite(&m->rows, sizeof(int), 1, file); - fwrite(&m->cols, sizeof(int), 1, file); - fwrite(m->buffer, sizeof(MatrixType), m->rows * m->cols, file); + unsigned int outputDim = (unsigned int)weights.rows; + fwrite(&outputDim, sizeof(unsigned int), 1, file); + if (weights.buffer != NULL) + fwrite(weights.buffer, sizeof(MatrixType), outputDim * inputDim, file); + + if (biases.buffer != NULL) + fwrite(biases.buffer, sizeof(MatrixType), outputDim, file); } + static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) { FILE *file = fopen(path, "wb"); - if (!file) - return; + if (!file) return; - const char *header = "__info2_neural_network_file_format__"; - fwrite(header, sizeof(char), strlen(header), file); - fwrite(&nn.numberOfLayers, sizeof(int), 1, file); + const char tag[] = "__info2_neural_network_file_format__"; + fwrite(tag, sizeof(char), strlen(tag), file); + + if (nn.numberOfLayers == 0) + { + unsigned int zero = 0; + fwrite(&zero, sizeof(unsigned int), 1, file); + fclose(file); + return; + } + + unsigned int inputDim = (unsigned int)nn.layers[0].weights.cols; + fwrite(&inputDim, sizeof(unsigned int), 1, file); for (int i = 0; i < nn.numberOfLayers; i++) { - erzeugeMatrix(file, &nn.layers[i].weights); - erzeugeMatrix(file, &nn.layers[i].biases); + writeLayer(file, nn.layers[i].weights, nn.layers[i].biases, inputDim); + inputDim = (unsigned int)nn.layers[i].weights.rows; } + unsigned int zero = 0; + fwrite(&zero, sizeof(unsigned int), 1, file); fclose(file); }