Skip to content

Commit

Permalink
get rid of sg_rand
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeLing authored and vigsterkr committed Feb 22, 2018
1 parent ea17588 commit 4ec68aa
Show file tree
Hide file tree
Showing 59 changed files with 349 additions and 304 deletions.
1 change: 0 additions & 1 deletion examples/undocumented/libshogun/classifier_latent_svm.cpp
Expand Up @@ -115,7 +115,6 @@ static void read_dataset(char* fname, CLatentFeatures*& feats, CLatentLabels*& l
SG_REF(feats);

auto pb = progress(range(num_examples));
CMath::init_random();
for (int i = 0; (!feof(fd)) && (i < num_examples); ++i)
{
fgets(line, MAX_LINE_LENGTH, fd);
Expand Down
Expand Up @@ -30,9 +30,7 @@ const char fname_labels[]="../data/label_train_multiclass.dat";

void test_multiclass_mkl_cv()
{
/* init random number generator for reproducible results of cross-validation in the light of ASSERT(result->mean>0.81); some lines down below */
sg_rand->set_seed(12);

CMath::init_random(12);
/* dense features from matrix */
CCSVFile* feature_file = new CCSVFile(fname_feats);
SGMatrix<float64_t> mat=SGMatrix<float64_t>();
Expand Down Expand Up @@ -86,6 +84,7 @@ void test_multiclass_mkl_cv()
CMulticlassAccuracy* eval_crit=new CMulticlassAccuracy();
CStratifiedCrossValidationSplitting* splitting=
new CStratifiedCrossValidationSplitting(labels, n_folds);
splitting->set_seed(12);
CCrossValidation *cross=new CCrossValidation(mkl, cfeats, labels, splitting,
eval_crit);
cross->set_autolock(false);
Expand Down
6 changes: 6 additions & 0 deletions src/shogun/base/SGObject.cpp
Expand Up @@ -510,6 +510,7 @@ void CSGObject::init()
m_parameters = new Parameter();
m_model_selection_parameters = new Parameter();
m_gradient_parameters=new Parameter();
m_rng = std::unique_ptr<CRandom>(new CRandom());
m_generic = PT_NOT_GENERIC;
m_load_pre_called = false;
m_load_post_called = false;
Expand Down Expand Up @@ -1052,3 +1053,8 @@ namespace shogun
SGOBJECT_PUT_DEFINE_WITH_CONVERSION(float32_t)
SGOBJECT_PUT_DEFINE_WITH_CONVERSION(float64_t)
};

void CSGObject::set_seed(int32_t seed)
{
m_rng->set_seed(seed);
}
12 changes: 12 additions & 0 deletions src/shogun/base/SGObject.h
Expand Up @@ -26,6 +26,8 @@
#include <utility>
#include <vector>

#include <memory>

/** \namespace shogun
* @brief all of classes and functions are contained in the shogun namespace
*/
Expand All @@ -37,6 +39,7 @@ class Parallel;
class Parameter;
class CSerializableFile;
class ParameterObserverInterface;
class CRandom;

template <class T, class K> class CMap;

Expand Down Expand Up @@ -624,6 +627,11 @@ class CSGObject
*/
virtual CSGObject* clone();

/** Set random seed
* @param seed seed for random generator
*/
void set_seed(int32_t seed);

protected:
/** Returns an empty instance of own type.
*
Expand Down Expand Up @@ -740,6 +748,10 @@ class CSGObject
/** Hash of parameter values*/
uint32_t m_hash;

protected:
/** random generator */
std::unique_ptr<CRandom> m_rng;

private:

EPrimitiveType m_generic;
Expand Down
19 changes: 1 addition & 18 deletions src/shogun/base/init.cpp
Expand Up @@ -41,8 +41,8 @@ namespace shogun
SGIO* sg_io=NULL;
Version* sg_version=NULL;
CMath* sg_math=NULL;
CRandom* sg_rand=NULL;
std::unique_ptr<CSignal> sg_signal(nullptr);

std::unique_ptr<SGLinalg> sg_linalg(nullptr);

// Two global variables to over-ride CMath::fequals for certain
Expand Down Expand Up @@ -74,8 +74,6 @@ namespace shogun
sg_version = new shogun::Version();
if (!sg_math)
sg_math = new shogun::CMath();
if (!sg_rand)
sg_rand = new shogun::CRandom();
if (!sg_linalg)
sg_linalg = std::unique_ptr<SGLinalg>(new shogun::SGLinalg());
if (!sg_signal)
Expand All @@ -91,7 +89,6 @@ namespace shogun
SG_REF(sg_parallel);
SG_REF(sg_version);
SG_REF(sg_math);
SG_REF(sg_rand);

sg_print_message=print_message;
sg_print_warning=print_warning;
Expand Down Expand Up @@ -123,7 +120,6 @@ namespace shogun
SG_UNREF(mallocs);
#endif

SG_UNREF(sg_rand);
SG_UNREF(sg_math);
SG_UNREF(sg_version);
SG_UNREF(sg_parallel);
Expand Down Expand Up @@ -210,19 +206,6 @@ namespace shogun
return sg_math;
}

void set_global_rand(CRandom* rand)
{
SG_REF(rand);
SG_UNREF(sg_rand);
sg_rand=rand;
}

CRandom* get_global_rand()
{
SG_REF(sg_rand);
return sg_rand;
}

CSignal* get_global_signal()
{
return sg_signal.get();
Expand Down
12 changes: 0 additions & 12 deletions src/shogun/base/init.h
Expand Up @@ -123,18 +123,6 @@ namespace shogun
*/
CMath* get_global_math();

/** set the global random object
*
* @param rand random object to use
*/
void set_global_rand(CRandom* rand);

/** get the global random object
*
* @return random object
*/
CRandom* get_global_rand();

#ifndef SWIG // SWIG should skip this part
/** get the global linalg library object
*
Expand Down
3 changes: 2 additions & 1 deletion src/shogun/clustering/KMeansMiniBatch.cpp
Expand Up @@ -128,11 +128,12 @@ SGVector<int32_t> CKMeansMiniBatch::mbchoose_rand(int32_t b, int32_t num)
{
SGVector<int32_t> chosen=SGVector<int32_t>(num);
SGVector<int32_t> ret=SGVector<int32_t>(b);
auto rng = std::unique_ptr<CRandom>(new CRandom());
chosen.zero();
int32_t ch=0;
while (ch<b)
{
const int32_t n=CMath::random(0,num-1);
const int32_t n = rng->random(0, num - 1);
if (chosen[n]==0)
{
chosen[n]+=1;
Expand Down
Expand Up @@ -88,7 +88,7 @@ SGMatrix<float64_t> CGaussianDistribution::sample(int32_t num_samples,
/* allocate memory and sample from std normal */
samples=SGMatrix<float64_t>(m_dimension, num_samples);
for (index_t i=0; i<m_dimension*num_samples; ++i)
samples.matrix[i]=sg_rand->std_normal_distrib();
samples.matrix[i] = m_rng->std_normal_distrib();
}

/* map into desired Gaussian covariance */
Expand Down
6 changes: 2 additions & 4 deletions src/shogun/evaluation/CrossValidationSplitting.cpp
Expand Up @@ -13,14 +13,12 @@ using namespace shogun;
CCrossValidationSplitting::CCrossValidationSplitting() :
CSplittingStrategy()
{
m_rng = sg_rand;
}

CCrossValidationSplitting::CCrossValidationSplitting(
CLabels* labels, index_t num_subsets) :
CSplittingStrategy(labels, num_subsets)
{
m_rng = sg_rand;
}

void CCrossValidationSplitting::build_subsets()
Expand All @@ -32,7 +30,7 @@ void CCrossValidationSplitting::build_subsets()
/* permute indices */
SGVector<index_t> indices(m_labels->get_num_labels());
indices.range_fill();
CMath::permute(indices, m_rng);
CMath::permute(indices, m_rng.get());

index_t num_subsets=m_subset_indices->get_num_elements();

Expand All @@ -57,5 +55,5 @@ void CCrossValidationSplitting::build_subsets()
/* finally shuffle to avoid that subsets with low indices have more
* elements, which happens if the number of class labels is not equal to
* the number of subsets (external random state important for threads) */
m_subset_indices->shuffle(m_rng);
m_subset_indices->shuffle(m_rng.get());
}
3 changes: 0 additions & 3 deletions src/shogun/evaluation/CrossValidationSplitting.h
Expand Up @@ -41,9 +41,6 @@ class CCrossValidationSplitting: public CSplittingStrategy

