forked from freudenreichan/info2Praktikum-NeuronalesNetz
done?
This commit is contained in:
parent
44f0bfc16d
commit
7837be1c3e
@ -9,41 +9,60 @@
|
|||||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||||
{
|
{
|
||||||
FILE *file = fopen(path, "wb");
|
FILE *file = fopen(path, "wb");
|
||||||
if (file == NULL) {
|
if (!file) {
|
||||||
perror("Fehler beim Erstellen der Testdatei");
|
perror("Fehler beim Erstellen der Testdatei");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dateikopf speichern
|
// File header
|
||||||
const char *fileTag = "info2_neural_network_file_format";
|
const char *fileTag = "__info2_neural_network_file_format__";
|
||||||
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
|
fwrite(fileTag, strlen(fileTag), 1, file);
|
||||||
|
|
||||||
// Dimensionen der Eingabe und Ausgabe für den ersten Layer speichern
|
|
||||||
unsigned int inputDimension = nn.layers[0].weights.rows; // Eingabedimension ist die Anzahl der Eingabeneuronen im ersten Layer
|
|
||||||
unsigned int outputDimension = nn.layers[0].weights.cols; // Ausgabedimension ist die Anzahl der Ausgabeneuronen im ersten Layer
|
|
||||||
fwrite(&inputDimension, sizeof(unsigned int), 1, file);
|
|
||||||
fwrite(&outputDimension, sizeof(unsigned int), 1, file);
|
|
||||||
|
|
||||||
// Alle Layer speichern
|
if (nn.numberOfLayers == 0)
|
||||||
for (unsigned int i = 0; i < nn.numberOfLayers; i++) {
|
{
|
||||||
// Layer-Dimensionen speichern
|
unsigned int zero = 0;
|
||||||
inputDimension = nn.layers[i].weights.rows;
|
fwrite(&zero, sizeof(unsigned int), 1, file);
|
||||||
outputDimension = nn.layers[i].weights.cols;
|
fclose(file);
|
||||||
|
return;
|
||||||
fwrite(&inputDimension, sizeof(unsigned int), 1, file);
|
|
||||||
fwrite(&outputDimension, sizeof(unsigned int), 1, file);
|
|
||||||
|
|
||||||
// Gewichte speichern
|
|
||||||
fwrite(&nn.layers[i].weights.rows, sizeof(unsigned int), 1, file);
|
|
||||||
fwrite(&nn.layers[i].weights.cols, sizeof(unsigned int), 1, file);
|
|
||||||
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), nn.layers[i].weights.rows * nn.layers[i].weights.cols, file);
|
|
||||||
|
|
||||||
// Biases speichern
|
|
||||||
fwrite(&nn.layers[i].biases.rows, sizeof(unsigned int), 1, file);
|
|
||||||
fwrite(&nn.layers[i].biases.cols, sizeof(unsigned int), 1, file);
|
|
||||||
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), nn.layers[i].biases.rows * nn.layers[i].biases.cols, file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// first layer dimension
|
||||||
|
unsigned int in = nn.layers[0].weights.cols;
|
||||||
|
unsigned int out = nn.layers[0].weights.rows;
|
||||||
|
|
||||||
|
fwrite(&in, sizeof(unsigned int), 1, file);
|
||||||
|
fwrite(&out, sizeof(unsigned int), 1, file);
|
||||||
|
|
||||||
|
// do all layers
|
||||||
|
for (unsigned int i = 0; i < nn.numberOfLayers; i++)
|
||||||
|
{
|
||||||
|
const Layer *L = &nn.layers[i];
|
||||||
|
|
||||||
|
// Write weights matrix
|
||||||
|
fwrite(L->weights.buffer,
|
||||||
|
sizeof(MatrixType),
|
||||||
|
L->weights.rows * L->weights.cols,
|
||||||
|
file);
|
||||||
|
|
||||||
|
// Write biases matrix
|
||||||
|
fwrite(L->biases.buffer,
|
||||||
|
sizeof(MatrixType),
|
||||||
|
L->biases.rows * L->biases.cols,
|
||||||
|
file);
|
||||||
|
|
||||||
|
// After layer i, write dimension of next layer
|
||||||
|
if (i + 1 < nn.numberOfLayers)
|
||||||
|
{
|
||||||
|
unsigned int nextOut = nn.layers[i+1].weights.rows;
|
||||||
|
fwrite(&nextOut, sizeof(unsigned int), 1, file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- 5. Write terminating zero ---
|
||||||
|
unsigned int zero = 0;
|
||||||
|
fwrite(&zero, sizeof(unsigned int), 1, file);
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user