XCSF  1.4.7
XCSF learning classifier system
neural_layer_recurrent.c File Reference

An implementation of a recurrent layer of perceptrons. More...

#include "neural_layer_recurrent.h"
#include "blas.h"
#include "neural_activations.h"
#include "neural_layer_connected.h"
#include "sam.h"
#include "utils.h"
Include dependency graph for neural_layer_recurrent.c:

Go to the source code of this file.

Macros

#define N_MU   (6)
 Number of mutation rates applied to a recurrent layer. More...
 

Functions

static void malloc_layer_arrays (struct Layer *l)
 Allocate memory used by a recurrent layer. More...
 
static void realloc_layer_arrays (struct Layer *l)
 Resize memory used by a recurrent layer. More...
 
static void free_layer_arrays (const struct Layer *l)
 Free memory used by a recurrent layer. More...
 
static void malloc_layers (struct Layer *l)
 Allocate memory for the sub-layers. More...
 
static void set_layer_n_active (struct Layer *l)
 Sets the number of active (non-zero) weights in a recurrent layer. More...
 
static void set_layer_n_weights (struct Layer *l)
 Sets the total number of weights in a recurrent layer. More...
 
static void set_layer_n_biases (struct Layer *l)
 Sets the total number of biases in a recurrent layer. More...
 
static bool mutate_eta (struct Layer *l)
 Mutates the gradient descent rate used to update a recurrent layer. More...
 
static bool mutate_neurons (struct Layer *l)
 Mutates the number of neurons in a recurrent layer. More...
 
static bool mutate_connectivity (struct Layer *l)
 Mutates the number of active weights in a recurrent layer. More...
 
static bool mutate_weights (struct Layer *l)
 Mutates the magnitude of weights and biases in a recurrent layer. More...
 
static bool mutate_functions (struct Layer *l)
 Mutates the activation function of a recurrent layer. More...
 
void neural_layer_recurrent_init (struct Layer *l, const struct ArgsLayer *args)
 Initialises a recurrent layer. More...
 
struct Layerneural_layer_recurrent_copy (const struct Layer *src)
 Initialises and creates a copy of one recurrent layer from another. More...
 
void neural_layer_recurrent_free (const struct Layer *l)
 Free memory used by a recurrent layer. More...
 
void neural_layer_recurrent_rand (struct Layer *l)
 Randomises a recurrent layer weights. More...
 
void neural_layer_recurrent_forward (const struct Layer *l, const struct Net *net, const double *input)
 Forward propagates a recurrent layer. More...
 
void neural_layer_recurrent_backward (const struct Layer *l, const struct Net *net, const double *input, double *delta)
 Backward propagates a recurrent layer. More...
 
void neural_layer_recurrent_update (const struct Layer *l)
 Updates the weights and biases of a recurrent layer. More...
 
void neural_layer_recurrent_resize (struct Layer *l, const struct Layer *prev)
 Resizes a recurrent layer if the previous layer has changed size. More...
 
double * neural_layer_recurrent_output (const struct Layer *l)
 Returns the output from a recurrent layer. More...
 
bool neural_layer_recurrent_mutate (struct Layer *l)
 Mutates a recurrent layer. More...
 
void neural_layer_recurrent_print (const struct Layer *l, const bool print_weights)
 Prints a recurrent layer. More...
 
char * neural_layer_recurrent_json_export (const struct Layer *l, const bool return_weights)
 Returns a json formatted string representation of a recurrent layer. More...
 
size_t neural_layer_recurrent_save (const struct Layer *l, FILE *fp)
 Writes a recurrent layer to a file. More...
 
size_t neural_layer_recurrent_load (struct Layer *l, FILE *fp)
 Reads a recurrent layer from a file. More...
 

Variables

static const int MU_TYPE [(6)]
 Self-adaptation method for mutating a recurrent layer. More...
 

Detailed Description

An implementation of a recurrent layer of perceptrons.

Author
Richard Preen rpree.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om
Date
2016–2024.

Fully-connected, stateful, and with a step of 1.

Definition in file neural_layer_recurrent.c.

Macro Definition Documentation

◆ N_MU

#define N_MU   (6)

Number of mutation rates applied to a recurrent layer.

Definition at line 32 of file neural_layer_recurrent.c.

Function Documentation

◆ free_layer_arrays()

static void free_layer_arrays ( const struct Layer l)
static

Free memory used by a recurrent layer.

Parameters
[in]lThe layer to be freed.

Definition at line 76 of file neural_layer_recurrent.c.

References Layer::mu, Layer::prev_state, and Layer::state.

Referenced by neural_layer_recurrent_free().

Here is the caller graph for this function:

◆ malloc_layer_arrays()

static void malloc_layer_arrays ( struct Layer l)
static

