forked from freudenreichan/info2Praktikum-NeuronalesNetz
unittests bestanden
This commit is contained in:
parent
84b65525a6
commit
f4427d2892
@ -5,29 +5,44 @@
|
|||||||
#include "unity.h"
|
#include "unity.h"
|
||||||
#include "neuralNetwork.h"
|
#include "neuralNetwork.h"
|
||||||
|
|
||||||
|
static void writeLayer(FILE *file, const Matrix weights, const Matrix biases, unsigned int inputDim)
|
||||||
static void erzeugeMatrix(FILE *file, const Matrix *m)
|
|
||||||
{
|
{
|
||||||
fwrite(&m->rows, sizeof(int), 1, file);
|
unsigned int outputDim = (unsigned int)weights.rows;
|
||||||
fwrite(&m->cols, sizeof(int), 1, file);
|
fwrite(&outputDim, sizeof(unsigned int), 1, file);
|
||||||
fwrite(m->buffer, sizeof(MatrixType), m->rows * m->cols, file);
|
if (weights.buffer != NULL)
|
||||||
|
fwrite(weights.buffer, sizeof(MatrixType), outputDim * inputDim, file);
|
||||||
|
|
||||||
|
if (biases.buffer != NULL)
|
||||||
|
fwrite(biases.buffer, sizeof(MatrixType), outputDim, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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)
|
if (!file) return;
|
||||||
return;
|
|
||||||
|
|
||||||
const char *header = "__info2_neural_network_file_format__";
|
const char tag[] = "__info2_neural_network_file_format__";
|
||||||
fwrite(header, sizeof(char), strlen(header), file);
|
fwrite(tag, sizeof(char), strlen(tag), file);
|
||||||
fwrite(&nn.numberOfLayers, sizeof(int), 1, file);
|
|
||||||
|
if (nn.numberOfLayers == 0)
|
||||||
|
{
|
||||||
|
unsigned int zero = 0;
|
||||||
|
fwrite(&zero, sizeof(unsigned int), 1, file);
|
||||||
|
fclose(file);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int inputDim = (unsigned int)nn.layers[0].weights.cols;
|
||||||
|
fwrite(&inputDim, sizeof(unsigned int), 1, file);
|
||||||
|
|
||||||
for (int i = 0; i < nn.numberOfLayers; i++)
|
for (int i = 0; i < nn.numberOfLayers; i++)
|
||||||
{
|
{
|
||||||
erzeugeMatrix(file, &nn.layers[i].weights);
|
writeLayer(file, nn.layers[i].weights, nn.layers[i].biases, inputDim);
|
||||||
erzeugeMatrix(file, &nn.layers[i].biases);
|
inputDim = (unsigned int)nn.layers[i].weights.rows;
|
||||||
}
|
}
|
||||||
|
unsigned int zero = 0;
|
||||||
|
fwrite(&zero, sizeof(unsigned int), 1, file);
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user