Skip to content

Commit

Permalink
Merge pull request #2579 from iglesias/clean_sgvector
Browse files Browse the repository at this point in the history
Substitute randperm in SGVector for permute in Math.
  • Loading branch information
iglesias committed Oct 28, 2014
2 parents efeec01 + 1e84335 commit ee8a8d2
Show file tree
Hide file tree
Showing 25 changed files with 100 additions and 161 deletions.
5 changes: 3 additions & 2 deletions examples/undocumented/libshogun/features_subset_labels.cpp
Expand Up @@ -38,8 +38,9 @@ void test()
SGVector<float64_t>::display_vector(labels_data.vector, labels_data.vlen, "labels");

/* create subset indices */
SGVector<index_t> subset_idx(SGVector<index_t>::randperm(num_subset_idx),
num_subset_idx);
SGVector<index_t> subset_idx(num_subset_idx);
subset_idx.range_fill();
CMath::permute(subset_idx);

/* print subset indices */
SGVector<index_t>::display_vector(subset_idx.vector, subset_idx.vlen, "subset indices");
Expand Down
Expand Up @@ -11,6 +11,7 @@
#include <shogun/base/init.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/features/Subset.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;

Expand Down Expand Up @@ -69,8 +70,9 @@ void test()
"feature matrix");

/* create subset indices */
SGVector<index_t> subset_idx(SGVector<index_t>::randperm(num_subset_idx),
num_subset_idx);
SGVector<index_t> subset_idx(num_subset_idx);
subset_idx.range_fill();
CMath::permute(subset_idx);

/* print subset indices */
SGVector<index_t>::display_vector(subset_idx.vector, subset_idx.vlen, "subset indices");
Expand Down
3 changes: 2 additions & 1 deletion examples/undocumented/libshogun/kernel_custom.cpp
Expand Up @@ -13,6 +13,7 @@
#include <shogun/features/DenseFeatures.h>
#include <shogun/features/DataGenerator.h>
#include <shogun/features/IndexFeatures.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;