Allocate memory used by a recurrent layer.

Parameters
[in]lThe layer to be allocated memory.

Definition at line 51 of file neural_layer_recurrent.c.

References layer_guard_outputs(), Layer::mu, N_MU, Layer::n_outputs, Layer::prev_state, and Layer::state.

Referenced by neural_layer_recurrent_copy(), neural_layer_recurrent_init(), and neural_layer_recurrent_load().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ malloc_layers()

static void malloc_layers ( struct Layer l)
static

Allocate memory for the sub-layers.

Parameters
[in]lThe layer to be allocated memory.

Definition at line 88 of file neural_layer_recurrent.c.

References Layer::input_layer, Layer::output_layer, and Layer::self_layer.

Referenced by neural_layer_recurrent_load().

Here is the caller graph for this function:

◆ mutate_connectivity()

static bool mutate_connectivity ( struct Layer l)
static

Mutates the number of active weights in a recurrent layer.

Parameters
[in]lThe layer whose number of active weights is to be mutated.
Returns
Whether any alterations were made.

Definition at line 183 of file neural_layer_recurrent.c.

References Layer::input_layer, LAYER_EVOLVE_CONNECT, layer_mutate_connectivity(), Layer::mu, Layer::options, Layer::output_layer, Layer::self_layer, and set_layer_n_active().

Referenced by neural_layer_recurrent_mutate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mutate_eta()

static bool mutate_eta ( struct Layer l)
static

Mutates the gradient descent rate used to update a recurrent layer.

Parameters
[in]lThe layer whose gradient descent rate is to be mutated.
Returns
Whether any alterations were made.

Definition at line 134 of file neural_layer_recurrent.c.

References Layer::eta, Layer::input_layer, LAYER_EVOLVE_ETA, layer_mutate_eta(), Layer::mu, Layer::options, Layer::output_layer, and Layer::self_layer.

Referenced by neural_layer_recurrent_mutate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mutate_functions()

static bool mutate_functions ( struct Layer l)
static

Mutates the activation function of a recurrent layer.

Parameters
[in]lThe layer whose activation function is to be mutated.
Returns
Whether any alterations were made.

Definition at line 230 of file neural_layer_recurrent.c.

References Layer::function, LAYER_EVOLVE_FUNCTIONS, layer_mutate_functions(), Layer::mu, Layer::options, and Layer::output_layer.

Referenced by neural_layer_recurrent_mutate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mutate_neurons()

static bool mutate_neurons ( struct Layer l)
static

Mutates the number of neurons in a recurrent layer.

Parameters
[in]lThe layer whose number of neurons is to be mutated.
Returns
Whether any alterations were made.

Definition at line 151 of file neural_layer_recurrent.c.

References Layer::delta, Layer::input_layer, layer_add_neurons(), LAYER_EVOLVE_NEURONS, layer_mutate_neurons(), layer_resize(), Layer::mu, Layer::n_outputs, Layer::options, Layer::out_c, Layer::out_h, Layer::out_w, Layer::output, Layer::output_layer, realloc_layer_arrays(), Layer::self_layer, set_layer_n_active(), set_layer_n_biases(), and set_layer_n_weights().

Referenced by neural_layer_recurrent_mutate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mutate_weights()

static bool mutate_weights ( struct Layer l)
static

Mutates the magnitude of weights and biases in a recurrent layer.

Parameters
[in]lThe layer whose weights are to be mutated.
Returns
Whether any alterations were made.

Definition at line 207 of file neural_layer_recurrent.c.

References Layer::input_layer, LAYER_EVOLVE_WEIGHTS, layer_mutate_weights(), Layer::mu, Layer::options, Layer::output_layer, and Layer::self_layer.

Referenced by neural_layer_recurrent_mutate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ neural_layer_recurrent_backward()

void neural_layer_recurrent_backward ( const struct Layer l,
const struct Net net,
const double *  input,
double *  delta 
)

Backward propagates a recurrent layer.

Parameters
[in]lThe layer to backward propagate.
[in]netNetwork containing the layer.
[in]inputThe input to the layer.
[out]deltaThe previous layer's error.

Definition at line 368 of file neural_layer_recurrent.c.

References Layer::delta, Layer::input_layer, layer_backward(), Layer::n_outputs, Layer::output_layer, Layer::prev_state, Layer::self_layer, and Layer::state.

Here is the call graph for this function:

◆ neural_layer_recurrent_copy()

struct Layer* neural_layer_recurrent_copy ( const struct Layer src)

Initialises and creates a copy of one recurrent layer from another.

Parameters
[in]srcThe source layer.
Returns
A pointer to the new layer.

Definition at line 283 of file neural_layer_recurrent.c.

