Go to the documentation of this file.00001 #include "dropout_layer.h"
00002 #include "utils.h"
00003 #include "cuda.h"
00004 #include <stdlib.h>
00005 #include <stdio.h>
00006
00007 dropout_layer make_dropout_layer(int batch, int inputs, float probability)
00008 {
00009 dropout_layer l = {0};
00010 l.type = DROPOUT;
00011 l.probability = probability;
00012 l.inputs = inputs;
00013 l.outputs = inputs;
00014 l.batch = batch;
00015 l.rand = calloc(inputs*batch, sizeof(float));
00016 l.scale = 1./(1.-probability);
00017 l.forward = forward_dropout_layer;
00018 l.backward = backward_dropout_layer;
00019 #ifdef GPU
00020 l.forward_gpu = forward_dropout_layer_gpu;
00021 l.backward_gpu = backward_dropout_layer_gpu;
00022 l.rand_gpu = cuda_make_array(l.rand, inputs*batch);
00023 #endif
00024 fprintf(stderr, "dropout p = %.2f %4d -> %4d\n", probability, inputs, inputs);
00025 return l;
00026 }
00027
00028 void resize_dropout_layer(dropout_layer *l, int inputs)
00029 {
00030 l->rand = realloc(l->rand, l->inputs*l->batch*sizeof(float));
00031 #ifdef GPU
00032 cuda_free(l->rand_gpu);
00033
00034 l->rand_gpu = cuda_make_array(l->rand, inputs*l->batch);
00035 #endif
00036 }
00037
00038 void forward_dropout_layer(dropout_layer l, network_state state)
00039 {
00040 int i;
00041 if (!state.train) return;
00042 for(i = 0; i < l.batch * l.inputs; ++i){
00043 float r = rand_uniform(0, 1);
00044 l.rand[i] = r;
00045 if(r < l.probability) state.input[i] = 0;
00046 else state.input[i] *= l.scale;
00047 }
00048 }
00049
00050 void backward_dropout_layer(dropout_layer l, network_state state)
00051 {
00052 int i;
00053 if(!state.delta) return;
00054 for(i = 0; i < l.batch * l.inputs; ++i){
00055 float r = l.rand[i];
00056 if(r < l.probability) state.delta[i] = 0;
00057 else state.delta[i] *= l.scale;
00058 }
00059 }
00060