generated from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1f1a7a23b1 | ||
|
|
0c85fb19bc |
No files matched your search
@@ -1,9 +1,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
|
||||||
|
|
||||||
Matrix createMatrix(size_t rows, size_t cols)
|
Matrix createMatrix(size_t rows, size_t cols)
|
||||||
{
|
{
|
||||||
@@ -17,7 +14,7 @@ Matrix createMatrix(size_t rows, size_t cols)
|
|||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Single allocation for entire matrix
|
//Allocate Matrix buffer
|
||||||
m.buffer = malloc(rows * cols * sizeof(MatrixType));
|
m.buffer = malloc(rows * cols * sizeof(MatrixType));
|
||||||
|
|
||||||
if(!m.buffer){
|
if(!m.buffer){
|
||||||
@@ -25,7 +22,7 @@ Matrix createMatrix(size_t rows, size_t cols)
|
|||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize (optional)
|
//Initialize matrix with default value
|
||||||
for(unsigned int i = 0; i < rows * cols; i++){
|
for(unsigned int i = 0; i < rows * cols; i++){
|
||||||
m.buffer[i] = UNDEFINED_MATRIX_VALUE;
|
m.buffer[i] = UNDEFINED_MATRIX_VALUE;
|
||||||
}
|
}
|
||||||
@@ -75,7 +72,7 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
|||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
|
||||||
bool doBroadcast = false;
|
unsigned int doBroadcast = 0;
|
||||||
Matrix larger, smaller;
|
Matrix larger, smaller;
|
||||||
|
|
||||||
if(matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols){
|
if(matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols){
|
||||||
@@ -86,13 +83,13 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
{
|
{
|
||||||
larger = matrix1;
|
larger = matrix1;
|
||||||
smaller = matrix2;
|
smaller = matrix2;
|
||||||
doBroadcast = true;
|
doBroadcast = 1;
|
||||||
}
|
}
|
||||||
else if (matrix1.rows == matrix2.rows && matrix1.cols == 1)
|
else if (matrix1.rows == matrix2.rows && matrix1.cols == 1)
|
||||||
{
|
{
|
||||||
larger = matrix2;
|
larger = matrix2;
|
||||||
smaller = matrix1;
|
smaller = matrix1;
|
||||||
doBroadcast = true;
|
doBroadcast = 1;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
Matrix m = {NULL, 0, 0};
|
Matrix m = {NULL, 0, 0};
|
||||||
@@ -101,6 +98,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
|
|
||||||
Matrix outputMatrix = createMatrix(larger.rows, larger.cols);
|
Matrix outputMatrix = createMatrix(larger.rows, larger.cols);
|
||||||
if(doBroadcast){
|
if(doBroadcast){
|
||||||
|
//Broadcasting
|
||||||
for(int i = 0; i < outputMatrix.rows; i++){
|
for(int i = 0; i < outputMatrix.rows; i++){
|
||||||
MatrixType broadcastValue = smaller.buffer[i];
|
MatrixType broadcastValue = smaller.buffer[i];
|
||||||
for(int j = 0; j < outputMatrix.cols; j++){
|
for(int j = 0; j < outputMatrix.cols; j++){
|
||||||
@@ -108,6 +106,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else{
|
} else{
|
||||||
|
//Classic execution
|
||||||
for (int i = 0; i < matrix1.rows;i++) {
|
for (int i = 0; i < matrix1.rows;i++) {
|
||||||
for (int j = 0; j < matrix1.cols; j++) {
|
for (int j = 0; j < matrix1.cols; j++) {
|
||||||
// how this should work in normal Matrix version:
|
// how this should work in normal Matrix version:
|
||||||
|
|||||||
@@ -164,7 +164,6 @@ NeuralNetwork loadModel(const char *path)
|
|||||||
|
|
||||||
assignActivations(model);
|
assignActivations(model);
|
||||||
}
|
}
|
||||||
printf("%d\n", model.numberOfLayers);
|
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,30 +13,30 @@ static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
|||||||
if(file != NULL){
|
if(file != NULL){
|
||||||
const char *fileTag = "__info2_neural_network_file_format__";
|
const char *fileTag = "__info2_neural_network_file_format__";
|
||||||
|
|
||||||
// Write file header
|
//Write header
|
||||||
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
|
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
|
||||||
|
|
||||||
// Write the input dimension of the first layer
|
//Write the input dimension of the first layer
|
||||||
if(nn.numberOfLayers > 0){
|
if(nn.numberOfLayers > 0){
|
||||||
fwrite(&nn.layers[0].weights.cols, sizeof(int), 1, file);
|
fwrite(&nn.layers[0].weights.cols, sizeof(int), 1, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write dimensions and data for each layer
|
//Write each layer into file
|
||||||
for(int i = 0; i < nn.numberOfLayers; i++){
|
for(int i = 0; i < nn.numberOfLayers; i++){
|
||||||
|
|
||||||
// Write output dimension (rows of weights)
|
//Write output dimension
|
||||||
fwrite(&nn.layers[i].weights.rows, sizeof(int), 1, file);
|
fwrite(&nn.layers[i].weights.rows, sizeof(int), 1, file);
|
||||||
|
|
||||||
// Write weight matrix data
|
//Write weight data
|
||||||
int weightSize = nn.layers[i].weights.rows * nn.layers[i].weights.cols;
|
int weightSize = nn.layers[i].weights.rows * nn.layers[i].weights.cols;
|
||||||
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), weightSize, file);
|
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), weightSize, file);
|
||||||
|
|
||||||
// Write bias matrix data
|
//Write bias data
|
||||||
int biasSize = nn.layers[i].biases.rows * nn.layers[i].biases.cols;
|
int biasSize = nn.layers[i].biases.rows * nn.layers[i].biases.cols;
|
||||||
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), biasSize, file);
|
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), biasSize, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write terminating 0 to signal end of layers
|
//EOF Terminator
|
||||||
int zero = 0;
|
int zero = 0;
|
||||||
fwrite(&zero, sizeof(int), 1, file);
|
fwrite(&zero, sizeof(int), 1, file);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user