116 l->
uf = malloc(
sizeof(
struct Layer));
117 l->
ui = malloc(
sizeof(
struct Layer));
118 l->
ug = malloc(
sizeof(
struct Layer));
119 l->
uo = malloc(
sizeof(
struct Layer));
120 l->
wf = malloc(
sizeof(
struct Layer));
121 l->
wi = malloc(
sizeof(
struct Layer));
122 l->
wg = malloc(
sizeof(
struct Layer));
123 l->
wo = malloc(
sizeof(
struct Layer));
175 size_t size = l->
n_outputs *
sizeof(double);
317 l->
mu = malloc(
sizeof(
double) *
N_MU);
330 printf(
"neural_layer_lstm_copy(): incorrect source layer type\n");
333 struct Layer *l = malloc(
sizeof(
struct Layer));
363 l->
mu = malloc(
sizeof(
double) *
N_MU);
364 memcpy(l->
mu, src->
mu,
sizeof(
double) *
N_MU);
462 const double *input,
double *
delta)
593 printf(
"%s\n", json_str);
607 cJSON *json = cJSON_CreateObject();
608 cJSON_AddStringToObject(json,
"type",
"lstm");
609 cJSON_AddStringToObject(json,
"activation",
611 cJSON_AddStringToObject(json,
"recurrent_activation",
613 cJSON_AddNumberToObject(json,
"n_inputs", l->
n_inputs);
614 cJSON_AddNumberToObject(json,
"n_outputs", l->
n_outputs);
615 cJSON_AddNumberToObject(json,
"eta", l->
eta);
616 cJSON *mutation = cJSON_CreateDoubleArray(l->
mu,
N_MU);
617 cJSON_AddItemToObject(json,
"mutation", mutation);
619 cJSON *
uf = cJSON_Parse(weights_str);
621 cJSON_AddItemToObject(json,
"uf_layer",
uf);
623 cJSON *
ui = cJSON_Parse(weights_str);
625 cJSON_AddItemToObject(json,
"ui_layer",
ui);
627 cJSON *
ug = cJSON_Parse(weights_str);
629 cJSON_AddItemToObject(json,
"ug_layer",
ug);
631 cJSON *
uo = cJSON_Parse(weights_str);
633 cJSON_AddItemToObject(json,
"uo_layer",
uo);
635 cJSON *
wf = cJSON_Parse(weights_str);
637 cJSON_AddItemToObject(json,
"wf_layer",
wf);
639 cJSON *
wi = cJSON_Parse(weights_str);
641 cJSON_AddItemToObject(json,
"wi_layer",
wi);
643 cJSON *
wg = cJSON_Parse(weights_str);
645 cJSON_AddItemToObject(json,
"wg_layer",
wg);
647 cJSON *
wo = cJSON_Parse(weights_str);
649 cJSON_AddItemToObject(json,
"wo_layer",
wo);
650 char *
string = cJSON_Print(json);
665 s += fwrite(&l->
n_inputs,
sizeof(
int), 1, fp);
666 s += fwrite(&l->
n_outputs,
sizeof(
int), 1, fp);
667 s += fwrite(&l->
n_weights,
sizeof(
int), 1, fp);
668 s += fwrite(&l->
n_biases,
sizeof(
int), 1, fp);
670 s += fwrite(&l->
n_active,
sizeof(
int), 1, fp);
671 s += fwrite(&l->
eta,
sizeof(
double), 1, fp);
672 s += fwrite(&l->
eta_max,
sizeof(
double), 1, fp);
673 s += fwrite(&l->
momentum,
sizeof(
double), 1, fp);
674 s += fwrite(&l->
decay,
sizeof(
double), 1, fp);
676 s += fwrite(&l->
options,
sizeof(uint32_t), 1, fp);
677 s += fwrite(l->
mu,
sizeof(
double),
N_MU, fp);
681 s += fwrite(l->
f,
sizeof(
double), l->
n_outputs, fp);
682 s += fwrite(l->
i,
sizeof(
double), l->
n_outputs, fp);
683 s += fwrite(l->
g,
sizeof(
double), l->
n_outputs, fp);
684 s += fwrite(l->
o,
sizeof(
double), l->
n_outputs, fp);
685 s += fwrite(l->
c,
sizeof(
double), l->
n_outputs, fp);
686 s += fwrite(l->
h,
sizeof(
double), l->
n_outputs, fp);
690 s += fwrite(l->
dc,
sizeof(
double), l->
n_outputs, fp);
712 s += fread(&l->
n_inputs,
sizeof(
int), 1, fp);
713 s += fread(&l->
n_outputs,
sizeof(
int), 1, fp);
714 s += fread(&l->
n_weights,
sizeof(
int), 1, fp);
715 s += fread(&l->
n_biases,
sizeof(
int), 1, fp);
717 s += fread(&l->
n_active,
sizeof(
int), 1, fp);
718 s += fread(&l->
eta,
sizeof(
double), 1, fp);
719 s += fread(&l->
eta_max,
sizeof(
double), 1, fp);
720 s += fread(&l->
momentum,
sizeof(
double), 1, fp);
721 s += fread(&l->
decay,
sizeof(
double), 1, fp);
723 s += fread(&l->
options,
sizeof(uint32_t), 1, fp);
728 l->
mu = malloc(
sizeof(
double) *
N_MU);
729 s += fread(l->
mu,
sizeof(
double),
N_MU, fp);
733 s += fread(l->
f,
sizeof(
double), l->
n_outputs, fp);
734 s += fread(l->
i,
sizeof(
double), l->
n_outputs, fp);
735 s += fread(l->
g,
sizeof(
double), l->
n_outputs, fp);
736 s += fread(l->
o,
sizeof(
double), l->
n_outputs, fp);
737 s += fread(l->
c,
sizeof(
double), l->
n_outputs, fp);
738 s += fread(l->
h,
sizeof(
double), l->
n_outputs, fp);
742 s += fread(l->
dc,
sizeof(
double), l->
n_outputs, fp);
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_mul(const int N, const double *X, const int INCX, double *Y, const int INCY)
Multiplies vector X by the vector Y and stores the result in vector Y.
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.
#define LINEAR
Linear [-inf,inf].
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_guard_outputs(const struct Layer *l)
Check number of outputs is within bounds.
void layer_add_neurons(struct Layer *l, const int N)
Adds N neurons to a layer. Negative N removes neurons.
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...
static void layer_rand(struct Layer *l)
Randomises a layer.
static void layer_resize(struct Layer *l, const struct Layer *prev)
Resizes a layer using the previous layer's inputs.
#define LAYER_EVOLVE_ETA
Layer may evolve rate of gradient descent.
#define LAYER_EVOLVE_FUNCTIONS
Layer may evolve functions.
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 LAYER_EVOLVE_WEIGHTS
Layer may evolve weights.
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 LAYER_EVOLVE_NEURONS
Layer may evolve neurons.
#define LAYER_EVOLVE_CONNECT
Layer may evolve connectivity.
static void layer_update(const struct Layer *l)
Updates the weights and biases of a layer.
static void layer_forward(const struct Layer *l, const struct Net *net, const double *input)
Forward propagates an input through the layer.
#define LAYER_SGD_WEIGHTS
Layer may perform gradient descent.
#define CONNECTED
Layer type connected.
uint32_t layer_args_opt(const struct ArgsLayer *args)
Returns a bitstring representing the permissions granted by a layer.
struct ArgsLayer * layer_args_copy(const struct ArgsLayer *src)
Creates and returns a copy of specified layer parameters.
An implementation of a fully-connected layer of perceptrons.
void neural_layer_lstm_backward(const struct Layer *l, const struct Net *net, const double *input, double *delta)
Backward propagates an LSTM layer.
void neural_layer_lstm_rand(struct Layer *l)
Randomises an LSTM layer weights.
bool neural_layer_lstm_mutate(struct Layer *l)
Mutates an LSTM layer.
static bool mutate_eta(struct Layer *l)
Mutates the gradient descent rate used to update an LSTM layer.
double * neural_layer_lstm_output(const struct Layer *l)
Returns the output from an LSTM layer.
void neural_layer_lstm_free(const struct Layer *l)
Free memory used by an LSTM layer.
static void free_layer_arrays(const struct Layer *l)
Free memory used by an LSTM layer.
static void malloc_layer_arrays(struct Layer *l)
Allocate memory used by an LSTM layer.
struct Layer * neural_layer_lstm_copy(const struct Layer *src)
Initialises and creates a copy of one LSTM layer from another.
size_t neural_layer_lstm_load(struct Layer *l, FILE *fp)
Reads an LSTM layer from a file.
static void set_layer_n_weights(struct Layer *l)
Sets the total number of weights in an LSTM layer.
void neural_layer_lstm_resize(struct Layer *l, const struct Layer *prev)
Resizes an LSTM layer if the previous layer has changed size.
#define N_MU
Number of mutation rates applied to an LSTM layer.
static bool mutate_neurons(struct Layer *l)
Mutates the number of neurons in an LSTM layer.
void neural_layer_lstm_print(const struct Layer *l, const bool print_weights)
Prints an LSTM layer.
static void set_layer_n_active(struct Layer *l)
Sets the number of active (non-zero) weights in an LSTM layer.
static const int MU_TYPE[(6)]
Self-adaptation method for mutating an LSTM layer.
size_t neural_layer_lstm_save(const struct Layer *l, FILE *fp)
Writes an LSTM layer to a file.
static bool mutate_weights(struct Layer *l)
Mutates the magnitude of weights and biases in an LSTM layer.
static void reset_layer_deltas(const struct Layer *l)
Zeros the deltas used to update an LSTM layer.
static void set_layer_n_biases(struct Layer *l)
Sets the total number of biases in an LSTM layer.
void neural_layer_lstm_update(const struct Layer *l)
Updates the weights and biases of an LSTM layer.
static void malloc_layers(struct Layer *l)
Allocate memory for the sub-layers.
char * neural_layer_lstm_json_export(const struct Layer *l, const bool return_weights)
Returns a json formatted string representation of an LSTM layer.
void neural_layer_lstm_init(struct Layer *l, const struct ArgsLayer *args)
Initialises a long short-term memory layer.
static bool mutate_connectivity(struct Layer *l)
Mutates the number of active weights in an LSTM layer.
static void set_eta(struct Layer *l)
Sets the gradient descent rate used to update an LSTM layer.
void neural_layer_lstm_forward(const struct Layer *l, const struct Net *net, const double *input)
Forward propagates an LSTM layer.
An implementation of a long short-term memory 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.
int n_max
Maximum number of units / neurons.
int n_inputs
Number of inputs.
int type
Layer type: CONNECTED, DROPOUT, etc.
int recurrent_function
Recurrent activation function.
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 recurrent_function
LSTM.
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.
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 eta_max
Maximum gradient descent rate.
int n_outputs
Number of layer outputs.
int n_active
Number of active weights / connections.
double * prev_state
Previous state for recursive layers.
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
Gradient descent rate.
double momentum
Momentum for gradient descent.
Neural network data structure.
Utility functions for random number handling, etc.