You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.6 KiB
67 lines
1.6 KiB
#include "dropout_layer.h"
|
|
#include "utils.h"
|
|
#include "dn_cuda.h"
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
namespace DN{
|
|
|
|
dropout_layer make_dropout_layer(int batch, int inputs, float probability)
|
|
{
|
|
dropout_layer l;
|
|
memset(&l, 0, sizeof(dropout_layer));
|
|
l.type = DROPOUT;
|
|
l.probability = probability;
|
|
l.inputs = inputs;
|
|
l.outputs = inputs;
|
|
l.batch = batch;
|
|
l.rand = (float*)calloc(inputs*batch, sizeof(float));
|
|
l.scale = (float)(1. / (1. - probability));
|
|
l.forward = forward_dropout_layer;
|
|
l.backward = backward_dropout_layer;
|
|
#ifdef GPU
|
|
l.forward_gpu = forward_dropout_layer_gpu;
|
|
l.backward_gpu = backward_dropout_layer_gpu;
|
|
l.rand_gpu = cuda_make_array(l.rand, inputs*batch);
|
|
#endif
|
|
fprintf(stderr, "dropout p = %.2f %4d -> %4d\n", probability, inputs, inputs);
|
|
return l;
|
|
}
|
|
|
|
void resize_dropout_layer(dropout_layer *l, int inputs)
|
|
{
|
|
l->rand = (float*)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 net)
|
|
{
|
|
int i;
|
|
if (!net.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) net.input[i] = 0;
|
|
else net.input[i] *= l.scale;
|
|
}
|
|
}
|
|
|
|
void backward_dropout_layer(dropout_layer l, network net)
|
|
{
|
|
int i;
|
|
if (!net.delta) return;
|
|
for (i = 0; i < l.batch * l.inputs; ++i){
|
|
float r = l.rand[i];
|
|
if (r < l.probability) net.delta[i] = 0;
|
|
else net.delta[i] *= l.scale;
|
|
}
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|