Skip to content

Commit

Permalink
use get_prng and move to C-11
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeLing authored and vigsterkr committed Aug 9, 2017
1 parent 02dfb1c commit aa9cfc3
Show file tree
Hide file tree
Showing 187 changed files with 1,138 additions and 1,901 deletions.
5 changes: 3 additions & 2 deletions benchmarks/hasheddoc_benchmarks.cpp
Expand Up @@ -26,13 +26,14 @@ 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());
auto prng = get_prng();
std::uniform_int_distribution<index_t> dist('A', 'Z');
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)m_rng->random('A', 'Z');
string_list.strings[i].string[j] = (char)dist(prng);
}
SG_SPRINT("Features were created.\n");

Expand Down
5 changes: 3 additions & 2 deletions benchmarks/rf_feats_benchmark.cpp
Expand Up @@ -16,7 +16,8 @@ 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());
auto prng = get_prng();
std::uniform_int_distribution<int32_t> dist(0, 1);
for (index_t d=0; d<3; d++)
{
int32_t num_dim = dims[d];
Expand All @@ -28,7 +29,7 @@ int main(int argv, char** argc)
{
for (index_t j=0; j<num_dim; j++)
{
mat(j, i) = m_rng->random(0, 1) + 0.5;
mat(j, i) = dist(prng) + 0.5;
}
}

Expand Down
7 changes: 4 additions & 3 deletions benchmarks/rf_feats_kernel_comp.cpp
Expand Up @@ -29,7 +29,8 @@ 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());
auto prng = get_prng();
std::uniform_int_distribution<int32_t> dist(0, 1);
CSqrtDiagKernelNormalizer* normalizer = new CSqrtDiagKernelNormalizer(true);
SG_REF(normalizer);
for (index_t d=0; d<4; d++)
Expand All @@ -49,12 +50,12 @@ int main(int argv, char** argc)
if ((i+j)%2==0)
{
labs[i] = -1;
mat(j, i) = m_rng->random(0, 1) + 0.5;
mat(j, i) = dist(prng) + 0.5;
}
else
{
labs[i] = 1;
mat(j, i) = m_rng->random(0, 1) - 0.5;
mat(j, i) = dist(prng) - 0.5;
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions examples/undocumented/libshogun/classifier_larank.cpp
Expand Up @@ -27,14 +27,15 @@ 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());
auto prng = get_prng();
std::normal_distribution<float64_t> dist(0, 1);
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) = m_rng->std_normal_distrib();
matrix_test(j, i) = m_rng->std_normal_distrib();
matrix(j, i) = dist(prng);
matrix_test(j, i) = dist(prng);
labels->set_label(i, label);
labels_test->set_label(i, label);
}
Expand Down
9 changes: 5 additions & 4 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());
auto prng = get_prng();
feats = new CLatentFeatures(num_examples);
SG_REF(feats);

Expand Down Expand Up @@ -144,10 +144,11 @@ static void read_dataset(char* fname, CLatentFeatures*& feats, CLatentLabels*& l
while ((*pchar)!='\n') pchar++;
*pchar = '\0';
height = atoi(last_pchar);

std::uniform_int_distribution<index_t> dist_w(0, width - 1);
std::uniform_int_distribution<index_t> dist_h(0, height - 1);
/* create latent label */
int x = m_rng->random(0, width - 1);
int y = m_rng->random(0, height - 1);
int x = dist_w(prng);
int y = dist_h(prng);
CBoundingBox* bb = new CBoundingBox(x,y);
labels->add_latent_label(bb);

Expand Down
Expand Up @@ -10,7 +10,8 @@ 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)
{
auto m_rng = std::unique_ptr<CRandom>(new CRandom());
auto prng = get_prng();
std::uniform_real_distribution<float64_t> dist(0, 1.0);
index_t num_samples = labels.vlen;
index_t dimensions = features.num_rows;
for (int32_t i = 0; i < num_samples; i++)
Expand All @@ -19,13 +20,13 @@ void gen_rand_data(SGMatrix<float64_t> features, SGVector<float64_t> labels, flo
{
labels[i] = -1.0;
for (int32_t j = 0; j < dimensions; j++)
features(j, i) = m_rng->random(0.0, 1.0) + distance;
features(j, i) = dist(prng) + 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;
features(j, i) = dist(prng) - distance;
}
}
labels.display_vector("labels");
Expand Down
Expand Up @@ -68,9 +68,10 @@ 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());
auto prng = get_prng();
std::normal_distribution<float64_t> dist(0, 1);
for (int32_t i=0; i<num_vectors*dim_vectors; i++)
matrix.matrix[i] = m_rng->std_normal_distrib();
matrix.matrix[i] = dist(prng);

/* create num_feautres 2-dimensional vectors */
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>();
Expand Down
Expand Up @@ -30,24 +30,26 @@ 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);
auto m_rng = std::unique_ptr<CRandom>(new CRandom(17));
auto prng = get_prng();
std::uniform_real_distribution<float64_t> dist_real(0.0, 1.0);
std::uniform_int_distribution<index_t> dist_int(1, max_length);

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 = m_rng->random(1, max_length);
index_t length = dist_int(prng);

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

/* fill with elements and set label */
if (p_x < m_rng->random(0.0, 1.0))
if (p_x < dist_real(prng))
{
labels->set_label(i, 1);
for (index_t j=0; j<length; ++j)
{
char c = mostly_prob < m_rng->random(0.0, 1.0) ? '0' : '1';
char c = mostly_prob < dist_real(prng) ? '0' : '1';
data.strings[i].string[j]=c;
}
}
Expand All @@ -56,7 +58,7 @@ void test_svmlight()
labels->set_label(i, -1);
for (index_t j=0; j<length; ++j)
{
char c = mostly_prob < m_rng->random(0.0, 1.0) ? '1' : '0';
char c = mostly_prob < dist_real(prng) ? '1' : '0';
data.strings[i].string[j]=c;
}
}
Expand Down
6 changes: 4 additions & 2 deletions examples/undocumented/libshogun/clustering_kmeans.cpp
Expand Up @@ -39,7 +39,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());
auto prng = get_prng();

