Bastian 2025-11-17 23:29:55 +01:00
commit 545acd0356
3 changed files with 66 additions and 13 deletions

View File

@ -8,19 +8,20 @@
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
Matrix matrix; Matrix matrix;
matrix.rows = rows; matrix.buffer = NULL;
matrix.cols = cols; matrix.rows = 0;
matrix.cols = 0;
if (rows == 0 || cols == 0) {
matrix.buffer = NULL;
return matrix;
}
matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType)); // Wenn die Dimensionen gültig sind, Speicher reservieren
if (matrix.buffer == NULL) if (rows > 0 && cols > 0)
{ {
matrix.rows = 0; matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
matrix.cols = 0; if (matrix.buffer != NULL)
{
matrix.rows = rows;
matrix.cols = cols;
}
} }
return matrix; return matrix;
} }

View File

@ -8,9 +8,10 @@ typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct typedef struct
{ {
MatrixType *buffer;
unsigned int rows; unsigned int rows;
unsigned int cols; unsigned int cols;
MatrixType *buffer;
} Matrix; } Matrix;

View File

@ -8,7 +8,58 @@
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) 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) void test_loadModelReturnsCorrectNumberOfLayers(void)