This commit is contained in:
Evan Shelhamer 2014-03-21 15:50:43 -07:00
Родитель 453fcf9095
Коммит aaa26466eb
5 изменённых файлов: 30 добавлений и 20 удалений

Просмотреть файл

@ -87,7 +87,9 @@ class Caffe {
// boost RNG
typedef boost::mt19937 random_generator_t;
inline static random_generator_t &rng_stream() { return Get().random_generator_; }
inline static random_generator_t &rng_stream() {
return Get().random_generator_;
}
// Returns the mode: running on CPU or GPU.
inline static Brew mode() { return Get().mode_; }

Просмотреть файл

@ -22,8 +22,7 @@ int64_t cluster_seedgen(void) {
Caffe::Caffe()
: mode_(Caffe::CPU), phase_(Caffe::TRAIN), cublas_handle_(NULL),
curand_generator_(NULL),
random_generator_()
{
random_generator_() {
// Try to create a cublas handler, and report an error if failed (but we will
// keep the program running as one might just want to run CPU code).
if (cublasCreate(&cublas_handle_) != CUBLAS_STATUS_SUCCESS) {

Просмотреть файл

@ -36,10 +36,12 @@ TEST_F(CommonTest, TestRandSeedCPU) {
SyncedMemory data_a(10 * sizeof(int));
SyncedMemory data_b(10 * sizeof(int));
Caffe::set_random_seed(1701);
caffe_vRngBernoulli(10, reinterpret_cast<int*>(data_a.mutable_cpu_data()), 0.5);
caffe_vRngBernoulli(10,
reinterpret_cast<int*>(data_a.mutable_cpu_data()), 0.5);
Caffe::set_random_seed(1701);
caffe_vRngBernoulli(10, reinterpret_cast<int*>(data_b.mutable_cpu_data()), 0.5);
caffe_vRngBernoulli(10,
reinterpret_cast<int*>(data_b.mutable_cpu_data()), 0.5);
for (int i = 0; i < 10; ++i) {
EXPECT_EQ(((const int*)(data_a.cpu_data()))[i],

Просмотреть файл

@ -1,6 +1,11 @@
// Copyright 2014 kloudkl@github
// Copyright 2014 Jeff Donahue
// Copyright 2014 Alejandro Dubrovsky
// Copyright 2014 Evan Shelhamer
#include <cuda_runtime.h>
#include <cmath>
#include <cstring>
#include <cuda_runtime.h>
#include "gtest/gtest.h"
#include "caffe/common.hpp"
@ -15,8 +20,7 @@ class RandomNumberGeneratorTest : public ::testing::Test {
public:
virtual ~RandomNumberGeneratorTest() {}
Dtype sample_mean(const Dtype* const seqs, const size_t sample_size)
{
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];
@ -24,8 +28,7 @@ class RandomNumberGeneratorTest : public ::testing::Test {
return sum / sample_size;
}
Dtype sample_mean(const int* const seqs, const size_t 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]);
@ -33,9 +36,8 @@ class RandomNumberGeneratorTest : public ::testing::Test {
return sum / sample_size;
}
Dtype mean_bound(const Dtype std, const size_t sample_size)
{
return std/sqrt((double)sample_size);
Dtype mean_bound(const Dtype std, const size_t sample_size) {
return std/sqrt(static_cast<double>(sample_size));
}
};
@ -43,6 +45,7 @@ class RandomNumberGeneratorTest : public ::testing::Test {
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));
@ -50,12 +53,13 @@ TYPED_TEST(RandomNumberGeneratorTest, TestRngGaussian) {
TypeParam mu = 0;
TypeParam sigma = 1;
caffe_vRngGaussian(sample_size,
(TypeParam*)data_a.mutable_cpu_data(), mu, sigma);
reinterpret_cast<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 empirical_mean =
this->sample_mean((TypeParam*)data_a.cpu_data(), sample_size);
this->sample_mean(reinterpret_cast<const TypeParam*>(data_a.cpu_data()),
sample_size);
EXPECT_NEAR(empirical_mean, true_mean, bound);
}
@ -67,12 +71,13 @@ TYPED_TEST(RandomNumberGeneratorTest, TestRngUniform) {
TypeParam lower = 0;
TypeParam upper = 1;
caffe_vRngUniform(sample_size,
(TypeParam*)data_a.mutable_cpu_data(), lower, upper);
reinterpret_cast<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 empirical_mean =
this->sample_mean((TypeParam*)data_a.cpu_data(), sample_size);
this->sample_mean(reinterpret_cast<const TypeParam*>(data_a.cpu_data()),
sample_size);
EXPECT_NEAR(empirical_mean, true_mean, bound);
}
@ -82,7 +87,8 @@ TYPED_TEST(RandomNumberGeneratorTest, TestRngBernoulli) {
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);
caffe_vRngBernoulli(sample_size,
static_cast<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);

Просмотреть файл

@ -1,11 +1,12 @@
// Copyright 2013 Yangqing Jia
// Copyright 2014 kloudkl@github
#include <limits>
#include <boost/math/special_functions/next.hpp>
#include <boost/random.hpp>
#include <cublas_v2.h>
#include <limits>
#include "caffe/common.hpp"
#include "caffe/util/math_functions.hpp"