References Layer::delta, Layer::eta, Layer::function, Layer::input_layer, layer_copy(), layer_defaults(), Layer::layer_vptr, malloc_layer_arrays(), Layer::max_outputs, Layer::mu, Layer::n_active, Layer::n_inputs, N_MU, Layer::n_outputs, Layer::options, Layer::out_c, Layer::out_h, Layer::out_w, Layer::output, Layer::output_layer, Layer::prev_state, RECURRENT, Layer::self_layer, and Layer::type.

Here is the call graph for this function:

◆ neural_layer_recurrent_forward()

void neural_layer_recurrent_forward ( const struct Layer l,
const struct Net net,
const double *  input 
)

Forward propagates a recurrent layer.

Parameters
[in]lLayer to forward propagate.
[in]netNetwork containing the layer.
[in]inputInput to the layer.

Definition at line 349 of file neural_layer_recurrent.c.

References blas_axpy(), Layer::input_layer, layer_forward(), Layer::n_outputs, Layer::output, Layer::output_layer, Layer::prev_state, Layer::self_layer, and Layer::state.

Here is the call graph for this function:

◆ neural_layer_recurrent_free()

void neural_layer_recurrent_free ( const struct Layer l)

Free memory used by a recurrent layer.

Parameters
[in]lThe layer to be freed.

Definition at line 319 of file neural_layer_recurrent.c.

References free_layer_arrays(), Layer::input_layer, layer_free(), Layer::output_layer, and Layer::self_layer.

Here is the call graph for this function:

◆ neural_layer_recurrent_init()

void neural_layer_recurrent_init ( struct Layer l,
const struct ArgsLayer args 
)

Initialises a recurrent layer.

Parameters
[in]lLayer to initialise.
[in]argsParameters to initialise the layer.

Definition at line 246 of file neural_layer_recurrent.c.

References CONNECTED, Layer::delta, Layer::eta, Layer::function, ArgsLayer::function, Layer::input_layer, layer_args_copy(), layer_args_opt(), layer_init(), LINEAR, malloc_layer_arrays(), Layer::max_outputs, Layer::mu, MU_TYPE, ArgsLayer::n_init, Layer::n_inputs, ArgsLayer::n_inputs, ArgsLayer::n_max, N_MU, Layer::n_outputs, Layer::options, Layer::out_c, Layer::out_h, Layer::out_w, Layer::output, Layer::output_layer, sam_init(), Layer::self_layer, set_layer_n_active(), set_layer_n_biases(), set_layer_n_weights(), and ArgsLayer::type.

Here is the call graph for this function:

◆ neural_layer_recurrent_json_export()

char* neural_layer_recurrent_json_export ( const struct Layer l,
const bool  return_weights 
)

Returns a json formatted string representation of a recurrent layer.

Parameters
[in]lThe layer to return.
[in]return_weightsWhether to return the values of weights and biases.
Returns
String encoded in json format.

Definition at line 458 of file neural_layer_recurrent.c.

References Layer::eta, Layer::function, Layer::input_layer, layer_weight_json(), Layer::mu, Layer::n_inputs, N_MU, Layer::n_outputs, neural_activation_string(), Layer::output_layer, and Layer::self_layer.

Referenced by neural_layer_recurrent_print().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ neural_layer_recurrent_load()

size_t neural_layer_recurrent_load ( struct Layer l,
FILE *  fp 
)

Reads a recurrent layer from a file.

Parameters
[in]lThe layer to load.
[in]fpPointer to the file to be read.
Returns
The number of elements read.

Definition at line 520 of file neural_layer_recurrent.c.

References Layer::eta, Layer::function, Layer::input_layer, layer_load(), malloc_layer_arrays(), malloc_layers(), Layer::max_outputs, Layer::mu, Layer::n_active, Layer::n_inputs, N_MU, Layer::n_outputs, Layer::options, Layer::out_c, Layer::out_h, Layer::out_w, Layer::output_layer, Layer::prev_state, Layer::self_layer, and Layer::state.

Here is the call graph for this function:

◆ neural_layer_recurrent_mutate()

bool neural_layer_recurrent_mutate ( struct Layer l)

Mutates a recurrent layer.

Parameters
[in]lThe layer to mutate.
Returns
Whether any alterations were made.

Definition at line 425 of file neural_layer_recurrent.c.

References Layer::mu, MU_TYPE, mutate_connectivity(), mutate_eta(), mutate_functions(), mutate_neurons(), mutate_weights(), N_MU, and sam_adapt().

Here is the call graph for this function:

◆ neural_layer_recurrent_output()

double* neural_layer_recurrent_output ( const struct Layer l)

Returns the output from a recurrent layer.

Parameters
[in]lThe layer whose output to return.
Returns
The layer output.

