Anpassung neuralNetworkTests
This commit is contained in:
parent
6d162b313c
commit
05fbd80b8c
@ -54,19 +54,28 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
|||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) { // Matrixen können nur addiert werden, sofern sie jeweils die gleiche Anzahl Spalten und Zeilen haben
|
size_t rows = (matrix1.rows > matrix2.rows) ? matrix1.rows : matrix2.rows;
|
||||||
|
size_t cols = (matrix1.cols > matrix2.cols) ? matrix1.cols : matrix2.cols;
|
||||||
|
|
||||||
|
// Prüfen, ob Broadcasting möglich ist
|
||||||
|
if (!((matrix1.rows == rows || matrix1.rows == 1) &&
|
||||||
|
(matrix2.rows == rows || matrix2.rows == 1) &&
|
||||||
|
(matrix1.cols == cols || matrix1.cols == 1) &&
|
||||||
|
(matrix2.cols == cols || matrix2.cols == 1))) {
|
||||||
Matrix errorMatrix = createMatrix(0, 0);
|
Matrix errorMatrix = createMatrix(0, 0);
|
||||||
errorMatrix.buffer = NULL;
|
|
||||||
return errorMatrix;
|
return errorMatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
Matrix result = createMatrix(rows, cols);
|
||||||
for (size_t i = 0; i < matrix1.rows; i++) {
|
|
||||||
for (size_t j = 0; j < matrix1.cols; j++) {
|
for (size_t i = 0; i < rows; i++) {
|
||||||
MatrixType sum = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
|
for (size_t j = 0; j < cols; j++) {
|
||||||
setMatrixAt(sum, result, i, j);
|
MatrixType val1 = matrix1.buffer[(i % matrix1.rows) * matrix1.cols + (j % matrix1.cols)];
|
||||||
|
MatrixType val2 = matrix2.buffer[(i % matrix2.rows) * matrix2.cols + (j % matrix2.cols)];
|
||||||
|
result.buffer[i * cols + j] = val1 + val2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -71,6 +71,32 @@ void test_addFailsOnDifferentInputDimensions(void)
|
|||||||
TEST_ASSERT_EQUAL_UINT32(0, result.cols);
|
TEST_ASSERT_EQUAL_UINT32(0, result.cols);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_addSupportsBroadcasting(void)
|
||||||
|
{
|
||||||
|
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
|
||||||
|
MatrixType buffer2[] = {7, 8};
|
||||||
|
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
|
||||||
|
Matrix matrix2 = {.rows=2, .cols=1, .buffer=buffer2};
|
||||||
|
|
||||||
|
Matrix result1 = add(matrix1, matrix2);
|
||||||
|
Matrix result2 = add(matrix2, matrix1);
|
||||||
|
|
||||||
|
float expectedResults[] = {8, 9, 10, 12, 13, 14};
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result1.rows);
|
||||||
|
TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result1.cols);
|
||||||
|
TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result2.rows);
|
||||||
|
TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result2.cols);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result1.rows * result1.cols);
|
||||||
|
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result1.buffer, result1.cols * result1.rows);
|
||||||
|
TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result2.rows * result2.cols);
|
||||||
|
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result2.buffer, result2.cols * result2.rows);
|
||||||
|
|
||||||
|
free(result1.buffer);
|
||||||
|
free(result2.buffer);
|
||||||
|
}
|
||||||
|
|
||||||
void test_multiplyReturnsCorrectResults(void)
|
void test_multiplyReturnsCorrectResults(void)
|
||||||
{
|
{
|
||||||
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
|
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
|
||||||
@ -159,6 +185,7 @@ int main()
|
|||||||
RUN_TEST(test_clearMatrixSetsMembersToNull);
|
RUN_TEST(test_clearMatrixSetsMembersToNull);
|
||||||
RUN_TEST(test_addReturnsCorrectResult);
|
RUN_TEST(test_addReturnsCorrectResult);
|
||||||
RUN_TEST(test_addFailsOnDifferentInputDimensions);
|
RUN_TEST(test_addFailsOnDifferentInputDimensions);
|
||||||
|
RUN_TEST(test_addSupportsBroadcasting);
|
||||||
RUN_TEST(test_multiplyReturnsCorrectResults);
|
RUN_TEST(test_multiplyReturnsCorrectResults);
|
||||||
RUN_TEST(test_multiplyFailsOnWrongInputDimensions);
|
RUN_TEST(test_multiplyFailsOnWrongInputDimensions);
|
||||||
RUN_TEST(test_getMatrixAtReturnsCorrectResult);
|
RUN_TEST(test_getMatrixAtReturnsCorrectResult);
|
||||||
|
|||||||
@ -8,7 +8,41 @@
|
|||||||
|
|
||||||
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) {
|
||||||
|
perror("Fehler beim Öffnen der Datei");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Header schreiben
|
||||||
|
const char *header = "__info2_neural_network_file_format__";
|
||||||
|
fwrite(header, sizeof(char), strlen(header), file);
|
||||||
|
|
||||||
|
// Alle Layer schreiben
|
||||||
|
for (unsigned int i = 0; i < nn.numberOfLayers; i++) {
|
||||||
|
Layer layer = nn.layers[i];
|
||||||
|
|
||||||
|
unsigned int inputDim = layer.weights.cols; // Anzahl Eingänge
|
||||||
|
unsigned int outputDim = layer.weights.rows; // Anzahl Ausgänge
|
||||||
|
|
||||||
|
// Dimensionen schreiben
|
||||||
|
fwrite(&inputDim, sizeof(unsigned int), 1, file);
|
||||||
|
fwrite(&outputDim, sizeof(unsigned int), 1, file);
|
||||||
|
|
||||||
|
// Gewichte schreiben
|
||||||
|
size_t weightCount = layer.weights.rows * layer.weights.cols;
|
||||||
|
fwrite(layer.weights.buffer, sizeof(MatrixType), weightCount, file);
|
||||||
|
|
||||||
|
// Biases schreiben
|
||||||
|
size_t biasCount = layer.biases.rows * layer.biases.cols;
|
||||||
|
fwrite(layer.biases.buffer, sizeof(MatrixType), biasCount, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// End-Marker (inputDim = 0)
|
||||||
|
unsigned int zero = 0;
|
||||||
|
fwrite(&zero, sizeof(unsigned int), 1, file);
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user