XCSF 1.4.8
XCSF learning classifier system
Loading...
Searching...
No Matches
neural_layer.h
Go to the documentation of this file.
1/*
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
15
24#pragma once
25
26#include "neural.h"
27#include "neural_layer_args.h"
28
29#define CONNECTED (0)
30#define DROPOUT (1)
31#define NOISE (2)
32#define SOFTMAX (3)
33#define RECURRENT (4)
34#define LSTM (5)
35#define MAXPOOL (6)
36#define CONVOLUTIONAL (7)
37#define AVGPOOL (8)
38#define UPSAMPLE (9)
39
40#define STRING_CONNECTED ("connected\0")
41#define STRING_DROPOUT ("dropout\0")
42#define STRING_NOISE ("noise\0")
43#define STRING_SOFTMAX ("softmax\0")
44#define STRING_RECURRENT ("recurrent\0")
45#define STRING_LSTM ("lstm\0")
46#define STRING_MAXPOOL ("maxpool\0")
47#define STRING_CONVOLUTIONAL ("convolutional\0")
48#define STRING_AVGPOOL ("avgpool\0")
49#define STRING_UPSAMPLE ("upsample\0")
50
51#define LAYER_EVOLVE_WEIGHTS (1 << 0)
52#define LAYER_EVOLVE_NEURONS (1 << 1)
53#define LAYER_EVOLVE_FUNCTIONS (1 << 2)
54#define LAYER_SGD_WEIGHTS (1 << 3)
55#define LAYER_EVOLVE_ETA (1 << 4)
56#define LAYER_EVOLVE_CONNECT (1 << 5)
57
58#define NEURON_MIN (-100)
59#define NEURON_MAX (100)
60#define WEIGHT_MIN (-10)
61#define WEIGHT_MAX (10)
62#define N_WEIGHTS_MAX (20000000)
63#define N_INPUTS_MAX (2000000)
64#define N_OUTPUTS_MAX (2000000)
65
66#define WEIGHT_SD_INIT (0.1)
67#define WEIGHT_SD (0.1)
68#define WEIGHT_SD_RAND (1.0)
69
73struct Layer {
74 int type;
75 double *state;
76 double *output;
77 uint32_t options;
78 double *weights;
80 double *biases;
81 double *bias_updates;
83 double *delta;
84 double *mu;
85 double eta;
86 double eta_max;
87 double eta_min;
88 double momentum;
89 double decay;
98 double scale;
99 double probability;
100 struct LayerVtbl const *layer_vptr;
101 double *prev_state;
106 struct Layer *uf;
107 struct Layer *ui;
108 struct Layer *ug;
109 struct Layer *uo;
110 struct Layer *wf;
111 struct Layer *wi;
112 struct Layer *wg;
113 struct Layer *wo;
114 double *cell;
115 double *prev_cell;
116 double *f;
117 double *i;
118 double *g;
119 double *o;
120 double *c;
121 double *h;
122 double *temp;
123 double *temp2;
124 double *temp3;
125 double *dc;
126 int height;
127 int width;
129 int pad;
130 int out_w;
131 int out_h;
132 int out_c;
133 int size;
134 int stride;
135 int *indexes;
137};
138
143struct LayerVtbl {
144 void (*layer_impl_init)(struct Layer *l, const struct ArgsLayer *args);
145 bool (*layer_impl_mutate)(struct Layer *l);
146 void (*layer_impl_resize)(struct Layer *l, const struct Layer *prev);
147 struct Layer *(*layer_impl_copy)(const struct Layer *src);
148 void (*layer_impl_free)(const struct Layer *l);
149 void (*layer_impl_rand)(struct Layer *l);
150 void (*layer_impl_print)(const struct Layer *l, const bool print_weights);
151 void (*layer_impl_update)(const struct Layer *l);
152 void (*layer_impl_backward)(const struct Layer *l, const struct Net *net,
153 const double *input, double *delta);
154 void (*layer_impl_forward)(const struct Layer *l, const struct Net *net,
155 const double *input);
156 double *(*layer_impl_output)(const struct Layer *l);
157 size_t (*layer_impl_save)(const struct Layer *l, FILE *fp);
158 size_t (*layer_impl_load)(struct Layer *l, FILE *fp);
159 char *(*layer_impl_json_export)(const struct Layer *l,
160 const bool return_weights);
161};
162
168static inline double *
169layer_output(const struct Layer *l)
170{
171 return (*l->layer_vptr->layer_impl_output)(l);
172}
173
180static inline void
181layer_forward(const struct Layer *l, const struct Net *net, const double *input)
182{
183 (*l->layer_vptr->layer_impl_forward)(l, net, input);
184}
185
193static inline void
194layer_backward(const struct Layer *l, const struct Net *net,
195 const double *input, double *delta)
196{
197 (*l->layer_vptr->layer_impl_backward)(l, net, input, delta);
198}
199
204static inline void
205layer_update(const struct Layer *l)
206{
208}
209
215static inline bool
217{
218 return (*l->layer_vptr->layer_impl_mutate)(l);
219}
220
226static inline void
227layer_resize(struct Layer *l, const struct Layer *prev)
228{
229 (*l->layer_vptr->layer_impl_resize)(l, prev);
230}
231
237static inline struct Layer *
238layer_copy(const struct Layer *src)
239{
240 return (*src->layer_vptr->layer_impl_copy)(src);
241}
242
247static inline void
248layer_free(const struct Layer *l)
249{
250 (*l->layer_vptr->layer_impl_free)(l);
251}
252
257static inline void
259{
260 (*l->layer_vptr->layer_impl_rand)(l);
261}
262
268static inline void
269layer_print(const struct Layer *l, const bool print_weights)
270{
271 (*l->layer_vptr->layer_impl_print)(l, print_weights);
272}
273
279static inline char *
280layer_json_export(const struct Layer *l, const bool return_weights)
281{
282 return (*l->layer_vptr->layer_impl_json_export)(l, return_weights);
283}
284
285bool
286layer_mutate_connectivity(struct Layer *l, const double mu_enable,
287 const double mu_disable);
288
289bool
290layer_mutate_eta(struct Layer *l, const double mu);
291
292bool
293layer_mutate_functions(struct Layer *l, const double mu);
294
295bool
296layer_mutate_weights(struct Layer *l, const double mu);
297
298int
299layer_mutate_neurons(const struct Layer *l, const double mu);
300
301void
302layer_add_neurons(struct Layer *l, const int n);
303
304void
305layer_calc_n_active(struct Layer *l);
306
307void
308layer_defaults(struct Layer *l);
309
310void
311layer_init_eta(struct Layer *l);
312
313void
314layer_set_vptr(struct Layer *l);
315
316void
317layer_weight_clamp(const struct Layer *l);
318
319void
320layer_weight_print(const struct Layer *l, const bool print_weights);
321
322char *
323layer_weight_json(const struct Layer *l, const bool return_weights);
324
325void
326layer_weight_rand(struct Layer *l);
327
328void
330
331const char *
332layer_type_as_string(const int type);
333
334int
335layer_type_as_int(const char *type);
336
337bool
338layer_receives_images(const int type);
339
340void
341layer_guard_biases(const struct Layer *l);
342
343void
344layer_guard_outputs(const struct Layer *l);
345
346void
347layer_guard_weights(const struct Layer *l);
348
354static inline struct Layer *
355layer_init(const struct ArgsLayer *args)
356{
357 struct Layer *l = (struct Layer *) malloc(sizeof(struct Layer));
359 l->type = args->type;
361 (*l->layer_vptr->layer_impl_init)(l, args);
362 return l;
363}
364
371static inline size_t
372layer_save(const struct Layer *l, FILE *fp)
373{
374 size_t s = fwrite(&l->type, sizeof(int), 1, fp);
375 s += (*l->layer_vptr->layer_impl_save)(l, fp);
376 return s;
377}
378
385static inline size_t
386layer_load(struct Layer *l, FILE *fp)
387{
389 size_t s = fread(&l->type, sizeof(int), 1, fp);
391 s += (*l->layer_vptr->layer_impl_load)(l, fp);
392 return s;
393}
An implementation of a multi-layer perceptron neural network.
void layer_guard_biases(const struct Layer *l)
Check number of biases is within bounds.
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_add_neurons(struct Layer *l, const int n)
Adds N neurons to a layer. Negative N removes neurons.
void layer_weight_print(const struct Layer *l, const bool print_weights)
Prints a layer's weights and biases.
static void layer_rand(struct Layer *l)
Randomises a layer.
void layer_defaults(struct Layer *l)
Initialises a layer to default values.
static void layer_resize(struct Layer *l, const struct Layer *prev)
Resizes a layer using the previous layer's inputs.
static size_t layer_save(const struct Layer *l, FILE *fp)
Writes the layer to a file.
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.
int layer_type_as_int(const char *type)
Returns the integer representation of a layer type given a name.
static void layer_free(const struct Layer *l)
Frees the memory used by the layer.
static double * layer_output(const struct Layer *l)
Returns the outputs of a 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.
void layer_set_vptr(struct Layer *l)
Sets a neural network layer's functions to the implementations.
void layer_weight_clamp(const struct Layer *l)
Clamps a layer's weights and biases in range [WEIGHT_MIN, WEIGHT_MAX].
static char * layer_json_export(const struct Layer *l, const bool return_weights)
Returns a json formatted string representation of a layer.
void layer_guard_outputs(const struct Layer *l)
Check number of outputs is within bounds.
void layer_weight_rand(struct Layer *l)
Randomises a layer's weights and biases.
void layer_ensure_input_represention(struct Layer *l)
Ensures that each neuron is connected to at least one input and each input is connected to at least o...
static void layer_update(const struct Layer *l)
Updates the weights and biases of a layer.
static struct Layer * layer_init(const struct ArgsLayer *args)
Creates and initialises a new layer.
static void layer_forward(const struct Layer *l, const struct Net *net, const double *input)
Forward propagates an input through the layer.
char * layer_weight_json(const struct Layer *l, const bool return_weights)
Returns a json formatted string representation of a layer's weights.
static struct Layer * layer_copy(const struct Layer *src)
Creates and returns a copy of a specified layer.
void layer_calc_n_active(struct Layer *l)
Recalculates the number of active connections within a layer.
const char * layer_type_as_string(const int type)
Returns a string representation of a layer type from an integer.
bool layer_receives_images(const int type)
Returns a whether a layer type expects images as input.
static bool layer_mutate(struct Layer *l)
Performs layer mutation.
void layer_init_eta(struct Layer *l)
Initialises a layer's gradient descent rate.
bool layer_mutate_eta(struct Layer *l, const double mu)
Mutates the gradient descent rate of a neural layer.
static void layer_print(const struct Layer *l, const bool print_weights)
Prints the 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...
void layer_guard_weights(const struct Layer *l)
Check number of weights is within bounds.
Functions operating on neural network arguments/constants.
Parameters for initialising a neural network layer.
int type
Layer type: CONNECTED, DROPOUT, etc.
Neural network layer interface data structure.
void(* layer_impl_resize)(struct Layer *l, const struct Layer *prev)
void(* layer_impl_rand)(struct Layer *l)
void(* layer_impl_forward)(const struct Layer *l, const struct Net *net, const double *input)
char *(* layer_impl_json_export)(const struct Layer *l, const bool return_weights)
double *(* layer_impl_output)(const struct Layer *l)
struct Layer *(* layer_impl_copy)(const struct Layer *src)
void(* layer_impl_free)(const struct Layer *l)
bool(* layer_impl_mutate)(struct Layer *l)
void(* layer_impl_init)(struct Layer *l, const struct ArgsLayer *args)
size_t(* layer_impl_load)(struct Layer *l, FILE *fp)
size_t(* layer_impl_save)(const struct Layer *l, FILE *fp)
void(* layer_impl_print)(const struct Layer *l, const bool print_weights)
void(* layer_impl_backward)(const struct Layer *l, const struct Net *net, const double *input, double *delta)
void(* layer_impl_update)(const struct Layer *l)
Neural network layer data structure.
struct Layer * wf
LSTM.
double * output
Current neuron outputs (after activation function)
double decay
Weight decay for gradient descent.
struct Layer * wo
LSTM.
int size
Pool and Conv.
struct Layer * input_layer
Recursive layer input.
double * state
Current neuron states (before activation function)
struct Layer * uo
LSTM.
int pad
Pool and Conv.
int recurrent_function
LSTM.
int max_neuron_grow
Maximum number neurons to add per mutation event.
struct Layer * ug
LSTM.
int stride
Pool, Conv, and Upsample.
int n_inputs
Number of layer inputs.
double * g
LSTM.
int n_biases
Number of layer biases.
bool * weight_active
Whether each connection is present in the layer.
double * weights
Weights for calculating neuron states.
double * weight_updates
Updates to weights.
int n_filters
Conv.
double * mu
Mutation rates.
int channels
Pool, Conv, and Upsample.
double scale
Usage depends on layer implementation.
int function
Layer activation function.
double * c
LSTM.
int height
Pool, Conv, and Upsample.
double * temp
LSTM.
struct Layer * wg
LSTM.
struct LayerVtbl const * layer_vptr
Functions acting on layers.
int max_outputs
Maximum number of neurons in the layer.
double * h
LSTM.
int width
Pool, Conv, and Upsample.
int n_weights
Number of layer weights.
double * dc
LSTM.
double probability
Usage depends on layer implementation.
double * bias_updates
Updates to biases.
double * temp3
LSTM.
double eta_max
Maximum gradient descent rate.
struct Layer * uf
LSTM.
double * i
LSTM.
double * temp2
LSTM.
struct Layer * output_layer
Recursive layer output.
double * o
LSTM.
int n_outputs
Number of layer outputs.
double * biases
Biases for calculating neuron states.
struct Layer * self_layer
Recursive layer self.
int n_active
Number of active weights / connections.
struct Layer * ui
LSTM.
double * prev_state
Previous state for recursive layers.
int out_w
Pool, Conv, and Upsample.
int type
Layer type: CONNECTED, DROPOUT, etc.
double * cell
LSTM.
int out_c
Pool, Conv, and Upsample.
int * indexes
Pool.
struct Layer * wi
LSTM.
double * delta
Delta for updating weights.
uint32_t options
Bitwise layer options permitting evolution, SGD, etc.
double * f
LSTM.
double * prev_cell
LSTM.
int out_h
Pool, Conv, and Upsample.
double eta_min
Minimum gradient descent rate.
double eta
Gradient descent rate.
double momentum
Momentum for gradient descent.
Neural network data structure.
Definition neural.h:48