diff --git a/matrix.c b/matrix.c index a0e2ccf..253d848 100644 --- a/matrix.c +++ b/matrix.c @@ -8,19 +8,20 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { Matrix matrix; - matrix.rows = rows; - matrix.cols = cols; + matrix.buffer = NULL; + matrix.rows = 0; + matrix.cols = 0; + - if (rows == 0 || cols == 0) { - matrix.buffer = NULL; - return matrix; - } - - matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType)); - if (matrix.buffer == NULL) + // Wenn die Dimensionen gültig sind, Speicher reservieren + if (rows > 0 && cols > 0) { - matrix.rows = 0; - matrix.cols = 0; + matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType)); + if (matrix.buffer != NULL) + { + matrix.rows = rows; + matrix.cols = cols; + } } return matrix; } diff --git a/matrix.h b/matrix.h index c85e38c..02202c5 100644 --- a/matrix.h +++ b/matrix.h @@ -8,9 +8,10 @@ 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..66731e0 100644 --- a/neuralNetworkTests.c +++ b/neuralNetworkTests.c @@ -8,7 +8,58 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) { - // TODO + // TODO : Fehlerbehandlung + // Öffne die Datei zum Schreiben im Binärmodus + FILE *file = fopen(path, "wb"); + if (!file) return; + + // Schreibe den Datei-Tag + const char *tag = "__info2_neural_network_file_format__"; + fwrite(tag, 1, strlen(tag), file); + + // Schreibe die Anzahl der Layer + if (nn.numberOfLayers == 0) { + fclose(file); + return; + } + + // Schreibe die Eingabe- und Ausgabegrößen des Netzwerks + int input = nn.layers[0].weights.cols; + int output = nn.layers[0].weights.rows; + + fwrite(&input, sizeof(int), 1, file); + fwrite(&output, sizeof(int), 1, file); + + // Schreibe die Layer-Daten + for (int i = 0; i < nn.numberOfLayers; i++) + { + const Layer *layer = &nn.layers[i]; + int out = layer->weights.rows; + int in = layer->weights.cols; + + + fwrite(layer->weights.buffer, sizeof(MatrixType), out * in, file); + + + fwrite(layer->biases.buffer, sizeof(MatrixType), out * 1, file); + + if (i + 1 < nn.numberOfLayers) + { + int nextOut = nn.layers[i + 1].weights.rows; + fwrite(&nextOut, sizeof(int), 1, file); + } + } + + fclose(file); + + // Debuging-Ausgabe + printf("prepareNeuralNetworkFile: Datei '%s' erstellt mit %u Layer(n)\n", path, nn.numberOfLayers); + for (unsigned int i = 0; i < nn.numberOfLayers; i++) { + Layer layer = nn.layers[i]; + printf("Layer %u: weights (%u x %u), biases (%u x %u)\n", + i, layer.weights.rows, layer.weights.cols, layer.biases.rows, layer.biases.cols); + } + } void test_loadModelReturnsCorrectNumberOfLayers(void)