From 44f0bfc16d88e4a5902ab077211f9ee0d366b793 Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 14 Nov 2025 13:01:03 +0100 Subject: [PATCH] sync --- matrix.c | 4 ++-- matrix.h | 2 +- neuralNetworkTests.c | 38 +++++++++++++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/matrix.c b/matrix.c index f9ea09a..85fc869 100644 --- a/matrix.c +++ b/matrix.c @@ -6,13 +6,13 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { - Matrix m = {0, 0, NULL}; + Matrix m = {NULL, 0, 0}; if (rows > 0 && cols > 0) { + m.buffer = malloc(rows * cols * sizeof(int)); m.rows = rows; m.cols = cols; - m.buffer = malloc(rows * cols * sizeof(int)); } return m; diff --git a/matrix.h b/matrix.h index a27c11a..8ceedc9 100644 --- a/matrix.h +++ b/matrix.h @@ -9,9 +9,9 @@ typedef float MatrixType; typedef struct { + MatrixType *buffer; // Zeiger auf die Matrixdaten unsigned int rows; // Anzahl der Zeilen unsigned int cols; // Anzahl der Spalten - MatrixType *buffer; // Zeiger auf die Matrixdaten } Matrix; diff --git a/neuralNetworkTests.c b/neuralNetworkTests.c index 21ab370..cbe0737 100644 --- a/neuralNetworkTests.c +++ b/neuralNetworkTests.c @@ -8,7 +8,43 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) { - // TODO + FILE *file = fopen(path, "wb"); + if (file == NULL) { + perror("Fehler beim Erstellen der Testdatei"); + exit(EXIT_FAILURE); + } + + // Dateikopf speichern + const char *fileTag = "info2_neural_network_file_format"; + fwrite(fileTag, sizeof(char), strlen(fileTag), file); + + // Dimensionen der Eingabe und Ausgabe für den ersten Layer speichern + unsigned int inputDimension = nn.layers[0].weights.rows; // Eingabedimension ist die Anzahl der Eingabeneuronen im ersten Layer + unsigned int outputDimension = nn.layers[0].weights.cols; // Ausgabedimension ist die Anzahl der Ausgabeneuronen im ersten Layer + fwrite(&inputDimension, sizeof(unsigned int), 1, file); + fwrite(&outputDimension, sizeof(unsigned int), 1, file); + + // Alle Layer speichern + for (unsigned int i = 0; i < nn.numberOfLayers; i++) { + // Layer-Dimensionen speichern + inputDimension = nn.layers[i].weights.rows; + outputDimension = nn.layers[i].weights.cols; + + fwrite(&inputDimension, sizeof(unsigned int), 1, file); + fwrite(&outputDimension, sizeof(unsigned int), 1, file); + + // Gewichte speichern + fwrite(&nn.layers[i].weights.rows, sizeof(unsigned int), 1, file); + fwrite(&nn.layers[i].weights.cols, sizeof(unsigned int), 1, file); + fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), nn.layers[i].weights.rows * nn.layers[i].weights.cols, file); + + // Biases speichern + fwrite(&nn.layers[i].biases.rows, sizeof(unsigned int), 1, file); + fwrite(&nn.layers[i].biases.cols, sizeof(unsigned int), 1, file); + fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), nn.layers[i].biases.rows * nn.layers[i].biases.cols, file); + } + + fclose(file); } void test_loadModelReturnsCorrectNumberOfLayers(void)