Initial commit

This commit is contained in:
= 2018-07-09 09:21:21 +02:00
commit 1d7f709e5b
2 changed files with 166 additions and 0 deletions

7
CMakeLists.txt Normal file
View File

@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.10)
project(MatrixTests C)
set(CMAKE_C_STANDARD 11)
add_executable(MatrixTests main.c)
target_link_libraries(MatrixTests m)

159
main.c Normal file
View File

@ -0,0 +1,159 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
float vectors_dot_prod(const float *x, const float *y, int n) {
float res = 0.0;
int i;
for (i = 0; i < n; i++) {
res += x[i] * y[i];
}
return res;
}
float** Make2DFloatArray(int arraySizeX, int arraySizeY) {
float** theArray;
theArray = (float**) malloc(arraySizeX*sizeof(float*));
for (int i = 0; i < arraySizeX; i++)
theArray[i] = (float*) malloc(arraySizeY*sizeof(float));
return theArray;
}
float calc_sigmoid(float x) {
return (1 / (1 + expf(-x)));
}
float sigmoid_deriv(float x) {
float sigmoid_computed = calc_sigmoid(x);
return sigmoid_computed * (1 - sigmoid_computed);
}
float* vector_matrix_prod(float **x, const float *y, int rows, int cols) {
int col, row;
float *result = (float *)malloc(cols * sizeof(float *));
float res;
for (col = 0; col < cols; col++) {
int column = col;
res = 0.0;
for (row = 0; row < rows; row++) {
res += y[row] * x[row][column];
}
result[column] = res;
}
return result;
}
void print_vector(float *x, int size) {
printf("Vector is\n");
for (int i = 0; i < size; i++) {
printf("%f\t", x[i]);
}
printf("\n\n");
}
void print_matrix(float **x, int rows, int cols) {
printf("Matrix is\n");
for (int row=0; row < rows; row++) {
for (int col=0; col < cols; col++) {
printf("%f\t", x[row][col]);
}
printf("\n");
}
}
float genRand()
{
float num = ((float)rand()/(float)(RAND_MAX/2)) - 1;
return num;
}
float* create_starting_board() {
float* input = (float *)malloc(26 * sizeof(float *));
float tmp_input[26] = {0, 2, 0, 0, 0, 0, -5, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -2, 0};
for (int i=0; i < 26; i++) {
input[i] = tmp_input[i];
}
return input;
}
float* create_1D_layer(int size) {
float *input = (float *)malloc(size * sizeof(float *));
int i;
for (i=0; i<size; i++)
input[i] = 0.01;
//input[i] = genRand();
return input;
}
float** create_ND_layer(int rows, int cols) {
float** matrix = Make2DFloatArray(rows, cols);
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
matrix[i][j] = 0.4;
//matrix[i][j] = genRand();
}
}
return matrix;
}
float* perform_activation(float* res_vector, int size) {
float* activated_res = (float *)malloc(size * sizeof(float *));
for (int i=0; i < size; i++) {
activated_res[i] = calc_sigmoid(res_vector[i]);
}
return activated_res;
}
float feed_forward(float* input, float** hidden, float* output, int input_size, int hidden_size) {
float* hidden_res = vector_matrix_prod(hidden, input, input_size, hidden_size);
float* activated_res = perform_activation(hidden_res, hidden_size);
float output_res = vectors_dot_prod(activated_res, output, hidden_size);
float activated_output = calc_sigmoid(output_res);
return activated_output;
}
float TD_error(float expected, float actual) {
return expected - actual;
}
int main() {
srand((unsigned int) time(0));
clock_t begin = clock();
const int input_size = 26;
const int hidden_size = 40;
const int output_size = 1;
float* input_layer = create_starting_board();
float** hidden_layer = create_ND_layer(input_size, hidden_size);
float* output_layer = create_1D_layer(hidden_size);
float activated_output;
for (int i=0; i < 20000; i++) {
activated_output = feed_forward(input_layer, hidden_layer, output_layer, input_size, hidden_size);
}
// print_vector(input, input_size);
// print_matrix(matrix, input_size, hidden_size);
// print_vector(hidden_res, hidden_size);
// print_vector(output_layer, hidden_size);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("\nSpent %f time\nand the final result is %f",time_spent, activated_output);
}