XCSF  1.4.7
XCSF learning classifier system
neural.c File Reference

An implementation of a multi-layer perceptron neural network. More...

Include dependency graph for neural.c:

Go to the source code of this file.

Functions

void neural_init (struct Net *net)
 Initialises an empty neural network. More...
 
void neural_create (struct Net *net, struct ArgsLayer *arg)
 Initialises and creates a new neural network from a parameter list. More...
 
void neural_insert (struct Net *net, struct Layer *l, const int pos)
 Inserts a layer into a neural network. More...
 
void neural_remove (struct Net *net, const int pos)
 Removes a layer from a neural network. More...
 
void neural_push (struct Net *net, struct Layer *l)
 Inserts a layer at the head of a neural network. More...
 
void neural_pop (struct Net *net)
 Removes the layer at the head of a neural network. More...
 
void neural_copy (struct Net *dest, const struct Net *src)
 Copies a neural network. More...
 
void neural_free (struct Net *net)
 Frees a neural network. More...
 
void neural_rand (const struct Net *net)
 Randomises the layers within a neural network. More...
 
bool neural_mutate (const struct Net *net)
 Mutates a neural network. More...
 
void neural_resize (const struct Net *net)
 Resizes neural network layers as necessary. More...
 
void neural_propagate (struct Net *net, const double *input, const bool train)
 Forward propagates a neural network. More...
 
void neural_learn (const struct Net *net, const double *truth, const double *input)
 Performs a gradient descent update on a neural network. More...
 
double neural_output (const struct Net *net, const int IDX)
 Returns the output of a specified neuron in the output layer of a neural network. More...
 
double * neural_outputs (const struct Net *net)
 Returns the outputs from the output layer of a neural network. More...
 
void neural_print (const struct Net *net, const bool print_weights)
 Prints a neural network. More...
 
char * neural_json_export (const struct Net *net, const bool return_weights)
 Returns a json formatted string representation of a neural network. More...
 
void neural_json_import (struct Net *net, const struct ArgsLayer *arg, const cJSON *json)
 Creates a neural network from a cJSON object. More...
 
double neural_size (const struct Net *net)
 Returns the total number of non-zero weights in a neural network. More...
 
size_t neural_save (const struct Net *net, FILE *fp)
 Writes a neural network to a file. More...
 
size_t neural_load (struct Net *net, FILE *fp)
 Reads a neural network from a file. More...
 

Detailed Description

An implementation of a multi-layer perceptron neural network.

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

Definition in file neural.c.

Function Documentation

◆ neural_copy()

void neural_copy ( struct Net dest,
const struct Net src 
)

Copies a neural network.

Parameters
[in]destThe destination neural network.
[in]srcThe source neural network.

Definition at line 208 of file neural.c.

References Llist::layer, layer_copy(), neural_init(), neural_push(), Llist::prev, and Net::tail.

Referenced by act_neural_copy(), cond_neural_copy(), pred_neural_copy(), and rule_neural_cond_copy().

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

◆ neural_create()

void neural_create ( struct Net net,
struct ArgsLayer arg 
)

Initialises and creates a new neural network from a parameter list.

Parameters
[in]netThe neural network to initialise.
[in]argList of layer parameters defining the initial network.

Definition at line 54 of file neural.c.

References AVGPOOL, ArgsLayer::channels, DROPOUT, ArgsLayer::height, layer_init(), MAXPOOL, ArgsLayer::n_init, Net::n_inputs, ArgsLayer::n_inputs, Net::n_layers, Net::n_outputs, Layer::n_outputs, neural_init(), neural_push(), ArgsLayer::next, NOISE, Layer::out_c, Layer::out_h, Layer::out_w, SOFTMAX, ArgsLayer::type, UPSAMPLE, and ArgsLayer::width.

Referenced by act_neural_init(), cond_neural_init(), layer_args_json_export(), pred_neural_init(), and rule_neural_cond_init().

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

◆ neural_free()

void neural_free ( struct Net net)

Frees a neural network.

Parameters
[in]netThe neural network to free.

Definition at line 224 of file neural.c.

References Llist::layer, layer_free(), Net::n_layers, Llist::prev, and Net::tail.

