XCSF
1.4.7
XCSF learning classifier system
|
An implementation of a multi-layer perceptron neural network. More...
#include "utils.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Go to the source code of this file.
Data Structures | |
struct | Llist |
Forward declaration of layer structure. More... | |
struct | Net |
Neural network data structure. More... | |
Functions | |
bool | neural_mutate (const struct Net *net) |
Mutates 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_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... | |
double | neural_size (const struct Net *net) |
Returns the total number of non-zero weights in a neural network. More... | |
size_t | neural_load (struct Net *net, FILE *fp) |
Reads a neural network from a file. More... | |
size_t | neural_save (const struct Net *net, FILE *fp) |
Writes a neural network to a file. 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_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_learn (const struct Net *net, const double *output, const double *input) |
Performs a gradient descent update on a neural network. More... | |
void | neural_print (const struct Net *net, const bool print_weights) |
Prints a neural network. More... | |
void | neural_propagate (struct Net *net, const double *input, const bool train) |
Forward propagates a neural network. More... | |
void | neural_rand (const struct Net *net) |
Randomises the layers within a neural network. More... | |
void | neural_resize (const struct Net *net) |
Resizes neural network layers as necessary. More... | |
An implementation of a multi-layer perceptron neural network.
Definition in file neural.h.
Copies a neural network.
[in] | dest | The destination neural network. |
[in] | src | The 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().
Initialises and creates a new neural network from a parameter list.
[in] | net | The neural network to initialise. |
[in] | arg | List 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().
void neural_free | ( | struct Net * | net | ) |
Frees a neural network.
[in] | net | The 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().
void neural_init | ( | struct Net * | net | ) |
Initialises an empty neural network.
[in] | net | The 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().
Inserts a layer into a neural network.
[in] | net | The neural network receiving the layer. |
[in] | l | The layer to insert. |
[in] | pos | The 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().
char* neural_json_export | ( | const struct Net * | net, |
const bool | return_weights | ||
) |
Returns a json formatted string representation of a neural network.
[in] | net | The neural network to return. |
[in] | return_weights | Whether to return the weights in each layer. |
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().
Creates a neural network from a cJSON object.
[in,out] | net | The neural network to return. |
[in] | arg | List of layer parameters defining the initial network. |
[in] | json | cJSON 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().
void neural_learn | ( | const struct Net * | net, |
const double * | truth, | ||
const double * | input | ||
) |
Performs a gradient descent update on a neural network.
[in] | net | The neural network to be updated. |
[in] | truth | The desired network output. |
[in] | input | The 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().
size_t neural_load | ( | struct Net * | net, |
FILE * | fp | ||
) |
Reads a neural network from a file.
[in] | net | The neural network to load. |
[in] | fp | Pointer to the file to be 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().
bool neural_mutate | ( | const struct Net * | net | ) |
Mutates a neural network.
[in] | net | The neural network to mutate. |
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().
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.
[in] | net | The neural network to output. |
[in] | IDX | Which neuron in the output layer to return. |
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().
double* neural_outputs | ( | const struct Net * | net | ) |
Returns the outputs from the output layer of a neural network.
[in] | net | The neural network to output. |
Definition at line 384 of file neural.c.
References Net::head, Llist::layer, and layer_output().
Referenced by act_neural_compute().
void neural_pop | ( | struct Net * | net | ) |
Removes the layer at the head of a neural network.
[in] | net | The 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().
void neural_print | ( | const struct Net * | net, |
const bool | print_weights | ||
) |
Prints a neural network.
[in] | net | The neural network to print. |
[in] | print_weights | Whether to print the weights in each layer. |
Definition at line 395 of file neural.c.
References neural_json_export().
void neural_propagate | ( | struct Net * | net, |
const double * | input, | ||
const bool | train | ||
) |
Forward propagates a neural network.
[in] | net | Neural network to propagate. |
[in] | input | Input state. |
[in] | train | Whether 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().
Inserts a layer at the head of a neural network.
[in] | net | The neural network receiving the layer. |
[in] | l | The 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().
void neural_rand | ( | const struct Net * | net | ) |
Randomises the layers within a neural network.
[in] | net | The 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().
void neural_remove | ( | struct Net * | net, |
const int | pos | ||
) |
Removes a layer from a neural network.
[in] | net | The neural network removing the layer. |
[in] | pos | The 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().
void neural_resize | ( | const struct Net * | net | ) |
Resizes neural network layers as necessary.
[in] | net | The 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().
size_t neural_save | ( | const struct Net * | net, |
FILE * | fp | ||
) |
Writes a neural network to a file.
[in] | net | The neural network to save. |
[in] | fp | Pointer to the file to be 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().
double neural_size | ( | const struct Net * | net | ) |
Returns the total number of non-zero weights in a neural network.
[in] | net | A neural network. |
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().