57 const struct Layer *prev_layer = NULL;
59 if (prev_layer != NULL) {
83 printf(
"neural_create() error: initialising network\n");
97 if (net->
head == NULL || net->
tail == NULL) {
108 for (
int i = 0; i < pos && iter != NULL; ++i) {
111 struct Llist *
new = malloc(
sizeof(
struct Llist));
121 new->next = iter->
next;
123 if (iter->
next == NULL) {
127 new->next->prev =
new;
144 for (
int i = 0; i < pos && iter != NULL; ++i) {
148 printf(
"neural_layer_remove(): error finding layer to remove\n");
150 }
else if (iter->
next == NULL && iter->
prev == NULL) {
151 printf(
"neural_layer_remove(): attempted to remove the only layer\n");
155 if (iter->
prev == NULL) {
157 if (iter->
next != NULL) {
164 if (iter->
next == NULL) {
166 if (iter->
prev != NULL) {
171 if (iter->
prev != NULL && iter->
next != NULL) {
212 while (iter != NULL) {
227 while (iter != NULL) {
245 while (iter != NULL) {
260 bool do_resize =
false;
261 const struct Layer *prev = NULL;
263 while (iter != NULL) {
292 const struct Layer *prev = NULL;
294 while (iter != NULL) {
314 while (iter != NULL) {
332 while (iter != NULL) {
343 while (iter != NULL) {
345 if (iter->
next == NULL) {
355 while (iter != NULL) {
372 printf(
"neural_output(): error (%d) >= (%d)\n", IDX, net->
n_outputs);
409 cJSON *json = cJSON_CreateObject();
412 char layer_name[256];
413 while (iter != NULL) {
415 cJSON *
layer = cJSON_Parse(str);
417 snprintf(layer_name, 256,
"layer_%d", i);
418 cJSON_AddItemToObject(json, layer_name,
layer);
422 char *
string = cJSON_Print(json);
440 printf(
"Import error: neural networks not yet implemented\n");
454 while (iter != NULL) {
481 s += fwrite(&net->
n_layers,
sizeof(
int), 1, fp);
482 s += fwrite(&net->
n_inputs,
sizeof(
int), 1, fp);
483 s += fwrite(&net->
n_outputs,
sizeof(
int), 1, fp);
485 while (iter != NULL) {
505 s += fread(&nlayers,
sizeof(
int), 1, fp);
506 s += fread(&ninputs,
sizeof(
int), 1, fp);
507 s += fread(&noutputs,
sizeof(
int), 1, fp);
509 for (
int i = 0; i < nlayers; ++i) {
510 struct Layer *l = malloc(
sizeof(
struct Layer));
double * neural_outputs(const struct Net *net)
Returns the outputs from the output layer of a neural network.
void neural_push(struct Net *net, struct Layer *l)
Inserts a layer at the head of a neural network.
bool neural_mutate(const struct Net *net)
Mutates a neural network.
double neural_size(const struct Net *net)
Returns the total number of non-zero weights in a neural network.
void neural_create(struct Net *net, struct ArgsLayer *arg)
Initialises and creates a new neural network from a parameter list.
double neural_output(const struct Net *net, const int IDX)
Returns the output of a specified neuron in the output layer of a neural network.
void neural_resize(const struct Net *net)
Resizes neural network layers as necessary.
size_t neural_load(struct Net *net, FILE *fp)
Reads a neural network from a file.
void neural_json_import(struct Net *net, const struct ArgsLayer *arg, const cJSON *json)
Creates a neural network from a cJSON object.
void neural_learn(const struct Net *net, const double *truth, const double *input)
Performs a gradient descent update on a neural network.
void neural_free(struct Net *net)
Frees a neural network.
void neural_remove(struct Net *net, const int pos)
Removes a layer from a neural network.
void neural_print(const struct Net *net, const bool print_weights)
Prints a neural network.
void neural_rand(const struct Net *net)
Randomises the layers within a neural network.
void neural_copy(struct Net *dest, const struct Net *src)
Copies a neural network.
void neural_init(struct Net *net)
Initialises an empty neural network.
void neural_propagate(struct Net *net, const double *input, const bool train)
Forward propagates a neural network.
char * neural_json_export(const struct Net *net, const bool return_weights)
Returns a json formatted string representation of a neural network.
void neural_pop(struct Net *net)
Removes the layer at the head of a neural network.
size_t neural_save(const struct Net *net, FILE *fp)
Writes a neural network to a file.
void neural_insert(struct Net *net, struct Layer *l, const int pos)
Inserts a layer into a neural network.
An implementation of a multi-layer perceptron neural network.
static void layer_rand(struct Layer *l)
Randomises a layer.
#define NOISE
Layer type noise.
static void layer_resize(struct Layer *l, const struct Layer *prev)
Resizes a layer using the previous layer's inputs.
static double * layer_output(const struct Layer *l)
Returns the outputs of a layer.
static size_t layer_save(const struct Layer *l, FILE *fp)
Writes the layer to a file.
static struct Layer * layer_init(const struct ArgsLayer *args)
Creates and initialises a new layer.
static struct Layer * layer_copy(const struct Layer *src)
Creates and returns a copy of a specified layer.
#define UPSAMPLE
Layer type upsample.
static void layer_free(const struct Layer *l)
Frees the memory used by the layer.
static void layer_backward(const struct Layer *l, const struct Net *net, const double *input, double *delta)
Backward propagates the error through a layer.
static size_t layer_load(struct Layer *l, FILE *fp)
Reads the layer from a file.
#define LSTM
Layer type LSTM.
#define SOFTMAX
Layer type softmax.
static void layer_update(const struct Layer *l)
Updates the weights and biases of a layer.
#define RECURRENT
Layer type recurrent.
static void layer_forward(const struct Layer *l, const struct Net *net, const double *input)
Forward propagates an input through the layer.
static char * layer_json_export(const struct Layer *l, const bool return_weights)
Returns a json formatted string representation of a layer.
#define AVGPOOL
Layer type average pooling.
static bool layer_mutate(struct Layer *l)
Performs layer mutation.
#define DROPOUT
Layer type dropout.
#define CONVOLUTIONAL
Layer type convolutional.
#define MAXPOOL
Layer type maxpooling.
#define CONNECTED
Layer type connected.
An implementation of a fully-connected layer of perceptrons.
An implementation of a dropout layer.
An implementation of a Gaussian noise adding layer.
An implementation of a recurrent layer of perceptrons.
An implementation of a softmax layer.
Parameters for initialising a neural network layer.
int n_init
Initial number of units / neurons / filters.
int channels
Pool, Conv, and Upsample.
int width
Pool, Conv, and Upsample.
int height
Pool, Conv, and Upsample.
int n_inputs
Number of inputs.
int type
Layer type: CONNECTED, DROPOUT, etc.
struct ArgsLayer * next
Next layer parameters.
Neural network layer data structure.
double * output
Current neuron outputs (after activation function)
int n_inputs
Number of layer inputs.
int n_outputs
Number of layer outputs.
int n_active
Number of active weights / connections.
int out_w
Pool, Conv, and Upsample.
int type
Layer type: CONNECTED, DROPOUT, etc.
int out_c
Pool, Conv, and Upsample.
double * delta
Delta for updating weights.
int out_h
Pool, Conv, and Upsample.
Forward declaration of layer structure.
struct Llist * prev
Pointer to the previous layer (forward)
struct Layer * layer
Pointer to the layer data structure.
struct Llist * next
Pointer to the next layer (backward)
Neural network data structure.
int n_layers
Number of layers (hidden + output)
double * output
Pointer to the network output.
struct Llist * tail
Pointer to the tail layer (first layer)
struct Llist * head
Pointer to the head layer (output layer)
bool train
Whether the network is in training mode.
int n_outputs
Number of network outputs.
int n_inputs
Number of network inputs.
Utility functions for random number handling, etc.