Referenced by act_neural_free(), cond_neural_free(), layer_args_json_export(), pred_neural_free(), and rule_neural_cond_free().

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

◆ neural_init()

void neural_init ( struct Net net)

Initialises an empty neural network.

Parameters
[in]netThe neural network to initialise.

Definition at line 37 of file neural.c.

References Net::head, Net::n_inputs, Net::n_layers, Net::n_outputs, Net::output, Net::tail, and Net::train.

Referenced by layer_args_json_export(), neural_copy(), neural_create(), and neural_load().

Here is the caller graph for this function:

◆ neural_insert()

void neural_insert ( struct Net net,
struct Layer l,
const int  pos 
)

Inserts a layer into a neural network.

Parameters
[in]netThe neural network receiving the layer.
[in]lThe layer to insert.
[in]posThe position in the network to insert the layer.

Definition at line 95 of file neural.c.

References Net::head, Llist::layer, Net::n_inputs, Layer::n_inputs, Net::n_layers, Net::n_outputs, Layer::n_outputs, Llist::next, Net::output, Layer::output, Llist::prev, and Net::tail.

Referenced by neural_push(), and pred_neural_expand().

Here is the caller graph for this function:

◆ neural_json_export()

char* neural_json_export ( const struct Net net,
const bool  return_weights 
)

Returns a json formatted string representation of a neural network.

Parameters
[in]netThe neural network to return.
[in]return_weightsWhether to return the weights in each layer.
Returns
String encoded in json format.

Definition at line 407 of file neural.c.

References Llist::layer, layer_json_export(), Llist::prev, and Net::tail.

Referenced by act_neural_json_export(), cond_neural_json_export(), neural_print(), pred_neural_json_export(), and rule_neural_cond_json_export().

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

◆ neural_json_import()

void neural_json_import ( struct Net net,
const struct ArgsLayer arg,
const cJSON *  json 
)

Creates a neural network from a cJSON object.

Parameters
[in,out]netThe neural network to return.
[in]argList of layer parameters defining the initial network.
[in]jsoncJSON object.

Definition at line 434 of file neural.c.

Referenced by act_neural_json_import(), cond_neural_json_import(), pred_neural_json_import(), and rule_neural_cond_json_import().

Here is the caller graph for this function:

◆ neural_learn()

void neural_learn ( const struct Net net,
const double *  truth,
const double *  input 
)

Performs a gradient descent update on a neural network.

Parameters
[in]netThe neural network to be updated.
[in]truthThe desired network output.
[in]inputThe input state.

Definition at line 328 of file neural.c.

References Layer::delta, Net::head, Layer::i, Llist::layer, layer_backward(), layer_update(), Layer::n_outputs, Llist::next, Layer::output, Llist::prev, and Net::tail.

Referenced by pred_neural_update().

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

◆ neural_load()

size_t neural_load ( struct Net net,
FILE *  fp 
)

Reads a neural network from a file.

Parameters
[in]netThe neural network to load.
[in]fpPointer to the file to be read.
Returns
The number of elements read.

Definition at line 499 of file neural.c.

References layer_load(), neural_init(), and neural_push().

Referenced by act_neural_load(), cond_neural_load(), pred_neural_load(), and rule_neural_cond_load().

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

◆ neural_mutate()

bool neural_mutate ( const struct Net net)

Mutates a neural network.

Parameters
[in]netThe neural network to mutate.
Returns
Whether any alterations were made.

Definition at line 257 of file neural.c.

References Llist::layer, layer_mutate(), layer_resize(), Layer::n_outputs, Llist::prev, and Net::tail.

Referenced by act_neural_mutate(), cond_neural_mutate(), pred_neural_mutate(), and rule_neural_cond_mutate().

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

◆ neural_output()

double neural_output ( const struct Net net,
const int  IDX 
)

Returns the output of a specified neuron in the output layer of a neural network.

Parameters
[in]netThe neural network to output.
[in]IDXWhich neuron in the output layer to return.
Returns
The output of the specified neuron.

Definition at line 369 of file neural.c.

References Net::head, Llist::layer, layer_output(), and Net::n_outputs.

Referenced by cond_neural_match(), pred_neural_compute(), rule_neural_act_compute(), and rule_neural_cond_match().

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

