generated from freudenreichan/info2Praktikum-NeuronalesNetz
Pflichtaufgaben fertig: Matrix-Fixes, Bilder einlesen, NN-Testdatei
- matrix.c: multiply summiert in MatrixType statt int (Nachkommastellen gingen verloren), clearMatrix gibt Speicher frei, Indexpruefung pro Zeile/Spalte, NULL-Pruefungen in add/multiply - imageInput.c: fseek-Spruenge entfernt, Dateikopf pruefen, Lesefehler behandeln, clearSeries implementiert - neuralNetworkTests.c: prepareNeuralNetworkFile implementiert - neuralNetwork.c: clearModel gibt das Layer-Array frei (Speicherleck) - Zusaetzliche Unittests fuer Matrix und Bildeinlesen - Testartefakte und Testprogramme in .gitignore, testFile.info2 entfernt Alle 33 Tests gruen, keine Warnungen, 87.37 % Genauigkeit (wie Referenz). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
570e3fa930
commit
4d6a86f293
8 files changed
+326
-87
No files matched your search
+60
-28
@@ -8,7 +8,8 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
Matrix m;
|
||||
|
||||
if (rows == 0 || cols == 0) //Ueberprüfen, ob ungültige Werte für Matrix vorhanden
|
||||
if (rows == 0 || cols == 0)
|
||||
//Ueberprüfen, ob ungültige Werte für Matrix vorhanden
|
||||
{
|
||||
m.rows = 0;
|
||||
m.cols = 0;
|
||||
@@ -18,21 +19,40 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
|
||||
m.rows = rows;
|
||||
m.cols = cols;
|
||||
m.buffer = (MatrixType*)calloc(rows*cols, sizeof(MatrixType)); //Bei Fehlschalg gibt calloc entweder NULL oder Zeiger verschieden von NULL aus
|
||||
m.buffer = (MatrixType*)calloc(rows*cols, sizeof(MatrixType));
|
||||
//Bei Fehlschalg gibt calloc entweder NULL oder Zeiger verschieden von NULL aus
|
||||
|
||||
if (m.buffer == NULL)
|
||||
//Kein Speicher: leere Matrix zurueckgeben, damit Groesse und Puffer zusammenpassen
|
||||
{
|
||||
m.rows = 0;
|
||||
m.cols = 0;
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
matrix->buffer = 0;
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
if (matrix != NULL)
|
||||
{
|
||||
free(matrix->buffer);
|
||||
//Speicher leeren
|
||||
matrix->buffer = NULL;
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int isValidIndex(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
//Zeile und Spalte einzeln pruefen: sonst wuerde z.B. (0, cols) faelschlich auf (1, 0) zugreifen
|
||||
return matrix.buffer != NULL && rowIdx < matrix.rows && colIdx < matrix.cols;
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
if((rowIdx * matrix.cols + colIdx) < (matrix.rows*matrix.cols))
|
||||
if(isValidIndex(matrix, rowIdx, colIdx))
|
||||
{
|
||||
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
||||
}
|
||||
@@ -40,8 +60,8 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
MatrixType value = 0;
|
||||
if((rowIdx * matrix.cols + colIdx) < (matrix.rows*matrix.cols))
|
||||
MatrixType value = UNDEFINED_MATRIX_VALUE;
|
||||
if(isValidIndex(matrix, rowIdx, colIdx))
|
||||
{
|
||||
value = matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||
}
|
||||
@@ -53,28 +73,38 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
typedef enum {elemt_add, broadcast} Addiermodi;
|
||||
Addiermodi modus;
|
||||
|
||||
if((matrix1.cols == 1 || matrix2.cols ==1) && (matrix1.rows == matrix2.rows))
|
||||
Matrix m = {NULL, 0, 0};
|
||||
|
||||
if(matrix1.buffer == NULL || matrix2.buffer == NULL)
|
||||
{
|
||||
modus = broadcast;
|
||||
return m;
|
||||
}
|
||||
else if((matrix1.rows == matrix2.rows) && (matrix1.cols == matrix2.cols))
|
||||
|
||||
//Elementweise zuerst pruefen: zwei Spaltenvektoren gleicher Groesse sind ein normaler elementweiser Fall
|
||||
if((matrix1.rows == matrix2.rows) && (matrix1.cols == matrix2.cols))
|
||||
{
|
||||
modus = elemt_add;
|
||||
}
|
||||
else if((matrix1.cols == 1 || matrix2.cols ==1) && (matrix1.rows == matrix2.rows))
|
||||
{
|
||||
modus = broadcast;
|
||||
}
|
||||
else
|
||||
{
|
||||
Matrix failure = {NULL, 0, 0};
|
||||
return failure;
|
||||
return m;
|
||||
}
|
||||
|
||||
Matrix m;
|
||||
if(modus == elemt_add)
|
||||
{
|
||||
m = createMatrix(matrix1.rows, matrix1.cols);
|
||||
|
||||
for(int i = 0; i< matrix1.rows; i++)
|
||||
if(m.buffer == NULL)
|
||||
{
|
||||
for(int j = 0; j < matrix1.cols ; j++)
|
||||
return m;
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i< matrix1.rows; i++)
|
||||
{
|
||||
for(unsigned int j = 0; j < matrix1.cols ; j++)
|
||||
{
|
||||
m.buffer[i*matrix1.cols+j] = matrix1.buffer[i*matrix1.cols+j] + matrix2.buffer[i*matrix1.cols+j];
|
||||
}
|
||||
@@ -87,9 +117,9 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
if(matrix1.cols == 1)
|
||||
{
|
||||
m = createMatrix(matrix2.rows, matrix2.cols);
|
||||
for(int i = 0; i< matrix2.rows; i++)
|
||||
for(unsigned int i = 0; i< m.rows; i++)
|
||||
{
|
||||
for(int j = 0; j < matrix2.cols ; j++)
|
||||
for(unsigned int j = 0; j < m.cols ; j++)
|
||||
{
|
||||
m.buffer[i*matrix2.cols+j] = matrix2.buffer[i*matrix2.cols+j] + matrix1.buffer[i*matrix1.cols];
|
||||
}
|
||||
@@ -98,9 +128,9 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
else
|
||||
{
|
||||
m = createMatrix(matrix1.rows, matrix1.cols);
|
||||
for(int i = 0; i< matrix1.rows; i++)
|
||||
for(unsigned int i = 0; i< m.rows; i++)
|
||||
{
|
||||
for(int j = 0; j < matrix1.cols ; j++)
|
||||
for(unsigned int j = 0; j < m.cols ; j++)
|
||||
{
|
||||
m.buffer[i*matrix1.cols+j] = matrix1.buffer[i*matrix1.cols+j] + matrix2.buffer[i*matrix2.cols];
|
||||
}
|
||||
@@ -112,7 +142,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
if(matrix1.cols != matrix2.rows)
|
||||
if(matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
|
||||
{
|
||||
Matrix failure = {NULL, 0, 0};
|
||||
return failure;
|
||||
@@ -120,16 +150,18 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
|
||||
Matrix m = createMatrix(matrix1.rows, matrix2.cols);
|
||||
|
||||
for(int i = 0; i < matrix1.rows; i++)
|
||||
//Reihenfolge i-k-j statt i-j-k: matrix2 und m werden zeilenweise am Stueck durchlaufen.
|
||||
//m startet dank calloc mit 0, daher wird direkt aufaddiert.
|
||||
for(unsigned int i = 0; i < m.rows; i++)
|
||||
{
|
||||
for(int j = 0; j < matrix2.cols ; j++)
|
||||
for(unsigned int k = 0; k < matrix1.cols; k++)
|
||||
{
|
||||
int value = 0;
|
||||
for(int k = 0; k < matrix1.cols; k++)
|
||||
MatrixType factor = matrix1.buffer[i*matrix1.cols+k];
|
||||
//MatrixType statt int, hat die zahlen gerundet.
|
||||
for(unsigned int j = 0; j < m.cols ; j++)
|
||||
{
|
||||
value += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k , j);
|
||||
m.buffer[i*m.cols+j] += factor * matrix2.buffer[k*matrix2.cols+j];
|
||||
}
|
||||
m.buffer[i*matrix2.cols+j] = value;
|
||||
}
|
||||
}
|
||||
return m;
|
||||
|
||||
Reference in new issue
Block a user