92 printf(
"neural_layer_avgpool_copy(): incorrect source layer type\n");
95 struct Layer *l = malloc(
sizeof(
struct Layer));
134 for (
int k = 0; k < l->
channels; ++k) {
136 for (
int i = 0;
i < n; ++
i) {
137 l->
output[k] += input[
i + n * k];
152 const double *input,
double *
delta)
158 for (
int k = 0; k < l->
channels; ++k) {
159 for (
int i = 0;
i < n; ++
i) {
196 const int h = prev->
out_h;
197 const int w = prev->
out_w;
198 const int c = prev->
out_c;
229 printf(
"%s\n", json_str);
242 const bool return_weights)
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);
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);
275 s += fwrite(&l->
n_inputs,
sizeof(
int), 1, fp);
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);
297 s += fread(&l->
n_inputs,
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 AVGPOOL
Layer type average pooling.
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.
double * output
Current neuron outputs (after activation function)
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.