XCSF  1.4.7
XCSF learning classifier system
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 
73 struct Layer {
74  int type;
75  double *state;
76  double *output;
77  uint32_t options;
78  double *weights;
79  bool *weight_active;
80  double *biases;
81  double *bias_updates;
82  double *weight_updates;
83  double *delta;
84  double *mu;
85  double eta;
86  double eta_max;
87  double eta_min;
88  double momentum;
89  double decay;
90  int n_inputs;
91  int n_outputs;
94  int n_weights;
95  int n_biases;
96  int n_active;
97  int function;
98  double scale;
99  double probability;
100  struct LayerVtbl const *layer_vptr;
101  double *prev_state;
102  struct Layer *input_layer;
103  struct Layer *self_layer;
104  struct Layer *output_layer;
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;
128  int channels;
129  int pad;
130  int out_w;
131  int out_h;
132  int out_c;
133  int size;
134  int stride;
135  int *indexes;
136  int n_filters;
137 };
138 
143 struct 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 
168 static inline double *
169 layer_output(const struct Layer *l)
170 {
171  return (*l->layer_vptr->layer_impl_output)(l);
172 }
173 
180 static inline void
181 layer_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 
193 static inline void
194 layer_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 
204 static inline void
205 layer_update(const struct Layer *l)
206 {
207  (*l->layer_vptr->layer_impl_update)(l);
208 }
209 
215 static inline bool
216 layer_mutate(struct Layer *l)
217 {
218  return (*l->layer_vptr->layer_impl_mutate)(l);
219 }
220 
227 static inline void
228 layer_resize(struct Layer *l, const struct Layer *prev)
229 {
230  (*l->layer_vptr->layer_impl_resize)(l, prev);
231 }
232 
238 static inline struct Layer *
239 layer_copy(const struct Layer *src)
240 {
241  return (*src->layer_vptr->layer_impl_copy)(src);
242 }
243 
248 static inline void
249 layer_free(const struct Layer *l)
250 {
251  (*l->layer_vptr->layer_impl_free)(l);
252 }
253 
258 static inline void
259 layer_rand(struct Layer *l)
260 {
261  (*l->layer_vptr->layer_impl_rand)(l);
262 }
263 
269 static inline void
270 layer_print(const struct Layer *l, const bool print_weights)
271 {
272  (*l->layer_vptr->layer_impl_print)(l, print_weights);
273 }
274 
280 static inline char *
281 layer_json_export(const struct Layer *l, const bool return_weights)
282 {
283  return (*l->layer_vptr->layer_impl_json_export)(l, return_weights);
284 }
285 
286 bool
287 layer_mutate_connectivity(struct Layer *l, const double mu_enable,
288  const double mu_disable);
289 
290 bool
291 layer_mutate_eta(struct Layer *l, const double mu);
292 
293 bool
294 layer_mutate_functions(struct Layer *l, const double mu);
295 
296 bool
297 layer_mutate_weights(struct Layer *l, const double mu);
298 
299 int
300 layer_mutate_neurons(const struct Layer *l, const double mu);
301 
302 void
303 layer_add_neurons(struct Layer *l, const int n);
304 
305 void
306 layer_calc_n_active(struct Layer *l);
307 
308 void
309 layer_defaults(struct Layer *l);
310 
311 void
312 layer_init_eta(struct Layer *l);
313 
314 void
315 layer_set_vptr(struct Layer *l);
316 
317 void
318 layer_weight_clamp(const struct Layer *l);
319 
320 void
321 layer_weight_print(const struct Layer *l, const bool print_weights);
322 
323 char *
324 layer_weight_json(const struct Layer *l, const bool return_weights);
325 
326 void
327 layer_weight_rand(struct Layer *l);
328 
329 void
331 
332 const char *
333 layer_type_as_string(const int type);
334 
335 int
336 layer_type_as_int(const char *type);
337 
338 bool
339 layer_receives_images(const int type);
340 
341 void
342 layer_guard_biases(const struct Layer *l);
343 
344 void
345 layer_guard_outputs(const struct Layer *l);
346 
347 void
348 layer_guard_weights(const struct Layer *l);
349 
355 static inline struct Layer *
356 layer_init(const struct ArgsLayer *args)
357 {
358  struct Layer *l = (struct Layer *) malloc(sizeof(struct Layer));
359  layer_defaults(l);
360  l->type = args->type;
361  layer_set_vptr(l);
362  (*l->layer_vptr->layer_impl_init)(l, args);
363  return l;
364 }
365 
372 static inline size_t
373 layer_save(const struct Layer *l, FILE *fp)
374 {
375  size_t s = fwrite(&l->type, sizeof(int), 1, fp);
376  s += (*l->layer_vptr->layer_impl_save)(l, fp);
377  return s;
378 }
379 
386 static inline size_t
387 layer_load(struct Layer *l, FILE *fp)
388 {
389  layer_defaults(l);
390  size_t s = fread(&l->type, sizeof(int), 1, fp);
391  layer_set_vptr(l);
392  s += (*l->layer_vptr->layer_impl_load)(l, fp);
393  return s;
394 }
An implementation of a multi-layer perceptron neural network.
void layer_guard_biases(const struct Layer *l)
Check number of biases is within bounds.
Definition: neural_layer.c:581
bool layer_mutate_connectivity(struct Layer *l, const double mu_enable, const double mu_disable)
Mutates a layer's connectivity by zeroing weights.
Definition: neural_layer.c:176
void layer_add_neurons(struct Layer *l, const int n)
Adds N neurons to a layer. Negative N removes neurons.
Definition: neural_layer.c:130
void layer_weight_print(const struct Layer *l, const bool print_weights)
Prints a layer's weights and biases.
Definition: neural_layer.c:309
const char * layer_type_as_string(const int type)
Returns a string representation of a layer type from an integer.
Definition: neural_layer.c:486
static void layer_rand(struct Layer *l)
Randomises a layer.
Definition: neural_layer.h:259
void layer_defaults(struct Layer *l)
Initialises a layer to default values.
Definition: neural_layer.c:413
static void layer_resize(struct Layer *l, const struct Layer *prev)
Resizes a layer using the previous layer's inputs.
Definition: neural_layer.h:228
static double * layer_output(const struct Layer *l)
Returns the outputs of a layer.
Definition: neural_layer.h:169
static size_t layer_save(const struct Layer *l, FILE *fp)
Writes the layer to a file.
Definition: neural_layer.h:373
static struct Layer * layer_init(const struct ArgsLayer *args)
Creates and initialises a new layer.
Definition: neural_layer.h:356
int layer_mutate_neurons(const struct Layer *l, const double mu)
Returns the number of neurons to add or remove from a layer.
Definition: neural_layer.c:106
static struct Layer * layer_copy(const struct Layer *src)
Creates and returns a copy of a specified layer.
Definition: neural_layer.h:239
bool layer_mutate_functions(struct Layer *l, const double mu)
Mutates a layer's activation function by random selection.
Definition: neural_layer.c:283
int layer_type_as_int(const char *type)
Returns the integer representation of a layer type given a name.
Definition: neural_layer.c:540
static void layer_free(const struct Layer *l)
Frees the memory used by the layer.
Definition: neural_layer.h:249
static void layer_backward(const struct Layer *l, const struct Net *net, const double *input, double *delta)
Backward propagates the error through a layer.
Definition: neural_layer.h:194
static size_t layer_load(struct Layer *l, FILE *fp)
Reads the layer from a file.
Definition: neural_layer.h:387
char * layer_weight_json(const struct Layer *l, const bool return_weights)
Returns a json formatted string representation of a layer's weights.
Definition: neural_layer.c:324
void layer_set_vptr(struct Layer *l)
Sets a neural network layer's functions to the implementations.
Definition: neural_layer.c:42
void layer_weight_clamp(const struct Layer *l)
Clamps a layer's weights and biases in range [WEIGHT_MIN, WEIGHT_MAX].
Definition: neural_layer.c:365
void layer_guard_outputs(const struct Layer *l)
Check number of outputs is within bounds.
Definition: neural_layer.c:595
void layer_weight_rand(struct Layer *l)
Randomises a layer's weights and biases.
Definition: neural_layer.c:348
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...
Definition: neural_layer.c:204
static void layer_update(const struct Layer *l)
Updates the weights and biases of a layer.
Definition: neural_layer.h:205
static void layer_forward(const struct Layer *l, const struct Net *net, const double *input)
Forward propagates an input through the layer.
Definition: neural_layer.h:181
void layer_calc_n_active(struct Layer *l)
Recalculates the number of active connections within a layer.
Definition: neural_layer.c:384
static char * layer_json_export(const struct Layer *l, const bool return_weights)
Returns a json formatted string representation of a layer.
Definition: neural_layer.h:281
bool layer_receives_images(const int type)
Returns a whether a layer type expects images as input.
Definition: neural_layer.c:521
static bool layer_mutate(struct Layer *l)
Performs layer mutation.
Definition: neural_layer.h:216
void layer_init_eta(struct Layer *l)
Initialises a layer's gradient descent rate.
Definition: neural_layer.c:399
bool layer_mutate_eta(struct Layer *l, const double mu)
Mutates the gradient descent rate of a neural layer.
Definition: neural_layer.c:88
static void layer_print(const struct Layer *l, const bool print_weights)
Prints the layer.
Definition: neural_layer.h:270
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...
Definition: neural_layer.c:252
void layer_guard_weights(const struct Layer *l)
Check number of weights is within bounds.
Definition: neural_layer.c:609
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.
Definition: neural_layer.h:143
void(* layer_impl_resize)(struct Layer *l, const struct Layer *prev)
Definition: neural_layer.h:146
void(* layer_impl_rand)(struct Layer *l)
Definition: neural_layer.h:149
void(* layer_impl_forward)(const struct Layer *l, const struct Net *net, const double *input)
Definition: neural_layer.h:154
char *(* layer_impl_json_export)(const struct Layer *l, const bool return_weights)
Definition: neural_layer.h:159
void(* layer_impl_free)(const struct Layer *l)
Definition: neural_layer.h:148
double *(* layer_impl_output)(const struct Layer *l)
Definition: neural_layer.h:156
bool(* layer_impl_mutate)(struct Layer *l)
Definition: neural_layer.h:145
struct Layer *(* layer_impl_copy)(const struct Layer *src)
Definition: neural_layer.h:147
void(* layer_impl_init)(struct Layer *l, const struct ArgsLayer *args)
Definition: neural_layer.h:144
size_t(* layer_impl_load)(struct Layer *l, FILE *fp)
Definition: neural_layer.h:158
size_t(* layer_impl_save)(const struct Layer *l, FILE *fp)
Definition: neural_layer.h:157
void(* layer_impl_print)(const struct Layer *l, const bool print_weights)
Definition: neural_layer.h:150
void(* layer_impl_backward)(const struct Layer *l, const struct Net *net, const double *input, double *delta)
Definition: neural_layer.h:152
void(* layer_impl_update)(const struct Layer *l)
Definition: neural_layer.h:151
Neural network layer data structure.
Definition: neural_layer.h:73
struct Layer * wf
LSTM.
Definition: neural_layer.h:110
double * output
Current neuron outputs (after activation function)
Definition: neural_layer.h:76
double decay
Weight decay for gradient descent.
Definition: neural_layer.h:89
struct Layer * wo
LSTM.
Definition: neural_layer.h:113
int size
Pool and Conv.
Definition: neural_layer.h:133
struct Layer * input_layer
Recursive layer input.
Definition: neural_layer.h:102
double * state
Current neuron states (before activation function)
Definition: neural_layer.h:75
struct Layer * uo
LSTM.
Definition: neural_layer.h:109
int pad
Pool and Conv.
Definition: neural_layer.h:129
int recurrent_function
LSTM.
Definition: neural_layer.h:105
int max_neuron_grow
Maximum number neurons to add per mutation event.
Definition: neural_layer.h:93
struct Layer * ug
LSTM.
Definition: neural_layer.h:108
int stride
Pool, Conv, and Upsample.
Definition: neural_layer.h:134
int n_inputs
Number of layer inputs.
Definition: neural_layer.h:90
double * g
LSTM.
Definition: neural_layer.h:118
int n_biases
Number of layer biases.
Definition: neural_layer.h:95
bool * weight_active
Whether each connection is present in the layer.
Definition: neural_layer.h:79
double * weights
Weights for calculating neuron states.
Definition: neural_layer.h:78
double * weight_updates
Updates to weights.
Definition: neural_layer.h:82
int n_filters
Conv.
Definition: neural_layer.h:136
double * mu
Mutation rates.
Definition: neural_layer.h:84
int channels
Pool, Conv, and Upsample.
Definition: neural_layer.h:128
double scale
Usage depends on layer implementation.
Definition: neural_layer.h:98
double * c
LSTM.
Definition: neural_layer.h:120
int height
Pool, Conv, and Upsample.
Definition: neural_layer.h:126
double * temp
LSTM.
Definition: neural_layer.h:122
struct Layer * wg
LSTM.
Definition: neural_layer.h:112
struct LayerVtbl const * layer_vptr
Functions acting on layers.
Definition: neural_layer.h:100
int max_outputs
Maximum number of neurons in the layer.
Definition: neural_layer.h:92
double * h
LSTM.
Definition: neural_layer.h:121
int width
Pool, Conv, and Upsample.
Definition: neural_layer.h:127
int n_weights
Number of layer weights.
Definition: neural_layer.h:94
double * dc
LSTM.
Definition: neural_layer.h:125
double probability
Usage depends on layer implementation.
Definition: neural_layer.h:99
double * bias_updates
Updates to biases.
Definition: neural_layer.h:81
double * temp3
LSTM.
Definition: neural_layer.h:124
double eta_max
Maximum gradient descent rate.
Definition: neural_layer.h:86
struct Layer * uf
LSTM.
Definition: neural_layer.h:106
double * i
LSTM.
Definition: neural_layer.h:117
double * temp2
LSTM.
Definition: neural_layer.h:123
struct Layer * output_layer
Recursive layer output.
Definition: neural_layer.h:104
double * o
LSTM.
Definition: neural_layer.h:119
int n_outputs
Number of layer outputs.
Definition: neural_layer.h:91
double * biases
Biases for calculating neuron states.
Definition: neural_layer.h:80
struct Layer * self_layer
Recursive layer self.
Definition: neural_layer.h:103
int n_active
Number of active weights / connections.
Definition: neural_layer.h:96
struct Layer * ui
LSTM.
Definition: neural_layer.h:107
double * prev_state
Previous state for recursive layers.
Definition: neural_layer.h:101
int out_w
Pool, Conv, and Upsample.
Definition: neural_layer.h:130
int type
Layer type: CONNECTED, DROPOUT, etc.
Definition: neural_layer.h:74
double * cell
LSTM.
Definition: neural_layer.h:114
int out_c
Pool, Conv, and Upsample.
Definition: neural_layer.h:132
int * indexes
Pool.
Definition: neural_layer.h:135
struct Layer * wi
LSTM.
Definition: neural_layer.h:111
double * delta
Delta for updating weights.
Definition: neural_layer.h:83
uint32_t options
Bitwise layer options permitting evolution, SGD, etc.
Definition: neural_layer.h:77
double * f
LSTM.
Definition: neural_layer.h:116
double * prev_cell
LSTM.
Definition: neural_layer.h:115
int out_h
Pool, Conv, and Upsample.
Definition: neural_layer.h:131
double eta_min
Minimum gradient descent rate.
Definition: neural_layer.h:87
double eta
Gradient descent rate.
Definition: neural_layer.h:85
double momentum
Momentum for gradient descent.
Definition: neural_layer.h:88
Neural network data structure.
Definition: neural.h:48