From 7e5dcc8574ce8f416c5f4b7d837415af6b721712 Mon Sep 17 00:00:00 2001 From: muellermo100295 Date: Wed, 26 Nov 2025 17:59:49 +0100 Subject: [PATCH] Update MM --- matrix.c | 6 +++--- matrix.h | 2 +- neuralNetworkTests.c | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/matrix.c b/matrix.c index bc11915..b2ca76a 100644 --- a/matrix.c +++ b/matrix.c @@ -41,7 +41,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) // Erstellt eine neue Matrix mit gegebener Zeilen- und Spaltenanzahl Matrix createMatrix(unsigned int rows, unsigned int cols) { - Matrix matrix = {0, 0, NULL}; // Initialisierung mit leeren Werten + Matrix matrix = {NULL,0, 0}; // Initialisierung mit leeren Werten if (rows == 0 || cols == 0) return matrix; // Ungültige Dimensionen → leere Matrix zurückgeben matrix.rows = rows; @@ -85,7 +85,7 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co // Addiert zwei Matrizen miteinander (entweder elementweise oder mit Broadcasting) Matrix add(const Matrix matrix1, const Matrix matrix2) { - Matrix result = {0, 0, NULL}; + Matrix result = {NULL,0, 0}; // Elementweise Addition: gleiche Dimensionen if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) { @@ -120,7 +120,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) // Multipliziert zwei Matrizen miteinander (Matrixmultiplikation) Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - Matrix result = {0, 0, NULL}; + Matrix result = {NULL,0, 0}; if (matrix1.cols != matrix2.rows) return result; // Dimensionen müssen passen: A.cols == B.rows diff --git a/matrix.h b/matrix.h index 00942ca..47a2319 100644 --- a/matrix.h +++ b/matrix.h @@ -8,9 +8,9 @@ typedef float MatrixType; // TODO Matrixtyp definieren // ***************************************************** typedef struct { + MatrixType *buffer; unsigned int rows; unsigned int cols; - MatrixType *buffer; } Matrix; // ***************************************************** diff --git a/neuralNetworkTests.c b/neuralNetworkTests.c index 21ab370..e993e5f 100644 --- a/neuralNetworkTests.c +++ b/neuralNetworkTests.c @@ -8,7 +8,38 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) { - // TODO + FILE *file = fopen(path, "wb"); + if (!file) { + fprintf(stderr, "Fehler: Datei konnte nicht geöffnet werden.\n"); + return; + } + + // Header schreiben + const char *header = "__info2_neural_network_file_format__"; + fwrite(header, sizeof(char), strlen(header), file); + + // Erste Dimension: Input der ersten Schicht + unsigned int inputDim = nn.layers[0].weights.cols; + fwrite(&inputDim, sizeof(unsigned int), 1, file); + + // Für jede Schicht + for (int i = 0; i < nn.numberOfLayers; i++) { + Layer layer = nn.layers[i]; + + // Output-Dimension + unsigned int outputDim = layer.weights.rows; + fwrite(&outputDim, sizeof(unsigned int), 1, file); + + // Weights + fwrite(layer.weights.buffer, sizeof(MatrixType), + layer.weights.rows * layer.weights.cols, file); + + // Biases + fwrite(layer.biases.buffer, sizeof(MatrixType), + layer.biases.rows * layer.biases.cols, file); + } + + fclose(file); } void test_loadModelReturnsCorrectNumberOfLayers(void)