random numbers are now somehow random

This commit is contained in:
Jakob Stornig 2023-09-21 10:19:22 +02:00
parent 2654733c27
commit 614cfef7f2
2 changed files with 10 additions and 4 deletions

View file

@ -2,8 +2,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include <time.h>
#define MAX_BYTES 100 #define MAX_BYTES 100
static int RANDOMIZED = 0;
// operational functions // operational functions
Matrix* matrix_create(int rows, int columns) { Matrix* matrix_create(int rows, int columns) {
@ -17,7 +19,7 @@ Matrix* matrix_create(int rows, int columns) {
// allocate memory for the numbers (2D-Array) // allocate memory for the numbers (2D-Array)
matrix->numbers = malloc(sizeof(double*) * rows); matrix->numbers = malloc(sizeof(double*) * rows);
for (int i = 0; i < rows; i++) { for (int i = 0; i < rows; i++) {
matrix->numbers[i] = malloc(sizeof(double) * columns); matrix->numbers[i] = calloc(sizeof(double), columns);
} }
// return the pointer to the allocated memory // return the pointer to the allocated memory
@ -189,7 +191,8 @@ Matrix* apply(double (*function)(double), Matrix* matrix) {
// apply the function to all values in the matrix // apply the function to all values in the matrix
for (int i = 0; i < matrix->rows; i++) { for (int i = 0; i < matrix->rows; i++) {
for (int j = 0; j < matrix->columns; j++) { for (int j = 0; j < matrix->columns; j++) {
matrix->numbers[i][j] = (*function)(matrix->numbers[i][j]); result_matrix->numbers[i][j] = (*function)(matrix->numbers[i][j]);
int k = 0;
} }
} }
@ -350,6 +353,10 @@ int matrix_argmax(Matrix* matrix) {
void matrix_randomize(Matrix* matrix, int n) { void matrix_randomize(Matrix* matrix, int n) {
if(!RANDOMIZED){
srand(time(NULL));
RANDOMIZED = 1;
}
//make a min and max //make a min and max
double min = -1.0f / sqrt(n); double min = -1.0f / sqrt(n);
double max = 1.0f / sqrt(n); double max = 1.0f / sqrt(n);

View file

@ -159,11 +159,10 @@ Matrix* predict(Neural_Network* network, Matrix* image_data) {
//void batch_train_network(Neural_Network* network, Image** images, int size); //void batch_train_network(Neural_Network* network, Image** images, int size);
double relu(double input) { double relu(double input) {
if (input < 0){ if (input <= 0){
return 0.0; return 0.0;
} }
return input; return input;
//TODO: relu formel
} }
Matrix* softmax(Matrix* matrix) { Matrix* softmax(Matrix* matrix) {