Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae2582cfbc | ||
|
|
7f980922d0 | ||
|
|
f807f8cc1a | ||
|
|
220db794b8 |
No files matched your search
@@ -0,0 +1,70 @@
|
|||||||
|
Informatik 2 Praktikum - Aufgabe 2: Neuronales Netz
|
||||||
|
Aufgabenverteilung
|
||||||
|
===================================================
|
||||||
|
|
||||||
|
Aufwand in Prozent vom Gesamtaufwand (inkl. optionaler Aufgabe),
|
||||||
|
geschaetzt anhand der Aufgabenstellung.
|
||||||
|
|
||||||
|
Gemeinsam (jeder einmal)
|
||||||
|
------------------------
|
||||||
|
- Projektstruktur anschauen: welche Module gibt es, welche Funktionen fehlen?
|
||||||
|
- Startprogramm bauen und testen:
|
||||||
|
make mnist_initial
|
||||||
|
./mnist mnist_test.info2 mnist_model.info2
|
||||||
|
|
||||||
|
|
||||||
|
Maxi - Matrix-Modul (ca. 35 %)
|
||||||
|
------------------------------
|
||||||
|
- Struktur "Matrix" an geeigneter Stelle (matrix.h) anlegen:
|
||||||
|
Anzahl Zeilen, Anzahl Spalten, Zeiger auf die Werte.
|
||||||
|
- Fehlende Funktionen in matrix.c implementieren (Hinweise aus den Unittests).
|
||||||
|
- Funktion add mit zwei Modi:
|
||||||
|
* Elementweise Addition (gleiche Groesse)
|
||||||
|
* Broadcasting: Matrix + Spaltenvektor (Spalten = 1, gleiche Zeilenanzahl),
|
||||||
|
Vektorwert jeder Zeile wird auf alle Elemente der Zeile addiert.
|
||||||
|
- Fertig, wenn gruen:
|
||||||
|
make matrixTests && ./runMatrixTests
|
||||||
|
- WICHTIG: Das Matrix-Struct moeglichst frueh pushen, David braucht es
|
||||||
|
fuer die Tests des neuronalen Netzes.
|
||||||
|
|
||||||
|
|
||||||
|
Hannes - Bilder einlesen (ca. 35 %)
|
||||||
|
-----------------------------------
|
||||||
|
- readImages in imageInput.c implementieren (Strukturen stehen in imageInput.h).
|
||||||
|
- Dateiformat (Binaerdatei, im Binaermodus "rb" oeffnen):
|
||||||
|
* String "__info2_image_file_format__"
|
||||||
|
* Anzahl Bilder (unsigned short)
|
||||||
|
* Breite (unsigned short)
|
||||||
|
* Hoehe (unsigned short)
|
||||||
|
* pro Bild: Breite*Hoehe Pixel (unsigned char, 0-255), dann Label (unsigned char, 0-9)
|
||||||
|
- Zuerst sinnvolle Hilfsfunktionen als static in imageInput.c schreiben
|
||||||
|
(z.B. Header pruefen, Header lesen, ein Bild lesen) und dafuer eigene
|
||||||
|
Unittests in imageInputTests.c ergaenzen.
|
||||||
|
- Danach readImages mit den Hilfsfunktionen zusammenbauen.
|
||||||
|
- Fertig, wenn gruen:
|
||||||
|
make imageInputTests && ./runImageInputTests
|
||||||
|
- Tipp: nach jedem Teilschritt testen und committen.
|
||||||
|
|
||||||
|
|
||||||
|
David - Neuronales Netz, Feinschliff, Optional (ca. 30 %)
|
||||||
|
---------------------------------------------------------
|
||||||
|
- prepareNeuralNetworkFile() in neuralNetworkTests.c implementieren (ca. 15 %):
|
||||||
|
Erzeugt eine gueltige Testdatei. Dateiformat in neuralNetwork.c nachlesen
|
||||||
|
(Lesefunktion) und genau spiegelbildlich schreiben.
|
||||||
|
Datei beginnt mit "__info2_neural_network_file_format__", danach die Schichten.
|
||||||
|
- Fertig, wenn gruen:
|
||||||
|
make neuralNetworkTests && ./runNeuralNetworkTests
|
||||||
|
- Feinschliff (ca. 5 %), wenn alle Teile da sind:
|
||||||
|
* Projekt kompiliert ohne Warnungen
|
||||||
|
* alle Unittests laufen durch
|
||||||
|
* make mnist && ./mnist mnist_test.info2 mnist_model.info2
|
||||||
|
- Optional (ca. 10 %): Ziffern mit der Maus malen und erkennen lassen,
|
||||||
|
in mnistVisualization.c mit Raylib.
|
||||||
|
|
||||||
|
|
||||||
|
Git-Regeln im Team
|
||||||
|
------------------
|
||||||
|
- Vor jedem Arbeiten: git pull
|
||||||
|
- Nur eigene Dateien committen: git add <datei> (nicht git add .)
|
||||||
|
- Ordner und Dateien NICHT verschieben oder umbenennen.
|
||||||
|
- .idea/ und cmake-build-*/ nicht committen.
|
||||||
+114
-3
@@ -2,34 +2,145 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
|
// ungueltiges ergebniss definieren
|
||||||
|
Matrix matrix = {NULL, 0, 0};
|
||||||
|
|
||||||
|
// fehlerfall wenn zeile oder spaltenanzahl = 0
|
||||||
|
if (rows == 0 || cols == 0)
|
||||||
|
{
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
// anfangsadresse fuer speicher ermittlen
|
||||||
|
matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType)); // malloc liefert freie adresse oder NULL wenn kein speicher frei
|
||||||
|
|
||||||
|
// wenn speicher frei -> zeilen und spalten weitergeben
|
||||||
|
if (matrix.buffer != NULL)
|
||||||
|
{
|
||||||
|
matrix.rows = rows;
|
||||||
|
matrix.cols = cols;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
|
// pruefen ob gueltige adresse übergeben wurde
|
||||||
|
if (matrix != NULL)
|
||||||
|
{
|
||||||
|
free(matrix->buffer); // speicher freigeben (*matrix).buffer = matrix->buffer
|
||||||
|
matrix->buffer = NULL; // zeiger auf NULL setzen
|
||||||
|
matrix->rows = 0; // zeilen und spalten auf 0 setzen (sonst absturz in neuralNetwork.c z. 45)
|
||||||
|
matrix->cols = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
if (rowIdx < matrix.rows && colIdx < matrix.cols)
|
||||||
|
{
|
||||||
|
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||||
|
}
|
||||||
|
|
||||||
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
// laut matrixTests muessen 3 versch faelle funktionieren:
|
||||||
|
// 1. beide matrizen gleich groß
|
||||||
|
// 2. matrix1 ist spaltenvektor (durch broadcasting wird spaltenvektor verbreitert durch kopieren der spalte)
|
||||||
|
// 3. matirx2 ist spaltenvektor ''
|
||||||
|
|
||||||
|
// ungueltiges ergebnis definieren
|
||||||
|
Matrix result = {NULL, 0, 0};
|
||||||
|
|
||||||
|
// 1. beide matrizen gleich groß
|
||||||
|
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols)
|
||||||
|
{
|
||||||
|
result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows; i++)
|
||||||
|
{
|
||||||
|
for (unsigned int j = 0; j < matrix1.cols; j++)
|
||||||
|
{
|
||||||
|
MatrixType summe = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
|
||||||
|
setMatrixAt(summe, result, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 2. matrix1 ist spaltenvektor
|
||||||
|
else if (matrix1.rows == matrix2.rows && matrix1.cols == 1)
|
||||||
|
{
|
||||||
|
result = createMatrix(matrix2.rows, matrix2.cols);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix2.rows; i++)
|
||||||
|
{
|
||||||
|
for (unsigned int j = 0; j < matrix2.cols; j++)
|
||||||
|
{
|
||||||
|
MatrixType summe = getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j);
|
||||||
|
setMatrixAt(summe, result, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 3. matirx2 ist spaltenvektor
|
||||||
|
else if (matrix1.rows == matrix2.rows && matrix2.cols == 1)
|
||||||
|
{
|
||||||
|
result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows; i++)
|
||||||
|
{
|
||||||
|
for (unsigned int j = 0; j < matrix1.cols; j++)
|
||||||
|
{
|
||||||
|
MatrixType summe = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0);
|
||||||
|
setMatrixAt(summe, result, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
Matrix result = {NULL, 0, 0};
|
||||||
|
|
||||||
|
// #spalten matrix1 muss = #zeilen matirx2 sein
|
||||||
|
if (matrix1.cols != matrix2.rows)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = createMatrix(matrix1.rows, matrix2.cols);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows; i++) // je unsigned weil rows und cols unsigned
|
||||||
|
{
|
||||||
|
for (unsigned int j = 0; j < matrix2.cols; j++)
|
||||||
|
{
|
||||||
|
MatrixType summe = 0;
|
||||||
|
for (unsigned int k = 0; k < matrix1.cols; k++)
|
||||||
|
{
|
||||||
|
summe += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
||||||
|
}
|
||||||
|
setMatrixAt(summe, result, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
+6
-2
@@ -5,8 +5,12 @@
|
|||||||
|
|
||||||
typedef float MatrixType;
|
typedef float MatrixType;
|
||||||
|
|
||||||
// TODO Matrixtyp definieren
|
typedef struct
|
||||||
|
{
|
||||||
|
MatrixType *buffer; // zeiger auf inhalt
|
||||||
|
unsigned int rows; // anzahl zeilen
|
||||||
|
unsigned int cols; // anzahl spalten
|
||||||
|
} Matrix;
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
void clearMatrix(Matrix *matrix);
|
void clearMatrix(Matrix *matrix);
|
||||||
|
|||||||
Reference in new issue
Block a user