Expand All @@ -36,7 +37,7 @@ void test_custom_kernel_subsets()
for (index_t run=0; run<100; ++run)
{
subset.range_fill();
subset.permute();
CMath::permute(subset);
// 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_index.cpp
Expand Up @@ -58,9 +58,9 @@ void test_custom_kernel_index_subsets()
SGVector<index_t> row_subset(num_sub_row);
SGVector<index_t> col_subset(num_sub_col);
row_subset.range_fill();
row_subset.permute();
CMath::permute(row_subset);
col_subset.range_fill();
col_subset.permute();
CMath::permute(col_subset);

/* create index features */
CIndexFeatures* row_idx_feat=new CIndexFeatures(row_subset);
Expand Down
2 changes: 1 addition & 1 deletion examples/undocumented/libshogun/kernel_custom_kernel.cpp
Expand Up @@ -35,7 +35,7 @@ void test_custom_kernel_subsets()
for (index_t run=0; run<100; ++run)
{
subset.range_fill();
subset.permute();
CMath::permute(subset);
// subset.display_vector("permutation");
features->add_subset(subset);
k->init(features, features);
Expand Down
Expand Up @@ -97,7 +97,7 @@ void quadratic_time_mmd()
for (index_t i=0; i<num_trials; ++i)
{
/* this effectively means that p=q - rejecting is tpye I error */
inds.permute();
CMath::permute(inds);
precomputed->add_row_subset(inds);
precomputed->add_col_subset(inds);
type_I_errors[i]=mmd->perform_test()>alpha;
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/evaluation/CrossValidationSplitting.cpp
Expand Up @@ -35,7 +35,7 @@ void CCrossValidationSplitting::build_subsets()
/* permute indices */
SGVector<index_t> indices(m_labels->get_num_labels());
indices.range_fill();
indices.permute(m_rng);
CMath::permute(indices, m_rng);

index_t num_subsets=m_subset_indices->get_num_elements();

Expand Down
90 changes: 0 additions & 90 deletions src/shogun/lib/SGVector.cpp
Expand Up @@ -185,14 +185,6 @@ void SGVector<T>::random(T min_value, T max_value)

COMPLEX128_ERROR_TWOARGS(random)

template<class T>
void SGVector<T>::randperm()
{
randperm(vector, vlen);
}

COMPLEX128_ERROR_NOARG(randperm)

template <class T>
void SGVector<T>::qsort()
{
Expand Down Expand Up @@ -689,88 +681,6 @@ void SGVector<complex128_t>::random_vector(complex128_t* vec, int32_t len,
SG_SNOTIMPLEMENTED
}

template <class T>
SGVector<T> SGVector<T>::randperm_vec(int32_t n)
{
return SGVector<T>(randperm(n), n);
}

template <>
SGVector<complex128_t> SGVector<complex128_t>::randperm_vec(int32_t n)
{
SG_SNOTIMPLEMENTED
SGVector<complex128_t> perm(n);
return perm;
}

template <class T>
T* SGVector<T>::randperm(int32_t n)
{
T* perm = SG_MALLOC(T, n);
randperm(perm, n);

return perm;
}

template <>
complex128_t* SGVector<complex128_t>::randperm(int32_t n)
{
SG_SNOTIMPLEMENTED
SGVector<complex128_t> perm(n);
return perm.vector;
}

template <class T>
void SGVector<T>::randperm(T* perm, int32_t n)
{
for (int32_t i = 0; i < n; i++)
perm[i] = i;
permute(perm,n);
}

template <>
void SGVector<complex128_t>::randperm(complex128_t* perm, int32_t n)
{
SG_SNOTIMPLEMENTED
}

/** permute */
template <class T>
void SGVector<T>::permute(T* vec, int32_t n)
{
for (int32_t i = 0; i < n; i++)
CMath::swap(vec[i], vec[CMath::random(i, n-1)]);
}

template <class T>
void SGVector<T>::permute(T* vec, int32_t n, CRandom * rand)
{
for (int32_t i = 0; i < n; i++)
CMath::swap(vec[i], vec[rand->random(i, n-1)]);
}

template<class T>
void SGVector<T>::permute()
{
SGVector<T>::permute(vector, vlen);
}

template<class T>
void SGVector<T>::permute(CRandom * rand)
{
SGVector<T>::permute(vector, vlen, rand);
}

template <class T>
void SGVector<T>::permute_vector(SGVector<T> vec)
{
for (index_t i=0; i<vec.vlen; ++i)
{
CMath::swap(vec.vector[i],
vec.vector[CMath::random(i, vec.vlen-1)]);
}
}

template <>
bool SGVector<bool>::twonorm(const bool* x, int32_t len)
{
Expand Down
32 changes: 0 additions & 32 deletions src/shogun/lib/SGVector.h
Expand Up @@ -116,20 +116,6 @@ template<class T> class SGVector : public SGReferencedData
*/
void random(T min_value, T max_value);

/** Returns a random permutation of number from 0 to len-1 */
void randperm();

/** Returns a random permutation of number from 0 to n-1 */
static SGVector<T> randperm_vec(int32_t n);

/** Returns a random permutation of number from 0 to n-1.
* Caller has to free memory.
*
* @param n range of permutation
* @return random permutation of number from 0 to n-1
*/
static T* randperm(int32_t n);

/** Returns a vector with n linearly spaced elements between start and end.
*
* @param start beginning of the interval to divide
Expand Down Expand Up @@ -190,15 +176,6 @@ template<class T> class SGVector : public SGReferencedData
/** Random vector */
static void random_vector(T* vec, int32_t len, T min_value, T max_value);

/** Random permatutaion */
static void randperm(T* perm, int32_t n);

/** Permute */
static void permute(T* vec, int32_t n);

/** Permute with given CRandom state */
static void permute(T* vec, int32_t n, CRandom * rand);

/**
* Get the vector (no copying is done here)
*
Expand Down Expand Up @@ -353,15 +330,6 @@ template<class T> class SGVector : public SGReferencedData
*/
bool equals(SGVector<T>& other);

/** Permute vector */
static void permute_vector(SGVector<T> vec);

/** Create a random permutation in place */
void permute();

/** Create a random permutation with given CRandom state */
void permute(CRandom * rand);

/// || x ||_2
static T twonorm(const T* x, int32_t len);

Expand Down
5 changes: 3 additions & 2 deletions src/shogun/machine/StochasticGBMachine.cpp
Expand Up @@ -30,6 +30,7 @@

#include <shogun/machine/StochasticGBMachine.h>
#include <shogun/optimization/lbfgs/lbfgs.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;

Expand Down Expand Up @@ -275,8 +276,8 @@ void CStochasticGBMachine::apply_subset(CDenseFeatures<float64_t>* f, CLabels* i
{
int32_t subset_size=m_subset_frac*(f->get_num_vectors());
SGVector<index_t> idx(f->get_num_vectors());
idx.range_fill(0);
idx.randperm();
idx.range_fill();
CMath::permute(idx);

SGVector<index_t> subset(subset_size);
memcpy(subset.vector,idx.vector,subset.vlen*sizeof(index_t));
Expand Down
7 changes: 4 additions & 3 deletions src/shogun/machine/gp/EPInferenceMethod.cpp
Expand Up @@ -169,7 +169,8 @@ void CEPInferenceMethod::update()
}

// create vector of the random permutation
SGVector<index_t> randperm=SGVector<index_t>::randperm_vec(n);
SGVector<index_t> v(n);
v.range_fill();

// cavity tau and nu vectors
SGVector<float64_t> tau_n(n);
Expand All @@ -189,11 +190,11 @@ void CEPInferenceMethod::update()
sweep++;

// shuffle random permutation
randperm.permute();
CMath::permute(v);

for (index_t j=0; j<n; j++)
{
index_t i=randperm[j];
index_t i=v[j];

// find cavity paramters
tau_n[i]=1.0/m_Sigma(i,i)-m_ttau[i];
Expand Down
26 changes: 24 additions & 2 deletions src/shogun/mathematics/Math.h
Expand Up @@ -27,6 +27,7 @@
#include <shogun/io/SGIO.h>
#include <shogun/base/Parallel.h>
#include <shogun/mathematics/Random.h>
#include <shogun/lib/SGVector.h>

#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
Expand Down Expand Up @@ -694,6 +695,27 @@ class CMath : public CSGObject
return sg_rand->std_normal_distrib();
}

/** Permute randomly the elements of the vector. If provided, use the
* random object to generate the permutations.
*
* @param v the vector to permute.
* @param rand random object that might be used to generate the permutations.
*/
template <class T>
static void permute(SGVector<T> v, CRandom* rand=NULL)
{
if (rand)
{
for (index_t i=0; i<v.vlen; ++i)
swap(v[i], v[rand->random(i, v.vlen-1)]);
}
else
{
for (index_t i=0; i<v.vlen; ++i)
swap(v[i], v[random(i, v.vlen-1)]);
}
}

template <class T>
static int32_t get_num_nonzero(T* vec, int32_t len)
{
Expand Down Expand Up @@ -1659,9 +1681,9 @@ void CMath::nmin(float64_t* output, T* index, int32_t size, int32_t n)
{
if (6*n*size<13*size*CMath::log(size))
for (int32_t i=0; i<n; i++)
min(&output[i], &index[i], size-i) ;
min(&output[i], &index[i], size-i);
else
qsort_index(output, index, size) ;
qsort_index(output, index, size);
}

/* move the smallest entry in the array to the beginning */
Expand Down
6 changes: 3 additions & 3 deletions src/shogun/multiclass/tree/RandomCARTree.cpp
Expand Up @@ -28,8 +28,8 @@
* either expressed or implied, of the Shogun Development Team.
*/

#include <shogun/mathematics/Math.h>
#include <shogun/multiclass/tree/RandomCARTree.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;

Expand Down Expand Up @@ -95,8 +95,8 @@ int32_t CRandomCARTree::compute_best_attribute(SGMatrix<float64_t> mat, SGVector
// randomly choose w/o replacement the attributes from which best will be chosen
// randomly permute and choose 1st randsubset_size elements
SGVector<index_t> idx(num_feats);
idx.range_fill(0);
idx.randperm();
idx.range_fill();
CMath::permute(idx);

float64_t max_gain=MIN_SPLIT_GAIN;
int32_t best_attribute=-1;
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/statistics/IndependenceTest.cpp
Expand Up @@ -90,7 +90,7 @@ SGVector<float64_t> CIndependenceTest::sample_null()
* and compute statistic. This is done using subsets here */

/* create index permutation and add as subset to features from p */
SGVector<index_t>::permute_vector(ind_permutation);
CMath::permute(ind_permutation);

/* compute statistic for this permutation of mixed samples */
m_p->add_subset(ind_permutation);
Expand Down
3 changes: 2 additions & 1 deletion src/shogun/statistics/KernelIndependenceTest.cpp
Expand Up @@ -33,6 +33,7 @@
#include <shogun/features/Features.h>
#include <shogun/kernel/Kernel.h>
#include <shogun/kernel/CustomKernel.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;

Expand Down Expand Up @@ -102,7 +103,7 @@ SGVector<float64_t> CKernelIndependenceTest::sample_null()
* and compute statistic. This is done using subsets here. add to
* custom kernel since it has no features to subset. CustomKernel
* has not to be re-initialised after each subset setting */
SGVector<index_t>::permute_vector(ind_permutation);
CMath::permute(ind_permutation);

custom_kernel_p->add_row_subset(ind_permutation);
custom_kernel_p->add_col_subset(ind_permutation);
Expand Down
3 changes: 2 additions & 1 deletion src/shogun/statistics/KernelTwoSampleTest.cpp
Expand Up @@ -11,6 +11,7 @@
#include <shogun/features/Features.h>
#include <shogun/kernel/Kernel.h>
#include <shogun/kernel/CustomKernel.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;

Expand Down Expand Up @@ -91,7 +92,7 @@ SGVector<float64_t> CKernelTwoSampleTest::sample_null()
* This is done using subsets here. add to custom kernel since
* it has no features to subset. CustomKernel has not to be
* re-initialised after each subset setting */
SGVector<int32_t>::permute_vector(ind_permutation);
CMath::permute(ind_permutation);

custom_kernel->add_row_subset(ind_permutation);
custom_kernel->add_col_subset(ind_permutation);
Expand Down

0 comments on commit ee8a8d2

Please sign in to comment.