From d2ec11859df5784521665b090dc186b2d25e3636 Mon Sep 17 00:00:00 2001 From: Marcel Plch Date: Tue, 29 Oct 2024 17:07:08 +0100 Subject: [PATCH] Implement a function for acquiring neural data. * Some good refactoring --- include/neural.h | 10 +++++++++- src/cx.c | 42 ++++++++++++++++++++------------------ src/neural.c | 52 ++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 74 insertions(+), 30 deletions(-) diff --git a/include/neural.h b/include/neural.h index 1d53b72..5a6dff4 100644 --- a/include/neural.h +++ b/include/neural.h @@ -18,10 +18,18 @@ typedef struct _neural_network { ssize_t layer_count; } Neural_Network; +typedef struct _neural_data { + float *neural_vector; + size_t vect_len; + float *synapse_matrix; + size_t mat_len; +} Neural_Data; + Neural_Network *neural_new(size_t, size_t, size_t); void neural_randomize(Neural_Network *); float *neural_process(Neural_Network *, float *); -int neural_getMesh(ModelRegistry *, Neural_Network *); +Neural_Data *neural_getData(Neural_Network *, size_t); +int neural_getMesh(Neural_Network *, ModelRegistry *); #endif diff --git a/src/cx.c b/src/cx.c index fc3ac48..c17c6f0 100644 --- a/src/cx.c +++ b/src/cx.c @@ -47,6 +47,7 @@ cx_glrender(GLFWwindow *window, GLuint programID, cx_glBindBuffer(render_buffer, vertexbuffer, 0, 4, mr->models[i]->bufsize*4*sizeof(GLfloat)); + cx_glBindBuffer(mr->models[i]->colors, colorbuffer, 2, 3, mr->models[i]->bufsize*3*sizeof(GLfloat)); @@ -122,6 +123,27 @@ cx_glinit(GLFWwindow **window) { return 0; } +int +cx_nninit(Neural_Network **nn) { + // Allocate a Neural Network + *nn = neural_new(64, 4, 8); + if(!*nn) { + fprintf(stderr, "Failed to initialize Neural Network.\n"); + return -1; + } + + // Populate the neural network with sensible values. + neural_randomize(*nn); + + return 0; +} + +static int +cx_glrun() { + + return 0; +} + static int cx_nnrun(Neural_Network *nn) { @@ -146,17 +168,15 @@ cx_run(GLFWwindow *window, Neural_Network *nn) { // Establish a model registry mr = modelRegistry_new(); // Fill the model registry with mesh models - neural_getMesh(mr, nn); + neural_getMesh(nn, mr); // Remainder from cursor experiments, might be useful later double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos); - int t = 0; do { cx_glrender(window, programID, mr); - t++; usleep(1000000/60); // Check if the ESC key was pressed or the window was closed } while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS @@ -169,19 +189,3 @@ cx_run(GLFWwindow *window, Neural_Network *nn) { return 0; } -int -cx_nninit(Neural_Network **nn) { - // Allocate a Neural Network - *nn = neural_new(64, 4, 8); - if(!*nn) { - fprintf(stderr, "Failed to initialize Neural Network.\n"); - return -1; - } - - // Populate the neural network with sensible values. - neural_randomize(*nn); - - return 0; -} - - diff --git a/src/neural.c b/src/neural.c index c68391c..22246dd 100644 --- a/src/neural.c +++ b/src/neural.c @@ -66,9 +66,10 @@ neural_randomize(Neural_Network *self) { nl->layer_size_next, f); for (int j = 0; j < nl->layer_size; j++) { for (int k = 0; k < nl->layer_size_next; k++) { - nl->neurons[j].synapses[k] = UINT64_MAX / rand_vals[k]; + nl->neurons[j].synapses[k] = (float)rand_vals[k] / UINT64_MAX; } } + free(rand_vals); } } @@ -155,20 +156,50 @@ neural_backprop_down(Neural_Network *self, size_t neuron, size_t layer) { int neural_train(Neural_Network *self, - const float *testdata, - const float *testresult) { - float *retval; - int k, i; + const float *expected_result) { + Neural_Data *input_data; // What the neural network received + Neural_Data *result_data; // What the neural network computed - retval = malloc(self->layers[self->layer_count-1]->layer_size * sizeof(float)); - k = 0; - i = 0; + input_data = neural_getData(self, 0); + result_data = neural_getData(self, self->layer_count-1); + + return 0; +} + +Neural_Data * +neural_getData(Neural_Network *self, size_t layer) { + Neural_Layer *nl; + Neural_Data *retval; + + retval = malloc(1 * sizeof(Neural_Data)); + + nl = self->layers[layer]; + + retval->neural_vector = malloc(nl->layer_size * sizeof(float)); + retval->vect_len = nl->layer_size; + if (!nl->layer_size_next) { + retval->synapse_matrix = NULL; + retval->mat_len = 0; + } + else { + retval->synapse_matrix = malloc(nl->layer_size * nl->layer_size_next + * sizeof(float)); + for (int i = 0; i < nl->layer_size; i++) { + for (int j = 0; j < nl->layer_size_next; j++) { + retval->synapse_matrix[i*j+i] = nl->neurons[i].synapses[j]; + } + } + } + for (int i = 0; i < nl->layer_size; i++) { + retval->neural_vector[i] = nl->neurons[i].value; + } return retval; + } int -neural_getMesh(ModelRegistry *mr, Neural_Network *nn) { +neural_getMesh(Neural_Network *nn, ModelRegistry *mr) { Model *model; for (int j = 0; j < nn->layer_count; j++) { Neural_Layer *nl = nn->layers[j]; @@ -187,7 +218,8 @@ neural_getMesh(ModelRegistry *mr, Neural_Network *nn) { .001 // girth ); - brightness = nl->neurons[i].synapses[k] <= 1.0 ? nl->neurons[i].synapses[k] : 255; + brightness = nl->neurons[i].synapses[k] <= 1.0 ? nl->neurons[i].synapses[k] * 255 : 255; + if (brightness) model_colorXYZ(model, brightness, 0, 0); modelRegistry_register(mr, model); }