diff --git a/.idea/editor.xml b/.idea/editor.xml index 95d51a7..1f0ef49 100644 --- a/.idea/editor.xml +++ b/.idea/editor.xml @@ -1,484 +1,6 @@ - \ No newline at end of file diff --git a/matrix.h b/matrix.h index a9f100e..ff77f24 100644 --- a/matrix.h +++ b/matrix.h @@ -7,9 +7,9 @@ typedef float MatrixType; // TODO Matrixtyp definieren typedef struct { + MatrixType *buffer; MatrixType rows; MatrixType cols; - MatrixType *buffer; }Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols); diff --git a/neuralNetwork.c b/neuralNetwork.c index bd8f164..ed804a6 100644 --- a/neuralNetwork.c +++ b/neuralNetwork.c @@ -74,7 +74,8 @@ static unsigned int readDimension(FILE *file) static Matrix readMatrix(FILE *file, unsigned int rows, unsigned int cols) { Matrix matrix = createMatrix(rows, cols); - + + if(matrix.buffer != NULL) { if(fread(matrix.buffer, sizeof(MatrixType), rows*cols, file) != rows*cols) diff --git a/neuralNetworkTests.c b/neuralNetworkTests.c index 36abca4..2c824cc 100644 --- a/neuralNetworkTests.c +++ b/neuralNetworkTests.c @@ -6,12 +6,54 @@ #include "neuralNetwork.h" -static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) -{ // TODO - +// Write a single dimension (unsigned int) in binary +static void writeDimension(FILE *file,int dim) { + if (fwrite(&dim, sizeof(int), 1, file) != 1) { + perror("Fehler beim Schreiben der Dimension"); + } } +static void writeMatrix(FILE *file, const Matrix *m) { + size_t count = m->rows * m->cols; + if (fwrite(m->buffer, sizeof(MatrixType), count, file) != count) { + perror("Fehler beim Schreiben der Matrix"); + } +} + +static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) { + FILE *file = fopen(path, "wb"); + if (!file) { + perror("Fehler beim Öffnen der Datei"); + return; + } + + // 1. Header (with null terminator) + const char *header = "__info2_neural_network_file_format__"; + fwrite(header, sizeof(char), strlen(header), file); + + // 2. Layer data + for (unsigned int i = 0; i < nn.numberOfLayers; ++i) { + const Layer *layer = &nn.layers[i]; + + // Input and output dimensions + writeDimension(file, layer->weights.cols); // inputDimension + writeDimension(file, layer->weights.rows); // outputDimension + + // Weights (buffer only) + writeMatrix(file, &layer->weights); + + // Biases (buffer only) + writeMatrix(file, &layer->biases); + } + + // 3. End marker + writeDimension(file, 0); + + fclose(file); +} + + void test_loadModelReturnsCorrectNumberOfLayers(void) { const char *path = "some__nn_test_file.info2";