00001 #include "shortcut_layer.h" 00002 #include "cuda.h" 00003 #include "blas.h" 00004 #include <stdio.h> 00005 #include <assert.h> 00006 00007 layer make_shortcut_layer(int batch, int index, int w, int h, int c, int w2, int h2, int c2) 00008 { 00009 fprintf(stderr,"Shortcut Layer: %d\n", index); 00010 layer l = {0}; 00011 l.type = SHORTCUT; 00012 l.batch = batch; 00013 l.w = w2; 00014 l.h = h2; 00015 l.c = c2; 00016 l.out_w = w; 00017 l.out_h = h; 00018 l.out_c = c; 00019 l.outputs = w*h*c; 00020 l.inputs = l.outputs; 00021 00022 l.index = index; 00023 00024 l.delta = calloc(l.outputs*batch, sizeof(float)); 00025 l.output = calloc(l.outputs*batch, sizeof(float));; 00026 00027 l.forward = forward_shortcut_layer; 00028 l.backward = backward_shortcut_layer; 00029 #ifdef GPU 00030 l.forward_gpu = forward_shortcut_layer_gpu; 00031 l.backward_gpu = backward_shortcut_layer_gpu; 00032 00033 l.delta_gpu = cuda_make_array(l.delta, l.outputs*batch); 00034 l.output_gpu = cuda_make_array(l.output, l.outputs*batch); 00035 #endif 00036 return l; 00037 } 00038 00039 void forward_shortcut_layer(const layer l, network_state state) 00040 { 00041 copy_cpu(l.outputs*l.batch, state.input, 1, l.output, 1); 00042 shortcut_cpu(l.batch, l.w, l.h, l.c, state.net.layers[l.index].output, l.out_w, l.out_h, l.out_c, l.output); 00043 activate_array(l.output, l.outputs*l.batch, l.activation); 00044 } 00045 00046 void backward_shortcut_layer(const layer l, network_state state) 00047 { 00048 gradient_array(l.output, l.outputs*l.batch, l.activation, l.delta); 00049 axpy_cpu(l.outputs*l.batch, 1, l.delta, 1, state.delta, 1); 00050 shortcut_cpu(l.batch, l.out_w, l.out_h, l.out_c, l.delta, l.w, l.h, l.c, state.net.layers[l.index].delta); 00051 } 00052 00053 #ifdef GPU 00054 void forward_shortcut_layer_gpu(const layer l, network_state state) 00055 { 00056 copy_ongpu(l.outputs*l.batch, state.input, 1, l.output_gpu, 1); 00057 shortcut_gpu(l.batch, l.w, l.h, l.c, state.net.layers[l.index].output_gpu, l.out_w, l.out_h, l.out_c, l.output_gpu); 00058 activate_array_ongpu(l.output_gpu, l.outputs*l.batch, l.activation); 00059 } 00060 00061 void backward_shortcut_layer_gpu(const layer l, network_state state) 00062 { 00063 gradient_array_ongpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu); 00064 axpy_ongpu(l.outputs*l.batch, 1, l.delta_gpu, 1, state.delta, 1); 00065 shortcut_gpu(l.batch, l.out_w, l.out_h, l.out_c, l.delta_gpu, l.w, l.h, l.c, state.net.layers[l.index].delta_gpu); 00066 } 00067 #endif