75 const size_t workspace_size = (size_t) l->
out_h * l->
out_w * l->
size *
77 if (workspace_size < 1) {
78 printf(
"neural_layer_convolutional: invalid workspace size\n");
82 return workspace_size;
114 l->
mu = malloc(
sizeof(
double) *
N_MU);
206 printf(
"neural_layer_convolut_copy() incorrect source layer type\n");
209 struct Layer *l = malloc(
sizeof(
struct Layer));
241 memcpy(l->
mu, src->
mu,
sizeof(
double) *
N_MU);
274 blas_gemm(0, 0, m, n, k, 1, a, k, input, n, 1,
c, n);
278 blas_gemm(0, 0, m, n, k, 1, a, k, b, n, 1,
c, n);
281 for (
int j = 0; j < n; ++j) {
297 const struct Net *net,
const double *input,
309 const double *a = l->
delta;
313 blas_gemm(0, 1, m, n, k, 1, a, k, input, k, 1,
c, n);
317 blas_gemm(0, 1, m, n, k, 1, a, k, b, k, 1,
c, n);
322 const double *b = l->
delta;
327 blas_gemm(1, 0, n, k, m, 1, a, n, b, k, 0,
c, k);
420 const int old_n_biases = l->
n_biases;
499 const bool print_weights)
502 printf(
"%s\n", json_str);
515 const bool return_weights)
517 cJSON *json = cJSON_CreateObject();
518 cJSON_AddStringToObject(json,
"type",
"convolutional");
519 cJSON_AddStringToObject(json,
"activation",
521 cJSON_AddNumberToObject(json,
"n_inputs", l->
n_inputs);
522 cJSON_AddNumberToObject(json,
"n_outputs", l->
n_outputs);
523 cJSON_AddNumberToObject(json,
"n_filters", l->
n_filters);
524 cJSON_AddNumberToObject(json,
"size", l->
size);
525 cJSON_AddNumberToObject(json,
"stride", l->
stride);
526 cJSON_AddNumberToObject(json,
"pad", l->
pad);
527 cJSON_AddNumberToObject(json,
"eta", l->
eta);
528 cJSON *mutation = cJSON_CreateDoubleArray(l->
mu,
N_MU);
529 cJSON_AddItemToObject(json,
"mutation", mutation);
531 cJSON *
weights = cJSON_Parse(weights_str);
533 cJSON_AddItemToObject(json,
"weights",
weights);
534 char *
string = cJSON_Print(json);
549 s += fwrite(&l->
options,
sizeof(uint32_t), 1, fp);
550 s += fwrite(&l->
function,
sizeof(
int), 1, fp);
551 s += fwrite(&l->
height,
sizeof(
int), 1, fp);
552 s += fwrite(&l->
width,
sizeof(
int), 1, fp);
553 s += fwrite(&l->
channels,
sizeof(
int), 1, fp);
554 s += fwrite(&l->
n_filters,
sizeof(
int), 1, fp);
555 s += fwrite(&l->
stride,
sizeof(
int), 1, fp);
556 s += fwrite(&l->
size,
sizeof(
int), 1, fp);
557 s += fwrite(&l->
pad,
sizeof(
int), 1, fp);
558 s += fwrite(&l->
out_h,
sizeof(
int), 1, fp);
559 s += fwrite(&l->
out_w,
sizeof(
int), 1, fp);
560 s += fwrite(&l->
out_c,
sizeof(
int), 1, fp);
561 s += fwrite(&l->
n_biases,
sizeof(
int), 1, fp);
562 s += fwrite(&l->
n_outputs,
sizeof(
int), 1, fp);
563 s += fwrite(&l->
n_inputs,
sizeof(
int), 1, fp);
565 s += fwrite(&l->
n_weights,
sizeof(
int), 1, fp);
566 s += fwrite(&l->
n_active,
sizeof(
int), 1, fp);
567 s += fwrite(&l->
eta,
sizeof(
double), 1, fp);
568 s += fwrite(&l->
eta_max,
sizeof(
double), 1, fp);
569 s += fwrite(&l->
eta_min,
sizeof(
double), 1, fp);
570 s += fwrite(&l->
momentum,
sizeof(
double), 1, fp);
571 s += fwrite(&l->
decay,
sizeof(
double), 1, fp);
578 s += fwrite(l->
mu,
sizeof(
double),
N_MU, fp);
592 s += fread(&l->
options,
sizeof(uint32_t), 1, fp);
593 s += fread(&l->
function,
sizeof(
int), 1, fp);
594 s += fread(&l->
height,
sizeof(
int), 1, fp);
595 s += fread(&l->
width,
sizeof(
int), 1, fp);
596 s += fread(&l->
channels,
sizeof(
int), 1, fp);
597 s += fread(&l->
n_filters,
sizeof(
int), 1, fp);
598 s += fread(&l->
stride,
sizeof(
int), 1, fp);
599 s += fread(&l->
size,
sizeof(
int), 1, fp);
600 s += fread(&l->
pad,
sizeof(
int), 1, fp);
601 s += fread(&l->
out_h,
sizeof(
int), 1, fp);
602 s += fread(&l->
out_w,
sizeof(
int), 1, fp);
603 s += fread(&l->
out_c,
sizeof(
int), 1, fp);
604 s += fread(&l->
n_biases,
sizeof(
int), 1, fp);
605 s += fread(&l->
n_outputs,
sizeof(
int), 1, fp);
606 s += fread(&l->
n_inputs,
sizeof(
int), 1, fp);
608 s += fread(&l->
n_weights,
sizeof(
int), 1, fp);
609 s += fread(&l->
n_active,
sizeof(
int), 1, fp);
610 s += fread(&l->
eta,
sizeof(
double), 1, fp);
611 s += fread(&l->
eta_max,
sizeof(
double), 1, fp);
612 s += fread(&l->
eta_min,
sizeof(
double), 1, fp);
613 s += fread(&l->
momentum,
sizeof(
double), 1, fp);
614 s += fread(&l->
decay,
sizeof(
double), 1, fp);
622 s += fread(l->
mu,
sizeof(
double),
N_MU, fp);
void blas_scal(const int N, const double ALPHA, double *X, const int INCX)
Scales vector X by the scalar ALPHA and overwrites it with the result.
double blas_sum(const double *X, const int N)
Returns the sum of the vector X.
void blas_axpy(const int N, const double ALPHA, const double *X, const int INCX, double *Y, const int INCY)
Multiplies vector X by the scalar ALPHA and adds it to the vector Y.
void blas_gemm(const int TA, const int TB, const int M, const int N, const int K, const double ALPHA, const double *A, const int lda, const double *B, const int ldb, const double BETA, double *C, const int ldc)
Performs the matrix-matrix multiplication: .
Basic linear algebra functions.
void im2col(const double *data_im, const int channels, const int height, const int width, const int ksize, const int stride, const int pad, double *data_col)
Transforms an image vector to a column vector.
void col2im(const double *data_col, const int channels, const int height, const int width, const int ksize, const int stride, const int pad, double *data_im)
Transforms a column vector to an image vector.
Image handling functions.
const char * neural_activation_string(const int a)
Returns the name of a specified activation function.
void neural_gradient_array(const double *state, double *delta, const int n, const int a)
Applies a gradient function to a vector of neuron states.
void neural_activate_array(double *state, double *output, const int n, const int a)
Applies an activation function to a vector of neuron states.
Neural network activation functions.
void layer_guard_biases(const struct Layer *l)
Check number of biases is within bounds.
bool layer_mutate_connectivity(struct Layer *l, const double mu_enable, const double mu_disable)
Mutates a layer's connectivity by zeroing weights.
void layer_defaults(struct Layer *l)
Initialises a layer to default values.
bool layer_mutate_functions(struct Layer *l, const double mu)
Mutates a layer's activation function by random selection.
char * layer_weight_json(const struct Layer *l, const bool return_weights)
Returns a json formatted string representation of a layer's weights.
void layer_weight_clamp(const struct Layer *l)
Clamps a layer's weights and biases in range [WEIGHT_MIN, WEIGHT_MAX].
void layer_guard_outputs(const struct Layer *l)
Check number of outputs is within bounds.
void layer_weight_rand(struct Layer *l)
Randomises a layer's weights and biases.
void layer_calc_n_active(struct Layer *l)
Recalculates the number of active connections within a layer.
void layer_init_eta(struct Layer *l)
Initialises a layer's gradient descent rate.
bool layer_mutate_eta(struct Layer *l, const double mu)
Mutates the gradient descent rate of a neural layer.
bool layer_mutate_weights(struct Layer *l, const double mu)
Mutates a layer's weights and biases by adding random numbers from a Gaussian normal distribution wit...
void layer_guard_weights(const struct Layer *l)
Check number of weights is within bounds.
#define LAYER_EVOLVE_ETA
Layer may evolve rate of gradient descent.
#define LAYER_EVOLVE_FUNCTIONS
Layer may evolve functions.
#define WEIGHT_SD_INIT
Std dev of Gaussian for weight initialisation.
#define LAYER_EVOLVE_WEIGHTS
Layer may evolve weights.
#define LAYER_EVOLVE_NEURONS
Layer may evolve neurons.
#define LAYER_EVOLVE_CONNECT
Layer may evolve connectivity.
#define WEIGHT_SD
Std dev of Gaussian for weight resizing.
#define LAYER_SGD_WEIGHTS
Layer may perform gradient descent.
#define CONVOLUTIONAL
Layer type convolutional.
static void layer_print(const struct Layer *l, const bool print_weights)
Prints the layer.
uint32_t layer_args_opt(const struct ArgsLayer *args)
Returns a bitstring representing the permissions granted by a layer.
static int get_out_width(const struct Layer *l)
Returns the output width of a convolutional layer.
double * neural_layer_convolutional_output(const struct Layer *l)
Returns the output from a convolutional layer.
void neural_layer_convolutional_update(const struct Layer *l)
Updates the weights and biases of a convolutional layer.
void neural_layer_convolutional_forward(const struct Layer *l, const struct Net *net, const double *input)
Forward propagates a convolutional layer.
void neural_layer_convolutional_resize(struct Layer *l, const struct Layer *prev)
Resizes a convolutional layer if the previous layer has changed size.
static void malloc_layer_arrays(struct Layer *l)
Allocate memory used by a convolutional layer.
static void realloc_layer_arrays(struct Layer *l)
Resize memory used by a convolutional layer.
#define N_MU
Number of mutation rates applied to a convolutional layer.
struct Layer * neural_layer_convolutional_copy(const struct Layer *src)
Initialises and copies one convolutional layer from another.
void neural_layer_convolutional_print(const struct Layer *l, const bool print_weights)
Prints a convolutional layer.
static size_t get_workspace_size(const struct Layer *l)
Returns the memory workspace size for a convolutional layer.
bool neural_layer_convolutional_mutate(struct Layer *l)
Mutates a convolutional layer.
static const int MU_TYPE[(6)]
Self-adaptation method for mutating a convolutional layer.
void neural_layer_convolutional_free(const struct Layer *l)
Free memory used by a convolutional layer.
static int get_out_height(const struct Layer *l)
Returns the output height of a convolutional layer.
size_t neural_layer_convolutional_save(const struct Layer *l, FILE *fp)
Writes a convolutional layer to a file.
static void neural_layer_convolutional_add_filters(struct Layer *l, const int N)
Adds N filters to a layer. Negative N removes filters.
static int neural_layer_convolutional_mutate_filter(const struct Layer *l, const double mu)
Returns the number of kernel filters to add or remove from a layer.
size_t neural_layer_convolutional_load(struct Layer *l, FILE *fp)
Reads a convolutional layer from a file.
static void guard_malloc(const struct Layer *l)
Check memory allocation is within bounds.
void neural_layer_convolutional_rand(struct Layer *l)
Randomises the weights of a convolutional layer.
void neural_layer_convolutional_init(struct Layer *l, const struct ArgsLayer *args)
Initialises a 2D convolutional layer.
void neural_layer_convolutional_backward(const struct Layer *l, const struct Net *net, const double *input, double *delta)
Backward propagates a convolutional layer.
char * neural_layer_convolutional_json_export(const struct Layer *l, const bool return_weights)
Returns a json formatted string of a convolutional layer.
An implementation of a 2D convolutional layer.
void sam_init(double *mu, const int N, const int *type)
Initialises self-adaptive mutation rates.
void sam_adapt(double *mu, const int N, const int *type)
Self-adapts mutation rates.
Self-adaptive mutation functions.
#define SAM_RATE_SELECT
Ten normally distributed rates.
Parameters for initialising a neural network layer.
int n_init
Initial number of units / neurons / filters.
double decay
Weight decay for gradient descent.
double momentum
Momentum for gradient descent.
int function
Activation function.
int max_neuron_grow
Maximum number neurons to add per mutation event.
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.
int width
Pool, Conv, and Upsample.
int height
Pool, Conv, and Upsample.
int stride
Pool, Conv, and Upsample.
Neural network layer data structure.
double * output
Current neuron outputs (after activation function)
double decay
Weight decay for gradient descent.
double * state
Current neuron states (before activation function)
int max_neuron_grow
Maximum number neurons to add per mutation event.
int stride
Pool, Conv, and Upsample.
int n_inputs
Number of layer inputs.
int n_biases
Number of layer biases.
bool * weight_active
Whether each connection is present in the layer.
double * weights
Weights for calculating neuron states.
double * weight_updates
Updates to weights.
double * mu
Mutation rates.
int channels
Pool, Conv, and Upsample.
int function
Layer activation function.
int height
Pool, Conv, and Upsample.
struct LayerVtbl const * layer_vptr
Functions acting on layers.
int max_outputs
Maximum number of neurons in the layer.
int width
Pool, Conv, and Upsample.
int n_weights
Number of layer weights.
double * bias_updates
Updates to biases.
double eta_max
Maximum gradient descent rate.
int n_outputs
Number of layer outputs.
double * biases
Biases for calculating neuron states.
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.
uint32_t options
Bitwise layer options permitting evolution, SGD, etc.
int out_h
Pool, Conv, and Upsample.
double eta_min
Minimum gradient descent rate.
double eta
Gradient descent rate.
double momentum
Momentum for gradient descent.
Neural network data structure.
double rand_normal(const double mu, const double sigma)
Returns a random Gaussian with specified mean and standard deviation.
double rand_uniform(const double min, const double max)
Returns a uniform random float [min,max].
Utility functions for random number handling, etc.
static double clamp(const double a, const double min, const double max)
Returns a float clamped within the specified range.