Implement training data loading
This commit is contained in:
parent
9fe8afb68a
commit
ef18b57d61
6 changed files with 34 additions and 12 deletions
12
include/cx.h
12
include/cx.h
|
@ -29,11 +29,13 @@
|
||||||
// Declare common data structures.
|
// Declare common data structures.
|
||||||
|
|
||||||
typedef struct _cx_gl_ctx {
|
typedef struct _cx_gl_ctx {
|
||||||
|
void (*free)(void *self);
|
||||||
|
uint8_t master_lock;
|
||||||
|
uint8_t *worker_locks;
|
||||||
|
CX_ThreadGroup **workers;
|
||||||
GLFWwindow *window;
|
GLFWwindow *window;
|
||||||
ModelRegistry *mr;
|
ModelRegistry *mr;
|
||||||
GLuint *VertexArrayIDs;
|
GLuint *VertexArrayIDs;
|
||||||
void (*free)(void *self);
|
|
||||||
uint8_t master_lock;
|
|
||||||
size_t VertexArray_count;
|
size_t VertexArray_count;
|
||||||
size_t VertexArray_size;
|
size_t VertexArray_size;
|
||||||
GLuint *programIDs;
|
GLuint *programIDs;
|
||||||
|
@ -42,11 +44,13 @@ typedef struct _cx_gl_ctx {
|
||||||
} CX_GL_CTX;
|
} CX_GL_CTX;
|
||||||
|
|
||||||
typedef struct _cx_nn_ctx {
|
typedef struct _cx_nn_ctx {
|
||||||
|
void (*free)(void *self);
|
||||||
|
uint8_t master_lock;
|
||||||
|
uint8_t *worker_locks;
|
||||||
|
CX_ThreadGroup **workers;
|
||||||
Neural_Network *nn;
|
Neural_Network *nn;
|
||||||
float *input_buffer;
|
float *input_buffer;
|
||||||
float *output_buffer;
|
float *output_buffer;
|
||||||
void (*free)(void *self);
|
|
||||||
uint8_t master_lock;
|
|
||||||
} CX_NN_CTX;
|
} CX_NN_CTX;
|
||||||
|
|
||||||
typedef struct _cx_ctx {
|
typedef struct _cx_ctx {
|
||||||
|
|
|
@ -28,6 +28,7 @@ typedef struct _neural_data {
|
||||||
Neural_Network *neural_new(size_t, size_t, size_t);
|
Neural_Network *neural_new(size_t, size_t, size_t);
|
||||||
void neural_free(Neural_Network *);
|
void neural_free(Neural_Network *);
|
||||||
void neural_randomize(Neural_Network *);
|
void neural_randomize(Neural_Network *);
|
||||||
|
float *neural_loadData(Neural_Network *, const char *);
|
||||||
float *neural_process(Neural_Network *, float *);
|
float *neural_process(Neural_Network *, float *);
|
||||||
Neural_Data *neural_getData(Neural_Network *, size_t);
|
Neural_Data *neural_getData(Neural_Network *, size_t);
|
||||||
int neural_getMesh(Neural_Network *, ModelRegistry *);
|
int neural_getMesh(Neural_Network *, ModelRegistry *);
|
||||||
|
|
|
@ -7,7 +7,7 @@ typedef struct _tensor {
|
||||||
size_t width;
|
size_t width;
|
||||||
} Tensor;
|
} Tensor;
|
||||||
|
|
||||||
Tensor *tensor_new(size_t, size_t);
|
Tensor *tensor_new(size_t, size_t, int);
|
||||||
|
|
||||||
Tensor *tensor_fromVertexBuffer(float *, size_t);
|
Tensor *tensor_fromVertexBuffer(float *, size_t);
|
||||||
|
|
||||||
|
|
12
src/cx.c
12
src/cx.c
|
@ -278,8 +278,20 @@ static void *
|
||||||
cx_nnthread(void *self) {
|
cx_nnthread(void *self) {
|
||||||
CX_Thread *self_t = self;
|
CX_Thread *self_t = self;
|
||||||
CX_NN_CTX *nn_ctx = self_t->ctx;
|
CX_NN_CTX *nn_ctx = self_t->ctx;
|
||||||
|
float *input, *output;
|
||||||
|
|
||||||
cx_nninit(&nn_ctx->nn);
|
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;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -121,6 +121,7 @@ neural_loadData(Neural_Network *self, const char *filename) {
|
||||||
return NULL;
|
return NULL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
read_cursor++;
|
||||||
}
|
}
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
@ -134,10 +135,10 @@ neural_process(Neural_Network *self, float *input) {
|
||||||
for (int i = 0; i < self->layers[0]->layer_size; i++) {
|
for (int i = 0; i < self->layers[0]->layer_size; i++) {
|
||||||
nl->neurons[i].value = input[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++) {
|
for (int i = 0; i < self->layer_count; i++) {
|
||||||
nl = self->layers[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++) {
|
for (int j = 0; j < nl->layer_size; j++) {
|
||||||
neural_vector->data[j] = nl->neurons[j].value;
|
neural_vector->data[j] = nl->neurons[j].value;
|
||||||
for (int k = 0; k < nl->layer_size_next; k++) {
|
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 ?
|
brightness = nl->neurons[i].value <= 1.0 ?
|
||||||
nl->neurons[i].value : 255;
|
nl->neurons[i].value : 255;
|
||||||
model_colorXYZ(model, 0, brightness, 0);
|
model_colorXYZ(model, 0, brightness, 0);
|
||||||
Tensor *translation_matrix = tensor_new(4, 4);
|
Tensor *translation_matrix = tensor_new(4, 4, 1);
|
||||||
Tensor *aspectRatio_matrix = tensor_new(4, 4);
|
Tensor *aspectRatio_matrix = tensor_new(4, 4, 1);
|
||||||
aspectRatio_matrix->data[0] = (GLfloat)9/16;
|
aspectRatio_matrix->data[0] = (GLfloat)9/16;
|
||||||
|
|
||||||
translation_matrix->data[3] = (((GLfloat)-1*16/9)*.90)
|
translation_matrix->data[3] = (((GLfloat)-1*16/9)*.90)
|
||||||
|
|
10
src/tensor.c
10
src/tensor.c
|
@ -1,7 +1,7 @@
|
||||||
#include "cx.h"
|
#include "cx.h"
|
||||||
|
|
||||||
Tensor *
|
Tensor *
|
||||||
tensor_new(size_t len, size_t width) {
|
tensor_new(size_t len, size_t width, int is_identity) {
|
||||||
Tensor *mat;
|
Tensor *mat;
|
||||||
|
|
||||||
mat = malloc(1 * sizeof(Tensor));
|
mat = malloc(1 * sizeof(Tensor));
|
||||||
|
@ -10,6 +10,10 @@ tensor_new(size_t len, size_t width) {
|
||||||
mat->len = len;
|
mat->len = len;
|
||||||
mat->width = width;
|
mat->width = width;
|
||||||
|
|
||||||
|
if (!is_identity) {
|
||||||
|
return mat;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
mat->data[i*width+(i % width)] = 1;
|
mat->data[i*width+(i % width)] = 1;
|
||||||
}
|
}
|
||||||
|
@ -24,7 +28,7 @@ tensor_fromVertexBuffer(float *buffer, size_t bufsize) {
|
||||||
|
|
||||||
mat_width = 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 i = 0; i < bufsize; i++) {
|
||||||
for (int j = 0; j < 4; j++) {
|
for (int j = 0; j < 4; j++) {
|
||||||
|
@ -44,7 +48,7 @@ tensor_multip(Tensor *mat2, Tensor *mat1) {
|
||||||
Tensor *result;
|
Tensor *result;
|
||||||
float dot_prod;
|
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++) {
|
for (int i = 0; i < mat1->width; i++) {
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue