FinalVersion
This commit is contained in:
parent
a7f3969682
commit
3caafaca8c
@ -9,50 +9,48 @@
|
||||
|
||||
#define NN_HEADER "__info2_neural_network_file_format__\n"
|
||||
|
||||
//das auch zu machen
|
||||
static void writeMatrix(FILE *file, const Matrix *m) {
|
||||
unsigned short rows = (unsigned short)m->rows;
|
||||
unsigned short cols = (unsigned short)m->cols;
|
||||
|
||||
fwrite(&rows, sizeof(unsigned short), 1, file);
|
||||
fwrite(&cols, sizeof(unsigned short), 1, file);
|
||||
fwrite(m->buffer, sizeof(MatrixType), rows * cols, file);
|
||||
}
|
||||
|
||||
|
||||
//erste FKt war zu machen!
|
||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||
{
|
||||
// TODO
|
||||
FILE *file = fopen(path, "wb");
|
||||
if (!file) return;
|
||||
if (path == NULL || nn.layers == NULL || nn.numberOfLayers == 0)
|
||||
return;
|
||||
|
||||
// 1. Header (ohne \n!)
|
||||
const char *header = "__info2_neural_network_file_format__";
|
||||
fwrite(header, sizeof(char), strlen(header), file);
|
||||
FILE *file = fopen(path, "wb");
|
||||
if (file == NULL)
|
||||
return;
|
||||
|
||||
// 2. Schichten durchgehen
|
||||
for (unsigned int i = 0; i < nn.numberOfLayers; i++) {
|
||||
unsigned int inputDim = (unsigned int)nn.layers[i].weights.cols;
|
||||
unsigned int outputDim = (unsigned int)nn.layers[i].weights.rows;
|
||||
// 1) Header schreiben – muss zu FILE_HEADER_STRING in neuralNetwork.c passen
|
||||
const char *fileTag = "__info2_neural_network_file_format__";
|
||||
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
|
||||
|
||||
// 3. Dimensionen schreiben
|
||||
fwrite(&inputDim, sizeof(unsigned int), 1, file);
|
||||
fwrite(&outputDim, sizeof(unsigned int), 1, file);
|
||||
// 2) Erste Eingabedimension: Spalten der ersten Gewichtsmatrix
|
||||
int inputDim = (int)nn.layers[0].weights.cols;
|
||||
fwrite(&inputDim, sizeof(int), 1, file);
|
||||
|
||||
// 4. Matrizen schreiben
|
||||
writeMatrix(file, &nn.layers[i].weights);
|
||||
writeMatrix(file, &nn.layers[i].biases);
|
||||
// 3) Für jede Schicht: Output-Dimension + Gewichte + Biases schreiben
|
||||
for (unsigned int i = 0; i < nn.numberOfLayers; i++)
|
||||
{
|
||||
Matrix weights = nn.layers[i].weights;
|
||||
Matrix biases = nn.layers[i].biases;
|
||||
|
||||
int outputDim = (int)weights.rows;
|
||||
fwrite(&outputDim, sizeof(int), 1, file);
|
||||
|
||||
int nWeights = (int)(weights.rows * weights.cols);
|
||||
int nBiases = (int)(biases.rows * biases.cols);
|
||||
|
||||
fwrite(weights.buffer, sizeof(MatrixType), nWeights, file);
|
||||
fwrite(biases.buffer, sizeof(MatrixType), nBiases, file);
|
||||
}
|
||||
|
||||
// 5. Endsignal: letzte Output-Dimension = 0
|
||||
unsigned int zero = 0;
|
||||
fwrite(&zero, sizeof(unsigned int), 1, file);
|
||||
// 4) Terminierende 0-Dimension, damit loadModel die Schleife beendet
|
||||
int zero = 0;
|
||||
fwrite(&zero, sizeof(int), 1, file);
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
|
||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||
{
|
||||
const char *path = "some__nn_test_file.info2";
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user