All tests Pass Programm works
This commit is contained in:
parent
4b2cbfb836
commit
49977a86c5
@ -153,7 +153,7 @@ NeuralNetwork loadModel(const char *path)
|
|||||||
unsigned int inputDimension = readDimension(file);
|
unsigned int inputDimension = readDimension(file);
|
||||||
unsigned int outputDimension = readDimension(file);
|
unsigned int outputDimension = readDimension(file);
|
||||||
|
|
||||||
fprintf(stderr, "[loadModel] first dims: input=%u output=%u\n", inputDimension, outputDimension);
|
//fprintf(stderr, "[loadModel] first dims: input=%u output=%u\n", inputDimension, outputDimension);
|
||||||
|
|
||||||
while (inputDimension > 0 && outputDimension > 0)
|
while (inputDimension > 0 && outputDimension > 0)
|
||||||
{
|
{
|
||||||
@ -177,16 +177,16 @@ NeuralNetwork loadModel(const char *path)
|
|||||||
model.layers[model.numberOfLayers] = layer;
|
model.layers[model.numberOfLayers] = layer;
|
||||||
model.numberOfLayers++;
|
model.numberOfLayers++;
|
||||||
|
|
||||||
fprintf(stderr, "[loadModel] loaded layer %d: weights %u x %u, biases %u x %u\n",
|
/* fprintf(stderr, "[loadModel] loaded layer %d: weights %u x %u, biases %u x %u\n",
|
||||||
model.numberOfLayers,
|
model.numberOfLayers,
|
||||||
layer.weights.rows, layer.weights.cols,
|
layer.weights.rows, layer.weights.cols,
|
||||||
layer.biases.rows, layer.biases.cols);
|
layer.biases.rows, layer.biases.cols); */
|
||||||
|
|
||||||
/* Lese das nächste Dimensions-Paar (writer schreibt für jede Schicht ein Paar) */
|
/* Lese das nächste Dimensions-Paar (writer schreibt für jede Schicht ein Paar) */
|
||||||
unsigned int nextInput = readDimension(file);
|
unsigned int nextInput = readDimension(file);
|
||||||
unsigned int nextOutput = readDimension(file);
|
unsigned int nextOutput = readDimension(file);
|
||||||
|
|
||||||
fprintf(stderr, "[loadModel] next raw dims read: nextInput=%u nextOutput=%u\n", nextInput, nextOutput);
|
//fprintf(stderr, "[loadModel] next raw dims read: nextInput=%u nextOutput=%u\n", nextInput, nextOutput);
|
||||||
|
|
||||||
/* Wenn nächstes Paar (0,0) -> Ende */
|
/* Wenn nächstes Paar (0,0) -> Ende */
|
||||||
if (nextInput == 0 || nextOutput == 0)
|
if (nextInput == 0 || nextOutput == 0)
|
||||||
@ -200,7 +200,7 @@ NeuralNetwork loadModel(const char *path)
|
|||||||
inputDimension = nextInput;
|
inputDimension = nextInput;
|
||||||
outputDimension = nextOutput;
|
outputDimension = nextOutput;
|
||||||
|
|
||||||
fprintf(stderr, "[loadModel] next dims: input=%u output=%u\n", inputDimension, outputDimension);
|
//fprintf(stderr, "[loadModel] next dims: input=%u output=%u\n", inputDimension, outputDimension);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|||||||
@ -10,47 +10,46 @@
|
|||||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||||
{
|
{
|
||||||
// TODO
|
// TODO
|
||||||
FILE *file = fopen(path, "wb"); // Binärmodus zum Schreiben öffnen
|
FILE *file = fopen(path, "wb");
|
||||||
|
if (!file) return;
|
||||||
|
|
||||||
if (file != NULL)
|
// 1) Header-Tag WORTGENAU (OHNE Nullterminator) schreiben
|
||||||
|
fwrite(FILE_HEADER_STRING, sizeof(char), strlen(FILE_HEADER_STRING), file);
|
||||||
|
|
||||||
|
// 2) Für jeden Layer die Paare (inputDim, outputDim) und dann Gewichte & Biases schreiben
|
||||||
|
// inputDim == weights.cols
|
||||||
|
// outputDim == weights.rows
|
||||||
|
for (unsigned int i = 0; i < nn.numberOfLayers; ++i)
|
||||||
{
|
{
|
||||||
// 1. Identifikationstag schreiben
|
const Layer *lay = &nn.layers[i];
|
||||||
const char *fileTag = FILE_HEADER_STRING;
|
|
||||||
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
|
|
||||||
|
|
||||||
// 2. Schichten (Layer) sequenziell schreiben
|
unsigned int inputDim = lay->weights.cols;
|
||||||
for (unsigned int i = 0; i < nn.numberOfLayers; i++)
|
unsigned int outputDim = lay->weights.rows;
|
||||||
{
|
|
||||||
const Layer currentLayer = nn.layers[i];
|
|
||||||
|
|
||||||
unsigned int inputDimension = currentLayer.weights.cols;
|
// schreibe Dimensionen (4-Byte int / unsigned int)
|
||||||
unsigned int outputDimension = currentLayer.weights.rows;
|
fwrite(&inputDim, sizeof(unsigned int), 1, file);
|
||||||
|
fwrite(&outputDim, sizeof(unsigned int), 1, file);
|
||||||
|
|
||||||
// Schreibe Input Dimension. Wichtig: Nutze sizeof(int)
|
// schreibe Gewichtsmatrix (row-major: rows*cols Elemente vom Typ MatrixType)
|
||||||
// da readDimension in neuralNetwork.c in einen int liest.
|
size_t weightCount = (size_t)lay->weights.rows * (size_t)lay->weights.cols;
|
||||||
fwrite(&inputDimension, sizeof(int), 1, file);
|
if (weightCount > 0 && lay->weights.buffer != NULL) {
|
||||||
|
fwrite(lay->weights.buffer, sizeof(MatrixType), weightCount, file);
|
||||||
// Schreibe Output Dimension.
|
|
||||||
fwrite(&outputDimension, sizeof(int), 1, file);
|
|
||||||
|
|
||||||
// Schreibe Gewichtsmatrix (Weights) Daten
|
|
||||||
size_t numWeights = currentLayer.weights.rows * currentLayer.weights.cols;
|
|
||||||
fwrite(currentLayer.weights.buffer, sizeof(MatrixType), numWeights, file);
|
|
||||||
|
|
||||||
// Schreibe Biasmatrix (Biases) Daten
|
|
||||||
size_t numBiases = currentLayer.biases.rows * currentLayer.biases.cols;
|
|
||||||
fwrite(currentLayer.biases.buffer, sizeof(MatrixType), numBiases, file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Ende des Modells signalisieren
|
// schreibe Biasmatrix (rows * cols) - normalerweise rows x 1
|
||||||
|
size_t biasCount = (size_t)lay->biases.rows * (size_t)lay->biases.cols;
|
||||||
|
if (biasCount > 0 && lay->biases.buffer != NULL) {
|
||||||
|
fwrite(lay->biases.buffer, sizeof(MatrixType), biasCount, file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3) Endmarkierung: nächstes Input / Output = 0, damit loadModel() die Schleife beendet
|
||||||
unsigned int zero = 0;
|
unsigned int zero = 0;
|
||||||
// Wichtig: Auch hier sizeof(int) verwenden
|
fwrite(&zero, sizeof(unsigned int), 1, file);
|
||||||
fwrite(&zero, sizeof(int), 1, file); // Input Dimension = 0
|
fwrite(&zero, sizeof(unsigned int), 1, file);
|
||||||
fwrite(&zero, sizeof(int), 1, file); // Output Dimension = 0
|
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user