XCSF  1.4.7
XCSF learning classifier system
neural_layer_noise.c
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 #include "neural_layer_noise.h"
25 #include "neural_activations.h"
26 #include "utils.h"
27 
32 static void
34 {
36  l->output = calloc(l->n_outputs, sizeof(double));
37  l->delta = calloc(l->n_outputs, sizeof(double));
38  l->state = calloc(l->n_outputs, sizeof(double));
39 }
40 
45 static void
46 free_layer_arrays(const struct Layer *l)
47 {
48  free(l->output);
49  free(l->delta);
50  free(l->state);
51 }
52 
58 void
59 neural_layer_noise_init(struct Layer *l, const struct ArgsLayer *args)
60 {
61  l->n_inputs = args->n_inputs;
62  l->n_outputs = args->n_inputs;
63  l->max_outputs = args->n_inputs;
64  l->out_w = args->width;
65  l->out_h = args->height;
66  l->out_c = args->channels;
67  l->probability = args->probability;
68  l->scale = args->scale;
70 }
71 
77 struct Layer *
78 neural_layer_noise_copy(const struct Layer *src)
79 {
80  if (src->type != NOISE) {
81  printf("neural_layer_noise_copy(): incorrect source layer type\n");
82  exit(EXIT_FAILURE);
83  }
84  struct Layer *l = malloc(sizeof(struct Layer));
85  layer_defaults(l);
86  l->type = src->type;
87  l->layer_vptr = src->layer_vptr;
88  l->n_inputs = src->n_inputs;
89  l->n_outputs = src->n_outputs;
90  l->out_w = src->out_w;
91  l->out_c = src->out_c;
92  l->out_h = src->out_h;
93  l->max_outputs = src->max_outputs;
94  l->probability = src->probability;
95  l->scale = src->scale;
97  return l;
98 }
99 
104 void
106 {
108 }
109 
114 void
116 {
117  (void) l;
118 }
119 
126 void
127 neural_layer_noise_forward(const struct Layer *l, const struct Net *net,
128  const double *input)
129 {
130  if (!net->train) {
131  for (int i = 0; i < l->n_inputs; ++i) {
132  l->output[i] = input[i];
133  }
134  } else {
135  for (int i = 0; i < l->n_inputs; ++i) {
136  l->state[i] = rand_uniform(0, 1);
137  if (l->state[i] < l->probability) {
138  l->output[i] = input[i] + rand_normal(0, l->scale);
139  } else {
140  l->output[i] = input[i];
141  }
142  }
143  }
144 }
145 
153 void
154 neural_layer_noise_backward(const struct Layer *l, const struct Net *net,
155  const double *input, double *delta)
156 {
157  (void) net;
158  (void) input;
159  if (delta) {
160  for (int i = 0; i < l->n_inputs; ++i) {
161  delta[i] += l->delta[i];
162  }
163  }
164 }
165 
170 void
172 {
173  (void) l;
174 }
175 
181 bool
183 {
184  (void) l;
185  return false;
186 }
187 
193 void
194 neural_layer_noise_resize(struct Layer *l, const struct Layer *prev)
195 {
196  l->n_inputs = prev->n_outputs;
197  l->n_outputs = prev->n_outputs;
198  l->max_outputs = prev->n_outputs;
199  l->out_w = prev->out_w;
200  l->out_h = prev->out_h;
201  l->out_c = prev->out_c;
204 }
205 
211 double *
213 {
214  return l->output;
215 }
216 
222 void
223 neural_layer_noise_print(const struct Layer *l, const bool print_weights)
224 {
225  char *json_str = neural_layer_noise_json_export(l, print_weights);
226  printf("%s\n", json_str);
227  free(json_str);
228 }
229 
237 char *
238 neural_layer_noise_json_export(const struct Layer *l, const bool return_weights)
239 {
240  (void) return_weights;
241  cJSON *json = cJSON_CreateObject();
242  cJSON_AddStringToObject(json, "type", "noise");
243  cJSON_AddNumberToObject(json, "n_inputs", l->n_inputs);
244  cJSON_AddNumberToObject(json, "n_outputs", l->n_outputs);
245  cJSON_AddNumberToObject(json, "probability", l->probability);
246  cJSON_AddNumberToObject(json, "stdev", l->scale);
247  char *string = cJSON_Print(json);
248  cJSON_Delete(json);
249  return string;
250 }
251 
258 size_t
259 neural_layer_noise_save(const struct Layer *l, FILE *fp)
260 {
261  size_t s = 0;
262  s += fwrite(&l->n_inputs, sizeof(int), 1, fp);
263  s += fwrite(&l->n_outputs, sizeof(int), 1, fp);
264  s += fwrite(&l->max_outputs, sizeof(int), 1, fp);
265  s += fwrite(&l->probability, sizeof(double), 1, fp);
266  s += fwrite(&l->scale, sizeof(double), 1, fp);
267  s += fwrite(&l->out_w, sizeof(int), 1, fp);
268  s += fwrite(&l->out_h, sizeof(int), 1, fp);
269  s += fwrite(&l->out_c, sizeof(int), 1, fp);
270  return s;
271 }
272 
279 size_t
280 neural_layer_noise_load(struct Layer *l, FILE *fp)
281 {
282  size_t s = 0;
283  s += fread(&l->n_inputs, sizeof(int), 1, fp);
284  s += fread(&l->n_outputs, sizeof(int), 1, fp);
285  s += fread(&l->max_outputs, sizeof(int), 1, fp);
286  s += fread(&l->probability, sizeof(double), 1, fp);
287  s += fread(&l->scale, sizeof(double), 1, fp);
288  s += fread(&l->out_w, sizeof(int), 1, fp);
289  s += fread(&l->out_h, sizeof(int), 1, fp);
290  s += fread(&l->out_c, sizeof(int), 1, fp);
292  return s;
293 }
Neural network activation functions.
void layer_defaults(struct Layer *l)
Initialises a layer to default values.
Definition: neural_layer.c:413
void layer_guard_outputs(const struct Layer *l)
Check number of outputs is within bounds.
Definition: neural_layer.c:595
#define NOISE
Layer type noise.
Definition: neural_layer.h:31
void neural_layer_noise_free(const struct Layer *l)
Free memory used by a noise layer.
void neural_layer_noise_rand(struct Layer *l)
Dummy function since noise layers have no weights.
void neural_layer_noise_print(const struct Layer *l, const bool print_weights)
Prints a noise layer.
void neural_layer_noise_resize(struct Layer *l, const struct Layer *prev)
Resizes a noise layer if the previous layer has changed size.
void neural_layer_noise_update(const struct Layer *l)
Dummy function since a noise layer has no weights.
void neural_layer_noise_init(struct Layer *l, const struct ArgsLayer *args)
Initialises a noise layer.
size_t neural_layer_noise_save(const struct Layer *l, FILE *fp)
Writes a noise layer to a file.
static void free_layer_arrays(const struct Layer *l)
Free memory used by a noise layer.
static void malloc_layer_arrays(struct Layer *l)
Allocate memory used by a noise layer.
size_t neural_layer_noise_load(struct Layer *l, FILE *fp)
Reads a noise layer from a file.
char * neural_layer_noise_json_export(const struct Layer *l, const bool return_weights)
Returns a json formatted string representation of a noise layer.
void neural_layer_noise_backward(const struct Layer *l, const struct Net *net, const double *input, double *delta)
Backward propagates a noise layer.
struct Layer * neural_layer_noise_copy(const struct Layer *src)
Initialises and creates a copy of one noise layer from another.
bool neural_layer_noise_mutate(struct Layer *l)
Dummy function since a noise layer cannot be mutated.
void neural_layer_noise_forward(const struct Layer *l, const struct Net *net, const double *input)
Forward propagates a noise layer.
double * neural_layer_noise_output(const struct Layer *l)
Returns the output from a noise layer.
An implementation of a Gaussian noise adding layer.
Parameters for initialising a neural network layer.
double probability
Usage depends on layer implementation.
int channels
Pool, Conv, and Upsample.
double scale
Usage depends on layer implementation.
int width
Pool, Conv, and Upsample.
int height
Pool, Conv, and Upsample.
int n_inputs
Number of inputs.
Neural network layer data structure.
Definition: neural_layer.h:73
double * output
Current neuron outputs (after activation function)
Definition: neural_layer.h:76
double * state
Current neuron states (before activation function)
Definition: neural_layer.h:75
int n_inputs
Number of layer inputs.
Definition: neural_layer.h:90
double scale
Usage depends on layer implementation.
Definition: neural_layer.h:98
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 probability
Usage depends on layer implementation.
Definition: neural_layer.h:99
double * i
LSTM.
Definition: neural_layer.h:117
int n_outputs
Number of layer outputs.
Definition: neural_layer.h:91
int out_w
Pool, Conv, and Upsample.
Definition: neural_layer.h:130
int type
Layer type: CONNECTED, DROPOUT, etc.
Definition: neural_layer.h:74
int out_c
Pool, Conv, and Upsample.
Definition: neural_layer.h:132
double * delta
Delta for updating weights.
Definition: neural_layer.h:83
int out_h
Pool, Conv, and Upsample.
Definition: neural_layer.h:131
Neural network data structure.
Definition: neural.h:48
bool train
Whether the network is in training mode.
Definition: neural.h:55
double rand_normal(const double mu, const double sigma)
Returns a random Gaussian with specified mean and standard deviation.
Definition: utils.c:87
double rand_uniform(const double min, const double max)
Returns a uniform random float [min,max].
Definition: utils.c:62
Utility functions for random number handling, etc.