Skip to content

Commit

Permalink
Merge pull request BVLC#839 from jeffdonahue/test-dropout-ratio
Browse files Browse the repository at this point in the history
Test that DropoutLayer obeys dropout_ratio
  • Loading branch information
jeffdonahue committed Aug 2, 2014
2 parents 829aa56 + b767859 commit 7d658e2
Showing 1 changed file with 41 additions and 16 deletions.
57 changes: 41 additions & 16 deletions src/caffe/test/test_neuron_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace caffe {
template <typename TypeParam>
class NeuronLayerTest : public MultiDeviceTest<TypeParam> {
typedef typename TypeParam::Dtype Dtype;

protected:
NeuronLayerTest()
: blob_bottom_(new Blob<Dtype>(2, 3, 4, 5)),
Expand All @@ -34,6 +35,38 @@ class NeuronLayerTest : public MultiDeviceTest<TypeParam> {
Blob<Dtype>* const blob_top_;
vector<Blob<Dtype>*> blob_bottom_vec_;
vector<Blob<Dtype>*> blob_top_vec_;

void TestDropoutForward(const float dropout_ratio) {
LayerParameter layer_param;
// Fill in the given dropout_ratio, unless it's 0.5, in which case we don't
// set it explicitly to test that 0.5 is the default.
if (dropout_ratio != 0.5) {
layer_param.mutable_dropout_param()->set_dropout_ratio(dropout_ratio);
}
Caffe::set_phase(Caffe::TRAIN);
DropoutLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_));
layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
// Now, check values
const Dtype* bottom_data = this->blob_bottom_->cpu_data();
const Dtype* top_data = this->blob_top_->cpu_data();
float scale = 1. / (1. - layer_param.dropout_param().dropout_ratio());
const int count = this->blob_bottom_->count();
// Initialize num_kept to count the number of inputs NOT dropped out.
int num_kept = 0;
for (int i = 0; i < count; ++i) {
if (top_data[i] != 0) {
++num_kept;
EXPECT_EQ(top_data[i], bottom_data[i] * scale);
}
}
const Dtype std_error = sqrt(dropout_ratio * (1 - dropout_ratio) / count);
// Fail if the number dropped was more than 1.96 * std_error away from the
// expected number -- requires 95% confidence that the dropout layer is not
// obeying the given dropout_ratio for test failure.
const Dtype empirical_dropout_ratio = 1 - num_kept / Dtype(count);
EXPECT_NEAR(empirical_dropout_ratio, dropout_ratio, 1.96 * std_error);
}
};

TYPED_TEST_CASE(NeuronLayerTest, TestDtypesAndDevices);
Expand Down Expand Up @@ -114,22 +147,14 @@ TYPED_TEST(NeuronLayerTest, TestSigmoidGradient) {
&(this->blob_top_vec_));
}

TYPED_TEST(NeuronLayerTest, TestDropout) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
Caffe::set_phase(Caffe::TRAIN);
DropoutLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_));
layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
// Now, check values
const Dtype* bottom_data = this->blob_bottom_->cpu_data();
const Dtype* top_data = this->blob_top_->cpu_data();
float scale = 1. / (1. - layer_param.dropout_param().dropout_ratio());
for (int i = 0; i < this->blob_bottom_->count(); ++i) {
if (top_data[i] != 0) {
EXPECT_EQ(top_data[i], bottom_data[i] * scale);
}
}
TYPED_TEST(NeuronLayerTest, TestDropoutHalf) {
const float kDropoutRatio = 0.5;
this->TestDropoutForward(kDropoutRatio);
}

TYPED_TEST(NeuronLayerTest, TestDropoutThreeQuarters) {
const float kDropoutRatio = 0.75;
this->TestDropoutForward(kDropoutRatio);
}

TYPED_TEST(NeuronLayerTest, TestDropoutTestPhase) {
Expand Down

0 comments on commit 7d658e2

Please sign in to comment.