NeuralNetworks auf Datei schreiben implementiert
This commit is contained in:
1 parent
fe70850d8b
commit
79402be355
1 file changed
+37
-2
+37
-2
@@ -4,13 +4,48 @@
|
||||
#include <math.h>
|
||||
#include "unity.h"
|
||||
#include "neuralNetwork.h"
|
||||
#define FILE_HEADER_STRING "__info2_neural_network_file_format__"
|
||||
|
||||
|
||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||
{
|
||||
// TODO
|
||||
FILE *file = fopen(path, "wb");
|
||||
if (file != NULL) {
|
||||
// Header schreiben
|
||||
fwrite(FILE_HEADER_STRING, sizeof(char), strlen(FILE_HEADER_STRING), file);
|
||||
|
||||
// cols der ersten Weights-Matrix schreiben
|
||||
if (nn.numberOfLayers > 0)
|
||||
{
|
||||
int inputDimension = (int)nn.layers[0].weights.cols;
|
||||
fwrite(&inputDimension, sizeof(int), 1, file);
|
||||
}
|
||||
|
||||
// Erst rows, dann weights, dann biases
|
||||
for (unsigned int i = 0; i < nn.numberOfLayers; i++)
|
||||
{
|
||||
int outputDimension = (int)nn.layers[i].weights.rows;
|
||||
fwrite(&outputDimension, sizeof(int), 1, file);
|
||||
|
||||
Matrix w = nn.layers[i].weights;
|
||||
Matrix b = nn.layers[i].biases;
|
||||
|
||||
fwrite(w.buffer, sizeof(MatrixType), (size_t)w.rows * (size_t)w.cols, file);
|
||||
fwrite(b.buffer, sizeof(MatrixType), (size_t)b.rows * (size_t)b.cols, file);
|
||||
}
|
||||
|
||||
// Endmarker schreiben, damit die while-Schleife in loadModel beendet wird
|
||||
int endMarker = 0;
|
||||
fwrite(&endMarker, sizeof(int), 1, file);
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||
{
|
||||
const char *path = "some__nn_test_file.info2";
|
||||
@@ -228,7 +263,7 @@ int main()
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
printf("\n============================\nNeural network tests\n============================\n");
|
||||
printf("\n============================\nNeural nn tests\n============================\n");
|
||||
RUN_TEST(test_loadModelReturnsCorrectNumberOfLayers);
|
||||
RUN_TEST(test_loadModelReturnsCorrectWeightDimensions);
|
||||
RUN_TEST(test_loadModelReturnsCorrectBiasDimensions);
|
||||
|
||||
Reference in new issue
Block a user