Go to the documentation of this file.00001 #include "avgpool_layer.h"
00002 #include "cuda.h"
00003 #include <stdio.h>
00004
00005 avgpool_layer make_avgpool_layer(int batch, int w, int h, int c)
00006 {
00007 fprintf(stderr, "avg %4d x%4d x%4d -> %4d\n", w, h, c, c);
00008 avgpool_layer l = {0};
00009 l.type = AVGPOOL;
00010 l.batch = batch;
00011 l.h = h;
00012 l.w = w;
00013 l.c = c;
00014 l.out_w = 1;
00015 l.out_h = 1;
00016 l.out_c = c;
00017 l.outputs = l.out_c;
00018 l.inputs = h*w*c;
00019 int output_size = l.outputs * batch;
00020 l.output = calloc(output_size, sizeof(float));
00021 l.delta = calloc(output_size, sizeof(float));
00022 l.forward = forward_avgpool_layer;
00023 l.backward = backward_avgpool_layer;
00024 #ifdef GPU
00025 l.forward_gpu = forward_avgpool_layer_gpu;
00026 l.backward_gpu = backward_avgpool_layer_gpu;
00027 l.output_gpu = cuda_make_array(l.output, output_size);
00028 l.delta_gpu = cuda_make_array(l.delta, output_size);
00029 #endif
00030 return l;
00031 }
00032
00033 void resize_avgpool_layer(avgpool_layer *l, int w, int h)
00034 {
00035 l->w = w;
00036 l->h = h;
00037 l->inputs = h*w*l->c;
00038 }
00039
00040 void forward_avgpool_layer(const avgpool_layer l, network_state state)
00041 {
00042 int b,i,k;
00043
00044 for(b = 0; b < l.batch; ++b){
00045 for(k = 0; k < l.c; ++k){
00046 int out_index = k + b*l.c;
00047 l.output[out_index] = 0;
00048 for(i = 0; i < l.h*l.w; ++i){
00049 int in_index = i + l.h*l.w*(k + b*l.c);
00050 l.output[out_index] += state.input[in_index];
00051 }
00052 l.output[out_index] /= l.h*l.w;
00053 }
00054 }
00055 }
00056
00057 void backward_avgpool_layer(const avgpool_layer l, network_state state)
00058 {
00059 int b,i,k;
00060
00061 for(b = 0; b < l.batch; ++b){
00062 for(k = 0; k < l.c; ++k){
00063 int out_index = k + b*l.c;
00064 for(i = 0; i < l.h*l.w; ++i){
00065 int in_index = i + l.h*l.w*(k + b*l.c);
00066 state.delta[in_index] += l.delta[out_index] / (l.h*l.w);
00067 }
00068 }
00069 }
00070 }
00071