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 Jul 11, 2017
1 parent ed1bb04 commit 706872f
Show file tree
Hide file tree
Showing 59 changed files with 351 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 @@ -33,9 +33,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 @@ -89,6 +87,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 @@ -494,6 +494,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 @@ -800,3 +801,8 @@ bool CSGObject::type_erased_has(const BaseTag& _tag) const
{
return self->has(_tag);
}

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 @@ -23,6 +23,8 @@
#include <shogun/lib/tag.h>
#include <shogun/lib/any.h>

#include <memory>

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

template <class T, class K> class CMap;

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

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

protected:
/* Iteratively clones all parameters of the provided instance into this instance.
* This will fail if the objects have different sets of registered parameters,
Expand Down Expand Up @@ -567,6 +575,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
18 changes: 1 addition & 17 deletions src/shogun/base/init.cpp
Expand Up @@ -38,7 +38,7 @@ namespace shogun
SGIO* sg_io=NULL;
Version* sg_version=NULL;
CMath* sg_math=NULL;
CRandom* sg_rand=NULL;

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

/// function called to print normal messages
Expand Down Expand Up @@ -67,8 +67,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());

Expand All @@ -82,7 +80,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 @@ -116,7 +113,6 @@ namespace shogun
sg_print_error=NULL;
sg_cancel_computations=NULL;

SG_UNREF(sg_rand);
SG_UNREF(sg_math);
SG_UNREF(sg_version);
SG_UNREF(sg_parallel);
Expand Down Expand Up @@ -179,18 +175,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;
}
#ifndef SWIG // SWIG should skip this part
SGLinalg* get_global_linalg()
{
Expand Down
12 changes: 0 additions & 12 deletions src/shogun/base/init.h
Expand Up @@ -97,18 +97,6 @@ void set_global_math(CMath* math);
*/
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 @@ -131,11 +131,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 @@ -91,7 +91,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 @@ -16,14 +16,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 @@ -35,7 +33,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 @@ -60,5 +58,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 @@ -45,9 +45,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 @@ -18,7 +18,6 @@ using namespace shogun;
CStratifiedCrossValidationSplitting::CStratifiedCrossValidationSplitting() :
CSplittingStrategy()
{
m_rng = sg_rand;
}

CStratifiedCrossValidationSplitting::CStratifiedCrossValidationSplitting(
Expand Down Expand Up @@ -70,7 +69,6 @@ CStratifiedCrossValidationSplitting::CStratifiedCrossValidationSplitting(
}
}

m_rng = sg_rand;
}

void CStratifiedCrossValidationSplitting::build_subsets()
Expand Down Expand Up @@ -124,7 +122,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 @@ -152,5 +150,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 @@ -47,9 +47,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 @@ -36,6 +36,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 @@ -54,7 +56,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 706872f

Please sign in to comment.