61 l->
mu = malloc(
sizeof(
double) *
N_MU);
125 printf(
"neural_layer_connected_copy(): incorrect source layer type\n");
128 struct Layer *l = malloc(
sizeof(
struct Layer));
153 memcpy(l->
mu, src->
mu,
sizeof(
double) *
N_MU);
180 const double *a = input;
184 blas_gemm(0, 1, 1, n, k, 1, a, k, b, k, 1,
c, n);
197 const double *input,
double *
delta)
204 const double *a = l->
delta;
205 const double *b = input;
208 blas_gemm(1, 0, m, n, 1, 1, a, m, b, n, 1,
c, n);
213 const double *a = l->
delta;
216 blas_gemm(0, 0, 1, n, k, 1, a, k, b, n, 1,
c, n);
250 printf(
"neural_layer_connected: malloc() invalid resize\n");
260 for (
int j = 0; j < prev->
n_outputs; ++j) {
342 printf(
"%s\n", json_str);
355 const bool return_weights)
357 cJSON *json = cJSON_CreateObject();
358 cJSON_AddStringToObject(json,
"type",
"connected");
359 cJSON_AddStringToObject(json,
"activation",
361 cJSON_AddNumberToObject(json,
"n_inputs", l->
n_inputs);
362 cJSON_AddNumberToObject(json,
"n_outputs", l->
n_outputs);
363 cJSON_AddNumberToObject(json,
"eta", l->
eta);
364 cJSON *mutation = cJSON_CreateDoubleArray(l->
mu,
N_MU);
365 cJSON_AddItemToObject(json,
"mutation", mutation);
367 cJSON *
weights = cJSON_Parse(weights_str);
369 cJSON_AddItemToObject(json,
"weights",
weights);
370 char *
string = cJSON_Print(json);
385 s += fwrite(&l->
n_inputs,
sizeof(
int), 1, fp);
386 s += fwrite(&l->
n_outputs,
sizeof(
int), 1, fp);
387 s += fwrite(&l->
n_biases,
sizeof(
int), 1, fp);
389 s += fwrite(&l->
n_weights,
sizeof(
int), 1, fp);
390 s += fwrite(&l->
options,
sizeof(uint32_t), 1, fp);
391 s += fwrite(&l->
function,
sizeof(
int), 1, fp);
393 s += fwrite(&l->
eta,
sizeof(
double), 1, fp);
394 s += fwrite(&l->
eta_max,
sizeof(
double), 1, fp);
395 s += fwrite(&l->
eta_min,
sizeof(
double), 1, fp);
396 s += fwrite(&l->
momentum,
sizeof(
double), 1, fp);
397 s += fwrite(&l->
decay,
sizeof(
double), 1, fp);
398 s += fwrite(&l->
n_active,
sizeof(
int), 1, fp);
404 s += fwrite(l->
mu,
sizeof(
double),
N_MU, fp);
418 s += fread(&l->
n_inputs,
sizeof(
int), 1, fp);
419 s += fread(&l->
n_outputs,
sizeof(
int), 1, fp);
420 s += fread(&l->
n_biases,
sizeof(
int), 1, fp);
422 s += fread(&l->
n_weights,
sizeof(
int), 1, fp);
423 s += fread(&l->
options,
sizeof(uint32_t), 1, fp);
424 s += fread(&l->
function,
sizeof(
int), 1, fp);
426 s += fread(&l->
eta,
sizeof(
double), 1, fp);
427 s += fread(&l->
eta_max,
sizeof(
double), 1, fp);
428 s += fread(&l->
eta_min,
sizeof(
double), 1, fp);
429 s += fread(&l->
momentum,
sizeof(
double), 1, fp);
430 s += fread(&l->
decay,
sizeof(
double), 1, fp);
431 s += fread(&l->
n_active,
sizeof(
int), 1, fp);
441 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.
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.
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.
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.
int layer_mutate_neurons(const struct Layer *l, const double mu)
Returns the number of neurons to add or remove from a layer.
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_add_neurons(struct Layer *l, const int N)
Adds N neurons to a layer. Negative N removes neurons.
void layer_ensure_input_represention(struct Layer *l)
Ensures that each neuron is connected to at least one input and each input is connected to at least o...
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.
static void layer_print(const struct Layer *l, const bool print_weights)
Prints the layer.
#define CONNECTED
Layer type connected.
#define N_WEIGHTS_MAX
Maximum number of weights per layer.
uint32_t layer_args_opt(const struct ArgsLayer *args)
Returns a bitstring representing the permissions granted by a layer.
size_t neural_layer_connected_save(const struct Layer *l, FILE *fp)
Writes a connected layer to a file.
struct Layer * neural_layer_connected_copy(const struct Layer *src)
Initialises and creates a copy of one connected layer from another.
void neural_layer_connected_print(const struct Layer *l, const bool print_weights)
Prints a connected layer.
char * neural_layer_connected_json_export(const struct Layer *l, const bool return_weights)
Returns a json formatted string representation of a connected layer.
void neural_layer_connected_rand(struct Layer *l)
Randomises a connected layer weights.
void neural_layer_connected_resize(struct Layer *l, const struct Layer *prev)
Resizes a connected layer if the previous layer has changed size.
double * neural_layer_connected_output(const struct Layer *l)
Returns the output from a connected layer.
static void malloc_layer_arrays(struct Layer *l)
Allocate memory used by a connected layer.
#define N_MU
Number of mutation rates applied to a connected layer.
static const int MU_TYPE[(6)]
Self-adaptation method for mutating a connected layer.
size_t neural_layer_connected_load(struct Layer *l, FILE *fp)
Reads a connected layer from a file.
void neural_layer_connected_backward(const struct Layer *l, const struct Net *net, const double *input, double *delta)
Backward propagates a connected layer.
bool neural_layer_connected_mutate(struct Layer *l)
Mutates a connected layer.
void neural_layer_connected_forward(const struct Layer *l, const struct Net *net, const double *input)
Forward propagates a connected layer.
void neural_layer_connected_init(struct Layer *l, const struct ArgsLayer *args)
Initialises a fully-connected layer.
void neural_layer_connected_update(const struct Layer *l)
Updates the weights and biases of a connected layer.
void neural_layer_connected_free(const struct Layer *l)
Free memory used by a connected layer.
An implementation of a fully-connected layer of perceptrons.
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 n_max
Maximum number of units / neurons.
int n_inputs
Number of inputs.
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 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 function
Layer activation function.
struct LayerVtbl const * layer_vptr
Functions acting on layers.
int max_outputs
Maximum number of neurons in the layer.
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.
Utility functions for random number handling, etc.