Struktur für nNTest rauslesen

This commit is contained in:
Timo Hertel 2025-11-16 11:44:17 +01:00
parent ac084a5ad2
commit 8b6c64623d

View File

@ -67,14 +67,14 @@ static unsigned int readDimension(FILE *file)
if(fread(&dimension, sizeof(int), 1, file) != 1) if(fread(&dimension, sizeof(int), 1, file) != 1)
dimension = 0; dimension = 0;
return dimension; return dimension;
} }
static Matrix readMatrix(FILE *file, unsigned int rows, unsigned int cols) static Matrix readMatrix(FILE *file, unsigned int rows, unsigned int cols)
{ {
Matrix matrix = createMatrix(rows, cols); Matrix matrix = createMatrix(rows, cols);
if(matrix.buffer != NULL) if(matrix.buffer != NULL)
{ {
if(fread(matrix.buffer, sizeof(MatrixType), rows*cols, file) != rows*cols) if(fread(matrix.buffer, sizeof(MatrixType), rows*cols, file) != rows*cols)
@ -126,13 +126,16 @@ NeuralNetwork loadModel(const char *path)
if(file != NULL) if(file != NULL)
{ {
if(checkFileHeader(file)) if(checkFileHeader(file)) // __info2_neural_network_file_format__
{ {
unsigned int inputDimension = readDimension(file); unsigned int inputDimension = readDimension(file); // ein int
unsigned int outputDimension = readDimension(file); unsigned int outputDimension = readDimension(file);// noch ein int
while(inputDimension > 0 && outputDimension > 0) while(inputDimension > 0 && outputDimension > 0)
{ {
// ließt zwei Matritzen (je eine Zeile mit allen Werten hintereinander, durch Leerzeichen getrennt)
// 1. Matrix: weights
// 2. Matrix: biases
Layer layer = readLayer(file, inputDimension, outputDimension); Layer layer = readLayer(file, inputDimension, outputDimension);
Layer *layerBuffer = NULL; Layer *layerBuffer = NULL;
@ -142,7 +145,7 @@ NeuralNetwork loadModel(const char *path)
clearModel(&model); clearModel(&model);
break; break;
} }
layerBuffer = (Layer *)realloc(model.layers, (model.numberOfLayers + 1) * sizeof(Layer)); layerBuffer = (Layer *)realloc(model.layers, (model.numberOfLayers + 1) * sizeof(Layer));
if(layerBuffer != NULL) if(layerBuffer != NULL)
@ -201,7 +204,7 @@ static Matrix forward(const NeuralNetwork model, Matrix inputBatch)
{ {
Matrix biasResult; Matrix biasResult;
Matrix weightResult; Matrix weightResult;
weightResult = multiply(model.layers[i].weights, result); weightResult = multiply(model.layers[i].weights, result);
clearMatrix(&result); clearMatrix(&result);
biasResult = add(model.layers[i].biases, weightResult); biasResult = add(model.layers[i].biases, weightResult);
@ -248,9 +251,9 @@ unsigned char *predict(const NeuralNetwork model, const GrayScaleImage images[],
Matrix outputBatch = forward(model, inputBatch); Matrix outputBatch = forward(model, inputBatch);
unsigned char *result = argmax(outputBatch); unsigned char *result = argmax(outputBatch);
clearMatrix(&outputBatch); clearMatrix(&outputBatch);
return result; return result;
} }
@ -265,4 +268,4 @@ void clearModel(NeuralNetwork *model)
model->layers = NULL; model->layers = NULL;
model->numberOfLayers = 0; model->numberOfLayers = 0;
} }
} }