/* build random cluster centers */
SGMatrix<float64_t> cluster_centers(dim_features, num_clusters);
Expand All @@ -60,7 +60,9 @@ 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] = m_rng->normal_random(entry, cluster_std_dev);
std::normal_distribution<float64_t> dist(
entry, cluster_std_dev);
data.matrix[idx] = dist(prng);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions examples/undocumented/libshogun/converter_jade_bss.cpp
Expand Up @@ -32,7 +32,8 @@ using namespace Eigen;
void test()
{
// Generate sample data
auto m_rng = std::unique_ptr<CRandom>(new CRandom(0));
auto prng = get_prng();
std::normal_distribution<float64_t> dist(0, 1);
int n_samples = 2000;
VectorXd time(n_samples, true);
time.setLinSpaced(n_samples,0,10);
Expand All @@ -43,11 +44,11 @@ void test()
{
// Sin wave
S(0,i) = sin(2*time[i]);
S(0, i) += 0.2 * m_rng->std_normal_distrib();
S(0, i) += 0.2 * dist(prng);

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

// Standardize data
Expand Down
Expand Up @@ -29,7 +29,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());
auto prng = get_prng();
/* 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 @@ -47,8 +47,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] =
m_rng->normal_random(mean, sigma);
std::normal_distribution<float64_t> dist(mean, sigma);
train_dat.matrix[i * num_features + j] = dist(prng);
}
}

Expand Down
Expand Up @@ -37,7 +37,8 @@ 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());
auto prng = get_prng();
std::normal_distribution<float64_t> dist(0, 1);

/* fill data matrix around mean */
SGMatrix<float64_t> train_dat(num_features, num_vectors);
Expand All @@ -46,8 +47,7 @@ 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] =
m_rng->normal_random(mean, sigma);
train_dat.matrix[i * num_features + j] = dist(prng);
}
}

Expand Down
Expand Up @@ -28,22 +28,23 @@ 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());
auto prng = get_prng();
std::uniform_real_distribution<float64_t> uniform_dist(0.0, 1.0);
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) = m_rng->random(0.0, 1.0) + dist;
feat(j, i) = uniform_dist(prng) + dist;
}
else
{
lab[i]=1.0;

for (int32_t j=0; j<dims; j++)
feat(j, i) = m_rng->random(0.0, 1.0) - dist;
feat(j, i) = uniform_dist(prng) - dist;
}
}
lab.display_vector("lab");
Expand Down
Expand Up @@ -33,15 +33,16 @@ void test_cross_validation()

/* training label data */
SGVector<float64_t> lab(num_vectors);
auto m_rng = std::unique_ptr<CRandom>(new CRandom());
auto prng = get_prng();
std::uniform_real_distribution<float64_t> dist(0.0, 1.0);

/* 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 + m_rng->normal_random(0, 1.0);
lab.vector[i] = i + dist(prng);
}

/* training features */
Expand Down
5 changes: 3 additions & 2 deletions examples/undocumented/libshogun/features_subset_labels.cpp
Expand Up @@ -24,8 +24,9 @@ const int32_t num_classes=3;

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

/* create labels */
CMulticlassLabels* labels=new CMulticlassLabels(num_labels);
Expand Down
Expand Up @@ -49,17 +49,19 @@ const int32_t dim_features=6;

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

/* create feature data matrix */
SGMatrix<int32_t> data(dim_features, num_vectors);

/* fill matrix with random data */
std::uniform_int_distribution<index_t> dist_s(-5, 5);
for (index_t i=0; i<num_vectors; ++i)
{
for (index_t j=0; j<dim_features; ++j)
data.matrix[i * dim_features + j] = m_rng->random(-5, 5);
data.matrix[i * dim_features + j] = dist_s(prng);
}

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

int32_t num_vectors = 5;
int32_t dim = 20;
auto m_rng = std::unique_ptr<CRandom>(new CRandom());
auto prng = get_prng();
std::uniform_int_distribution<index_t> dist(-dim, dim);
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) = m_rng->random(-dim, dim);
mat(d, v) = dist(prng);
}

int32_t hashing_dim = 12;
Expand Down
4 changes: 2 additions & 2 deletions examples/undocumented/libshogun/kernel_custom.cpp
Expand Up @@ -33,11 +33,11 @@ void test_custom_kernel_subsets()

/* create a random permutation */
SGVector<index_t> subset(m);
auto prng = std::unique_ptr<CRandom>(new CRandom());
auto prng = get_prng();
for (index_t run=0; run<100; ++run)
{
subset.range_fill();
CMath::permute(subset, prng.get());
CMath::permute(subset, prng);
// subset.display_vector("permutation");
features->add_subset(subset);
k->init(features, features);
Expand Down
4 changes: 2 additions & 2 deletions examples/undocumented/libshogun/kernel_custom_kernel.cpp
Expand Up @@ -31,11 +31,11 @@ void test_custom_kernel_subsets()

/* create a random permutation */
SGVector<index_t> subset(m);
auto prng = std::unique_ptr<CRandom>(new CRandom());
auto prng = get_prng();
for (index_t run=0; run<100; ++run)
{
subset.range_fill();
CMath::permute(subset, prng.get());
CMath::permute(subset, prng);
// subset.display_vector("permutation");
features->add_subset(subset);
k->init(features, features);
Expand Down

0 comments on commit aa9cfc3

Please sign in to comment.