Implement a function for acquiring neural data.
* Some good refactoring
This commit is contained in:
parent
58ae618e69
commit
d2ec11859d
3 changed files with 74 additions and 30 deletions
|
@ -18,10 +18,18 @@ typedef struct _neural_network {
|
||||||
ssize_t layer_count;
|
ssize_t layer_count;
|
||||||
} Neural_Network;
|
} 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);
|
Neural_Network *neural_new(size_t, size_t, size_t);
|
||||||
void neural_randomize(Neural_Network *);
|
void neural_randomize(Neural_Network *);
|
||||||
float *neural_process(Neural_Network *, float *);
|
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
|
#endif
|
||||||
|
|
||||||
|
|
42
src/cx.c
42
src/cx.c
|
@ -47,6 +47,7 @@ cx_glrender(GLFWwindow *window, GLuint programID,
|
||||||
|
|
||||||
cx_glBindBuffer(render_buffer, vertexbuffer, 0, 4,
|
cx_glBindBuffer(render_buffer, vertexbuffer, 0, 4,
|
||||||
mr->models[i]->bufsize*4*sizeof(GLfloat));
|
mr->models[i]->bufsize*4*sizeof(GLfloat));
|
||||||
|
|
||||||
cx_glBindBuffer(mr->models[i]->colors, colorbuffer, 2, 3,
|
cx_glBindBuffer(mr->models[i]->colors, colorbuffer, 2, 3,
|
||||||
mr->models[i]->bufsize*3*sizeof(GLfloat));
|
mr->models[i]->bufsize*3*sizeof(GLfloat));
|
||||||
|
|
||||||
|
@ -122,6 +123,27 @@ cx_glinit(GLFWwindow **window) {
|
||||||
return 0;
|
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
|
static int
|
||||||
cx_nnrun(Neural_Network *nn) {
|
cx_nnrun(Neural_Network *nn) {
|
||||||
|
|
||||||
|
@ -146,17 +168,15 @@ cx_run(GLFWwindow *window, Neural_Network *nn) {
|
||||||
// Establish a model registry
|
// Establish a model registry
|
||||||
mr = modelRegistry_new();
|
mr = modelRegistry_new();
|
||||||
// Fill the model registry with mesh models
|
// Fill the model registry with mesh models
|
||||||
neural_getMesh(mr, nn);
|
neural_getMesh(nn, mr);
|
||||||
|
|
||||||
// Remainder from cursor experiments, might be useful later
|
// Remainder from cursor experiments, might be useful later
|
||||||
double xpos, ypos;
|
double xpos, ypos;
|
||||||
glfwGetCursorPos(window, &xpos, &ypos);
|
glfwGetCursorPos(window, &xpos, &ypos);
|
||||||
|
|
||||||
int t = 0;
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
cx_glrender(window, programID, mr);
|
cx_glrender(window, programID, mr);
|
||||||
t++;
|
|
||||||
usleep(1000000/60);
|
usleep(1000000/60);
|
||||||
// Check if the ESC key was pressed or the window was closed
|
// Check if the ESC key was pressed or the window was closed
|
||||||
} while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS
|
} while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS
|
||||||
|
@ -169,19 +189,3 @@ cx_run(GLFWwindow *window, Neural_Network *nn) {
|
||||||
return 0;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
52
src/neural.c
52
src/neural.c
|
@ -66,9 +66,10 @@ neural_randomize(Neural_Network *self) {
|
||||||
nl->layer_size_next, f);
|
nl->layer_size_next, f);
|
||||||
for (int j = 0; j < nl->layer_size; j++) {
|
for (int j = 0; j < nl->layer_size; j++) {
|
||||||
for (int k = 0; k < nl->layer_size_next; k++) {
|
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
|
int
|
||||||
neural_train(Neural_Network *self,
|
neural_train(Neural_Network *self,
|
||||||
const float *testdata,
|
const float *expected_result) {
|
||||||
const float *testresult) {
|
Neural_Data *input_data; // What the neural network received
|
||||||
float *retval;
|
Neural_Data *result_data; // What the neural network computed
|
||||||
int k, i;
|
|
||||||
|
|
||||||
retval = malloc(self->layers[self->layer_count-1]->layer_size * sizeof(float));
|
input_data = neural_getData(self, 0);
|
||||||
k = 0;
|
result_data = neural_getData(self, self->layer_count-1);
|
||||||
i = 0;
|
|
||||||
|
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;
|
return retval;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
neural_getMesh(ModelRegistry *mr, Neural_Network *nn) {
|
neural_getMesh(Neural_Network *nn, ModelRegistry *mr) {
|
||||||
Model *model;
|
Model *model;
|
||||||
for (int j = 0; j < nn->layer_count; j++) {
|
for (int j = 0; j < nn->layer_count; j++) {
|
||||||
Neural_Layer *nl = nn->layers[j];
|
Neural_Layer *nl = nn->layers[j];
|
||||||
|
@ -187,7 +218,8 @@ neural_getMesh(ModelRegistry *mr, Neural_Network *nn) {
|
||||||
|
|
||||||
.001 // girth
|
.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);
|
model_colorXYZ(model, brightness, 0, 0);
|
||||||
modelRegistry_register(mr, model);
|
modelRegistry_register(mr, model);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue