Skip to content

Commit

Permalink
remove-random-functions-in-CMath
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeLing authored and vigsterkr committed Feb 22, 2018
1 parent 3b9f989 commit 5460d86
Show file tree
Hide file tree
Showing 167 changed files with 1,000 additions and 973 deletions.
5 changes: 2 additions & 3 deletions benchmarks/hasheddoc_benchmarks.cpp
Expand Up @@ -9,7 +9,6 @@
#include <shogun/features/HashedDocDotFeatures.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/lib/NGramTokenizer.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;

Expand All @@ -23,13 +22,13 @@ int main(int argv, char** argc)
int32_t num_strings = 5000;
int32_t max_str_length = 10000;
SGStringList<char> string_list(num_strings, max_str_length);

auto m_rng = std::unique_ptr<CRandom>(new CRandom());
SG_SPRINT("Creating features...\n");
for (index_t i=0; i<num_strings; i++)
{
string_list.strings[i] = SGString<char>(max_str_length);
for (index_t j=0; j<max_str_length; j++)
string_list.strings[i].string[j] = (char) CMath::random('A', 'Z');
string_list.strings[i].string[j] = (char)m_rng->random('A', 'Z');
}
SG_SPRINT("Features were created.\n");

Expand Down
3 changes: 2 additions & 1 deletion benchmarks/rf_feats_benchmark.cpp
Expand Up @@ -16,6 +16,7 @@ int main(int argv, char** argc)

int32_t dims[] = {100, 300, 600};
CTime* timer = new CTime();
auto m_rng = std::unique_ptr<CRandom>(new CRandom());
for (index_t d=0; d<3; d++)
{
int32_t num_dim = dims[d];
Expand All @@ -27,7 +28,7 @@ int main(int argv, char** argc)
{
for (index_t j=0; j<num_dim; j++)
{
mat(j,i) = CMath::random(0,1) + 0.5;
mat(j, i) = m_rng->random(0, 1) + 0.5;
}
}

Expand Down
5 changes: 3 additions & 2 deletions benchmarks/rf_feats_kernel_comp.cpp
Expand Up @@ -29,6 +29,7 @@ int main(int argv, char** argc)
float64_t lin_C = 0.1;
float64_t non_lin_C = 0.1;
CPRCEvaluation* evaluator = new CPRCEvaluation();
auto m_rng = std::unique_ptr<CRandom>(new CRandom());
CSqrtDiagKernelNormalizer* normalizer = new CSqrtDiagKernelNormalizer(true);
SG_REF(normalizer);
for (index_t d=0; d<4; d++)
Expand All @@ -48,12 +49,12 @@ int main(int argv, char** argc)
if ((i+j)%2==0)
{
labs[i] = -1;
mat(j,i) = CMath::random(0,1) + 0.5;
mat(j, i) = m_rng->random(0, 1) + 0.5;
}
else
{
labs[i] = 1;
mat(j,i) = CMath::random(0,1) - 0.5;
mat(j, i) = m_rng->random(0, 1) - 0.5;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/sparse_test.cpp
Expand Up @@ -112,7 +112,7 @@ int main(int argc, char** argv)
v.set_const(1.0);
Map<VectorXd> map_v(v.vector, v.vlen);
CTime time;
CMath::init_random(17);
set_global_seed(17);

SG_SPRINT("time\tshogun (s)\teigen3 (s)\n\n");
for (index_t t=0; t<times; ++t)
Expand Down
5 changes: 3 additions & 2 deletions examples/undocumented/libshogun/classifier_larank.cpp
Expand Up @@ -24,13 +24,14 @@ void test()
SGMatrix<float64_t> matrix_test(num_class, num_vec);
CMulticlassLabels* labels=new CMulticlassLabels(num_vec);
CMulticlassLabels* labels_test=new CMulticlassLabels(num_vec);
auto m_rng = std::unique_ptr<CRandom>(new CRandom());
for (index_t i=0; i<num_vec; ++i)
{
index_t label=i%num_class;
for (index_t j=0; j<num_feat; ++j)
{
matrix(j,i)=CMath::randn_double();
matrix_test(j,i)=CMath::randn_double();
matrix(j, i) = m_rng->std_normal_distrib();
matrix_test(j, i) = m_rng->std_normal_distrib();
labels->set_label(i, label);
labels_test->set_label(i, label);
}
Expand Down
6 changes: 3 additions & 3 deletions examples/undocumented/libshogun/classifier_latent_svm.cpp
Expand Up @@ -110,7 +110,7 @@ static void read_dataset(char* fname, CLatentFeatures*& feats, CLatentLabels*& l
SG_REF(labels);

CBinaryLabels* ys = new CBinaryLabels(num_examples);

auto m_rng = std::unique_ptr<CRandom>(new CRandom());
feats = new CLatentFeatures(num_examples);
SG_REF(feats);

Expand Down Expand Up @@ -146,8 +146,8 @@ static void read_dataset(char* fname, CLatentFeatures*& feats, CLatentLabels*& l
height = atoi(last_pchar);

/* create latent label */
int x = CMath::random(0, width-1);
int y = CMath::random(0, height-1);
int x = m_rng->random(0, width - 1);
int y = m_rng->random(0, height - 1);
CBoundingBox* bb = new CBoundingBox(x,y);
labels->add_latent_label(bb);

Expand Down
43 changes: 22 additions & 21 deletions examples/undocumented/libshogun/classifier_libsvm_probabilities.cpp
Expand Up @@ -10,27 +10,28 @@ using namespace shogun;
//generates data points (of different classes) randomly
void gen_rand_data(SGMatrix<float64_t> features, SGVector<float64_t> labels, float64_t distance)
{
index_t num_samples=labels.vlen;
index_t dimensions=features.num_rows;
for (int32_t i=0; i<num_samples; i++)
{
if (i<num_samples/2)
{
labels[i]=-1.0;
for(int32_t j=0; j<dimensions; j++)
features(j,i)=CMath::random(0.0,1.0)+distance;
}
else
{
labels[i]=1.0;
for(int32_t j=0; j<dimensions; j++)
features(j,i)=CMath::random(0.0,1.0)-distance;
}
}
labels.display_vector("labels");
std::cout<<std::endl;
features.display_matrix("features");
std::cout<<std::endl;
auto m_rng = std::unique_ptr<CRandom>(new CRandom());
index_t num_samples = labels.vlen;
index_t dimensions = features.num_rows;
for (int32_t i = 0; i < num_samples; i++)
{
if (i < num_samples / 2)
{
labels[i] = -1.0;
for (int32_t j = 0; j < dimensions; j++)
features(j, i) = m_rng->random(0.0, 1.0) + distance;
}
else
{
labels[i] = 1.0;
for (int32_t j = 0; j < dimensions; j++)
features(j, i) = m_rng->random(0.0, 1.0) - distance;
}
}
labels.display_vector("labels");
std::cout << std::endl;
features.display_matrix("features");
std::cout << std::endl;
}

int main(int argc, char** argv)
Expand Down
Expand Up @@ -66,8 +66,9 @@ void test()
/* create some data and labels */
SGMatrix<float64_t> matrix(dim_vectors, num_vectors);
CBinaryLabels* labels=new CBinaryLabels(num_vectors);
auto m_rng = std::unique_ptr<CRandom>(new CRandom());
for (int32_t i=0; i<num_vectors*dim_vectors; i++)
matrix.matrix[i]=CMath::randn_double();
matrix.matrix[i] = m_rng->std_normal_distrib();

/* create num_feautres 2-dimensional vectors */
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>();
Expand Down
Expand Up @@ -27,24 +27,24 @@ void test_svmlight()
float64_t p_x=0.5; // probability for class A
float64_t mostly_prob=0.8;
CDenseLabels* labels=new CBinaryLabels(num_train+num_test);
CMath::init_random(17);
auto m_rng = std::unique_ptr<CRandom>(new CRandom(17));

SGStringList<char> data(num_train+num_test, max_length);
for (index_t i=0; i<num_train+num_test; ++i)
{
/* determine length */
index_t length=CMath::random(1, max_length);
index_t length = m_rng->random(1, max_length);

/* allocate string */
data.strings[i]=SGString<char>(length);

/* fill with elements and set label */
if (p_x<CMath::random(0.0, 1.0))
if (p_x < m_rng->random(0.0, 1.0))
{
labels->set_label(i, 1);
for (index_t j=0; j<length; ++j)
{
char c=mostly_prob<CMath::random(0.0, 1.0) ? '0' : '1';
char c = mostly_prob < m_rng->random(0.0, 1.0) ? '0' : '1';
data.strings[i].string[j]=c;
}
}
Expand All @@ -53,7 +53,7 @@ void test_svmlight()
labels->set_label(i, -1);
for (index_t j=0; j<length; ++j)
{
char c=mostly_prob<CMath::random(0.0, 1.0) ? '1' : '0';
char c = mostly_prob < m_rng->random(0.0, 1.0) ? '1' : '0';
data.strings[i].string[j]=c;
}
}
Expand Down
3 changes: 2 additions & 1 deletion examples/undocumented/libshogun/clustering_kmeans.cpp
Expand Up @@ -36,6 +36,7 @@ int main(int argc, char **argv)
int32_t dim_features=3;
int32_t num_vectors_per_cluster=5;
float64_t cluster_std_dev=2.0;
auto m_rng = std::unique_ptr<CRandom>(new CRandom());

/* build random cluster centers */
SGMatrix<float64_t> cluster_centers(dim_features, num_clusters);
Expand All @@ -56,7 +57,7 @@ int main(int argc, char **argv)
idx+=j;
idx+=k*dim_features;
float64_t entry=cluster_centers.matrix[i*dim_features+j];
data.matrix[idx]=CMath::normal_random(entry, cluster_std_dev);
data.matrix[idx] = m_rng->normal_random(entry, cluster_std_dev);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/undocumented/libshogun/converter_jade_bss.cpp
Expand Up @@ -28,7 +28,7 @@ using namespace Eigen;
void test()
{
// Generate sample data
CMath::init_random(0);
auto m_rng = std::unique_ptr<CRandom>(new CRandom(0));
int n_samples = 2000;
VectorXd time(n_samples, true);
time.setLinSpaced(n_samples,0,10);
Expand All @@ -39,11 +39,11 @@ void test()
{
// Sin wave
S(0,i) = sin(2*time[i]);
S(0,i) += 0.2*CMath::randn_double();
S(0, i) += 0.2 * m_rng->std_normal_distrib();

// Square wave
S(1,i) = sin(3*time[i]) < 0 ? -1 : 1;
S(1,i) += 0.2*CMath::randn_double();
S(1, i) += 0.2 * m_rng->std_normal_distrib();
}

// Standardize data
Expand Down
Expand Up @@ -26,7 +26,7 @@ void test_cross_validation()
/* data matrix dimensions */
index_t num_vectors=40;
index_t num_features=5;

auto m_rng = std::unique_ptr<CRandom>(new CRandom());
/* data means -1, 1 in all components, std deviation of 3 */
SGVector<float64_t> mean_1(num_features);
SGVector<float64_t> mean_2(num_features);
Expand All @@ -44,7 +44,8 @@ void test_cross_validation()
for (index_t j=0; j<num_features; ++j)
{
float64_t mean=i<num_vectors/2 ? mean_1.vector[0] : mean_2.vector[0];
train_dat.matrix[i*num_features+j]=CMath::normal_random(mean, sigma);
train_dat.matrix[i * num_features + j] =
m_rng->normal_random(mean, sigma);
}
}

Expand Down
Expand Up @@ -35,6 +35,7 @@ void test_cross_validation()
SGVector<float64_t>::fill_vector(mean_1.vector, mean_1.vlen, -1.0);
SGVector<float64_t>::fill_vector(mean_2.vector, mean_2.vlen, 1.0);
float64_t sigma=1.5;
auto m_rng = std::unique_ptr<CRandom>(new CRandom());

/* fill data matrix around mean */
SGMatrix<float64_t> train_dat(num_features, num_vectors);
Expand All @@ -43,7 +44,8 @@ void test_cross_validation()
for (index_t j=0; j<num_features; ++j)
{
float64_t mean=i<num_vectors/2 ? mean_1.vector[0] : mean_2.vector[0];
train_dat.matrix[i*num_features+j]=CMath::normal_random(mean, sigma);
train_dat.matrix[i * num_features + j] =
m_rng->normal_random(mean, sigma);
}
}

Expand Down
Expand Up @@ -24,22 +24,22 @@ void gen_rand_data(SGVector<float64_t> lab, SGMatrix<float64_t> feat,
{
index_t dims=feat.num_rows;
index_t num=lab.vlen;

auto m_rng = std::unique_ptr<CRandom>(new CRandom());
for (int32_t i=0; i<num; i++)
{
if (i<num/2)
{
lab[i]=-1.0;

for (int32_t j=0; j<dims; j++)
feat(j, i)=CMath::random(0.0, 1.0)+dist;
feat(j, i) = m_rng->random(0.0, 1.0) + dist;
}
else
{
lab[i]=1.0;

for (int32_t j=0; j<dims; j++)
feat(j, i)=CMath::random(0.0, 1.0)-dist;
feat(j, i) = m_rng->random(0.0, 1.0) - dist;
}
}
lab.display_vector("lab");
Expand Down
Expand Up @@ -30,7 +30,7 @@ const char fname_labels[]="../data/label_train_multiclass.dat";

void test_multiclass_mkl_cv()
{
CMath::init_random(12);
set_global_seed(12);
/* dense features from matrix */
CCSVFile* feature_file = new CCSVFile(fname_feats);
SGMatrix<float64_t> mat=SGMatrix<float64_t>();
Expand Down Expand Up @@ -84,7 +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
Expand Up @@ -30,15 +30,15 @@ void test_cross_validation()

/* training label data */
SGVector<float64_t> lab(num_vectors);
auto m_rng = std::unique_ptr<CRandom>(new CRandom());

/* fill data matrix and labels */
SGMatrix<float64_t> train_dat(num_features, num_vectors);
SGVector<float64_t>::range_fill_vector(train_dat.matrix, num_vectors);
for (index_t i=0; i<num_vectors; ++i)
{
/* labels are linear plus noise */
lab.vector[i]=i+CMath::normal_random(0, 1.0);

lab.vector[i] = i + m_rng->normal_random(0, 1.0);
}

/* training features */
Expand Down
3 changes: 2 additions & 1 deletion examples/undocumented/libshogun/features_subset_labels.cpp
Expand Up @@ -20,7 +20,8 @@ const int32_t num_classes=3;

void test()
{
const int32_t num_subset_idx=CMath::random(1, num_labels);
auto m_rng = std::unique_ptr<CRandom>(new CRandom());
const int32_t num_subset_idx = m_rng->random(1, num_labels);

/* create labels */
CMulticlassLabels* labels=new CMulticlassLabels(num_labels);
Expand Down
Expand Up @@ -46,7 +46,8 @@ const int32_t dim_features=6;

void test()
{
const int32_t num_subset_idx=CMath::random(1, num_vectors);
auto m_rng = std::unique_ptr<CRandom>(new CRandom());
const int32_t num_subset_idx = m_rng->random(1, num_vectors);

/* create feature data matrix */
SGMatrix<int32_t> data(dim_features, num_vectors);
Expand All @@ -55,7 +56,7 @@ void test()
for (index_t i=0; i<num_vectors; ++i)
{
for (index_t j=0; j<dim_features; ++j)
data.matrix[i*dim_features+j]=CMath::random(-5, 5);
data.matrix[i * dim_features + j] = m_rng->random(-5, 5);
}

/* create simple features */
Expand Down
4 changes: 2 additions & 2 deletions examples/undocumented/libshogun/hashed_features_example.cpp
Expand Up @@ -12,12 +12,12 @@ int main()

int32_t num_vectors = 5;
int32_t dim = 20;

auto m_rng = std::unique_ptr<CRandom>(new CRandom());
SGMatrix<int32_t> mat(dim, num_vectors);
for (index_t v=0; v<num_vectors; v++)
{
for (index_t d=0; d<dim; d++)
mat(d,v) = CMath::random(-dim, dim);
mat(d, v) = m_rng->random(-dim, dim);
}

int32_t hashing_dim = 12;
Expand Down

0 comments on commit 5460d86

Please sign in to comment.