Definition at line 414 of file neural_layer_recurrent.c.

References Layer::output.

◆ neural_layer_recurrent_print()

void neural_layer_recurrent_print ( const struct Layer l,
const bool  print_weights 
)

Prints a recurrent layer.

Parameters
[in]lThe layer to print.
[in]print_weightsWhether to print the values of weights and biases.

Definition at line 443 of file neural_layer_recurrent.c.

References neural_layer_recurrent_json_export().

Here is the call graph for this function:

◆ neural_layer_recurrent_rand()

void neural_layer_recurrent_rand ( struct Layer l)

Randomises a recurrent layer weights.

Parameters
[in]lThe layer to randomise.

Definition at line 335 of file neural_layer_recurrent.c.

References Layer::input_layer, layer_rand(), Layer::output_layer, and Layer::self_layer.

Here is the call graph for this function:

◆ neural_layer_recurrent_resize()

void neural_layer_recurrent_resize ( struct Layer l,
const struct Layer prev 
)

Resizes a recurrent layer if the previous layer has changed size.

Parameters
[in]lThe layer to resize.
[in]prevThe layer previous to the one being resized.

Definition at line 400 of file neural_layer_recurrent.c.

References Layer::input_layer, layer_resize(), Layer::n_active, Layer::n_inputs, Layer::output_layer, and Layer::self_layer.

Here is the call graph for this function:

◆ neural_layer_recurrent_save()

size_t neural_layer_recurrent_save ( const struct Layer l,
FILE *  fp 
)

Writes a recurrent layer to a file.

Parameters
[in]lThe layer to save.
[in]fpPointer to the file to be written.
Returns
The number of elements written.

Definition at line 494 of file neural_layer_recurrent.c.

References Layer::eta, Layer::function, Layer::input_layer, layer_save(), Layer::max_outputs, Layer::mu, Layer::n_active, Layer::n_inputs, N_MU, Layer::n_outputs, Layer::options, Layer::output_layer, Layer::prev_state, Layer::self_layer, and Layer::state.

Here is the call graph for this function:

◆ neural_layer_recurrent_update()

void neural_layer_recurrent_update ( const struct Layer l)

Updates the weights and biases of a recurrent layer.

Parameters
[in]lThe layer to update.

Definition at line 385 of file neural_layer_recurrent.c.

References Layer::eta, Layer::input_layer, LAYER_SGD_WEIGHTS, layer_update(), Layer::options, Layer::output_layer, and Layer::self_layer.

Here is the call graph for this function:

◆ realloc_layer_arrays()

static void realloc_layer_arrays ( struct Layer l)
static

Resize memory used by a recurrent layer.

Parameters
[in]lThe layer to be allocated memory.

Definition at line 64 of file neural_layer_recurrent.c.

References layer_guard_outputs(), Layer::n_outputs, Layer::prev_state, and Layer::state.

Referenced by mutate_neurons().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_layer_n_active()

static void set_layer_n_active ( struct Layer l)
static

Sets the number of active (non-zero) weights in a recurrent layer.

Parameters
[in]lThe layer to update the number of active weights.

Definition at line 100 of file neural_layer_recurrent.c.

References Layer::input_layer, Layer::n_active, Layer::output_layer, and Layer::self_layer.

Referenced by mutate_connectivity(), mutate_neurons(), and neural_layer_recurrent_init().

Here is the caller graph for this function:

◆ set_layer_n_biases()

static void set_layer_n_biases ( struct Layer l)
static

Sets the total number of biases in a recurrent layer.

Parameters
[in]lThe layer to update the total number of biases.

Definition at line 122 of file neural_layer_recurrent.c.

References Layer::input_layer, Layer::n_biases, Layer::output_layer, and Layer::self_layer.

Referenced by mutate_neurons(), and neural_layer_recurrent_init().

Here is the caller graph for this function:

◆ set_layer_n_weights()

static void set_layer_n_weights ( struct Layer l)
static

Sets the total number of weights in a recurrent layer.

Parameters
[in]lThe layer to update the total number of weights.

Definition at line 111 of file neural_layer_recurrent.c.

References Layer::input_layer, Layer::n_weights, Layer::output_layer, and Layer::self_layer.

Referenced by mutate_neurons(), and neural_layer_recurrent_init().

Here is the caller graph for this function:

Variable Documentation

◆ MU_TYPE

const int MU_TYPE[(6)]
static
Initial value:
= {
(1) ,
(1) ,
(1) ,
(1) ,
(1) ,
(1)
}

Self-adaptation method for mutating a recurrent layer.

Definition at line 37 of file neural_layer_recurrent.c.

Referenced by neural_layer_recurrent_init(), and neural_layer_recurrent_mutate().