/** implementation of the standard cross-validation splitting strategy */
virtual void build_subsets();

/** custom rng if using cross validation across different threads */
CRandom * m_rng;
};
}

Expand Down
6 changes: 2 additions & 4 deletions src/shogun/evaluation/StratifiedCrossValidationSplitting.cpp
Expand Up @@ -14,7 +14,6 @@ using namespace shogun;
CStratifiedCrossValidationSplitting::CStratifiedCrossValidationSplitting() :
CSplittingStrategy()
{
m_rng = sg_rand;
}

CStratifiedCrossValidationSplitting::CStratifiedCrossValidationSplitting(
Expand Down Expand Up @@ -66,7 +65,6 @@ CStratifiedCrossValidationSplitting::CStratifiedCrossValidationSplitting(
}
}

m_rng = sg_rand;
}

void CStratifiedCrossValidationSplitting::build_subsets()
Expand Down Expand Up @@ -120,7 +118,7 @@ void CStratifiedCrossValidationSplitting::build_subsets()
label_indices.get_element(i);

// external random state important for threads
current->shuffle(m_rng);
current->shuffle(m_rng.get());

SG_UNREF(current);
}
Expand Down Expand Up @@ -148,5 +146,5 @@ void CStratifiedCrossValidationSplitting::build_subsets()
/* finally shuffle to avoid that subsets with low indices have more
* elements, which happens if the number of class labels is not equal to
* the number of subsets (external random state important for threads) */
m_subset_indices->shuffle(m_rng);
m_subset_indices->shuffle(m_rng.get());
}
3 changes: 0 additions & 3 deletions src/shogun/evaluation/StratifiedCrossValidationSplitting.h
Expand Up @@ -43,9 +43,6 @@ class CStratifiedCrossValidationSplitting: public CSplittingStrategy

/** implementation of the stratified cross-validation splitting strategy */
virtual void build_subsets();

/** custom rng if using cross validation across different threads */
CRandom * m_rng;
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/shogun/mathematics/Math.cpp
Expand Up @@ -33,6 +33,8 @@ int32_t CMath::LOGACCURACY = 0; // 100000 steps per integer

int32_t CMath::LOGRANGE = 0; // range for logtable: log(1+exp(x)) -25 <= x <= 0

CRandom* CMath::m_rng = new CRandom(12345);

const float64_t CMath::NOT_A_NUMBER = NAN;
const float64_t CMath::INFTY = INFINITY; // infinity
const float64_t CMath::ALMOST_INFTY = +1e+300; //a large number
Expand All @@ -51,7 +53,6 @@ const float64_t CMath::F_MIN_VAL64=(DBL_MIN * DBL_EPSILON);
#ifdef USE_LOGCACHE
float64_t* CMath::logtable = NULL;
#endif
uint32_t CMath::seed = 0;

CMath::CMath()
: CSGObject()
Expand Down

0 comments on commit 4ec68aa

Please sign in to comment.