XCSF  1.4.7
XCSF learning classifier system
neural_layer_lstm.c File Reference

An implementation of a long short-term memory layer. More...

#include "neural_layer_lstm.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_lstm.c:

Go to the source code of this file.

Macros

#define N_MU   (6)
 Number of mutation rates applied to an LSTM layer. More...
 

Functions

static void set_layer_n_weights (struct Layer *l)
 Sets the total number of weights in an LSTM layer. More...
 
static void set_layer_n_biases (struct Layer *l)
 Sets the total number of biases in an LSTM layer. More...
 
static void set_layer_n_active (struct Layer *l)
 Sets the number of active (non-zero) weights in an LSTM layer. More...
 
static void malloc_layer_arrays (struct Layer *l)
 Allocate memory used by an LSTM layer. More...
 
static void malloc_layers (struct Layer *l)
 Allocate memory for the sub-layers. More...
 
static void free_layer_arrays (const struct Layer *l)
 Free memory used by an LSTM layer. More...
 
static void set_eta (struct Layer *l)
 Sets the gradient descent rate used to update an LSTM layer. More...
 
static void reset_layer_deltas (const struct Layer *l)
 Zeros the deltas used to update an LSTM layer. More...
 
static bool mutate_eta (struct Layer *l)
 Mutates the gradient descent rate used to update an LSTM layer. More...
 
static bool mutate_neurons (struct Layer *l)
 Mutates the number of neurons in an LSTM layer. More...
 
static bool mutate_connectivity (struct Layer *l)
 Mutates the number of active weights in an LSTM layer. More...
 
static bool mutate_weights (struct Layer *l)
 Mutates the magnitude of weights and biases in an LSTM layer. More...
 
void neural_layer_lstm_init (struct Layer *l, const struct ArgsLayer *args)
 Initialises a long short-term memory layer. More...
 
struct Layerneural_layer_lstm_copy (const struct Layer *src)
 Initialises and creates a copy of one LSTM layer from another. More...
 
void neural_layer_lstm_free (const struct Layer *l)
 Free memory used by an LSTM layer. More...
 
void neural_layer_lstm_rand (struct Layer *l)
 Randomises an LSTM layer weights. More...
 
void neural_layer_lstm_forward (const struct Layer *l, const struct Net *net, const double *input)
 Forward propagates an LSTM layer. More...
 
void neural_layer_lstm_backward (const struct Layer *l, const struct Net *net, const double *input, double *delta)
 Backward propagates an LSTM layer. More...
 
void neural_layer_lstm_update (const struct Layer *l)
 Updates the weights and biases of an LSTM layer. More...
 
void neural_layer_lstm_resize (struct Layer *l, const struct Layer *prev)
 Resizes an LSTM layer if the previous layer has changed size. More...
 
double * neural_layer_lstm_output (const struct Layer *l)
 Returns the output from an LSTM layer. More...
 
bool neural_layer_lstm_mutate (struct Layer *l)
 Mutates an LSTM layer. More...
 
void neural_layer_lstm_print (const struct Layer *l, const bool print_weights)
 Prints an LSTM layer. More...
 
char * neural_layer_lstm_json_export (const struct Layer *l, const bool return_weights)
 Returns a json formatted string representation of an LSTM layer. More...
 
size_t neural_layer_lstm_save (const struct Layer *l, FILE *fp)
 Writes an LSTM layer to a file. More...
 
size_t neural_layer_lstm_load (struct Layer *l, FILE *fp)
 Reads an LSTM layer from a file. More...
 

Variables

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

Detailed Description

An implementation of a long short-term memory layer.

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

Stateful, and with a step of 1. Typically the output activation is TANH and recurrent activation LOGISTIC.

Definition in file neural_layer_lstm.c.

Macro Definition Documentation

◆ N_MU

#define N_MU   (6)

Number of mutation rates applied to an LSTM layer.

Definition at line 33 of file neural_layer_lstm.c.

Function Documentation

◆ free_layer_arrays()

static void free_layer_arrays ( const struct Layer l)
static

Free memory used by an LSTM layer.

Parameters
[in]lThe layer to be freed.

Definition at line 131 of file neural_layer_lstm.c.

References Layer::c, Layer::cell, Layer::dc, Layer::delta, Layer::f, Layer::g, Layer::h, Layer::i, Layer::o, Layer::output, Layer::prev_cell, Layer::prev_state, Layer::state, Layer::temp, Layer::temp2, and Layer::temp3.

Referenced by mutate_neurons(), and neural_layer_lstm_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 an LSTM layer.

Parameters
[in]lThe layer to be allocated memory.

Definition at line 88 of file neural_layer_lstm.c.

