generated from freudenreichan/info2Praktikum-NeuronalesNetz
neusten Stand ziehen #5
6
matrix.c
6
matrix.c
@ -41,7 +41,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|||||||
// Erstellt eine neue Matrix mit gegebener Zeilen- und Spaltenanzahl
|
// Erstellt eine neue Matrix mit gegebener Zeilen- und Spaltenanzahl
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
Matrix matrix = {0, 0, NULL}; // Initialisierung mit leeren Werten
|
Matrix matrix = {NULL,0, 0}; // Initialisierung mit leeren Werten
|
||||||
if (rows == 0 || cols == 0) return matrix; // Ungültige Dimensionen → leere Matrix zurückgeben
|
if (rows == 0 || cols == 0) return matrix; // Ungültige Dimensionen → leere Matrix zurückgeben
|
||||||
|
|
||||||
matrix.rows = rows;
|
matrix.rows = rows;
|
||||||
@ -85,7 +85,7 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
|||||||
// Addiert zwei Matrizen miteinander (entweder elementweise oder mit Broadcasting)
|
// Addiert zwei Matrizen miteinander (entweder elementweise oder mit Broadcasting)
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
Matrix result = {0, 0, NULL};
|
Matrix result = {NULL,0, 0};
|
||||||
|
|
||||||
// Elementweise Addition: gleiche Dimensionen
|
// Elementweise Addition: gleiche Dimensionen
|
||||||
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) {
|
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) {
|
||||||
@ -120,7 +120,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
// Multipliziert zwei Matrizen miteinander (Matrixmultiplikation)
|
// Multipliziert zwei Matrizen miteinander (Matrixmultiplikation)
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
Matrix result = {0, 0, NULL};
|
Matrix result = {NULL,0, 0};
|
||||||
|
|
||||||
if (matrix1.cols != matrix2.rows) return result; // Dimensionen müssen passen: A.cols == B.rows
|
if (matrix1.cols != matrix2.rows) return result; // Dimensionen müssen passen: A.cols == B.rows
|
||||||
|
|
||||||
|
|||||||
2
matrix.h
2
matrix.h
@ -8,9 +8,9 @@ typedef float MatrixType;
|
|||||||
// TODO Matrixtyp definieren
|
// TODO Matrixtyp definieren
|
||||||
// *****************************************************
|
// *****************************************************
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
MatrixType *buffer;
|
||||||
unsigned int rows;
|
unsigned int rows;
|
||||||
unsigned int cols;
|
unsigned int cols;
|
||||||
MatrixType *buffer;
|
|
||||||
} Matrix;
|
} Matrix;
|
||||||
// *****************************************************
|
// *****************************************************
|
||||||
|
|
||||||
|
|||||||
@ -8,7 +8,38 @@
|
|||||||
|
|
||||||
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) {
|
||||||
|
fprintf(stderr, "Fehler: Datei konnte nicht geöffnet werden.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Header schreiben
|
||||||
|
const char *header = "__info2_neural_network_file_format__";
|
||||||
|
fwrite(header, sizeof(char), strlen(header), file);
|
||||||
|
|
||||||
|
// Erste Dimension: Input der ersten Schicht
|
||||||
|
unsigned int inputDim = nn.layers[0].weights.cols;
|
||||||
|
fwrite(&inputDim, sizeof(unsigned int), 1, file);
|
||||||
|
|
||||||
|
// Für jede Schicht
|
||||||
|
for (int i = 0; i < nn.numberOfLayers; i++) {
|
||||||
|
Layer layer = nn.layers[i];
|
||||||
|
|
||||||
|
// Output-Dimension
|
||||||
|
unsigned int outputDim = layer.weights.rows;
|
||||||
|
fwrite(&outputDim, sizeof(unsigned int), 1, file);
|
||||||
|
|
||||||
|
// Weights
|
||||||
|
fwrite(layer.weights.buffer, sizeof(MatrixType),
|
||||||
|
layer.weights.rows * layer.weights.cols, file);
|
||||||
|
|
||||||
|
// Biases
|
||||||
|
fwrite(layer.biases.buffer, sizeof(MatrixType),
|
||||||
|
layer.biases.rows * layer.biases.cols, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user