87 printf(
"neural_layer_maxpool_copy(): incorrect source layer type\n");
90 struct Layer *l = malloc(
sizeof(
struct Layer));
146 const int w_offset = -l->
pad / 2;
147 const int h_offset = w_offset;
148 double max = -DBL_MAX;
150 for (
int n = 0; n < l->
size; ++n) {
151 for (
int m = 0; m < l->
size; ++m) {
152 const int cur_h = h_offset +
i * l->
stride + n;
153 const int cur_w = w_offset + j * l->
stride + m;
154 const int index = cur_w + l->
width * (cur_h + l->
height * k);
155 if (cur_h >= 0 && cur_h < l->
height && cur_w >= 0 &&
156 cur_w < l->
width && index < l->
n_inputs && input[index] > max) {
162 if (max_index < 0 || max_index >= l->
n_inputs) {
163 printf(
"max_pool() error: invalid max_index: (%d)\n", max_index);
181 for (
int k = 0; k < l->
channels; ++k) {
182 for (
int i = 0;
i < l->
out_h; ++
i) {
183 for (
int j = 0; j < l->
out_w; ++j) {
184 const int out_index = j + l->
out_w * (
i + l->
out_h * k);
186 const int max_index =
max_pool(l, input,
i, j, k);
187 l->
indexes[out_index] = max_index;
188 l->
output[out_index] = input[max_index];
204 const double *input,
double *
delta)
245 const int w = prev->
out_w;
246 const int h = prev->
out_h;
247 const int c = prev->
out_c;
280 printf(
"%s\n", json_str);
293 const bool return_weights)
295 (void) return_weights;
296 cJSON *json = cJSON_CreateObject();
297 cJSON_AddStringToObject(json,
"type",
"maxpool");
298 cJSON_AddNumberToObject(json,
"n_inputs", l->
n_inputs);
299 cJSON_AddNumberToObject(json,
"n_outputs", l->
n_outputs);
300 cJSON_AddNumberToObject(json,
"height", l->
height);
301 cJSON_AddNumberToObject(json,
"width", l->
width);
302 cJSON_AddNumberToObject(json,
"channels", l->
channels);
303 cJSON_AddNumberToObject(json,
"size", l->
size);
304 cJSON_AddNumberToObject(json,
"stride", l->
stride);
305 cJSON_AddNumberToObject(json,
"pad", l->
pad);
306 cJSON_AddNumberToObject(json,
"out_w", l->
out_w);
307 cJSON_AddNumberToObject(json,
"out_h", l->
out_h);
308 cJSON_AddNumberToObject(json,
"out_c", l->
out_c);
309 char *
string = cJSON_Print(json);
324 s += fwrite(&l->
height,
sizeof(
int), 1, fp);
325 s += fwrite(&l->
width,
sizeof(
int), 1, fp);
326 s += fwrite(&l->
channels,
sizeof(
int), 1, fp);
327 s += fwrite(&l->
pad,
sizeof(
int), 1, fp);
328 s += fwrite(&l->
out_w,
sizeof(
int), 1, fp);
329 s += fwrite(&l->
out_h,
sizeof(
int), 1, fp);
330 s += fwrite(&l->
out_c,
sizeof(
int), 1, fp);
331 s += fwrite(&l->
n_outputs,
sizeof(
int), 1, fp);
333 s += fwrite(&l->
n_inputs,
sizeof(
int), 1, fp);
334 s += fwrite(&l->
size,
sizeof(
int), 1, fp);
335 s += fwrite(&l->
stride,
sizeof(
int), 1, fp);
349 s += fread(&l->
height,
sizeof(
int), 1, fp);
350 s += fread(&l->
width,
sizeof(
int), 1, fp);
351 s += fread(&l->
channels,
sizeof(
int), 1, fp);
352 s += fread(&l->
pad,
sizeof(
int), 1, fp);
353 s += fread(&l->
out_w,
sizeof(
int), 1, fp);
354 s += fread(&l->
out_h,
sizeof(
int), 1, fp);
355 s += fread(&l->
out_c,
sizeof(
int), 1, fp);
356 s += fread(&l->
n_outputs,
sizeof(
int), 1, fp);
358 s += fread(&l->
n_inputs,
sizeof(
int), 1, fp);
359 s += fread(&l->
size,
sizeof(
int), 1, fp);
360 s += fread(&l->
stride,
sizeof(
int), 1, fp);
Neural network activation functions.
void layer_defaults(struct Layer *l)
Initialises a layer to default values.
void layer_guard_outputs(const struct Layer *l)
Check number of outputs is within bounds.
#define MAXPOOL
Layer type maxpooling.
static void layer_print(const struct Layer *l, const bool print_weights)
Prints the layer.
static int max_pool(const struct Layer *l, const double *input, const int i, const int j, const int k)
Returns the index of the maximum input value corresponding to a specified output height,...
void neural_layer_maxpool_print(const struct Layer *l, const bool print_weights)
Prints a maxpooling layer.
void neural_layer_maxpool_init(struct Layer *l, const struct ArgsLayer *args)
Initialises a 2D maxpooling layer.
void neural_layer_maxpool_rand(struct Layer *l)
Dummy function since maxpooling layers have no weights.
void neural_layer_maxpool_resize(struct Layer *l, const struct Layer *prev)
Resizes a maxpooling layer if the previous layer has changed size.
void neural_layer_maxpool_update(const struct Layer *l)
Dummy function since a maxpooling layer has no weights.
void neural_layer_maxpool_free(const struct Layer *l)
Free memory used by a maxpooling layer.
struct Layer * neural_layer_maxpool_copy(const struct Layer *src)
Initialises and creates a copy of one maxpooling layer from another.
bool neural_layer_maxpool_mutate(struct Layer *l)
Dummy function since a maxpooling layer cannot be mutated.
double * neural_layer_maxpool_output(const struct Layer *l)
Returns the output from a maxpooling layer.
static void malloc_layer_arrays(struct Layer *l)
Allocate memory used by a maxpooling layer.
static void realloc_layer_arrays(struct Layer *l)
Resize memory used by a maxpooling layer.
char * neural_layer_maxpool_json_export(const struct Layer *l, const bool return_weights)
Returns a json formatted string representation of a maxpooling layer.
size_t neural_layer_maxpool_save(const struct Layer *l, FILE *fp)
Writes a maxpooling layer to a file.
void neural_layer_maxpool_forward(const struct Layer *l, const struct Net *net, const double *input)
Forward propagates a maxpooling layer.
size_t neural_layer_maxpool_load(struct Layer *l, FILE *fp)
Reads a maxpooling layer from a file.
void neural_layer_maxpool_backward(const struct Layer *l, const struct Net *net, const double *input, double *delta)
Backward propagates a maxpooling layer.
An implementation of a 2D maxpooling 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.
int stride
Pool, Conv, and Upsample.
Neural network layer data structure.
double * output
Current neuron outputs (after activation function)
int stride
Pool, Conv, and Upsample.
int n_inputs
Number of layer inputs.
int channels
Pool, Conv, and Upsample.
int height
Pool, Conv, and Upsample.
struct LayerVtbl const * layer_vptr
Functions acting on layers.
int max_outputs
Maximum number of neurons in the layer.
int width
Pool, Conv, and Upsample.
int n_outputs
Number of layer outputs.
int out_w
Pool, Conv, and Upsample.
int type
Layer type: CONNECTED, DROPOUT, etc.
int out_c
Pool, Conv, and Upsample.
double * delta
Delta for updating weights.
int out_h
Pool, Conv, and Upsample.
Neural network data structure.
Utility functions for random number handling, etc.