24 Commits
Author SHA1 Message Date
Max-R fb72278d55 Merge branch 'main' into RMax 2025-11-25 11:12:36 +01:00
Kristin f1af6c1e4a Merge branch 'Krisp2' 2025-11-25 10:49:32 +01:00
Kristin 9606b5a03e kp 2025-11-25 10:48:24 +01:00
Kristin 3c4e4df496 Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/kachelto100370/info2Praktikum-NeuronalesNetz into Krisp2 2025-11-25 10:41:30 +01:00
Kristin fd1bc886a7 neu 2025-11-25 10:38:40 +01:00
Max-R efa260ccbe 0 fehler bei add abfangen 2025-11-25 10:15:52 +01:00
Max-R 4a1b6cbb40 Fehler 0 Matrix bei add abfangen 2025-11-25 10:13:01 +01:00
Max-R 801abc1b66 Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/kachelto100370/info2Praktikum-NeuronalesNetz 2025-11-25 10:00:31 +01:00
Max-R 8e518a3bdd Merge branch 'RMax' matrix.c voll 2025-11-25 09:58:03 +01:00
Max-R 0baf646832 add files to gitignore 2025-11-25 09:57:28 +01:00
kachelto100370 98dd789680 input image things 2025-11-25 09:10:54 +01:00
Max-R 21d9b5c01d so finde ich es schöner... 2025-11-22 15:29:32 +01:00
Max-R e7930c7eb0 kommentaare update 2025-11-22 15:23:50 +01:00
Max-R 5075c34983 kommentaare update 2025-11-22 15:19:41 +01:00
Max-R b187a13b17 multiply, besteht MatrixTests 2025-11-22 15:17:12 +01:00
Max-R 4e2ee7078a alles bis uf multiply 2025-11-22 12:41:46 +01:00
Max-R 35a598a276 broadcasting 2025-11-22 11:54:32 +01:00
Max-R e1ea9f33cd create matrix mit null 2025-11-22 10:55:27 +01:00
Max-R 0886489d49 Matrix noch ohne broadcasting 2025-11-20 16:03:44 +01:00
Max-R f9c46a6784 Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/kachelto100370/info2Praktikum-NeuronalesNetz into RMax 2025-11-20 14:50:33 +01:00
Max-R 5fcc3cd042 Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/kachelto100370/info2Praktikum-NeuronalesNetz into RMax 2025-11-18 10:51:55 +01:00
Max-R 3de79e2b83 clearMatrix füllen 2025-11-11 11:05:28 +01:00
Max-R ec54bdd951 create Matrix gefüllt, test unit 2025-11-11 10:36:11 +01:00
Max-R 0e3f03a03d Matrix definiert 2025-11-11 09:20:40 +01:00
8 changed files with 277 additions and 183 deletions
+3
View File
@@ -2,3 +2,6 @@ mnist
runTests
*.o
*.exe
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/settings.json
+18
View File
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "windows-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "C:/ProgramData/mingw64/mingw64/bin/gcc.exe",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "windows-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}
+24
View File
@@ -0,0 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": true,
"cwd": "c:/Users/Max-R/I2Pr/repoKachelto/I2-Pr_neuronalesNetz/info2Praktikum-NeuronalesNetz",
"program": "c:/Users/Max-R/I2Pr/repoKachelto/I2-Pr_neuronalesNetz/info2Praktikum-NeuronalesNetz/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
+57 -1
View File
@@ -1,3 +1,59 @@
{
"makefile.configureOnOpen": false
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
}
+3
View File
@@ -28,6 +28,7 @@ static int readMeta(FILE *file, unsigned short *count, unsigned short *width,
return 0;
if (fread(height, sizeof(unsigned short), 1, file) != 1)
return 0;
return 1;
}
@@ -74,9 +75,11 @@ GrayScaleImageSeries *readImages(const char *path) {
unsigned short count, width, height;
if (!readMeta(file, &count, &width, &height)) {
fclose(file);
return NULL;
}
// printf("%d, %d, %d", count, width, height);
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
if (!series) {
+1
View File
@@ -63,3 +63,4 @@ ifeq ($(OS),Windows_NT)
else
rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests
endif
+154 -166
View File
@@ -3,35 +3,22 @@
#include <stdlib.h>
#include <string.h>
// TODO Matrix-Funktionen implementieren
/*typedef struct {
unsigned int rows; //Zeilen
unsigned int cols; //Spalten
MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten
} Matrix;*/
Matrix createMatrix(unsigned int rows, unsigned int cols) {
Matrix matrix;
Matrix errorMatrix = {0, 0, NULL};
if (rows == 0 || cols == 0) {
Matrix createMatrix(unsigned int rows, unsigned int cols) {
if (cols == 0 || rows == 0) {
Matrix errorMatrix = {0, 0, NULL};
return errorMatrix;
}
matrix.rows = rows;
matrix.cols = cols;
matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
if (matrix.buffer == NULL) {
matrix.rows = 0;
matrix.cols = 0;
return matrix;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix.buffer[i * matrix.cols + j] = UNDEFINED_MATRIX_VALUE;
}
}
return matrix;
MatrixType *buffer =
malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren, malloc
// liefert Zeiger auf Speicher
Matrix newMatrix = {rows, cols, buffer}; // neue Matrix nach struct
return newMatrix;
}
void clearMatrix(Matrix *matrix) {
@@ -47,14 +34,13 @@ void setMatrixAt(const MatrixType value, Matrix matrix,
const unsigned int rowIdx, // Kopie der Matrix wird übergeben
const unsigned int colIdx) {
if (rowIdx >= matrix.rows || colIdx >= matrix.cols ||
matrix.buffer == NULL) { // Speichergröße nicht überschreiten
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) {
// Speichergröße nicht überschreiten
return;
}
matrix.buffer[rowIdx * matrix.cols + colIdx] =
value; // rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte
// innerhalb der Zeile
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
// rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte
// innerhalb der Zeile
}
MatrixType getMatrixAt(const Matrix matrix,
unsigned int rowIdx, // Kopie der Matrix wird übergeben
@@ -68,187 +54,189 @@ MatrixType getMatrixAt(const Matrix matrix,
return value;
}
Matrix broadCastCols(const Matrix matrix, const unsigned int rows,
const unsigned int cols) {
Matrix copy = createMatrix(
rows, cols); // Matrix 1 Kopie erstellen mit Dimensionen von Matrix2
for (int r = 0; r < rows; r++) {
MatrixType value = getMatrixAt(matrix, r, 0);
Matrix broadcastingCols(const Matrix matrix, const unsigned int cols) {
Matrix copy1 = createMatrix(matrix.rows, cols);
for (int r = 0; r < matrix.rows; r++) {
MatrixType valueMatrix1 = getMatrixAt(matrix, r, 0);
for (int c = 0; c < cols; c++) {
setMatrixAt(value, copy, r, c);
setMatrixAt(valueMatrix1, copy1, r, c);
}
}
return copy;
return copy1;
}
Matrix broadCastRows(const Matrix matrix, const unsigned int rows,
const unsigned int cols) {
Matrix copy = createMatrix(rows, cols);
for (int c = 0; c < cols; c++) {
MatrixType value = getMatrixAt(matrix, 0, c);
Matrix broadcastingRows(const Matrix matrix, const unsigned int rows) {
Matrix copy1 = createMatrix(rows, matrix.cols);
for (int c = 0; c < matrix.cols; c++) {
MatrixType valueMatrix1 = getMatrixAt(matrix, 0, c);
for (int r = 0; r < rows; r++) {
setMatrixAt(value, copy, r, c);
setMatrixAt(valueMatrix1, copy1, r, c);
}
}
return copy;
return copy1;
}
Matrix add(const Matrix matrix1, const Matrix matrix2) {
const unsigned int rows1 = matrix1.rows;
const unsigned int rows2 = matrix2.rows;
const unsigned int cols1 = matrix1.cols;
const unsigned int cols2 = matrix2.cols;
// Ergebnismatrix
Matrix result;
const int cols1 = matrix1.cols;
const int rows1 = matrix1.rows;
const int cols2 = matrix2.cols;
const int rows2 = matrix2.rows;
const int rowsEqual = ((rows1 == rows2) ? 1 : 0);
const int rowsEqual = (matrix1.rows == matrix2.rows) ? 1 : 0;
const int colsEqual = (matrix1.cols == matrix2.cols) ? 1 : 0;
const int colsEqual = ((cols1 == cols2) ? 1 : 0);
if (rowsEqual && colsEqual) // addieren
{
Matrix result = createMatrix(rows1, cols1); // Speicher reservieren
// Broadcasting nur bei Vektor und Matrix, Fehlermeldung bei zwei unpassender
// Matrix
if (rowsEqual == 1 && colsEqual == 1) {
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
<<<<<<< HEAD
if (result.buffer == NULL){
return (Matrix){0,0,NULL};
}
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);
=======
if (result.buffer == NULL) {
return (Matrix){0, 0, NULL};
}
for (int i = 0; i < (rows1 * cols1); i++) { // addieren
result.buffer[i] =
(matrix1.buffer[i] +
matrix2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
// Startadresse + (i * sizeof(MatrixType))
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);
>>>>>>> main
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
}
}
return result; // zurückgeben
}
else if (rowsEqual && !colsEqual) {
if (cols1 == 1) {
Matrix result = createMatrix(rows2, cols2);
return result;
} else if (rowsEqual == 1 && (cols1 == 1 || cols2 == 1)) {
if (cols1 == 1) { // broadcasting von vektor 1 zu matrix 1, add
Matrix newMatrix = broadcastingCols(matrix1, cols2);
// add
Matrix result = createMatrix(newMatrix.rows, newMatrix.cols);
<<<<<<< HEAD
if (result.buffer == NULL){
return (Matrix){0,0,NULL};
}
for (int i = 0; i< rows1; i++) {
for (int j= 0; j< cols2; j++){
int valueM1= getMatrixAt(newMatrix, i, j);
int valueM2= getMatrixAt(matrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
=======
if (result.buffer == NULL) {
return (Matrix){0, 0, NULL};
>>>>>>> main
}
Matrix copy1 = broadCastCols(matrix1, rows2, cols2);
if (!copy1.buffer) {
clearMatrix(&result);
return (Matrix){0, 0, NULL};
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
int valueM1 = getMatrixAt(newMatrix, i, j);
int valueM2 = getMatrixAt(matrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
}
}
for (unsigned int i = 0; i < rows2 * cols2; i++) {
result.buffer[i] = copy1.buffer[i] + matrix2.buffer[i];
}
/* freigeben, weil nicht mehr benötigt */
clearMatrix(&copy1);
return result;
// add und return
} else if (cols2 == 1) {
Matrix result = createMatrix(rows1, cols1);
} else {
Matrix newMatrix2 = broadcastingCols(matrix2, cols1);
// add
Matrix result = createMatrix(newMatrix2.rows, newMatrix2.cols);
<<<<<<< HEAD
if (result.buffer == NULL){
return (Matrix){0,0,NULL};
}
for (int i = 0; i< rows1; i++) {
for (int j= 0; j< cols1; j++){
int valueM1= getMatrixAt(matrix1, i, j);
int valueM2= getMatrixAt(newMatrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
=======
if (result.buffer == NULL) {
Matrix error = {0, 0, NULL};
return error;
return (Matrix){0, 0, NULL};
>>>>>>> main
}
// Matrix2 hat nur eine Spalte -> horizontal broadcasten
Matrix copy2 = broadCastCols(matrix2, rows1, cols1);
for (unsigned int i = 0; i < rows1 * cols1; i++) {
result.buffer[i] = matrix1.buffer[i] + copy2.buffer[i];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
int valueM1 = getMatrixAt(matrix1, i, j);
int valueM2 = getMatrixAt(newMatrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
}
}
// Optional: Speicher von copy2 freigeben
clearMatrix(&copy2);
return result;
}
else {
printf("Fehlermeldung"); // vielleicht Fehlermeldung ändern zu
// Programmabbruch
Matrix error = {0, 0, NULL};
return error;
}
}
else if (!rowsEqual && colsEqual) {
else if ((rows1 == 1 || rows2 == 1) && colsEqual == 1) {
if (rows1 == 1) {
Matrix result = createMatrix(rows2, cols2);
Matrix newMatrix = broadcastingRows(matrix1, rows2);
// add
Matrix result = createMatrix(newMatrix.rows, newMatrix.cols);
<<<<<<< HEAD
if (result.buffer == NULL){
return (Matrix){0,0,NULL};
}
for (int i = 0; i< rows2; i++) {
for (int j= 0; j< cols1; j++){
int valueM1= getMatrixAt(newMatrix, i, j);
int valueM2= getMatrixAt(matrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
=======
if (result.buffer == NULL) {
return (Matrix){0, 0, NULL};
>>>>>>> main
}
Matrix copy1 = broadCastRows(matrix1, rows2, cols2);
for (int i = 0; i < (rows2 * cols2); i++) { // addieren
result.buffer[i] =
(copy1.buffer[i] +
matrix2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
// Startadresse + (i * sizeof(MatrixType))
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols1; j++) {
int valueM1 = getMatrixAt(newMatrix, i, j);
int valueM2 = getMatrixAt(matrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
}
}
return result;
// add und return
} else if (rows2 == 1) {
Matrix result = createMatrix(rows1, cols1);
} else {
Matrix newMatrix2 = broadcastingRows(matrix2, rows1);
// add
Matrix result = createMatrix(newMatrix2.rows, newMatrix2.cols);
<<<<<<< HEAD
if (result.buffer == NULL){
return (Matrix){0,0,NULL};
}
for (int i = 0; i< rows1; i++) {
for (int j= 0; j< cols1; j++){
int valueM1= getMatrixAt(matrix1, i, j);
int valueM2= getMatrixAt(newMatrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
=======
if (result.buffer == NULL) {
return (Matrix){0, 0, NULL};
>>>>>>> main
}
Matrix copy2 = broadCastRows(matrix2, rows1, cols1);
// add und return
for (int i = 0; i < (rows1 * cols1); i++) { // addieren
result.buffer[i] =
(matrix1.buffer[i] +
copy2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
// Startadresse + (i * sizeof(MatrixType))
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
int valueM1 = getMatrixAt(matrix1, i, j);
int valueM2 = getMatrixAt(newMatrix2, i, j);
int sum = valueM1 + valueM2;
setMatrixAt(sum, result, i, j);
}
}
return result;
}
else {
printf("Fehlermeldung"); // vielleicht Fehlermeldung ändern zu
// Programmabbruch
Matrix error = {0, 0, NULL};
return error;
}
} else {
// kein add möglich
Matrix errorMatrix = {0, 0, NULL};
return errorMatrix;
}
else {
printf("Fehlermeldung"); // vielleicht Fehlermeldung ändern zu
// Programmabbruch
Matrix error = {0, 0, NULL};
return error;
}
return result;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2) {
// Spalten1 müssen gleich zeilen2 sein! dann multiplizieren
if (matrix1.cols == matrix2.rows) {
+15 -14
View File
@@ -30,20 +30,21 @@ 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) {
FILE *f = fopen(path, "wb"); // Binärdatei zum Schreiben öffnen
if (f == NULL)
return;
FILE *fptr = fopen(path, "wb"); // Binärdatei zum Schreiben öffnen
if (fptr == NULL)
return; // file konnte nicht geöffnet werden
// Header ist Erkennungsstring am Anfang der Datei, loadmodel erkennt
// Dateiformat
const char header[] = "__info2_neural_network_file_format__";
fwrite(header, sizeof(char), strlen(header), f);
const char header[] =
"__info2_neural_network_file_format__"; // header vor jedem Layer
fwrite(header, sizeof(char), strlen(header), fptr);
// Wenn es keine Layer gibt, 0 eintragen, LoadModel gibt 0 zurück
if (nn.numberOfLayers == 0) {
int zero = 0;
fwrite(&zero, sizeof(int), 1, f);
fclose(f);
fwrite(&zero, sizeof(int), 1, fptr);
fclose(fptr);
return;
}
@@ -51,8 +52,8 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
// Output-Neuronen
int inputDim = (int)nn.layers[0].weights.cols;
int outputDim = (int)nn.layers[0].weights.rows;
fwrite(&inputDim, sizeof(int), 1, f);
fwrite(&outputDim, sizeof(int), 1, f);
fwrite(&inputDim, sizeof(int), 1, fptr);
fwrite(&outputDim, sizeof(int), 1, fptr);
/* 3) Für jede Layer in Reihenfolge: Gewichte (output x input), Biases (output
x 1). Zwischen Layern wird nur die nächste outputDimension (int)
@@ -68,28 +69,28 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) {
/* Gewichte (MatrixType binär) */
if (wcount > 0 && layer.weights.buffer != NULL) {
fwrite(layer.weights.buffer, sizeof(MatrixType), (size_t)wcount, f);
fwrite(layer.weights.buffer, sizeof(MatrixType), (size_t)wcount, fptr);
}
/* Biases (MatrixType binär) */
if (bcount > 0 && layer.biases.buffer != NULL) {
fwrite(layer.biases.buffer, sizeof(MatrixType), (size_t)bcount, f);
fwrite(layer.biases.buffer, sizeof(MatrixType), (size_t)bcount, fptr);
}
/* Für die nächste Layer: falls vorhanden, schreibe deren outputDimension */
if (i + 1 < nn.numberOfLayers) {
int nextOutput = (int)nn.layers[i + 1].weights.rows;
fwrite(&nextOutput, sizeof(int), 1, f);
fwrite(&nextOutput, sizeof(int), 1, fptr);
} else {
/* Letzte Layer: wir können das Ende signalisieren, indem wir ein 0
schreiben. loadModel liest dann outputDimension = 0 und beendet die
Schleife. */
int zero = 0;
fwrite(&zero, sizeof(int), 1, f);
fwrite(&zero, sizeof(int), 1, fptr);
}
}
fclose(f);
fclose(fptr);
}
void test_loadModelReturnsCorrectNumberOfLayers(void) {