From: Jeff Donahue Date: Wed, 29 Jan 2014 21:03:42 +0000 (-0800) Subject: add bernoulli rng test to demonstrate bug (generates all 0s unless p == X-Git-Tag: submit/tizen/20180823.020014~692^2~83^2~12 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6cbf9f189b9318b264c4cfe73bd1412eba4646f2;p=platform%2Fupstream%2Fcaffeonacl.git add bernoulli rng test to demonstrate bug (generates all 0s unless p == 1) --- diff --git a/src/caffe/test/test_random_number_generator.cpp b/src/caffe/test/test_random_number_generator.cpp index 26c9f2e..c43a5d9 100644 --- a/src/caffe/test/test_random_number_generator.cpp +++ b/src/caffe/test/test_random_number_generator.cpp @@ -24,6 +24,15 @@ class RandomNumberGeneratorTest : public ::testing::Test { return sum / sample_size; } + Dtype sample_mean(const int* const seqs, const size_t sample_size) + { + Dtype sum = 0; + for (int i = 0; i < sample_size; ++i) { + sum += Dtype(seqs[i]); + } + return sum / sample_size; + } + Dtype mean_bound(const Dtype std, const size_t sample_size) { return std/sqrt((double)sample_size); @@ -40,28 +49,47 @@ TYPED_TEST(RandomNumberGeneratorTest, TestRngGaussian) { Caffe::set_random_seed(1701); TypeParam mu = 0; TypeParam sigma = 1; - caffe_vRngGaussian(sample_size, (TypeParam*)data_a.mutable_cpu_data(), mu, sigma); + caffe_vRngGaussian(sample_size, + (TypeParam*)data_a.mutable_cpu_data(), mu, sigma); TypeParam true_mean = mu; TypeParam true_std = sigma; TypeParam bound = this->mean_bound(true_std, sample_size); - TypeParam real_mean = this->sample_mean((TypeParam*)data_a.cpu_data(), sample_size); - EXPECT_NEAR(real_mean, true_mean, bound); + TypeParam empirical_mean = + this->sample_mean((TypeParam*)data_a.cpu_data(), sample_size); + EXPECT_NEAR(empirical_mean, true_mean, bound); } + TYPED_TEST(RandomNumberGeneratorTest, TestRngUniform) { size_t sample_size = 10000; SyncedMemory data_a(sample_size * sizeof(TypeParam)); Caffe::set_random_seed(1701); TypeParam lower = 0; TypeParam upper = 1; - caffe_vRngUniform(sample_size, (TypeParam*)data_a.mutable_cpu_data(), lower, upper); + caffe_vRngUniform(sample_size, + (TypeParam*)data_a.mutable_cpu_data(), lower, upper); TypeParam true_mean = (lower + upper) / 2; TypeParam true_std = (upper - lower) / sqrt(12); TypeParam bound = this->mean_bound(true_std, sample_size); - TypeParam real_mean = this->sample_mean((TypeParam*)data_a.cpu_data(), sample_size); - EXPECT_NEAR(real_mean, true_mean, bound); + TypeParam empirical_mean = + this->sample_mean((TypeParam*)data_a.cpu_data(), sample_size); + EXPECT_NEAR(empirical_mean, true_mean, bound); } +TYPED_TEST(RandomNumberGeneratorTest, TestRngBernoulli) { + size_t sample_size = 10000; + SyncedMemory data_a(sample_size * sizeof(int)); + Caffe::set_random_seed(1701); + double p = 0.3; + caffe_vRngBernoulli(sample_size, (int*)data_a.mutable_cpu_data(), p); + TypeParam true_mean = p; + TypeParam true_std = sqrt(p * (1 - p)); + TypeParam bound = this->mean_bound(true_std, sample_size); + TypeParam empirical_mean = + this->sample_mean((const int *)data_a.cpu_data(), sample_size); + EXPECT_NEAR(empirical_mean, true_mean, bound); +} + } // namespace caffe