References Layer::c, Layer::cell, Layer::dc, Layer::delta, Layer::f, Layer::g, Layer::h, Layer::i, layer_guard_outputs(), Layer::n_outputs, Layer::o, Layer::output, Layer::prev_cell, Layer::prev_state, Layer::state, Layer::temp, Layer::temp2, and Layer::temp3.

Referenced by mutate_neurons(), neural_layer_lstm_copy(), neural_layer_lstm_init(), and neural_layer_lstm_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 114 of file neural_layer_lstm.c.

References Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Referenced by neural_layer_lstm_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 an LSTM layer.

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

Definition at line 243 of file neural_layer_lstm.c.

References layer_mutate_connectivity(), Layer::mu, set_layer_n_active(), Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Referenced by neural_layer_lstm_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 an LSTM layer.

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

Definition at line 192 of file neural_layer_lstm.c.

References layer_mutate_eta(), Layer::mu, set_eta(), and Layer::uf.

Referenced by neural_layer_lstm_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 an LSTM layer.

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

Definition at line 207 of file neural_layer_lstm.c.

References free_layer_arrays(), layer_add_neurons(), layer_mutate_neurons(), layer_resize(), malloc_layer_arrays(), Layer::mu, Layer::n_outputs, Layer::out_c, Layer::out_h, Layer::out_w, set_layer_n_active(), set_layer_n_biases(), set_layer_n_weights(), Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Referenced by neural_layer_lstm_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 an LSTM layer.

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

Definition at line 264 of file neural_layer_lstm.c.

References layer_mutate_weights(), Layer::mu, Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Referenced by neural_layer_lstm_mutate().

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

◆ neural_layer_lstm_backward()

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

Backward propagates an LSTM 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 461 of file neural_layer_lstm.c.

References blas_axpy(), blas_mul(), Layer::c, Layer::dc, Layer::delta, Layer::f, Layer::function, Layer::g, Layer::i, layer_backward(), Layer::n_outputs, neural_activate_array(), neural_gradient_array(), Layer::o, Layer::prev_cell, Layer::prev_state, Layer::recurrent_function, reset_layer_deltas(), Layer::temp, Layer::temp2, Layer::temp3, Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Here is the call graph for this function:

◆ neural_layer_lstm_copy()

struct Layer* neural_layer_lstm_copy ( const struct Layer src)

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

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

Definition at line 327 of file neural_layer_lstm.c.

References Layer::decay, Layer::eta, Layer::eta_max, Layer::function, layer_copy(), layer_defaults(), Layer::layer_vptr, LSTM, malloc_layer_arrays(), Layer::max_neuron_grow, Layer::max_outputs, Layer::momentum, Layer::mu, Layer::n_active, Layer::n_biases, Layer::n_inputs, N_MU, Layer::n_outputs, Layer::n_weights, Layer::options, Layer::out_c, Layer::out_h, Layer::out_w, Layer::recurrent_function, Layer::type, Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Here is the call graph for this function:

◆ neural_layer_lstm_forward()

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

Forward propagates an LSTM layer.

Parameters
[in]lThe layer to forward propagate.
[in]netNetwork containing the layer.
[in]inputThe input to the layer.

Definition at line 419 of file neural_layer_lstm.c.

References blas_axpy(), blas_mul(), Layer::c, Layer::cell, Layer::f, Layer::function, Layer::g, Layer::h, Layer::i, layer_forward(), Layer::n_outputs, neural_activate_array(), Layer::o, Layer::output, Layer::recurrent_function, Layer::temp, Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Here is the call graph for this function:

◆ neural_layer_lstm_free()

void neural_layer_lstm_free ( const struct Layer l)

Free memory used by an LSTM layer.

Parameters
[in]lThe layer to be freed.

Definition at line 373 of file neural_layer_lstm.c.

References free_layer_arrays(), layer_free(), Layer::mu, Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Here is the call graph for this function:

◆ neural_layer_lstm_init()

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

◆ neural_layer_lstm_json_export()

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

Returns a json formatted string representation of an LSTM layer.

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

Definition at line 605 of file neural_layer_lstm.c.

References Layer::eta, Layer::function, layer_weight_json(), Layer::mu, Layer::n_inputs, N_MU, Layer::n_outputs, neural_activation_string(), Layer::recurrent_function, Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Referenced by neural_layer_lstm_print().

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

◆ neural_layer_lstm_load()

size_t neural_layer_lstm_load ( struct Layer l,
FILE *  fp 
)

Reads an LSTM 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 709 of file neural_layer_lstm.c.

References Layer::c, Layer::cell, Layer::dc, Layer::decay, Layer::eta, Layer::eta_max, Layer::f, Layer::g, Layer::h, Layer::i, layer_load(), malloc_layer_arrays(), malloc_layers(), Layer::max_neuron_grow, Layer::max_outputs, Layer::momentum, Layer::mu, Layer::n_active, Layer::n_biases, Layer::n_inputs, N_MU, Layer::n_outputs, Layer::n_weights, Layer::o, Layer::options, Layer::out_c, Layer::out_h, Layer::out_w, Layer::prev_state, Layer::state, Layer::temp, Layer::temp2, Layer::temp3, Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Here is the call graph for this function:

