forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
34cface815 |
No files matched your search
+1
-36
@@ -31,40 +31,5 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
// Überprüfen, ob die Matrizen gültig sind und ob ihre Dimensionen eine Multiplikation erlauben
|
|
||||||
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
|
|
||||||
{
|
|
||||||
return (Matrix){0};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ergebnismatrix erstellen
|
|
||||||
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
|
|
||||||
|
|
||||||
// Überprüfen, ob die Speicherreservierung erfolgreich war
|
|
||||||
if (result.buffer == NULL)
|
|
||||||
{
|
|
||||||
return (Matrix){0};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Jede Zeile der ersten Matrix durchlaufen
|
|
||||||
for (unsigned int i = 0; i < matrix1.rows; i++)
|
|
||||||
{
|
|
||||||
// Jede Spalte der zweiten Matrix durchlaufen
|
|
||||||
for (unsigned int j = 0; j < matrix2.cols; j++)
|
|
||||||
{
|
|
||||||
MatrixType sum = 0;
|
|
||||||
|
|
||||||
// Summe der Produkte berechnen
|
|
||||||
for (unsigned int k = 0; k < matrix1.cols; k++)
|
|
||||||
{
|
|
||||||
sum += matrix1.buffer[i * matrix1.cols + k] *
|
|
||||||
matrix2.buffer[k * matrix2.cols + j];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Berechneten Wert in der Ergebnismatrix speichern
|
|
||||||
result.buffer[i * result.cols + j] = sum;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
@@ -9,38 +9,53 @@
|
|||||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||||
{
|
{
|
||||||
// TODO
|
// TODO
|
||||||
FILE *file = fopen(path, "wb");
|
|
||||||
|
// Datei im binären Schreibmodus öffnen
|
||||||
|
FILE *file = fopen(path, "wb"); // wb = write binary (weights, biases, matrix dimensions)
|
||||||
|
|
||||||
|
// Beenden, wenn die Datei nicht geöffnet werden konnte
|
||||||
if (file == NULL)
|
if (file == NULL)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *fileTag = "__info2_neural_network_file_format__";
|
// Dateiformat-Kennung definieren und in die Datei schreiben
|
||||||
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
|
const char *fileTag = "__info2_neural_network_file_format__"; // string, Kennzeichnung
|
||||||
|
fwrite(fileTag, sizeof(char), strlen(fileTag), file); // Daten in eine Datei schreiben (data, size of char 1 byte, number of chars - null terminator, file)
|
||||||
|
|
||||||
|
// Überprüfen, ob das neuronale Netzwerk Layer enthält
|
||||||
if (nn.numberOfLayers > 0)
|
if (nn.numberOfLayers > 0)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
// Dimensionen des ersten Layers schreiben
|
||||||
fwrite(&nn.layers[0].weights.cols, sizeof(int), 1, file);
|
fwrite(&nn.layers[0].weights.cols, sizeof(int), 1, file);
|
||||||
fwrite(&nn.layers[0].weights.rows, sizeof(int), 1, file);
|
fwrite(&nn.layers[0].weights.rows, sizeof(int), 1, file);
|
||||||
|
|
||||||
|
// Alle Layer des neuronalen Netzwerks durchlaufen
|
||||||
for (unsigned int i = 0; i < nn.numberOfLayers; i++)
|
for (unsigned int i = 0; i < nn.numberOfLayers; i++)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
// Zeilenanzahl der nachfolgenden Layer schreiben
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
{
|
{
|
||||||
fwrite(&nn.layers[i].weights.rows, sizeof(int), 1, file);
|
fwrite(&nn.layers[i].weights.rows, sizeof(int), 1, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Alle Gewichte des aktuellen Layers schreiben
|
||||||
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType),
|
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType),
|
||||||
nn.layers[i].weights.rows * nn.layers[i].weights.cols, file);
|
nn.layers[i].weights.rows * nn.layers[i].weights.cols, file);
|
||||||
|
|
||||||
|
// Alle Bias-Werte des aktuellen Layers schreiben
|
||||||
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType),
|
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType),
|
||||||
nn.layers[i].biases.rows * nn.layers[i].biases.cols, file);
|
nn.layers[i].biases.rows * nn.layers[i].biases.cols, file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Null als Endmarkierung schreiben (Ende des Layers markieren)
|
||||||
int endDimension = 0;
|
int endDimension = 0;
|
||||||
fwrite(&endDimension, sizeof(int), 1, file);
|
fwrite(&endDimension, sizeof(int), 1, file);
|
||||||
|
|
||||||
|
// Datei schließen
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user