forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3a9d8275a8
|
||
|
|
bbb0ea1cf5
|
||
|
|
12825cc1d3
|
||
|
|
6ba9ba3195 | ||
|
|
f4427d2892 | ||
|
|
84b65525a6 |
@@ -67,14 +67,18 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
return createMatrix(0, 0);
|
return createMatrix(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (matrix1.cols != matrix2.cols)
|
// matrices not compatible
|
||||||
{
|
|
||||||
if (matrix1.rows != matrix2.rows)
|
if (matrix1.rows != matrix2.rows)
|
||||||
{
|
{
|
||||||
clearMatrix(&resMat);
|
clearMatrix(&resMat);
|
||||||
return resMat;
|
return resMat;
|
||||||
}
|
}
|
||||||
else if (matrix1.cols == 1)
|
|
||||||
|
// check if broadcasting is possible
|
||||||
|
if (matrix1.cols != matrix2.cols)
|
||||||
|
{
|
||||||
|
// matrix1 is a vector
|
||||||
|
if (matrix1.cols == 1)
|
||||||
{
|
{
|
||||||
// broadcast vector
|
// broadcast vector
|
||||||
for (size_t m = 0; m < matrix2.rows; m++)
|
for (size_t m = 0; m < matrix2.rows; m++)
|
||||||
@@ -86,6 +90,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
}
|
}
|
||||||
return resMat;
|
return resMat;
|
||||||
}
|
}
|
||||||
|
// matrix2 is a vector
|
||||||
else if (matrix2.cols == 1)
|
else if (matrix2.cols == 1)
|
||||||
{
|
{
|
||||||
// broadcast vector
|
// broadcast vector
|
||||||
@@ -98,6 +103,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
}
|
}
|
||||||
return resMat;
|
return resMat;
|
||||||
}
|
}
|
||||||
|
// addition not possible
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
clearMatrix(&resMat);
|
clearMatrix(&resMat);
|
||||||
|
|||||||
+37
-1
@@ -5,10 +5,46 @@
|
|||||||
#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)
|
||||||
|
{
|
||||||
|
unsigned int outputDim = (unsigned int)weights.rows;
|
||||||
|
fwrite(&outputDim, sizeof(unsigned int), 1, 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)
|
||||||
{
|
{
|
||||||
// TODO
|
FILE *file = fopen(path, "wb");
|
||||||
|
if (!file) return;
|
||||||
|
|
||||||
|
const char tag[] = "__info2_neural_network_file_format__";
|
||||||
|
fwrite(tag, sizeof(char), strlen(tag), 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++)
|
||||||
|
{
|
||||||
|
writeLayer(file, nn.layers[i].weights, nn.layers[i].biases, inputDim);
|
||||||
|
inputDim = (unsigned int)nn.layers[i].weights.rows;
|
||||||
|
}
|
||||||
|
unsigned int zero = 0;
|
||||||
|
fwrite(&zero, sizeof(unsigned int), 1, file);
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||||
|
|||||||
Reference in New Issue
Block a user