From ef18b57d61fb418422e30ddae33a5cd2bae44524 Mon Sep 17 00:00:00 2001 From: Marcel Plch Date: Wed, 20 Nov 2024 01:00:18 +0100 Subject: [PATCH] Implement training data loading --- include/cx.h | 12 ++++++++---- include/neural.h | 1 + include/tensor.h | 2 +- src/cx.c | 12 ++++++++++++ src/neural.c | 9 +++++---- src/tensor.c | 10 +++++++--- 6 files changed, 34 insertions(+), 12 deletions(-) diff --git a/include/cx.h b/include/cx.h index a39f90e..29a2cf2 100644 --- a/include/cx.h +++ b/include/cx.h @@ -29,11 +29,13 @@ // Declare common data structures. typedef struct _cx_gl_ctx { + void (*free)(void *self); + uint8_t master_lock; + uint8_t *worker_locks; + CX_ThreadGroup **workers; GLFWwindow *window; ModelRegistry *mr; GLuint *VertexArrayIDs; - void (*free)(void *self); - uint8_t master_lock; size_t VertexArray_count; size_t VertexArray_size; GLuint *programIDs; @@ -42,11 +44,13 @@ typedef struct _cx_gl_ctx { } CX_GL_CTX; typedef struct _cx_nn_ctx { + void (*free)(void *self); + uint8_t master_lock; + uint8_t *worker_locks; + CX_ThreadGroup **workers; Neural_Network *nn; float *input_buffer; float *output_buffer; - void (*free)(void *self); - uint8_t master_lock; } CX_NN_CTX; typedef struct _cx_ctx { diff --git a/include/neural.h b/include/neural.h index eaa7919..eb1bf3d 100644 --- a/include/neural.h +++ b/include/neural.h @@ -28,6 +28,7 @@ typedef struct _neural_data { Neural_Network *neural_new(size_t, size_t, size_t); void neural_free(Neural_Network *); void neural_randomize(Neural_Network *); +float *neural_loadData(Neural_Network *, const char *); float *neural_process(Neural_Network *, float *); Neural_Data *neural_getData(Neural_Network *, size_t); int neural_getMesh(Neural_Network *, ModelRegistry *); diff --git a/include/tensor.h b/include/tensor.h index a861de2..f655f09 100644 --- a/include/tensor.h +++ b/include/tensor.h @@ -7,7 +7,7 @@ typedef struct _tensor { size_t width; } Tensor; -Tensor *tensor_new(size_t, size_t); +Tensor *tensor_new(size_t, size_t, int); Tensor *tensor_fromVertexBuffer(float *, size_t); diff --git a/src/cx.c b/src/cx.c index bd452ab..2e200a9 100644 --- a/src/cx.c +++ b/src/cx.c @@ -278,8 +278,20 @@ static void * cx_nnthread(void *self) { CX_Thread *self_t = self; CX_NN_CTX *nn_ctx = self_t->ctx; + float *input, *output; cx_nninit(&nn_ctx->nn); + input = neural_loadData(nn_ctx->nn, "../training_data/0"); + + for (int i = 0; i < 64; i++) { + nn_ctx->nn->layers[0]->neurons[i].value = input[i]; + } + + output = neural_process(nn_ctx->nn, input); + + for (int i = 0; i < 4; i++) { + nn_ctx->nn->layers[7]->neurons[i].value = output[i]; + } return NULL; } diff --git a/src/neural.c b/src/neural.c index 5c40676..1fc6816 100644 --- a/src/neural.c +++ b/src/neural.c @@ -121,6 +121,7 @@ neural_loadData(Neural_Network *self, const char *filename) { return NULL; break; } + read_cursor++; } return retval; } @@ -134,10 +135,10 @@ neural_process(Neural_Network *self, float *input) { for (int i = 0; i < self->layers[0]->layer_size; i++) { nl->neurons[i].value = input[i]; } - neural_vector = tensor_new(1, nl->layer_size); + neural_vector = tensor_new(1, nl->layer_size, 0); for (int i = 0; i < self->layer_count; i++) { nl = self->layers[i]; - synapse_matrix = tensor_new(nl->layer_size_next, nl->layer_size); + synapse_matrix = tensor_new(nl->layer_size_next, nl->layer_size, 0); for (int j = 0; j < nl->layer_size; j++) { neural_vector->data[j] = nl->neurons[j].value; for (int k = 0; k < nl->layer_size_next; k++) { @@ -246,8 +247,8 @@ neural_getMesh(Neural_Network *nn, ModelRegistry *mr) { brightness = nl->neurons[i].value <= 1.0 ? nl->neurons[i].value : 255; model_colorXYZ(model, 0, brightness, 0); - Tensor *translation_matrix = tensor_new(4, 4); - Tensor *aspectRatio_matrix = tensor_new(4, 4); + Tensor *translation_matrix = tensor_new(4, 4, 1); + Tensor *aspectRatio_matrix = tensor_new(4, 4, 1); aspectRatio_matrix->data[0] = (GLfloat)9/16; translation_matrix->data[3] = (((GLfloat)-1*16/9)*.90) diff --git a/src/tensor.c b/src/tensor.c index e3601df..1b1034f 100644 --- a/src/tensor.c +++ b/src/tensor.c @@ -1,7 +1,7 @@ #include "cx.h" Tensor * -tensor_new(size_t len, size_t width) { +tensor_new(size_t len, size_t width, int is_identity) { Tensor *mat; mat = malloc(1 * sizeof(Tensor)); @@ -10,6 +10,10 @@ tensor_new(size_t len, size_t width) { mat->len = len; mat->width = width; + if (!is_identity) { + return mat; + } + for (int i = 0; i < len; i++) { mat->data[i*width+(i % width)] = 1; } @@ -24,7 +28,7 @@ tensor_fromVertexBuffer(float *buffer, size_t bufsize) { mat_width = bufsize; - mat = tensor_new(4, mat_width); + mat = tensor_new(4, mat_width, 0); for (int i = 0; i < bufsize; i++) { for (int j = 0; j < 4; j++) { @@ -44,7 +48,7 @@ tensor_multip(Tensor *mat2, Tensor *mat1) { Tensor *result; float dot_prod; - result = tensor_new(mat2->len, mat1->width); + result = tensor_new(mat2->len, mat1->width, 0); for (int i = 0; i < mat1->width; i++) {