forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
1
Commits
KTobi2
..
ede0bd8bd8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ede0bd8bd8 |
+1
-7
@@ -1,10 +1,4 @@
|
|||||||
mnist
|
mnist
|
||||||
runTests
|
runTests
|
||||||
*.o
|
*.o
|
||||||
*.exe
|
*.exe
|
||||||
.vscode/settings.json
|
|
||||||
.vscode/launch.json
|
|
||||||
.vscode/settings.json
|
|
||||||
.vscode/settings.json
|
|
||||||
runImageInputTests
|
|
||||||
testFile.info2
|
|
||||||
+3
-29
@@ -15,36 +15,10 @@ GrayScaleImage readImage()
|
|||||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||||
GrayScaleImageSeries *readImages(const char *path)
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
{
|
{
|
||||||
unsigned short * numImages;
|
|
||||||
unsigned short * breiteBilder;
|
|
||||||
unsigned short * laengeBilder;
|
|
||||||
|
|
||||||
GrayScaleImageSeries *series = NULL;
|
GrayScaleImageSeries *series = NULL;
|
||||||
FILE *file = fopen(*path,"rb");
|
FILE *file = fopen("mnist_test.info2","rb");
|
||||||
char * headOfFile;
|
char headOfFile;
|
||||||
|
series = malloc();
|
||||||
fread(headOfFile, sizeof(FILE_HEADER_STRING),1, file); //liest den header ein und überprüft ob korrekte datei
|
|
||||||
|
|
||||||
if(strcmp(headOfFile, FILE_HEADER_STRING) != 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
// liest numIMages, breite und länge der Bilder ein
|
|
||||||
fseek(file, sizeof(FILE_HEADER_STRING), SEEK_SET);
|
|
||||||
fread(numImages, sizeof(short), 1, file);
|
|
||||||
|
|
||||||
fseek(file, sizeof(short), SEEK_CUR);
|
|
||||||
fread(breiteBilder, sizeof(short), 1, file);
|
|
||||||
|
|
||||||
fseek(file, sizeof(short), SEEK_CUR);
|
|
||||||
fread(laengeBilder, sizeof(short), 1, file);
|
|
||||||
|
|
||||||
series = malloc(*numImages * *breiteBilder * *laengeBilder * sizeof(short));
|
|
||||||
|
|
||||||
for(int i = 0; i < numImages; i++)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return series;
|
return series;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,7 +62,4 @@ ifeq ($(OS),Windows_NT)
|
|||||||
del /f *.o *.exe
|
del /f *.o *.exe
|
||||||
else
|
else
|
||||||
rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests
|
rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests
|
||||||
endif
|
endif
|
||||||
clean für windows
|
|
||||||
clean:
|
|
||||||
rm -f *.o *.exe
|
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// TODO Matrix-Funktionen implementieren
|
||||||
/*typedef struct {
|
/*typedef struct {
|
||||||
unsigned int rows; //Zeilen
|
unsigned int rows; //Zeilen
|
||||||
@@ -8,6 +10,12 @@
|
|||||||
MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten
|
MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten
|
||||||
} Matrix;*/
|
} Matrix;*/
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols) {
|
Matrix createMatrix(unsigned int rows, unsigned int cols) {
|
||||||
|
|
||||||
|
Matrix errorMatrix = {0, 0, NULL};
|
||||||
|
if (rows == 0 || cols == 0) {
|
||||||
|
|
||||||
|
return errorMatrix;
|
||||||
|
}
|
||||||
MatrixType *buffer =
|
MatrixType *buffer =
|
||||||
malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren, malloc
|
malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren, malloc
|
||||||
// liefert Zeiger auf Speicher
|
// liefert Zeiger auf Speicher
|
||||||
@@ -20,6 +28,7 @@ void clearMatrix(Matrix *matrix) {
|
|||||||
matrix->cols = UNDEFINED_MATRIX_VALUE;
|
matrix->cols = UNDEFINED_MATRIX_VALUE;
|
||||||
free((*matrix).buffer); // Speicher freigeben
|
free((*matrix).buffer); // Speicher freigeben
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMatrixAt(const MatrixType value, Matrix matrix,
|
void setMatrixAt(const MatrixType value, Matrix matrix,
|
||||||
const unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
const unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
||||||
const unsigned int colIdx) {
|
const unsigned int colIdx) {
|
||||||
@@ -40,29 +49,168 @@ MatrixType getMatrixAt(const Matrix matrix,
|
|||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
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);
|
||||||
|
for (int c = 0; c < cols; c++) {
|
||||||
|
|
||||||
|
setMatrixAt(value, copy, r, c);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
|
||||||
|
for (int r = 0; r < rows; r++) {
|
||||||
|
|
||||||
|
setMatrixAt(value, copy, r, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
||||||
|
|
||||||
|
// Broadcasting nur bei Vektor und Matrix, Fehlermeldung bei zwei unpassenden
|
||||||
|
// Matrizen
|
||||||
|
|
||||||
|
const unsigned int rows1 = matrix1.rows;
|
||||||
|
const unsigned int rows2 = matrix2.rows;
|
||||||
|
const unsigned int cols1 = matrix1.cols;
|
||||||
|
const unsigned int cols2 = matrix2.cols;
|
||||||
|
|
||||||
|
const int rowsEqual = ((rows1 == rows2) ? 1 : 0);
|
||||||
|
|
||||||
|
const int colsEqual = ((cols1 == cols2) ? 1 : 0);
|
||||||
|
|
||||||
|
if (rowsEqual && colsEqual) // addieren
|
||||||
|
|
||||||
|
{
|
||||||
|
Matrix result = createMatrix(rows1, cols1); // Speicher reservieren
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
|
||||||
|
return result; // zurückgeben
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (rowsEqual && !colsEqual) {
|
||||||
|
|
||||||
|
if (cols1 == 1) {
|
||||||
|
|
||||||
|
Matrix result = createMatrix(rows2, cols2);
|
||||||
|
|
||||||
|
Matrix copy1 = broadCastCols(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))
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
|
||||||
|
// add und return
|
||||||
|
|
||||||
|
} else if (cols2 == 1) {
|
||||||
|
|
||||||
|
Matrix result = createMatrix(rows1, cols1);
|
||||||
|
|
||||||
|
Matrix copy2 = broadCastCols(matrix2, rows1, cols1);
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
// add und return
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
|
||||||
|
printf("Fehlermeldung"); // vielleicht Fehlermeldung ändern zu
|
||||||
|
// Programmabbruch
|
||||||
|
Matrix error = {0, 0, NULL};
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (!rowsEqual && colsEqual) {
|
||||||
|
|
||||||
|
if (rows1 == 1) {
|
||||||
|
|
||||||
|
Matrix result = createMatrix(rows2, cols2);
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
|
||||||
|
// add und return
|
||||||
|
|
||||||
|
} else if (rows2 == 1) {
|
||||||
|
|
||||||
|
Matrix result = createMatrix(rows1, cols1);
|
||||||
|
|
||||||
|
Matrix copy2 = broadCastCols(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))
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
|
||||||
|
printf("Fehlermeldung"); // vielleicht Fehlermeldung ändern zu
|
||||||
|
// Programmabbruch
|
||||||
|
Matrix error = {0, 0, NULL};
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
printf(
|
||||||
|
"Fehlermeldung"); // vielleicht Fehlermeldung ändern zu Programmabbruch
|
||||||
|
Matrix error = {0, 0, NULL};
|
||||||
|
return error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2) { return matrix1; }
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2) { return matrix1; }
|
||||||
|
|||||||
@@ -19,6 +19,11 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx,
|
|||||||
unsigned int colIdx);
|
unsigned int colIdx);
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx,
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx,
|
||||||
unsigned int colIdx);
|
unsigned int colIdx);
|
||||||
|
|
||||||
|
Matrix broadCastCols(const Matrix matrix, const unsigned int rows,
|
||||||
|
const unsigned int cols);
|
||||||
|
Matrix broadCastRows(const Matrix matrix, const unsigned int rows,
|
||||||
|
const unsigned int cols);
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2);
|
Matrix add(const Matrix matrix1, const Matrix matrix2);
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user