-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed uniform distribution upper bound to be inclusive
- Loading branch information
Showing
4 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <cmath> | ||
#include <cstring> | ||
#include <cuda_runtime.h> | ||
|
||
#include "gtest/gtest.h" | ||
#include "caffe/common.hpp" | ||
#include "caffe/syncedmem.hpp" | ||
#include "caffe/util/math_functions.hpp" | ||
#include "caffe/test/test_caffe_main.hpp" | ||
|
||
namespace caffe { | ||
|
||
template <typename Dtype> | ||
class RandomNumberGeneratorTest : public ::testing::Test { | ||
public: | ||
virtual ~RandomNumberGeneratorTest() {} | ||
|
||
Dtype sample_mean(const Dtype* const seqs, const size_t sample_size) | ||
{ | ||
double sum = 0; | ||
for (int i = 0; i < sample_size; ++i) { | ||
sum += seqs[i]; | ||
} | ||
return sum / sample_size; | ||
} | ||
|
||
Dtype mean_bound(const Dtype std, const size_t sample_size) | ||
{ | ||
return std/sqrt((double)sample_size); | ||
} | ||
}; | ||
|
||
|
||
typedef ::testing::Types<float, double> Dtypes; | ||
TYPED_TEST_CASE(RandomNumberGeneratorTest, Dtypes); | ||
|
||
TYPED_TEST(RandomNumberGeneratorTest, TestRngGaussian) { | ||
size_t sample_size = 10000; | ||
SyncedMemory data_a(sample_size * sizeof(TypeParam)); | ||
Caffe::set_random_seed(1701); | ||
TypeParam mu = 0; | ||
TypeParam sigma = 1; | ||
caffe_vRngGaussian(sample_size, (TypeParam*)data_a.mutable_cpu_data(), mu, sigma); | ||
TypeParam true_mean = mu; | ||
TypeParam true_std = sigma; | ||
TypeParam bound = mean_bound(true_std, sample_size); | ||
TypeParam real_mean = sample_mean((TypeParam*)data_a.cpu_data(), sample_size); | ||
EXPECT_NEAR(real_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); | ||
TypeParam true_mean = (lower + upper) / 2; | ||
TypeParam true_std = (upper - lower) / sqrt(12); | ||
TypeParam bound = mean_bound(true_std, sample_size); | ||
TypeParam real_mean = sample_mean((TypeParam*)data_a.cpu_data(), sample_size); | ||
EXPECT_NEAR(real_mean, true_mean, bound); | ||
} | ||
|
||
|
||
|
||
} // namespace caffe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters