80 new->type = src->
type;
83 new->n_max = src->
n_max;
88 new->width = src->
width;
90 new->size = src->
size;
96 new->decay = src->
decay;
98 new->scale = src->
scale;
119 cJSON_AddNumberToObject(json,
"height", args->
height);
121 if (args->
width > 0) {
122 cJSON_AddNumberToObject(json,
"width", args->
width);
125 cJSON_AddNumberToObject(json,
"channels", args->
channels);
127 if (args->
size > 0) {
128 cJSON_AddNumberToObject(json,
"size", args->
size);
131 cJSON_AddNumberToObject(json,
"stride", args->
stride);
134 cJSON_AddNumberToObject(json,
"pad", args->
pad);
137 cJSON_AddNumberToObject(json,
"n_inputs", args->
n_inputs);
150 if (strncmp(json->string,
"height\0", 7) == 0 && cJSON_IsNumber(json)) {
151 args->
height = json->valueint;
152 }
else if (strncmp(json->string,
"width\0", 6) == 0 &&
153 cJSON_IsNumber(json)) {
154 args->
width = json->valueint;
155 }
else if (strncmp(json->string,
"channels\0", 9) == 0 &&
156 cJSON_IsNumber(json)) {
158 }
else if (strncmp(json->string,
"size\0", 5) == 0 &&
159 cJSON_IsNumber(json)) {
160 args->
size = json->valueint;
161 }
else if (strncmp(json->string,
"stride\0", 7) == 0 &&
162 cJSON_IsNumber(json)) {
163 args->
stride = json->valueint;
164 }
else if (strncmp(json->string,
"pad\0", 4) == 0 && cJSON_IsNumber(json)) {
165 args->
pad = json->valueint;
166 }
else if (strncmp(json->string,
"n_inputs\0", 9) == 0 &&
167 cJSON_IsNumber(json)) {
183 cJSON_AddBoolToObject(json,
"sgd_weights", args->
sgd_weights);
185 cJSON_AddNumberToObject(json,
"eta", args->
eta);
186 cJSON_AddBoolToObject(json,
"evolve_eta", args->
evolve_eta);
188 cJSON_AddNumberToObject(json,
"eta_min", args->
eta_min);
190 cJSON_AddNumberToObject(json,
"momentum", args->
momentum);
191 cJSON_AddNumberToObject(json,
"decay", args->
decay);
204 if (strncmp(json->string,
"sgd_weights\0", 12) == 0 && cJSON_IsBool(json)) {
205 args->
sgd_weights =
true ? json->type == cJSON_True :
false;
206 }
else if (strncmp(json->string,
"eta\0", 4) == 0 && cJSON_IsNumber(json)) {
207 args->
eta = json->valuedouble;
208 }
else if (strncmp(json->string,
"evolve_eta\0", 11) == 0 &&
209 cJSON_IsBool(json)) {
210 args->
evolve_eta =
true ? json->type == cJSON_True :
false;
211 }
else if (strncmp(json->string,
"eta_min\0", 8) == 0 &&
212 cJSON_IsNumber(json)) {
213 args->
eta_min = json->valuedouble;
214 }
else if (strncmp(json->string,
"momentum\0", 9) == 0 &&
215 cJSON_IsNumber(json)) {
217 }
else if (strncmp(json->string,
"decay\0", 6) == 0 &&
218 cJSON_IsNumber(json)) {
219 args->
decay = json->valuedouble;
234 cJSON_AddBoolToObject(json,
"evolve_weights", args->
evolve_weights);
236 cJSON_AddBoolToObject(json,
"evolve_connect", args->
evolve_connect);
237 cJSON_AddBoolToObject(json,
"evolve_neurons", args->
evolve_neurons);
239 cJSON_AddNumberToObject(json,
"n_max", args->
n_max);
240 cJSON_AddNumberToObject(json,
"max_neuron_grow", args->
max_neuron_grow);
253 if (strncmp(json->string,
"evolve_weights\0", 15) == 0 &&
254 cJSON_IsBool(json)) {
256 }
else if (strncmp(json->string,
"evolve_functions\0", 17) == 0 &&
257 cJSON_IsBool(json)) {
259 }
else if (strncmp(json->string,
"evolve_connect\0", 15) == 0 &&
260 cJSON_IsBool(json)) {
262 }
else if (strncmp(json->string,
"evolve_neurons\0", 15) == 0 &&
263 cJSON_IsBool(json)) {
265 }
else if (strncmp(json->string,
"n_max\0", 6) == 0 &&
266 cJSON_IsNumber(json)) {
267 args->
n_max = json->valueint;
268 }
else if (strncmp(json->string,
"max_neuron_grow\0", 16) == 0 &&
269 cJSON_IsNumber(json)) {
285 switch (args->
type) {
296 cJSON_AddStringToObject(json,
"activation",
299 cJSON_AddStringToObject(
300 json,
"recurrent_activation",
314 if (strncmp(json->string,
"activation\0", 15) == 0 &&
315 cJSON_IsString(json)) {
317 }
else if (strncmp(json->string,
"recurrent_activation\0", 17) == 0 &&
318 cJSON_IsString(json)) {
337 cJSON_AddNumberToObject(json,
"probability", args->
probability);
341 cJSON_AddNumberToObject(json,
"scale", args->
scale);
359 if (strncmp(json->string,
"probability\0", 15) == 0 &&
360 cJSON_IsNumber(json)) {
362 }
else if (strncmp(json->string,
"scale\0", 6) == 0 &&
363 cJSON_IsNumber(json)) {
364 args->
scale = json->valuedouble;
383 cJSON *json = cJSON_CreateObject();
385 for (
const struct ArgsLayer *a = args; a != NULL; a = a->
next) {
387 snprintf(name, 256,
"layer_%d", cnt);
389 cJSON *l = cJSON_CreateObject();
390 cJSON_AddItemToObject(json, name, l);
398 cJSON_AddNumberToObject(l,
"n_init", a->n_init);
403 char *
string = cJSON_Print(json);
417 for (cJSON *iter = json; iter != NULL; iter = iter->next) {
418 if (strncmp(iter->string,
"type\0", 5) == 0 && cJSON_IsString(iter)) {
431 if (strncmp(iter->string,
"n_init\0", 7) == 0 && cJSON_IsNumber(iter)) {
432 args->
n_init = iter->valueint;
453 while (*largs != NULL) {
455 *largs = (*largs)->
next;
478 printf(
"Error: input channels < 1\n");
482 printf(
"Error: input height < 1\n");
485 if (arg->
width < 1) {
486 printf(
"Error: input width < 1\n");
490 printf(
"Error: number of inputs < 1\n");
504 printf(
"Error: empty layer argument list\n");
510 printf(
"Error: evolving neurons but max_neuron_grow < 1\n");
517 }
while (arg != NULL);
529 while (tail->
next != NULL) {
575 while (iter != NULL) {
593 s += fwrite(&n,
sizeof(
int), 1, fp);
595 while (iter != NULL) {
596 s += fwrite(&iter->
type,
sizeof(
int), 1, fp);
597 s += fwrite(&iter->
n_inputs,
sizeof(
int), 1, fp);
598 s += fwrite(&iter->
n_init,
sizeof(
int), 1, fp);
599 s += fwrite(&iter->
n_max,
sizeof(
int), 1, fp);
601 s += fwrite(&iter->
function,
sizeof(
int), 1, fp);
603 s += fwrite(&iter->
height,
sizeof(
int), 1, fp);
604 s += fwrite(&iter->
width,
sizeof(
int), 1, fp);
605 s += fwrite(&iter->
channels,
sizeof(
int), 1, fp);
606 s += fwrite(&iter->
size,
sizeof(
int), 1, fp);
607 s += fwrite(&iter->
stride,
sizeof(
int), 1, fp);
608 s += fwrite(&iter->
pad,
sizeof(
int), 1, fp);
609 s += fwrite(&iter->
eta,
sizeof(
double), 1, fp);
610 s += fwrite(&iter->
eta_min,
sizeof(
double), 1, fp);
611 s += fwrite(&iter->
momentum,
sizeof(
double), 1, fp);
612 s += fwrite(&iter->
decay,
sizeof(
double), 1, fp);
613 s += fwrite(&iter->
probability,
sizeof(
double), 1, fp);
614 s += fwrite(&iter->
scale,
sizeof(
double), 1, fp);
618 s += fwrite(&iter->
evolve_eta,
sizeof(
bool), 1, fp);
620 s += fwrite(&iter->
sgd_weights,
sizeof(
bool), 1, fp);
638 s += fread(&n,
sizeof(
int), 1, fp);
639 for (
int i = 0; i < n; ++i) {
642 s += fread(&arg->
type,
sizeof(
int), 1, fp);
643 s += fread(&arg->
n_inputs,
sizeof(
int), 1, fp);
644 s += fread(&arg->
n_init,
sizeof(
int), 1, fp);
645 s += fread(&arg->
n_max,
sizeof(
int), 1, fp);
647 s += fread(&arg->
function,
sizeof(
int), 1, fp);
649 s += fread(&arg->
height,
sizeof(
int), 1, fp);
650 s += fread(&arg->
width,
sizeof(
int), 1, fp);
651 s += fread(&arg->
channels,
sizeof(
int), 1, fp);
652 s += fread(&arg->
size,
sizeof(
int), 1, fp);
653 s += fread(&arg->
stride,
sizeof(
int), 1, fp);
654 s += fread(&arg->
pad,
sizeof(
int), 1, fp);
655 s += fread(&arg->
eta,
sizeof(
double), 1, fp);
656 s += fread(&arg->
eta_min,
sizeof(
double), 1, fp);
657 s += fread(&arg->
momentum,
sizeof(
double), 1, fp);
658 s += fread(&arg->
decay,
sizeof(
double), 1, fp);
659 s += fread(&arg->
probability,
sizeof(
double), 1, fp);
660 s += fread(&arg->
scale,
sizeof(
double), 1, fp);
664 s += fread(&arg->
evolve_eta,
sizeof(
bool), 1, fp);
666 s += fread(&arg->
sgd_weights,
sizeof(
bool), 1, fp);
667 if (*largs == NULL) {
671 while (iter->
next != NULL) {
void neural_create(struct Net *net, struct ArgsLayer *arg)
Initialises and creates a new neural network from a parameter list.
void neural_free(struct Net *net)
Frees a neural network.
void neural_init(struct Net *net)
Initialises an empty neural network.
const char * neural_activation_string(const int a)
Returns the name of a specified activation function.
int neural_activation_as_int(const char *a)
Returns the integer representation of an activation function.
Neural network activation functions.
#define LOGISTIC
Logistic [0,1].
const char * layer_type_as_string(const int type)
Returns a string representation of a layer type from an integer.
int layer_type_as_int(const char *type)
Returns the integer representation of a layer type given a name.
bool layer_receives_images(const int type)
Returns a whether a layer type expects images as input.
#define NOISE
Layer type noise.
#define LAYER_EVOLVE_ETA
Layer may evolve rate of gradient descent.
#define LAYER_EVOLVE_FUNCTIONS
Layer may evolve functions.
#define UPSAMPLE
Layer type upsample.
#define LAYER_EVOLVE_WEIGHTS
Layer may evolve weights.
#define LSTM
Layer type LSTM.
#define SOFTMAX
Layer type softmax.
#define LAYER_EVOLVE_NEURONS
Layer may evolve neurons.
#define LAYER_EVOLVE_CONNECT
Layer may evolve connectivity.
#define AVGPOOL
Layer type average pooling.
#define DROPOUT
Layer type dropout.
#define LAYER_SGD_WEIGHTS
Layer may perform gradient descent.
#define MAXPOOL
Layer type maxpooling.
#define CONNECTED
Layer type connected.
static void layer_args_json_export_activation(cJSON *json, const struct ArgsLayer *args)
Adds layer activation function to a json object.
static bool layer_args_json_export_scale(cJSON *json, const struct ArgsLayer *args)
Adds layer scaling parameters to a json object.
static bool layer_args_json_import_inputs(struct ArgsLayer *args, const cJSON *json)
Sets the layer input parameters from a cJSON object.
char * layer_args_json_export(struct ArgsLayer *args)
Returns a json formatted string of the neural layer parameters.
size_t layer_args_load(struct ArgsLayer **largs, FILE *fp)
Loads neural network layer parameters.
void layer_args_init(struct ArgsLayer *args)
Sets layer parameters to default values.
char * layer_args_json_import(struct ArgsLayer *args, cJSON *json)
Sets the layer parameters from a cJSON object.
struct ArgsLayer * layer_args_tail(struct ArgsLayer *head)
Returns the current output layer arguments.
uint32_t layer_args_opt(const struct ArgsLayer *args)
Returns a bitstring representing the permissions granted by a layer.
void layer_args_free(struct ArgsLayer **largs)
Frees memory used by a list of layer parameters and points to NULL.
size_t layer_args_save(const struct ArgsLayer *args, FILE *fp)
Saves neural network layer parameters.
void layer_args_validate(struct ArgsLayer *args)
Checks network layer arguments are valid.
static bool layer_args_json_import_evo(struct ArgsLayer *args, const cJSON *json)
Sets the layer evolutionary parameters from a cJSON object.
static void layer_args_json_export_sgd(cJSON *json, const struct ArgsLayer *args)
Adds layer gradient descent parameters to a json object.
static void layer_args_json_export_inputs(cJSON *json, const struct ArgsLayer *args)
Adds layer input parameters to a json object.
static void layer_args_validate_inputs(struct ArgsLayer *arg)
Checks input layer arguments are valid.
static bool layer_args_json_import_sgd(struct ArgsLayer *args, const cJSON *json)
Sets the layer SGD parameters from a cJSON object.
static bool layer_args_json_import_scale(struct ArgsLayer *args, const cJSON *json)
Sets the layer scaling parameters from a cJSON object.
static void layer_args_json_export_evo(cJSON *json, const struct ArgsLayer *args)
Adds layer evolutionary parameters to a json object.
struct ArgsLayer * layer_args_copy(const struct ArgsLayer *src)
Creates and returns a copy of specified layer parameters.
static int layer_args_length(const struct ArgsLayer *args)
Returns the length of the neural network layer parameter list.
static bool layer_args_json_import_activation(struct ArgsLayer *args, const cJSON *json)
Sets the layer activation from a cJSON object.
An implementation of an average pooling layer.
An implementation of a fully-connected layer of perceptrons.
An implementation of a 2D convolutional layer.
An implementation of a dropout layer.
An implementation of a long short-term memory layer.
An implementation of a 2D maxpooling layer.
An implementation of a Gaussian noise adding layer.
An implementation of a recurrent layer of perceptrons.
An implementation of a softmax layer.
An implementation of a 2D upsampling layer.
Parameters for initialising a neural network layer.
_Bool evolve_weights
Ability to evolve weights.
int n_init
Initial number of units / neurons / filters.
_Bool evolve_neurons
Ability to evolve number of units.
_Bool sgd_weights
Ability to update weights with gradient descent.
_Bool evolve_functions
Ability to evolve activation function.
double decay
Weight decay for gradient descent.
double momentum
Momentum for gradient descent.
int function
Activation function.
_Bool evolve_eta
Ability to evolve gradient descent rate.
int max_neuron_grow
Maximum number neurons to add per mutation event.
double probability
Usage depends on layer implementation.
double eta
Gradient descent rate.
double eta_min
Current gradient descent rate.
int channels
Pool, Conv, and Upsample.
int n_max
Maximum number of units / neurons.
double scale
Usage depends on layer implementation.
int width
Pool, Conv, and Upsample.
int height
Pool, Conv, and Upsample.
int stride
Pool, Conv, and Upsample.
int n_inputs
Number of inputs.
int type
Layer type: CONNECTED, DROPOUT, etc.
struct ArgsLayer * next
Next layer parameters.
_Bool evolve_connect
Ability to evolve weight connectivity.
int recurrent_function
Recurrent activation function.
Neural network data structure.
Utility functions for random number handling, etc.