Compare commits
11
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d9f7a2ab3f | ||
|
|
57708089a4 | ||
|
|
0d41f5e631 | ||
|
|
e1284e9efc | ||
|
|
bea1545564 | ||
|
|
0ee879ba20 | ||
|
|
4a31d6388d | ||
|
|
240cc77288 | ||
|
|
b6069a0a73 | ||
|
|
fc2b14d0e0 | ||
|
|
954318593c |
No files matched your search
+65
-6
@@ -1,35 +1,94 @@
|
||||
#include "matrix.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "matrix.h"
|
||||
|
||||
// TODO Matrix-Funktionen implementieren
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
|
||||
Matrix matrix;
|
||||
matrix.rows = rows;
|
||||
matrix.cols = cols;
|
||||
matrix.buffer = NULL;
|
||||
if (matrix.rows == 0 || matrix.cols == 0)
|
||||
{
|
||||
matrix.rows = 0;
|
||||
matrix.cols = 0;
|
||||
return matrix;
|
||||
}
|
||||
matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
|
||||
return matrix;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
|
||||
free(matrix->buffer);
|
||||
matrix->buffer = NULL;
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if ((rowIdx < matrix.rows) && (colIdx < matrix.cols))
|
||||
{
|
||||
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if ((rowIdx < matrix.rows) || (colIdx < matrix.cols))
|
||||
{
|
||||
MatrixType MatrixAt = matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||
return MatrixAt;
|
||||
}
|
||||
return UNDEFINED_MATRIX_VALUE;
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
unsigned int resRows = (matrix1.rows >= matrix2.rows) ? matrix1.rows : matrix2.rows;
|
||||
unsigned int resCols = (matrix1.cols >= matrix2.cols) ? matrix1.cols : matrix2.cols;
|
||||
|
||||
Matrix MatrixAdd = createMatrix(resRows, resCols);
|
||||
if ((matrix1.rows == matrix2.rows || matrix1.rows == 1 || matrix2.rows == 1) && (matrix1.cols == matrix2.cols || (matrix1.cols == 1 || matrix2.cols == 1)) && matrix1.rows > 0 && matrix1.cols > 0 && matrix2.rows > 0 && matrix2.cols > 0)
|
||||
{
|
||||
for (int i = 0; i < resRows; i++)
|
||||
{
|
||||
for (int j = 0; j < resCols; j++)
|
||||
{
|
||||
MatrixType val1 = getMatrixAt(matrix1, i % matrix1.rows, j % matrix1.cols);
|
||||
MatrixType val2 = getMatrixAt(matrix2, i % matrix2.rows, j % matrix2.cols);
|
||||
setMatrixAt(val1 + val2, MatrixAdd, i, j);
|
||||
}
|
||||
}
|
||||
return MatrixAdd;
|
||||
}
|
||||
return createMatrix(0, 0);
|
||||
}
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
Matrix MatrixMult = createMatrix(matrix1.rows, matrix2.cols);
|
||||
if (matrix1.cols == matrix2.rows)
|
||||
{
|
||||
for (int i = 0; i < matrix1.rows; i++)
|
||||
{
|
||||
for (int j = 0; j < matrix2.cols; j++)
|
||||
{
|
||||
MatrixType skalar = 0;
|
||||
for (int k = 0; k < matrix1.cols; k++)
|
||||
{
|
||||
MatrixType val1 = getMatrixAt(matrix1, i, k);
|
||||
MatrixType val2 = getMatrixAt(matrix2, k, j);
|
||||
skalar += val1 * val2;
|
||||
}
|
||||
setMatrixAt(skalar, MatrixMult, i, j);
|
||||
}
|
||||
}
|
||||
return MatrixMult;
|
||||
}
|
||||
return createMatrix(0, 0);
|
||||
}
|
||||
+6
-2
@@ -6,7 +6,12 @@
|
||||
typedef float MatrixType;
|
||||
|
||||
// TODO Matrixtyp definieren
|
||||
|
||||
typedef struct Matrix
|
||||
{
|
||||
unsigned int rows;
|
||||
unsigned int cols;
|
||||
MatrixType *buffer;
|
||||
} Matrix;
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||
void clearMatrix(Matrix *matrix);
|
||||
@@ -15,5 +20,4 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2);
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -170,7 +170,7 @@ NeuralNetwork loadModel(const char *path)
|
||||
|
||||
static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count)
|
||||
{
|
||||
Matrix matrix = {NULL, 0, 0};
|
||||
Matrix matrix = createMatrix(0, 0);
|
||||
|
||||
if(count > 0 && images != NULL)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,31 @@
|
||||
|
||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||
{
|
||||
// TODO
|
||||
FILE *file = fopen(path, "wb");
|
||||
if (file != NULL) {
|
||||
const char *fileHeader = "__info2_neural_network_file_format__";
|
||||
fwrite(fileHeader, sizeof(char), strlen(fileHeader), file);
|
||||
|
||||
if (nn.numberOfLayers > 0 && nn.layers != NULL) {
|
||||
|
||||
int inputDimension = nn.layers[0].weights.cols;
|
||||
fwrite(&inputDimension, sizeof(int), 1, file);
|
||||
|
||||
for (unsigned int i = 0; i < nn.numberOfLayers; i++) {
|
||||
int outputDimension = nn.layers[i].weights.rows;
|
||||
fwrite(&outputDimension, sizeof(int), 1, file);
|
||||
|
||||
int weightsCount = nn.layers[i].weights.rows * nn.layers[i].weights.cols;
|
||||
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), weightsCount, file);
|
||||
|
||||
int biasesCount = nn.layers[i].biases.rows * nn.layers[i].biases.cols;
|
||||
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), biasesCount, file);
|
||||
}
|
||||
int endSignal = 0;
|
||||
fwrite(&endSignal, sizeof(int), 1, file);
|
||||
}
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
|
||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||
|
||||
Reference in new issue
Block a user