| | |
| | | #include "dropout_layer.h" |
| | | #include "stdlib.h" |
| | | #include "stdio.h" |
| | | #include "params.h" |
| | | #include "utils.h" |
| | | #include "cuda.h" |
| | | #include <stdlib.h> |
| | | #include <stdio.h> |
| | | |
| | | dropout_layer *make_dropout_layer(int batch, int inputs, float probability) |
| | | dropout_layer make_dropout_layer(int batch, int inputs, float probability) |
| | | { |
| | | fprintf(stderr, "Dropout Layer: %d inputs, %f probability\n", inputs, probability); |
| | | dropout_layer *layer = calloc(1, sizeof(dropout_layer)); |
| | | layer->probability = probability; |
| | | layer->inputs = inputs; |
| | | layer->batch = batch; |
| | | return layer; |
| | | dropout_layer l = {0}; |
| | | l.type = DROPOUT; |
| | | l.probability = probability; |
| | | l.inputs = inputs; |
| | | l.outputs = inputs; |
| | | l.batch = batch; |
| | | l.rand = calloc(inputs*batch, sizeof(float)); |
| | | l.scale = 1./(1.-probability); |
| | | #ifdef GPU |
| | | l.rand_gpu = cuda_make_array(l.rand, inputs*batch); |
| | | #endif |
| | | return l; |
| | | } |
| | | |
| | | void forward_dropout_layer(dropout_layer layer, float *input) |
| | | void resize_dropout_layer(dropout_layer *l, int inputs) |
| | | { |
| | | l->rand = realloc(l->rand, l->inputs*l->batch*sizeof(float)); |
| | | #ifdef GPU |
| | | cuda_free(l->rand_gpu); |
| | | |
| | | l->rand_gpu = cuda_make_array(l->rand, inputs*l->batch); |
| | | #endif |
| | | } |
| | | |
| | | void forward_dropout_layer(dropout_layer l, network_state state) |
| | | { |
| | | int i; |
| | | for(i = 0; i < layer.batch * layer.inputs; ++i){ |
| | | if((float)rand()/RAND_MAX < layer.probability) input[i] = 0; |
| | | else input[i] /= (1-layer.probability); |
| | | if (!state.train) return; |
| | | for(i = 0; i < l.batch * l.inputs; ++i){ |
| | | float r = rand_uniform(0, 1); |
| | | l.rand[i] = r; |
| | | if(r < l.probability) state.input[i] = 0; |
| | | else state.input[i] *= l.scale; |
| | | } |
| | | } |
| | | void backward_dropout_layer(dropout_layer layer, float *input, float *delta) |
| | | |
| | | void backward_dropout_layer(dropout_layer l, network_state state) |
| | | { |
| | | // Don't do shit LULZ |
| | | int i; |
| | | if(!state.delta) return; |
| | | for(i = 0; i < l.batch * l.inputs; ++i){ |
| | | float r = l.rand[i]; |
| | | if(r < l.probability) state.delta[i] = 0; |
| | | else state.delta[i] *= l.scale; |
| | | } |
| | | } |
| | | |