Compare commits

...

4 Commits

6 changed files with 163 additions and 66 deletions

View File

@ -39,8 +39,8 @@ mnistVisualization.o: mnistVisualization.c
matrixTests: matrix.o matrixTests.c matrixTests: matrix.o matrixTests.c
$(CC) $(CFLAGS) -I$(unityfolder) -o runMatrixTests matrixTests.c matrix.o $(BINARIES)/libunity.a $(CC) $(CFLAGS) -I$(unityfolder) -o runMatrixTests matrixTests.c matrix.o $(BINARIES)/libunity.a
neuralNetworkTests: neuralNetwork.o neuralNetworkTests.c neuralNetworkTests: neuralNetwork.o matrix.o neuralNetworkTests.c
$(CC) $(CFLAGS) -I$(unityfolder) -o runNeuralNetworkTests neuralNetworkTests.c neuralNetwork.o $(BINARIES)/libunity.a $(CC) $(CFLAGS) -I$(unityfolder) -o runNeuralNetworkTests neuralNetworkTests.c neuralNetwork.o matrix.o $(BINARIES)/libunity.a
imageInputTests: imageInput.o imageInputTests.c imageInputTests: imageInput.o imageInputTests.c
$(CC) $(CFLAGS) -I$(unityfolder) -o runImageInputTests imageInputTests.c imageInput.o $(BINARIES)/libunity.a $(CC) $(CFLAGS) -I$(unityfolder) -o runImageInputTests imageInputTests.c imageInput.o $(BINARIES)/libunity.a

View File

@ -12,64 +12,128 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
Matrix matrix; Matrix matrix;
matrix.rows = rows; matrix.rows = rows;
matrix.cols = cols; matrix.cols = cols;
matrix.data = EMPTY_CHAR; matrix.buffer = EMPTY_CHAR;
if(rows == 0 || cols == 0)
{
Matrix emptyMatix = {
.rows = 0,
.cols = 0,
.buffer = NULL};
return emptyMatix;
}
if(rows > 0 && cols > 0) if(rows > 0 && cols > 0)
{ {
matrix.data = (MatrixType*) calloc(rows * cols, sizeof(MatrixType)); matrix.buffer = (MatrixType*) calloc(rows * cols, sizeof(MatrixType));
} }
return matrix; return matrix;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
if(matrix && matrix->data) if(matrix && matrix->buffer)
{ {
free(matrix->data); free(matrix->buffer);
matrix->data = EMPTY_CHAR; matrix->buffer = EMPTY_CHAR;
matrix->rows = 0; matrix->rows = 0;
matrix->cols = 0; matrix->cols = 0;
} }
} }
void setMatrixAt(MatrixType value, Matrix *matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(MatrixType value, Matrix *matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if(matrix && matrix->data && rowIdx < matrix->rows && colIdx < matrix->cols) if(matrix && matrix->buffer && rowIdx < matrix->rows && colIdx < matrix->cols)
{ {
matrix->data[rowIdx * matrix->cols + colIdx] = value; matrix->buffer[rowIdx * matrix->cols + colIdx] = value;
} }
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if(matrix.data && rowIdx < matrix.rows && colIdx < matrix.cols) if(matrix.buffer && rowIdx < matrix.rows && colIdx < matrix.cols)
{ {
return matrix.data[rowIdx * matrix.cols + colIdx]; return matrix.buffer[rowIdx * matrix.cols + colIdx];
} }
return UNDEFINED_MATRIX_VALUE; return UNDEFINED_MATRIX_VALUE;
} }
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) unsigned int resRows = 0;
unsigned int resCols = 0;
//Ergebniszeilenbestimmung
if(matrix1.rows == matrix2.rows)
{
resRows = matrix1.rows;
}else if(matrix1.rows == 1)
{
resRows = matrix2.rows;
}else if(matrix2.rows == 1)
{
resRows = matrix1.rows;
}else
{ {
return createMatrix(0, 0); return createMatrix(0, 0);
} }
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
for(unsigned int i = 0; i < result.rows; ++i) //Ergebnisspaltenbestimmung
if(matrix1.cols == matrix2.cols)
{ {
for(unsigned int j = 0; j < result.cols; ++j) resCols = matrix1.cols;
}else if(matrix1.cols == 1)
{ {
MatrixType val = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j); resCols = matrix2.cols;
}else if(matrix2.cols == 1)
{
resCols = matrix1.cols;
}else
{
return createMatrix(0,0);
}
//Ergebnismatrix
Matrix result = createMatrix(resRows, resCols);
if(result.buffer == NULL && (resRows > 0 && resCols > 0))
{
return createMatrix(0, 0);
}
for(unsigned int i = 0; i < resRows; ++i)
{
for(unsigned int j = 0; i < resCols; ++j)
{
unsigned int i1 = (matrix1.rows == 1) ? 0 : i;
unsigned int j1 = (matrix1.cols == 1) ? 0 : j;
unsigned int i2 = (matrix2.rows == 1) ? 0 : i;
unsigned int j2 = (matrix2.cols == 1) ? 0 : j;
MatrixType val = getMatrixAt(matrix1, i1, j1) + getMatrixAt(matrix2, i2, j2);
setMatrixAt(val, &result, i, j); setMatrixAt(val, &result, i, j);
} }
} }
return result; return result;
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
if(matrix1.cols != matrix2.rows) if(matrix1.cols != matrix2.rows)
@ -92,15 +156,3 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
return result; return result;
} }
//Vergleich der MatrixReihen/Zeilen
// if((sizeof(matrix1) / sizeof(matrix1[0])) == sizeof(matrix2) / sizeof(matrix2[0]))
// {
// if(sizeof(matrix1[0]) / sizeof(matrix1[0][0]) == sizeof(matrix2[0] / sizeof(matrix2[0][0])))
// {
// }
//}

View File

@ -8,7 +8,7 @@ typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct { typedef struct {
MatrixType* data; MatrixType* buffer;
unsigned int cols; unsigned int cols;
unsigned int rows; unsigned int rows;
}Matrix; }Matrix;

View File

@ -153,7 +153,7 @@ void test_setMatrixAtSetsCorrectValue(void)
MatrixType buffer[] = {1, 2, 3, 4, 5, 6}; MatrixType buffer[] = {1, 2, 3, 4, 5, 6};
Matrix matrixUnderTest = {.rows=2, .cols=3, .buffer=buffer}; Matrix matrixUnderTest = {.rows=2, .cols=3, .buffer=buffer};
setMatrixAt(expectedResult, matrixUnderTest, 1, 2); setMatrixAt(expectedResult, &matrixUnderTest, 1, 2);
TEST_ASSERT_EQUAL_INT(expectedResult, buffer[5]); TEST_ASSERT_EQUAL_INT(expectedResult, buffer[5]);
} }
@ -163,7 +163,7 @@ void test_setMatrixAtFailsOnIndicesOutOfRange(void)
MatrixType buffer[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; MatrixType buffer[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer}; Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer};
setMatrixAt(-1, matrixToTest, 2, 3); setMatrixAt(-1, &matrixToTest, 2, 3);
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, matrixToTest.cols * matrixToTest.rows); TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, matrixToTest.cols * matrixToTest.rows);
} }

View File

@ -20,7 +20,7 @@ static void softmax(Matrix *matrix)
for(int rowIdx = 0; rowIdx < matrix->rows; rowIdx++) for(int rowIdx = 0; rowIdx < matrix->rows; rowIdx++)
{ {
MatrixType expValue = exp(getMatrixAt(*matrix, rowIdx, colIdx)); MatrixType expValue = exp(getMatrixAt(*matrix, rowIdx, colIdx));
setMatrixAt(expValue, *matrix, rowIdx, colIdx); setMatrixAt(expValue, matrix, rowIdx, colIdx);
colSums[colIdx] += expValue; colSums[colIdx] += expValue;
} }
} }
@ -30,7 +30,7 @@ static void softmax(Matrix *matrix)
for(int rowIdx = 0; rowIdx < matrix->rows; rowIdx++) for(int rowIdx = 0; rowIdx < matrix->rows; rowIdx++)
{ {
MatrixType normalizedValue = getMatrixAt(*matrix, rowIdx, colIdx) / colSums[colIdx]; MatrixType normalizedValue = getMatrixAt(*matrix, rowIdx, colIdx) / colSums[colIdx];
setMatrixAt(normalizedValue, *matrix, rowIdx, colIdx); setMatrixAt(normalizedValue, matrix, rowIdx, colIdx);
} }
} }
free(colSums); free(colSums);
@ -182,7 +182,7 @@ static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], un
{ {
for(int j = 0; j < images[i].width * images[i].height; j++) for(int j = 0; j < images[i].width * images[i].height; j++)
{ {
setMatrixAt((MatrixType)images[i].buffer[j], matrix, j, i); setMatrixAt((MatrixType)images[i].buffer[j], &matrix, j, i);
} }
} }
} }

View File

@ -23,49 +23,94 @@
// 2) Stellen Sie sicher, dass alle Unittests erfolgreich durchlaufen. // 2) Stellen Sie sicher, dass alle Unittests erfolgreich durchlaufen.
// make neuralNetworkTests && ./runNeuralNetworkTests // make neuralNetworkTests && ./runNeuralNetworkTests
// static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
// {
// // First Draft
//
// // 1. Datei im binären Schreibmodus öffnen
// FILE *file = fopen(path, "wb");
// if (file == NULL) {
// perror("Fehler beim Öffnen der Datei");
// return;
// }
//
// // 2. Den Identifikations-Tag schreiben
// const char *fileTag = "__info2_neural_network_file_format__";
// fwrite(fileTag, sizeof(char), strlen(fileTag), file);
//
// // 3. Die Anzahl der Schichten schreiben
// fwrite(&nn.numberOfLayers, sizeof(int), 1, file);
//
// // 4. Schleife über alle Schichten, um deren Daten zu schreiben
// for (int i = 0; i < nn.numberOfLayers; i++) {
// Layer currentLayer = nn.layers[i];
//
// // 4a. Daten der Gewichts-Matrix (weights) schreiben
// Matrix weights = currentLayer.weights;
// int weightElements = weights.rows * weights.cols;
//
// // Schreibe Dimensionen (Zeilen, Spalten)
// fwrite(&weights.rows, sizeof(int), 1, file);
// fwrite(&weights.cols, sizeof(int), 1, file);
// // Schreibe den Daten-Buffer (die eigentlichen Zahlen)
// fwrite(weights.buffer, sizeof(MatrixType), weightElements, file);
//
// // 4b. Daten der Bias-Matrix (biases) schreiben
// Matrix biases = currentLayer.biases;
// int biasElements = biases.rows * biases.cols;
//
// // Schreibe Dimensionen (Zeilen, Spalten)
// fwrite(&biases.rows, sizeof(int), 1, file);
// fwrite(&biases.cols, sizeof(int), 1, file);
// // Schreibe den Daten-Buffer
// fwrite(biases.buffer, sizeof(MatrixType), biasElements, file);
// }
// // 5. Datei schließen
// fclose(file);
// }
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
{ {
// First Draft
// 1. Datei im binären Schreibmodus öffnen
FILE *file = fopen(path, "wb"); FILE *file = fopen(path, "wb");
if (file == NULL) { if (file == NULL) {
perror("Fehler beim Öffnen der Datei");
return; return;
} }
// 2. Den Identifikations-Tag schreiben // 1. Header schreiben
const char *fileTag = "__info2_neural_network_file_format__"; const char *fileHeader = "__info2_neural_network_file_format__";
fwrite(fileTag, sizeof(char), strlen(fileTag), file); fwrite(fileHeader, sizeof(char), strlen(fileHeader), file);
// 3. Die Anzahl der Schichten schreiben // Prüfen ob Layer existieren
fwrite(&nn.numberOfLayers, sizeof(int), 1, file); if (nn.numberOfLayers > 0)
{
// 2. Die Input-Dimension der ALLERERSTEN Schicht schreiben
// (Das sind die Spalten der Gewichtsmatrix der ersten Schicht)
int inputDim = nn.layers[0].weights.cols;
fwrite(&inputDim, sizeof(int), 1, file);
// 4. Schleife über alle Schichten, um deren Daten zu schreiben // 3. Durch alle Schichten iterieren
for (int i = 0; i < nn.numberOfLayers; i++) { for(int i = 0; i < nn.numberOfLayers; i++)
Layer currentLayer = nn.layers[i]; {
// Die Output-Dimension dieser Schicht schreiben (Zeilen der Gewichtsmatrix)
int outputDim = nn.layers[i].weights.rows;
fwrite(&outputDim, sizeof(int), 1, file);
// 4a. Daten der Gewichts-Matrix (weights) schreiben // 4. Gewichte (Weights) schreiben (nur den Buffer, keine Dimensionen mehr!)
Matrix weights = currentLayer.weights; // loadModel weiß durch inputDim und outputDim schon, wie groß die Matrix ist.
int weightElements = weights.rows * weights.cols; int weightsCount = nn.layers[i].weights.rows * nn.layers[i].weights.cols;
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), weightsCount, file);
// Schreibe Dimensionen (Zeilen, Spalten) // 5. Biases schreiben (nur den Buffer)
fwrite(&weights.rows, sizeof(int), 1, file); int biasCount = nn.layers[i].biases.rows * nn.layers[i].biases.cols;
fwrite(&weights.cols, sizeof(int), 1, file); fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), biasCount, file);
// Schreibe den Daten-Buffer (die eigentlichen Zahlen)
fwrite(weights.buffer, sizeof(MatrixType), weightElements, file);
// 4b. Daten der Bias-Matrix (biases) schreiben
Matrix biases = currentLayer.biases;
int biasElements = biases.rows * biases.cols;
// Schreibe Dimensionen (Zeilen, Spalten)
fwrite(&biases.rows, sizeof(int), 1, file);
fwrite(&biases.cols, sizeof(int), 1, file);
// Schreibe den Daten-Buffer
fwrite(biases.buffer, sizeof(MatrixType), biasElements, file);
} }
// 5. Datei schließen }
// 6. Eine 0 schreiben, um das Ende der Dimensionen zu signalisieren
// (loadModel bricht die while-Schleife ab, wenn readDimension 0 liefert)
int stopMark = 0;
fwrite(&stopMark, sizeof(int), 1, file);
fclose(file); fclose(file);
} }