◆ neural_outputs()

double* neural_outputs ( const struct Net net)

Returns the outputs from the output layer of a neural network.

Parameters
[in]netThe neural network to output.
Returns
The neural network outputs.

Definition at line 384 of file neural.c.

References Net::head, Llist::layer, and layer_output().

Referenced by act_neural_compute().

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

◆ neural_pop()

void neural_pop ( struct Net net)

Removes the layer at the head of a neural network.

Parameters
[in]netThe neural network receiving the layer.

Definition at line 197 of file neural.c.

References Net::n_layers, and neural_remove().

Referenced by pred_neural_ae_to_classifier().

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

◆ neural_print()

void neural_print ( const struct Net net,
const bool  print_weights 
)

Prints a neural network.

Parameters
[in]netThe neural network to print.
[in]print_weightsWhether to print the weights in each layer.

Definition at line 395 of file neural.c.

References neural_json_export().

Here is the call graph for this function:

◆ neural_propagate()

void neural_propagate ( struct Net net,
const double *  input,
const bool  train 
)

Forward propagates a neural network.

Parameters
[in]netNeural network to propagate.
[in]inputInput state.
[in]trainWhether the network is in training mode.

Definition at line 310 of file neural.c.

References Llist::layer, layer_forward(), layer_output(), Llist::prev, Net::tail, and Net::train.

Referenced by act_neural_compute(), cond_neural_match(), pred_neural_compute(), and rule_neural_cond_match().

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

◆ neural_push()

void neural_push ( struct Net net,
struct Layer l 
)

Inserts a layer at the head of a neural network.

Parameters
[in]netThe neural network receiving the layer.
[in]lThe layer to insert.

Definition at line 187 of file neural.c.

References Net::n_layers, and neural_insert().

Referenced by neural_copy(), neural_create(), neural_load(), and pred_neural_ae_to_classifier().

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

◆ neural_rand()

void neural_rand ( const struct Net net)

Randomises the layers within a neural network.

Parameters
[in]netThe neural network to randomise.

Definition at line 242 of file neural.c.

References Llist::layer, layer_rand(), Llist::prev, and Net::tail.

Referenced by act_neural_cover(), cond_neural_cover(), and rule_neural_act_cover().

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

◆ neural_remove()

void neural_remove ( struct Net net,
const int  pos 
)

Removes a layer from a neural network.

Parameters
[in]netThe neural network removing the layer.
[in]posThe position of the layer in the network to be removed.

Definition at line 140 of file neural.c.

References Net::head, Llist::layer, layer_free(), Net::n_layers, Net::n_outputs, Layer::n_outputs, Llist::next, Net::output, Layer::output, Llist::prev, and Net::tail.

Referenced by neural_pop().

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

◆ neural_resize()

void neural_resize ( const struct Net net)

Resizes neural network layers as necessary.

Parameters
[in]netThe neural network to resize.

Definition at line 290 of file neural.c.

References Llist::layer, layer_resize(), Layer::n_inputs, Llist::prev, and Net::tail.

Referenced by pred_neural_expand().

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

◆ neural_save()

size_t neural_save ( const struct Net net,
FILE *  fp 
)

Writes a neural network to a file.

Parameters
[in]netThe neural network to save.
[in]fpPointer to the file to be written.
Returns
The number of elements written.

Definition at line 478 of file neural.c.

References Llist::layer, layer_save(), Net::n_inputs, Net::n_layers, Net::n_outputs, Llist::prev, and Net::tail.

Referenced by act_neural_save(), cond_neural_save(), pred_neural_save(), and rule_neural_cond_save().

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

◆ neural_size()

double neural_size ( const struct Net net)

Returns the total number of non-zero weights in a neural network.

Parameters
[in]netA neural network.
Returns
The calculated network size.

Definition at line 450 of file neural.c.

References CONNECTED, CONVOLUTIONAL, Llist::layer, LSTM, Layer::n_active, Llist::prev, RECURRENT, Layer::size, Net::tail, and Layer::type.

Referenced by cond_neural_size(), pred_neural_size(), and rule_neural_cond_size().

Here is the caller graph for this function: