forked from freudenreichan/info2Praktikum-NeuronalesNetz
neuralNetworkTests mit Kommentaren
This commit is contained in:
parent
da4eaa718d
commit
7aa57191da
32
matrix.c
32
matrix.c
@ -89,30 +89,6 @@ Matrix broadCastRows(const Matrix matrix, const unsigned int rows,
|
|||||||
}
|
}
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
||||||
Matrix result;
|
|
||||||
const int cols1 = matrix1.cols;
|
|
||||||
const int rows1 = matrix1.rows;
|
|
||||||
const int cols2 = matrix2.cols;
|
|
||||||
const int rows2 = matrix2.rows;
|
|
||||||
const int colsEqu = (matrix1.cols == matrix2.cols) ? 1 : 0;
|
|
||||||
const int rowsEqu = (matrix1.rows == matrix2.rows) ? 1 : 0;
|
|
||||||
if(colsEqu && rowsEqu)
|
|
||||||
{
|
|
||||||
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
|
||||||
for(int i = 0; i < rows1; i++)
|
|
||||||
{
|
|
||||||
for (int j = 0; j < cols1; j++)
|
|
||||||
{
|
|
||||||
int valueM1 = getMatrixAt(matrix1, i, j);
|
|
||||||
int valueM2 =getMatrixAt(matrix2, i, j);
|
|
||||||
int sum = valueM1 + valueM2;
|
|
||||||
setMatrixAt(sum, result,i,j);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
<<<<<<< HEAD
|
|
||||||
// Broadcasting nur bei Vektor und Matrix, Fehlermeldung bei zwei unpassenden
|
|
||||||
// Matrizen
|
|
||||||
|
|
||||||
const unsigned int rows1 = matrix1.rows;
|
const unsigned int rows1 = matrix1.rows;
|
||||||
const unsigned int rows2 = matrix2.rows;
|
const unsigned int rows2 = matrix2.rows;
|
||||||
@ -233,15 +209,11 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
printf(
|
printf("Fehlermeldung"); // vielleicht Fehlermeldung ändern zu
|
||||||
"Fehlermeldung"); // vielleicht Fehlermeldung ändern zu Programmabbruch
|
// Programmabbruch
|
||||||
Matrix error = {0, 0, NULL};
|
Matrix error = {0, 0, NULL};
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
>>>>>>> 7f3c6d1d3f7d4ee08747df6a4999e1908f5dd56d
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2) {
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2) {
|
||||||
|
|||||||
@ -5,28 +5,50 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
/*typedef struct
|
||||||
|
{
|
||||||
|
Matrix weights;
|
||||||
|
Matrix biases;
|
||||||
|
ActivationFunctionType activation;
|
||||||
|
} Layer;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
Layer *layers;
|
||||||
|
unsigned int numberOfLayers;
|
||||||
|
} NeuralNetwork;*/
|
||||||
|
|
||||||
|
/*Layer: Ebene im neuronalen Netzwerk, besteht aus mehreren Neuronen
|
||||||
|
Input-Layer: Eingabedatei
|
||||||
|
Hidden-Layer: verarbeiten die Daten
|
||||||
|
Output-Layer: Ergebnis
|
||||||
|
|
||||||
|
|
||||||
|
Gewichte: bestimmen, wie stark ein Eingangssignal auf ein Neuron wirkt
|
||||||
|
|
||||||
|
Dimension: Form der Matrizen für einen Layer*/
|
||||||
|
|
||||||
|
// speichert NeuralNetwork nn in binäre Datei->erzeugt Dateiformat
|
||||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
|
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
|
||||||
FILE *f = fopen(path, "wb");
|
FILE *f = fopen(path, "wb"); // Binärdatei zum Schreiben öffnen
|
||||||
if (f == NULL)
|
if (f == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* 1) Header: exakt das String, ohne '\n' oder abschließendes '\0' */
|
// Header ist Erkennungsstring am Anfang der Datei, loadmodel erkennt
|
||||||
|
// Dateiformat
|
||||||
const char header[] = "__info2_neural_network_file_format__";
|
const char header[] = "__info2_neural_network_file_format__";
|
||||||
fwrite(header, sizeof(char), strlen(header), f);
|
fwrite(header, sizeof(char), strlen(header), f);
|
||||||
|
|
||||||
/* Wenn es keine Layer gibt, kein Dimensionspaar schreiben (loadModel
|
// Wenn es keine Layer gibt, 0 eintragen, LoadModel gibt 0 zurück
|
||||||
wird beim Lesen dann 0 zurückgeben). Aber wir können auch frühzeitig
|
|
||||||
mit einem 0-Int terminieren — beides ist in Ordnung. */
|
|
||||||
if (nn.numberOfLayers == 0) {
|
if (nn.numberOfLayers == 0) {
|
||||||
/* optional: schreibe ein 0 als next outputDimension (nicht nötig) */
|
|
||||||
int zero = 0;
|
int zero = 0;
|
||||||
fwrite(&zero, sizeof(int), 1, f);
|
fwrite(&zero, sizeof(int), 1, f);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 2) Für die erste Layer schreiben wir inputDimension und outputDimension */
|
// Layer 0, inputDimension: Anzahl Input-Neuronen, outputDimension: Anzahl
|
||||||
/* inputDimension == weights.cols, outputDimension == weights.rows */
|
// Output-Neuronen
|
||||||
int inputDim = (int)nn.layers[0].weights.cols;
|
int inputDim = (int)nn.layers[0].weights.cols;
|
||||||
int outputDim = (int)nn.layers[0].weights.rows;
|
int outputDim = (int)nn.layers[0].weights.rows;
|
||||||
fwrite(&inputDim, sizeof(int), 1, f);
|
fwrite(&inputDim, sizeof(int), 1, f);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user