◆ neural_layer_lstm_mutate()

bool neural_layer_lstm_mutate ( struct Layer l)

Mutates an LSTM layer.

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

Definition at line 561 of file neural_layer_lstm.c.

References LAYER_EVOLVE_CONNECT, LAYER_EVOLVE_ETA, LAYER_EVOLVE_FUNCTIONS, LAYER_EVOLVE_NEURONS, LAYER_EVOLVE_WEIGHTS, layer_mutate_functions(), Layer::mu, MU_TYPE, mutate_connectivity(), mutate_eta(), mutate_neurons(), mutate_weights(), N_MU, Layer::options, and sam_adapt().

Here is the call graph for this function:

◆ neural_layer_lstm_output()

double* neural_layer_lstm_output ( const struct Layer l)

Returns the output from an LSTM layer.

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

Definition at line 550 of file neural_layer_lstm.c.

References Layer::output.

◆ neural_layer_lstm_print()

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

Prints an LSTM layer.

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

Definition at line 590 of file neural_layer_lstm.c.

References neural_layer_lstm_json_export().

Here is the call graph for this function:

◆ neural_layer_lstm_rand()

void neural_layer_lstm_rand ( struct Layer l)

Randomises an LSTM layer weights.

Parameters
[in]lThe layer to randomise.

Definition at line 400 of file neural_layer_lstm.c.

References layer_rand(), Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Here is the call graph for this function:

◆ neural_layer_lstm_resize()

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

Resizes an LSTM 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 531 of file neural_layer_lstm.c.

References layer_resize(), Layer::n_inputs, Layer::n_outputs, set_layer_n_active(), set_layer_n_biases(), set_layer_n_weights(), Layer::uf, Layer::ug, Layer::ui, and Layer::uo.

Here is the call graph for this function:

◆ neural_layer_lstm_save()

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

Writes an LSTM 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 662 of file neural_layer_lstm.c.

References Layer::c, Layer::cell, Layer::dc, Layer::decay, Layer::eta, Layer::eta_max, Layer::f, Layer::g, Layer::h, Layer::i, layer_save(), Layer::max_neuron_grow, Layer::max_outputs, Layer::momentum, Layer::mu, Layer::n_active, Layer::n_biases, Layer::n_inputs, N_MU, Layer::n_outputs, Layer::n_weights, Layer::o, Layer::options, Layer::prev_state, Layer::state, Layer::temp, Layer::temp2, Layer::temp3, Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Here is the call graph for this function:

◆ neural_layer_lstm_update()

void neural_layer_lstm_update ( const struct Layer l)

Updates the weights and biases of an LSTM layer.

Parameters
[in]lThe layer to update.

Definition at line 511 of file neural_layer_lstm.c.

References Layer::eta, LAYER_SGD_WEIGHTS, layer_update(), Layer::options, Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Here is the call graph for this function:

◆ reset_layer_deltas()

static void reset_layer_deltas ( const struct Layer l)
static

Zeros the deltas used to update an LSTM layer.

Parameters
[in]lThe layer whose deltas are to be reset.

Definition at line 173 of file neural_layer_lstm.c.

References Layer::delta, Layer::n_outputs, Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Referenced by neural_layer_lstm_backward().

Here is the caller graph for this function:

◆ set_eta()

static void set_eta ( struct Layer l)
static

Sets the gradient descent rate used to update an LSTM layer.

Parameters
[in]lThe layer whose gradient descent rate is to be set.

Definition at line 156 of file neural_layer_lstm.c.

References Layer::eta, Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Referenced by mutate_eta(), and neural_layer_lstm_init().

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 an LSTM layer.

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

Definition at line 76 of file neural_layer_lstm.c.

References Layer::n_active, Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Referenced by mutate_connectivity(), mutate_neurons(), neural_layer_lstm_init(), and neural_layer_lstm_resize().

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 an LSTM layer.

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

Definition at line 64 of file neural_layer_lstm.c.

References Layer::n_biases, Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Referenced by mutate_neurons(), neural_layer_lstm_init(), and neural_layer_lstm_resize().

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 an LSTM layer.

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

Definition at line 52 of file neural_layer_lstm.c.

References Layer::n_weights, Layer::uf, Layer::ug, Layer::ui, Layer::uo, Layer::wf, Layer::wg, Layer::wi, and Layer::wo.

Referenced by mutate_neurons(), neural_layer_lstm_init(), and neural_layer_lstm_resize().

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 an LSTM layer.

Definition at line 38 of file neural_layer_lstm.c.

Referenced by neural_layer_lstm_init(), and neural_layer_lstm_mutate().