XCSF  1.4.7
XCSF learning classifier system
neural_layer_avgpool.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_avgpool.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 }
39 
44 static void
46 {
48  l->output = realloc(l->output, sizeof(double) * l->n_outputs);
49  l->delta = realloc(l->delta, sizeof(double) * l->n_outputs);
50 }
51 
56 void
58 {
59  free(l->output);
60  free(l->delta);
61 }
62 
68 void
69 neural_layer_avgpool_init(struct Layer *l, const struct ArgsLayer *args)
70 {
71  l->height = args->height;
72  l->width = args->width;
73  l->channels = args->channels;
74  l->out_w = 1;
75  l->out_h = 1;
76  l->out_c = l->channels;
77  l->n_outputs = l->out_c;
78  l->max_outputs = l->n_outputs;
79  l->n_inputs = l->height * l->width * l->channels;
81 }
82 
88 struct Layer *
89 neural_layer_avgpool_copy(const struct Layer *src)
90 {
91  if (src->type != AVGPOOL) {
92  printf("neural_layer_avgpool_copy(): incorrect source layer type\n");
93  exit(EXIT_FAILURE);
94  }
95  struct Layer *l = malloc(sizeof(struct Layer));
96  layer_defaults(l);
97  l->type = src->type;
98  l->layer_vptr = src->layer_vptr;
99  l->height = src->height;
100  l->width = src->width;
101  l->channels = src->channels;
102  l->out_w = src->out_w;
103  l->out_h = src->out_h;
104  l->out_c = src->out_c;
105  l->n_outputs = src->n_outputs;
106  l->max_outputs = src->max_outputs;
107  l->n_inputs = src->n_inputs;
109  return l;
110 }
111 
116 void
118 {
119  (void) l;
120 }
121 
128 void
129 neural_layer_avgpool_forward(const struct Layer *l, const struct Net *net,
130  const double *input)
131 {
132  (void) net;
133  const int n = l->height * l->width;
134  for (int k = 0; k < l->channels; ++k) {
135  l->output[k] = 0;
136  for (int i = 0; i < n; ++i) {
137  l->output[k] += input[i + n * k];
138  }
139  l->output[k] /= n;
140  }
141 }
142 
150 void
151 neural_layer_avgpool_backward(const struct Layer *l, const struct Net *net,
152  const double *input, double *delta)
153 {
154  (void) net;
155  (void) input;
156  if (delta) {
157  const int n = l->height * l->width;
158  for (int k = 0; k < l->channels; ++k) {
159  for (int i = 0; i < n; ++i) {
160  delta[i + n * k] += l->delta[k] / n;
161  }
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_avgpool_resize(struct Layer *l, const struct Layer *prev)
195 {
196  const int h = prev->out_h;
197  const int w = prev->out_w;
198  const int c = prev->out_c;
199  l->height = h;
200  l->width = w;
201  l->channels = c;
202  l->out_c = c;
203  l->n_outputs = l->out_c;
204  l->max_outputs = l->n_outputs;
205  l->n_inputs = h * w * c;
207 }
208 
214 double *
216 {
217  return l->output;
218 }
219 
225 void
226 neural_layer_avgpool_print(const struct Layer *l, const bool print_weights)
227 {
228  char *json_str = neural_layer_avgpool_json_export(l, print_weights);
229  printf("%s\n", json_str);
230  free(json_str);
231 }
232 
240 char *
242  const bool return_weights)
243 {
244  (void) return_weights;
245  cJSON *json = cJSON_CreateObject();
246  cJSON_AddStringToObject(json, "type", "avgpool");
247  cJSON_AddNumberToObject(json, "n_inputs", l->n_inputs);
248  cJSON_AddNumberToObject(json, "n_outputs", l->n_outputs);
249  cJSON_AddNumberToObject(json, "height", l->height);
250  cJSON_AddNumberToObject(json, "width", l->width);
251  cJSON_AddNumberToObject(json, "channels", l->channels);
252  char *string = cJSON_Print(json);
253  cJSON_Delete(json);
254  return string;
255 }
256 
263 size_t
264 neural_layer_avgpool_save(const struct Layer *l, FILE *fp)
265 {
266  size_t s = 0;
267  s += fwrite(&l->height, sizeof(int), 1, fp);
268  s += fwrite(&l->width, sizeof(int), 1, fp);
269  s += fwrite(&l->channels, sizeof(int), 1, fp);
270  s += fwrite(&l->out_w, sizeof(int), 1, fp);
271  s += fwrite(&l->out_h, sizeof(int), 1, fp);
272  s += fwrite(&l->out_c, sizeof(int), 1, fp);
273  s += fwrite(&l->n_outputs, sizeof(int), 1, fp);
274  s += fwrite(&l->max_outputs, sizeof(int), 1, fp);
275  s += fwrite(&l->n_inputs, sizeof(int), 1, fp);
276  return s;
277 }
278 
285 size_t
286 neural_layer_avgpool_load(struct Layer *l, FILE *fp)
287 {
288  size_t s = 0;
289  s += fread(&l->height, sizeof(int), 1, fp);
290  s += fread(&l->width, sizeof(int), 1, fp);
291  s += fread(&l->channels, sizeof(int), 1, fp);
292  s += fread(&l->out_w, sizeof(int), 1, fp);
293  s += fread(&l->out_h, sizeof(int), 1, fp);
294  s += fread(&l->out_c, sizeof(int), 1, fp);
295  s += fread(&l->n_outputs, sizeof(int), 1, fp);
296  s += fread(&l->max_outputs, sizeof(int), 1, fp);
297  s += fread(&l->n_inputs, sizeof(int), 1, fp);
299  return s;
300 }
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 AVGPOOL
Layer type average pooling.
Definition: neural_layer.h:37
void neural_layer_avgpool_rand(struct Layer *l)
Dummy function since average pooling layers have no weights.
void neural_layer_avgpool_update(const struct Layer *l)
Dummy function since average pooling layers have no weights.
void neural_layer_avgpool_backward(const struct Layer *l, const struct Net *net, const double *input, double *delta)
Backward propagates an average pooling layer.
void neural_layer_avgpool_free(const struct Layer *l)
Free memory used by an average pooling layer.
void neural_layer_avgpool_init(struct Layer *l, const struct ArgsLayer *args)
Initialises an average pooling layer.
bool neural_layer_avgpool_mutate(struct Layer *l)
Dummy function since average pooling layers cannot be mutated.
static void malloc_layer_arrays(struct Layer *l)
Allocate memory used by an average pooling layer.
static void realloc_layer_arrays(struct Layer *l)
Resize memory used by an average pooling layer.
void neural_layer_avgpool_forward(const struct Layer *l, const struct Net *net, const double *input)
Forward propagates an average pooling layer.
size_t neural_layer_avgpool_load(struct Layer *l, FILE *fp)
Reads an average pooling layer from a file.
struct Layer * neural_layer_avgpool_copy(const struct Layer *src)
Initialises and copies one average pooling layer from another.
char * neural_layer_avgpool_json_export(const struct Layer *l, const bool return_weights)
Returns a json formatted string of an average pool layer.
size_t neural_layer_avgpool_save(const struct Layer *l, FILE *fp)
Writes an average pooling layer to a file.
void neural_layer_avgpool_print(const struct Layer *l, const bool print_weights)
Prints an average pooling layer.
void neural_layer_avgpool_resize(struct Layer *l, const struct Layer *prev)
Resizes an avg pooling layer if the previous layer has changed size.
double * neural_layer_avgpool_output(const struct Layer *l)
Returns the output from an average pooling layer.
An implementation of an average pooling layer.
Parameters for initialising a neural network layer.
int channels
Pool, Conv, and Upsample.
int width
Pool, Conv, and Upsample.
int height
Pool, Conv, and Upsample.
Neural network layer data structure.
Definition: neural_layer.h:73
double * output
Current neuron outputs (after activation function)
Definition: neural_layer.h:76
int n_inputs
Number of layer inputs.
Definition: neural_layer.h:90
int channels
Pool, Conv, and Upsample.
Definition: neural_layer.h:128
double * c
LSTM.
Definition: neural_layer.h:120
int height
Pool, Conv, and Upsample.
Definition: neural_layer.h:126
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
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
Utility functions for random number handling, etc.