[mlpack-svn] r11925 - in mlpack/trunk/src/mlpack: methods/hmm methods/kmeans methods/lars methods/local_coordinate_coding methods/radical methods/sparse_coding tests
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Tue Mar 20 22:34:53 EDT 2012
Author: rcurtin
Date: 2012-03-20 22:34:52 -0400 (Tue, 20 Mar 2012)
New Revision: 11925
Modified:
mlpack/trunk/src/mlpack/methods/hmm/hmm_impl.hpp
mlpack/trunk/src/mlpack/methods/kmeans/kmeans_impl.hpp
mlpack/trunk/src/mlpack/methods/kmeans/max_variance_new_cluster_impl.hpp
mlpack/trunk/src/mlpack/methods/lars/lars.cpp
mlpack/trunk/src/mlpack/methods/lars/lars.hpp
mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc.cpp
mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc.hpp
mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc_main.cpp
mlpack/trunk/src/mlpack/methods/radical/radical.cpp
mlpack/trunk/src/mlpack/methods/sparse_coding/sparse_coding.cpp
mlpack/trunk/src/mlpack/methods/sparse_coding/sparse_coding.hpp
mlpack/trunk/src/mlpack/methods/sparse_coding/sparse_coding_main.cpp
mlpack/trunk/src/mlpack/tests/local_coordinate_coding_test.cpp
mlpack/trunk/src/mlpack/tests/radical_test.cpp
mlpack/trunk/src/mlpack/tests/sparse_coding_test.cpp
Log:
Patch by andreasl: change u32 to uword (the generalized Armadillo type, which
can handle 64-bit Armadillo).
Modified: mlpack/trunk/src/mlpack/methods/hmm/hmm_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/hmm/hmm_impl.hpp 2012-03-21 02:33:58 UTC (rev 11924)
+++ mlpack/trunk/src/mlpack/methods/hmm/hmm_impl.hpp 2012-03-21 02:34:52 UTC (rev 11925)
@@ -343,7 +343,7 @@
emission[state].Probability(dataSeq.unsafe_col(0)));
// Store the best first state.
- arma::u32 index;
+ arma::uword index;
logStateProb.unsafe_col(0).max(index);
stateSeq[0] = index;
Modified: mlpack/trunk/src/mlpack/methods/kmeans/kmeans_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/kmeans/kmeans_impl.hpp 2012-03-21 02:33:58 UTC (rev 11924)
+++ mlpack/trunk/src/mlpack/methods/kmeans/kmeans_impl.hpp 2012-03-21 02:34:52 UTC (rev 11925)
@@ -417,7 +417,7 @@
while (clustersLeft != clusters)
{
- arma::u32 minIndex;
+ arma::uword minIndex;
distances.min(minIndex);
// Now we merge the clusters which that distance belongs to.
Modified: mlpack/trunk/src/mlpack/methods/kmeans/max_variance_new_cluster_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/kmeans/max_variance_new_cluster_impl.hpp 2012-03-21 02:33:58 UTC (rev 11924)
+++ mlpack/trunk/src/mlpack/methods/kmeans/max_variance_new_cluster_impl.hpp 2012-03-21 02:34:52 UTC (rev 11925)
@@ -37,7 +37,7 @@
}
// Now find the cluster with maximum variance.
- arma::u32 maxVarCluster;
+ arma::uword maxVarCluster;
variances.max(maxVarCluster);
// Now, inside this cluster, find the point which is furthest away.
Modified: mlpack/trunk/src/mlpack/methods/lars/lars.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/lars/lars.cpp 2012-03-21 02:33:58 UTC (rev 11924)
+++ mlpack/trunk/src/mlpack/methods/lars/lars.cpp 2012-03-21 02:34:52 UTC (rev 11925)
@@ -44,7 +44,7 @@
this->matGram = matGram;
}
-void LARS::SetGramMem(double* matGramMemPtr, u32 nDims)
+void LARS::SetGramMem(double* matGramMemPtr, uword nDims)
{
this->matGram = mat(matGramMemPtr, nDims, nDims, false);
}
@@ -68,7 +68,7 @@
// set up active set variables
nActive = 0;
- activeSet = std::vector<u32>(0);
+ activeSet = std::vector<uword>(0);
isActive = std::vector<bool>(matX.n_cols);
fill(isActive.begin(), isActive.end(), false);
@@ -87,7 +87,7 @@
vec corr = vecXTy;
vec absCorr = abs(corr);
- u32 changeInd;
+ uword changeInd;
double maxCorr = absCorr.max(changeInd); // change_ind gets set here
betaPath.push_back(beta);
@@ -110,7 +110,7 @@
matGram = trans(matX) * matX;
}
- //u32 iterations_run = 0;
+ //uword iterations_run = 0;
// MAIN LOOP
while ((nActive < matX.n_cols) && (maxCorr > EPS))
{
@@ -120,7 +120,7 @@
// explicit computation of max correlation, among inactive indices
changeInd = -1;
maxCorr = 0;
- for (u32 i = 0; i < matX.n_cols; i++)
+ for (uword i = 0; i < matX.n_cols; i++)
{
if (!isActive[i])
{
@@ -139,7 +139,7 @@
if (useCholesky)
{
// vec newGramCol = vec(nActive);
- // for (u32 i = 0; i < nActive; i++)
+ // for (uword i = 0; i < nActive; i++)
// {
// newGramCol[i] = dot(matX.col(activeSet[i]), matX.col(changeInd));
// }
@@ -155,7 +155,7 @@
// compute signs of correlations
vec s = vec(nActive);
- for (u32 i = 0; i < nActive; i++)
+ for (uword i = 0; i < nActive; i++)
{
s(i) = corr(activeSet[i]) / fabs(corr(activeSet[i]));
}
@@ -188,9 +188,9 @@
else
{
mat matGramActive = mat(nActive, nActive);
- for (u32 i = 0; i < nActive; i++)
+ for (uword i = 0; i < nActive; i++)
{
- for (u32 j = 0; j < nActive; j++)
+ for (uword j = 0; j < nActive; j++)
{
matGramActive(i,j) = matGram(activeSet[i], activeSet[j]);
}
@@ -213,7 +213,7 @@
if (nActive < matX.n_cols)
{
// compute correlations with direction
- for (u32 ind = 0; ind < matX.n_cols; ind++)
+ for (uword ind = 0; ind < matX.n_cols; ind++)
{
if (isActive[ind])
{
@@ -239,9 +239,9 @@
{
lassocond = false;
double lassoboundOnGamma = DBL_MAX;
- u32 activeIndToKickOut = -1;
+ uword activeIndToKickOut = -1;
- for (u32 i = 0; i < nActive; i++)
+ for (uword i = 0; i < nActive; i++)
{
double val = -beta(activeSet[i]) / betaDirection(i);
if ((val > 0) && (val < lassoboundOnGamma))
@@ -268,7 +268,7 @@
yHat += gamma * yHatDirection;
// update estimator
- for (u32 i = 0; i < nActive; i++)
+ for (uword i = 0; i < nActive; i++)
{
beta(activeSet[i]) += gamma * betaDirection(i);
}
@@ -304,7 +304,7 @@
corr -= lambda2 * beta;
}
double curLambda = 0;
- for (u32 i = 0; i < nActive; i++)
+ for (uword i = 0; i < nActive; i++)
{
curLambda += fabs(corr(activeSet[i]));
}
@@ -333,14 +333,14 @@
////////// private functions //////////
-void LARS::Deactivate(u32 activeVarInd)
+void LARS::Deactivate(uword activeVarInd)
{
nActive--;
isActive[activeSet[activeVarInd]] = false;
activeSet.erase(activeSet.begin() + activeVarInd);
}
-void LARS::Activate(u32 varInd)
+void LARS::Activate(uword varInd)
{
nActive++;
isActive[varInd] = true;
@@ -352,7 +352,7 @@
vec& yHatDirection)
{
yHatDirection.fill(0);
- for(u32 i = 0; i < nActive; i++)
+ for(uword i = 0; i < nActive; i++)
{
yHatDirection += betaDirection(i) * matX.col(activeSet[i]);
}
@@ -459,9 +459,9 @@
}
}
-void LARS::CholeskyDelete(u32 colToKill)
+void LARS::CholeskyDelete(uword colToKill)
{
- u32 n = matUtriCholFactor.n_rows;
+ uword n = matUtriCholFactor.n_rows;
if (colToKill == (n - 1))
{
@@ -472,7 +472,7 @@
matUtriCholFactor.shed_col(colToKill); // remove column colToKill
n--;
- for(u32 k = colToKill; k < n; k++)
+ for(uword k = colToKill; k < n; k++)
{
mat matG;
vec::fixed<2> rotatedVec;
Modified: mlpack/trunk/src/mlpack/methods/lars/lars.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/lars/lars.hpp 2012-03-21 02:33:58 UTC (rev 11924)
+++ mlpack/trunk/src/mlpack/methods/lars/lars.hpp 2012-03-21 02:34:52 UTC (rev 11925)
@@ -118,7 +118,7 @@
*
* @param matGram Matrix to which to set Gram matrix
*/
- void SetGramMem(double* matGramMemPtr, arma::u32 nDims);
+ void SetGramMem(double* matGramMemPtr, arma::uword nDims);
/**
* Compute Gram matrix. If elastic net, add lambda2 * identity to diagonal.
@@ -142,7 +142,7 @@
void Solution(arma::vec& beta);
//! Accessor for activeSet.
- const std::vector<arma::u32>& ActiveSet() const { return activeSet; }
+ const std::vector<arma::uword>& ActiveSet() const { return activeSet; }
//! Accessor for betaPath.
const std::vector<arma::vec>& BetaPath() const { return betaPath; }
@@ -175,19 +175,19 @@
std::vector<double> lambdaPath;
// number of dimensions in active set
- arma::u32 nActive;
+ arma::uword nActive;
// active set of dimensions
- std::vector<arma::u32> activeSet;
+ std::vector<arma::uword> activeSet;
// active set membership indicator (for each dimension)
std::vector<bool> isActive;
// remove activeVarInd'th element from active set
- void Deactivate(arma::u32 activeVarInd);
+ void Deactivate(arma::uword activeVarInd);
// add dimension varInd to active set
- void Activate(arma::u32 varInd);
+ void Activate(arma::uword varInd);
// compute "equiangular" direction in output space
void ComputeYHatDirection(const arma::mat& matX,
@@ -203,7 +203,7 @@
void GivensRotate(const arma::vec::fixed<2>& x, arma::vec::fixed<2>& rotatedX, arma::mat& G);
- void CholeskyDelete(arma::u32 colToKill);
+ void CholeskyDelete(arma::uword colToKill);
};
Modified: mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc.cpp 2012-03-21 02:33:58 UTC (rev 11924)
+++ mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc.cpp 2012-03-21 02:34:52 UTC (rev 11925)
@@ -17,7 +17,7 @@
namespace mlpack {
namespace lcc {
-LocalCoordinateCoding::LocalCoordinateCoding(const mat& matX, u32 nAtoms, double lambda) :
+LocalCoordinateCoding::LocalCoordinateCoding(const mat& matX, uword nAtoms, double lambda) :
nDims(matX.n_rows),
nAtoms(nAtoms),
nPoints(matX.n_cols),
@@ -44,7 +44,7 @@
void LocalCoordinateCoding::RandomInitDictionary() {
matD = randn(nDims, nAtoms);
- for(u32 j = 0; j < nAtoms; j++) {
+ for(uword j = 0; j < nAtoms; j++) {
matD.col(j) /= norm(matD.col(j), 2);
}
}
@@ -52,7 +52,7 @@
void LocalCoordinateCoding::DataDependentRandomInitDictionary() {
matD = mat(nDims, nAtoms);
- for(u32 j = 0; j < nAtoms; j++) {
+ for(uword j = 0; j < nAtoms; j++) {
vec vecD_j = matD.unsafe_col(j);
RandomAtom(vecD_j);
}
@@ -61,8 +61,8 @@
void LocalCoordinateCoding::RandomAtom(vec& atom) {
atom.zeros();
- const u32 nSeedAtoms = 3;
- for(u32 i = 0; i < nSeedAtoms; i++) {
+ const uword nSeedAtoms = 3;
+ for(uword i = 0; i < nSeedAtoms; i++) {
atom += matX.col(rand() % nPoints);
}
atom /= ((double) nSeedAtoms);
@@ -70,7 +70,7 @@
}
-void LocalCoordinateCoding::DoLCC(u32 nIterations) {
+void LocalCoordinateCoding::DoLCC(uword nIterations) {
bool converged = false;
double lastObjVal = 1e99;
@@ -84,7 +84,7 @@
<< "%\n";
Log::Info << "\tObjective value: " << Objective(adjacencies) << endl;
- for(u32 t = 1; t <= nIterations && !converged; t++) {
+ for(uword t = 1; t <= nIterations && !converged; t++) {
Log::Info << "Iteration " << t << " of " << nIterations << endl;
Log::Info << "Dictionary Step\n";
@@ -130,7 +130,7 @@
mat matDTD = trans(matD) * matD;
mat matDPrimeTDPrime(matDTD.n_rows, matDTD.n_cols);
- for(u32 i = 0; i < nPoints; i++) {
+ for(uword i = 0; i < nPoints; i++) {
// report progress
if((i % 100) == 0) {
Log::Debug << "\t" << i << endl;
@@ -168,15 +168,15 @@
uvec neighborCounts = zeros<uvec>(nPoints, 1);
if(adjacencies.n_elem > 0) {
// this gets the column index
- u32 curPointInd = (u32)(adjacencies(0) / nAtoms);
- u32 curCount = 1;
- for(u32 l = 1; l < adjacencies.n_elem; l++) {
- if((u32)(adjacencies(l) / nAtoms) == curPointInd) {
+ uword curPointInd = (uword)(adjacencies(0) / nAtoms);
+ uword curCount = 1;
+ for(uword l = 1; l < adjacencies.n_elem; l++) {
+ if((uword)(adjacencies(l) / nAtoms) == curPointInd) {
curCount++;
}
else {
neighborCounts(curPointInd) = curCount;
- curPointInd = (u32)(adjacencies(l) / nAtoms);
+ curPointInd = (uword)(adjacencies(l) / nAtoms);
curCount = 1;
}
}
@@ -187,8 +187,8 @@
// where each x^i is repeated for the number of neighbors x^i has
mat matXPrime = zeros(nDims, nPoints + adjacencies.n_elem);
matXPrime(span::all, span(0, nPoints - 1)) = matX;
- u32 curCol = nPoints;
- for(u32 i = 0; i < nPoints; i++) {
+ uword curCol = nPoints;
+ for(uword i = 0; i < nPoints; i++) {
if(neighborCounts(i) > 0) {
matXPrime(span::all, span(curCol, curCol + neighborCounts(i) - 1)) =
repmat(matX.col(i), 1, neighborCounts(i));
@@ -197,10 +197,10 @@
}
// handle the case of inactive atoms (atoms not used in the given coding)
- std::vector<u32> inactiveAtoms;
- std::vector<u32> activeAtoms;
+ std::vector<uword> inactiveAtoms;
+ std::vector<uword> activeAtoms;
activeAtoms.reserve(nAtoms);
- for(u32 j = 0; j < nAtoms; j++) {
+ for(uword j = 0; j < nAtoms; j++) {
if(accu(matZ.row(j) != 0) == 0) {
inactiveAtoms.push_back(j);
}
@@ -208,8 +208,8 @@
activeAtoms.push_back(j);
}
}
- u32 nActiveAtoms = activeAtoms.size();
- u32 nInactiveAtoms = inactiveAtoms.size();
+ uword nActiveAtoms = activeAtoms.size();
+ uword nInactiveAtoms = inactiveAtoms.size();
// efficient construction of Z restricted to active atoms
mat matActiveZ;
@@ -222,7 +222,7 @@
}
uvec atomReverseLookup = uvec(nAtoms);
- for(u32 i = 0; i < nActiveAtoms; i++) {
+ for(uword i = 0; i < nActiveAtoms; i++) {
atomReverseLookup(activeAtoms[i]) = i;
}
@@ -237,9 +237,9 @@
vec wSquared = ones(nPoints + adjacencies.n_elem, 1);
//Log::Debug << "building up matZPrime\n";
- for(u32 l = 0; l < adjacencies.n_elem; l++) {
- u32 atomInd = adjacencies(l) % nAtoms;
- u32 pointInd = (u32) (adjacencies(l) / nAtoms);
+ for(uword l = 0; l < adjacencies.n_elem; l++) {
+ uword atomInd = adjacencies(l) % nAtoms;
+ uword pointInd = (uword) (adjacencies(l) / nAtoms);
matZPrime(atomReverseLookup(atomInd), nPoints + l) = 1.0;
wSquared(nPoints + l) = matZ(atomInd, pointInd);
}
@@ -268,10 +268,10 @@
mat matDActiveEstimate =
trans(solve(matZPrime * diagmat(wSquared) * trans(matZPrime),
matZPrime * diagmat(wSquared) * trans(matXPrime)));
- for(u32 j = 0; j < nActiveAtoms; j++) {
+ for(uword j = 0; j < nActiveAtoms; j++) {
matDEstimate.col(activeAtoms[j]) = matDActiveEstimate.col(j);
}
- for(u32 j = 0; j < nInactiveAtoms; j++) {
+ for(uword j = 0; j < nInactiveAtoms; j++) {
vec vecD_j = matDEstimate.unsafe_col(inactiveAtoms[j]);
RandomAtom(vecD_j);
/*
@@ -288,10 +288,10 @@
double LocalCoordinateCoding::Objective(uvec adjacencies) {
double weightedL1NormZ = 0;
- u32 nAdjacencies = adjacencies.n_elem;
- for(u32 l = 0; l < nAdjacencies; l++) {
- u32 atomInd = adjacencies(l) % nAtoms;
- u32 pointInd = (u32) (adjacencies(l) / nAtoms);
+ uword nAdjacencies = adjacencies.n_elem;
+ for(uword l = 0; l < nAdjacencies; l++) {
+ uword atomInd = adjacencies(l) % nAtoms;
+ uword pointInd = (uword) (adjacencies(l) / nAtoms);
weightedL1NormZ += fabs(matZ(atomInd, pointInd)) * as_scalar(sum(square(matD.col(atomInd) - matX.col(pointInd))));
}
double froNormResidual = norm(matX - matD * matZ, "fro");
@@ -311,10 +311,10 @@
void RemoveRows(const mat& X, uvec rows_to_remove, mat& X_mod) {
- u32 n_cols = X.n_cols;
- u32 n_rows = X.n_rows;
- u32 n_to_remove = rows_to_remove.n_elem;
- u32 n_to_keep = n_rows - n_to_remove;
+ uword n_cols = X.n_cols;
+ uword n_rows = X.n_rows;
+ uword n_to_remove = rows_to_remove.n_elem;
+ uword n_to_keep = n_rows - n_to_remove;
if(n_to_remove == 0) {
X_mod = X;
@@ -322,19 +322,19 @@
else {
X_mod.set_size(n_to_keep, n_cols);
- u32 cur_row = 0;
- u32 remove_ind = 0;
+ uword cur_row = 0;
+ uword remove_ind = 0;
// first, check 0 to first row to remove
if(rows_to_remove(0) > 0) {
// note that this implies that n_rows > 1
- u32 height = rows_to_remove(0);
+ uword height = rows_to_remove(0);
X_mod(span(cur_row, cur_row + height - 1), span::all) =
X(span(0, rows_to_remove(0) - 1), span::all);
cur_row += height;
}
// now, check i'th row to remove to (i + 1)'th row to remove, until i = penultimate row
while(remove_ind < n_to_remove - 1) {
- u32 height =
+ uword height =
rows_to_remove[remove_ind + 1]
- rows_to_remove[remove_ind]
- 1;
Modified: mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc.hpp 2012-03-21 02:33:58 UTC (rev 11924)
+++ mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc.hpp 2012-03-21 02:34:52 UTC (rev 11925)
@@ -76,7 +76,7 @@
* @param nAtoms Number of atoms in dictionary
* @param lambda Regularization parameter for weighted l1-norm penalty
*/
- LocalCoordinateCoding(const arma::mat& matX, arma::u32 nAtoms, double lambda);
+ LocalCoordinateCoding(const arma::mat& matX, arma::uword nAtoms, double lambda);
/**
* Initialize dictionary somehow
@@ -118,7 +118,7 @@
*
* @param nIterations Maximum number of iterations to run algorithm
*/
- void DoLCC(arma::u32 nIterations);
+ void DoLCC(arma::uword nIterations);
/**
* Sparse code each point via distance-weighted LARS
@@ -163,9 +163,9 @@
private:
- arma::u32 nDims;
- arma::u32 nAtoms;
- arma::u32 nPoints;
+ arma::uword nDims;
+ arma::uword nAtoms;
+ arma::uword nPoints;
// data (columns are points)
arma::mat matX;
Modified: mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc_main.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc_main.cpp 2012-03-21 02:33:58 UTC (rev 11924)
+++ mlpack/trunk/src/mlpack/methods/local_coordinate_coding/lcc_main.cpp 2012-03-21 02:34:52 UTC (rev 11925)
@@ -45,10 +45,10 @@
mat matX;
matX.load(dataFullpath);
- u32 nPoints = matX.n_cols;
+ uword nPoints = matX.n_cols;
// normalize each point since these are images
- for(u32 i = 0; i < nPoints; i++) {
+ for(uword i = 0; i < nPoints; i++) {
matX.col(i) /= norm(matX.col(i), 2);
}
Modified: mlpack/trunk/src/mlpack/methods/radical/radical.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/radical/radical.cpp 2012-03-21 02:33:58 UTC (rev 11924)
+++ mlpack/trunk/src/mlpack/methods/radical/radical.cpp 2012-03-21 02:34:52 UTC (rev 11925)
@@ -57,8 +57,8 @@
// Apparently faster
double sum = 0;
- u32 range = z.n_elem - m;
- for(u32 i = 0; i < range; i++) {
+ uword range = z.n_elem - m;
+ for(uword i = 0; i < range; i++) {
sum += log(z(i + m) - z(i));
}
return sum;
@@ -93,7 +93,7 @@
values(i) = Vasicek(candidateY1) + Vasicek(candidateY2);
}
- u32 indOpt;
+ uword indOpt;
values.min(indOpt); // we ignore the return value; we don't care about it
return thetas(indOpt);
}
Modified: mlpack/trunk/src/mlpack/methods/sparse_coding/sparse_coding.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/sparse_coding/sparse_coding.cpp 2012-03-21 02:33:58 UTC (rev 11924)
+++ mlpack/trunk/src/mlpack/methods/sparse_coding/sparse_coding.cpp 2012-03-21 02:34:52 UTC (rev 11925)
@@ -19,7 +19,7 @@
namespace sparse_coding {
-SparseCoding::SparseCoding(const mat& matX, u32 nAtoms, double lambda1, double lambda2) :
+SparseCoding::SparseCoding(const mat& matX, uword nAtoms, double lambda1, double lambda2) :
nDims(matX.n_rows),
nAtoms(nAtoms),
nPoints(matX.n_cols),
@@ -52,7 +52,7 @@
// always a not good decision!
void SparseCoding::RandomInitDictionary() {
matD = randn(nDims, nAtoms);
- for(u32 j = 0; j < nAtoms; j++) {
+ for(uword j = 0; j < nAtoms; j++) {
matD.col(j) /= norm(matD.col(j), 2);
}
}
@@ -60,7 +60,7 @@
// the sensible heuristic
void SparseCoding::DataDependentRandomInitDictionary() {
matD = mat(nDims, nAtoms);
- for(u32 j = 0; j < nAtoms; j++) {
+ for(uword j = 0; j < nAtoms; j++) {
vec vecD_j = matD.unsafe_col(j);
RandomAtom(vecD_j);
}
@@ -69,15 +69,15 @@
void SparseCoding::RandomAtom(vec& atom) {
atom.zeros();
- const u32 nSeedAtoms = 3;
- for(u32 i = 0; i < nSeedAtoms; i++) {
+ const uword nSeedAtoms = 3;
+ for(uword i = 0; i < nSeedAtoms; i++) {
atom += matX.col(rand() % nPoints);
}
atom /= norm(atom, 2);
}
-void SparseCoding::DoSparseCoding(u32 nIterations) {
+void SparseCoding::DoSparseCoding(uword nIterations) {
bool converged = false;
double lastObjVal = 1e99;
@@ -91,7 +91,7 @@
<< "%\n";
Log::Info << "\tObjective value: " << Objective() << endl;
- for(u32 t = 1; t <= nIterations && !converged; t++) {
+ for(uword t = 1; t <= nIterations && !converged; t++) {
Log::Info << "Iteration " << t << " of " << nIterations << endl;
Log::Info << "Dictionary Step\n";
@@ -133,7 +133,7 @@
// matGram = trans(matD) * matD;
// }
- for(u32 i = 0; i < nPoints; i++) {
+ for(uword i = 0; i < nPoints; i++) {
// report progress
if((i % 100) == 0) {
Log::Debug << "\t" << i << endl;
@@ -163,15 +163,15 @@
uvec neighborCounts = zeros<uvec>(nPoints, 1);
if(adjacencies.n_elem > 0) {
// this gets the column index
- u32 curPointInd = (u32)(adjacencies(0) / nAtoms);
- u32 curCount = 1;
- for(u32 l = 1; l < adjacencies.n_elem; l++) {
- if((u32)(adjacencies(l) / nAtoms) == curPointInd) {
+ uword curPointInd = (uword)(adjacencies(0) / nAtoms);
+ uword curCount = 1;
+ for(uword l = 1; l < adjacencies.n_elem; l++) {
+ if((uword)(adjacencies(l) / nAtoms) == curPointInd) {
curCount++;
}
else {
neighborCounts(curPointInd) = curCount;
- curPointInd = (u32)(adjacencies(l) / nAtoms);
+ curPointInd = (uword)(adjacencies(l) / nAtoms);
curCount = 1;
}
}
@@ -179,10 +179,10 @@
}
// handle the case of inactive atoms (atoms not used in the given coding)
- std::vector<u32> inactiveAtoms;
- std::vector<u32> activeAtoms;
+ std::vector<uword> inactiveAtoms;
+ std::vector<uword> activeAtoms;
activeAtoms.reserve(nAtoms);
- for(u32 j = 0; j < nAtoms; j++) {
+ for(uword j = 0; j < nAtoms; j++) {
if(accu(matZ.row(j) != 0) == 0) {
inactiveAtoms.push_back(j);
}
@@ -190,8 +190,8 @@
activeAtoms.push_back(j);
}
}
- u32 nActiveAtoms = activeAtoms.size();
- u32 nInactiveAtoms = inactiveAtoms.size();
+ uword nActiveAtoms = activeAtoms.size();
+ uword nInactiveAtoms = inactiveAtoms.size();
// efficient construction of Z restricted to active atoms
mat matActiveZ;
@@ -204,7 +204,7 @@
}
uvec atomReverseLookup = uvec(nAtoms);
- for(u32 i = 0; i < nActiveAtoms; i++) {
+ for(uword i = 0; i < nActiveAtoms; i++) {
atomReverseLookup(activeAtoms[i]) = i;
}
@@ -221,7 +221,7 @@
//vec dualVars = 1e-14 * ones<vec>(nActiveAtoms);
//vec dualVars = 10.0 * randu(nActiveAtoms, 1); // method used by feature sign code - fails miserably here. perhaps the MATLAB optimizer fmincon does something clever?
/*vec dualVars = diagvec(solve(matD, matX * trans(matZ)) - matZ * trans(matZ));
- for(u32 i = 0; i < dualVars.n_elem; i++) {
+ for(uword i = 0; i < dualVars.n_elem; i++) {
if(dualVars(i) < 0) {
dualVars(i) = 0;
}
@@ -232,7 +232,7 @@
bool converged = false;
mat matZXT = matActiveZ * trans(matX);
mat matZZT = matActiveZ * trans(matActiveZ);
- for(u32 t = 1; !converged; t++) {
+ for(uword t = 1; !converged; t++) {
mat A = matZZT + diagmat(dualVars);
mat matAInvZXT = solve(A, matZXT);
@@ -312,10 +312,10 @@
else {
mat matDActiveEstimate = trans(solve(matZZT + diagmat(dualVars), matZXT));
matDEstimate = zeros(nDims, nAtoms);
- for(u32 i = 0; i < nActiveAtoms; i++) {
+ for(uword i = 0; i < nActiveAtoms; i++) {
matDEstimate.col(activeAtoms[i]) = matDActiveEstimate.col(i);
}
- for(u32 i = 0; i < nInactiveAtoms; i++) {
+ for(uword i = 0; i < nInactiveAtoms; i++) {
vec vecmatDi = matDEstimate.unsafe_col(inactiveAtoms[i]);
RandomAtom(vecmatDi);
}
@@ -325,7 +325,7 @@
void SparseCoding::ProjectDictionary() {
- for(u32 j = 0; j < nAtoms; j++) {
+ for(uword j = 0; j < nAtoms; j++) {
double normD_j = norm(matD.col(j), 2);
if(normD_j > 1) {
if(normD_j - 1.0 > 1e-9) {
@@ -367,10 +367,10 @@
void RemoveRows(const mat& X, uvec rows_to_remove, mat& X_mod) {
- u32 n_cols = X.n_cols;
- u32 n_rows = X.n_rows;
- u32 n_to_remove = rows_to_remove.n_elem;
- u32 n_to_keep = n_rows - n_to_remove;
+ uword n_cols = X.n_cols;
+ uword n_rows = X.n_rows;
+ uword n_to_remove = rows_to_remove.n_elem;
+ uword n_to_keep = n_rows - n_to_remove;
if(n_to_remove == 0) {
X_mod = X;
@@ -378,19 +378,19 @@
else {
X_mod.set_size(n_to_keep, n_cols);
- u32 cur_row = 0;
- u32 remove_ind = 0;
+ uword cur_row = 0;
+ uword remove_ind = 0;
// first, check 0 to first row to remove
if(rows_to_remove(0) > 0) {
// note that this implies that n_rows > 1
- u32 height = rows_to_remove(0);
+ uword height = rows_to_remove(0);
X_mod(span(cur_row, cur_row + height - 1), span::all) =
X(span(0, rows_to_remove(0) - 1), span::all);
cur_row += height;
}
// now, check i'th row to remove to (i + 1)'th row to remove, until i = penultimate row
while(remove_ind < n_to_remove - 1) {
- u32 height =
+ uword height =
rows_to_remove[remove_ind + 1]
- rows_to_remove[remove_ind]
- 1;
Modified: mlpack/trunk/src/mlpack/methods/sparse_coding/sparse_coding.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/sparse_coding/sparse_coding.hpp 2012-03-21 02:33:58 UTC (rev 11924)
+++ mlpack/trunk/src/mlpack/methods/sparse_coding/sparse_coding.hpp 2012-03-21 02:34:52 UTC (rev 11925)
@@ -82,8 +82,8 @@
class SparseCoding {
public:
- // void Init(double* memX, u32 nDims, u32 nPoints,
- // u32 nAtoms, double lambda1);
+ // void Init(double* memX, uword nDims, uword nPoints,
+ // uword nAtoms, double lambda1);
//void SetDictionary(double* memD);
@@ -96,7 +96,7 @@
* @param lambda1 Regularization parameter for l1-norm penalty
* @param lambda2 Regularization parameter for l2-norm penalty
*/
- SparseCoding(const arma::mat& matX, arma::u32 nAtoms, double lambda1, double lambda2 = 0);
+ SparseCoding(const arma::mat& matX, arma::uword nAtoms, double lambda1, double lambda2 = 0);
/**
@@ -139,7 +139,7 @@
*
* @param nIterations Maximum number of iterations to run algorithm
*/
- void DoSparseCoding(arma::u32 nIterations);
+ void DoSparseCoding(arma::uword nIterations);
/**
* Sparse code each point via LARS
@@ -191,9 +191,9 @@
void PrintCoding();
private:
- arma::u32 nDims;
- arma::u32 nAtoms;
- arma::u32 nPoints;
+ arma::uword nDims;
+ arma::uword nAtoms;
+ arma::uword nPoints;
// data (columns are points)
arma::mat matX;
Modified: mlpack/trunk/src/mlpack/methods/sparse_coding/sparse_coding_main.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/sparse_coding/sparse_coding_main.cpp 2012-03-21 02:33:58 UTC (rev 11924)
+++ mlpack/trunk/src/mlpack/methods/sparse_coding/sparse_coding_main.cpp 2012-03-21 02:34:52 UTC (rev 11925)
@@ -51,11 +51,11 @@
mat matX;
matX.load(dataFullpath);
- u32 nPoints = matX.n_cols;
+ uword nPoints = matX.n_cols;
printf("Loaded %d points in %d dimensions\n", nPoints, matX.n_rows);
// normalize each point since these are images
- for(u32 i = 0; i < nPoints; i++) {
+ for(uword i = 0; i < nPoints; i++) {
matX.col(i) /= norm(matX.col(i), 2);
}
Modified: mlpack/trunk/src/mlpack/tests/local_coordinate_coding_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/local_coordinate_coding_test.cpp 2012-03-21 02:33:58 UTC (rev 11924)
+++ mlpack/trunk/src/mlpack/tests/local_coordinate_coding_test.cpp 2012-03-21 02:34:52 UTC (rev 11925)
@@ -49,14 +49,14 @@
BOOST_AUTO_TEST_CASE(LocalCoordinateCodingTestCodingStep)
{
double lambda1 = 0.1;
- u32 nAtoms = 25;
+ uword nAtoms = 25;
mat X;
X.load("mnist_first250_training_4s_and_9s.arm");
- u32 nPoints = X.n_cols;
+ uword nPoints = X.n_cols;
// normalize each point since these are images
- for(u32 i = 0; i < nPoints; i++) {
+ for(uword i = 0; i < nPoints; i++) {
X.col(i) /= norm(X.col(i), 2);
}
@@ -67,9 +67,9 @@
mat D = lcc.MatD();
mat Z = lcc.MatZ();
- for(u32 i = 0; i < nPoints; i++) {
+ for(uword i = 0; i < nPoints; i++) {
vec sq_dists = vec(nAtoms);
- for(u32 j = 0; j < nAtoms; j++) {
+ for(uword j = 0; j < nAtoms; j++) {
vec diff = D.unsafe_col(j) - X.unsafe_col(i);
sq_dists[j] = dot(diff, diff);
}
@@ -86,14 +86,14 @@
const double tol = 1e-12;
double lambda = 0.1;
- u32 nAtoms = 25;
+ uword nAtoms = 25;
mat X;
X.load("mnist_first250_training_4s_and_9s.arm");
- u32 nPoints = X.n_cols;
+ uword nPoints = X.n_cols;
// normalize each point since these are images
- for(u32 i = 0; i < nPoints; i++) {
+ for(uword i = 0; i < nPoints; i++) {
X.col(i) /= norm(X.col(i), 2);
}
@@ -108,7 +108,7 @@
mat D = lcc.MatD();
mat grad = zeros(D.n_rows, D.n_cols);
- for(u32 i = 0; i < nPoints; i++) {
+ for(uword i = 0; i < nPoints; i++) {
grad += (D - repmat(X.unsafe_col(i), 1, nAtoms)) * diagmat(abs(Z.unsafe_col(i)));
}
grad = lambda * grad + (D * Z - X) * trans(Z);
Modified: mlpack/trunk/src/mlpack/tests/radical_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/radical_test.cpp 2012-03-21 02:33:58 UTC (rev 11924)
+++ mlpack/trunk/src/mlpack/tests/radical_test.cpp 2012-03-21 02:34:52 UTC (rev 11925)
@@ -29,7 +29,7 @@
mat matYT = trans(matY);
double valEst = 0;
- for(u32 i = 0; i < matYT.n_cols; i++)
+ for(uword i = 0; i < matYT.n_cols; i++)
{
vec y = vec(matYT.col(i));
valEst += rad.Vasicek(y);
@@ -42,7 +42,7 @@
matYT = trans(matY);
double valBest = 0;
- for (u32 i = 0; i < matYT.n_cols; i++)
+ for (uword i = 0; i < matYT.n_cols; i++)
{
vec y = vec(matYT.col(i));
valBest += rad.Vasicek(y);
Modified: mlpack/trunk/src/mlpack/tests/sparse_coding_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/sparse_coding_test.cpp 2012-03-21 02:33:58 UTC (rev 11924)
+++ mlpack/trunk/src/mlpack/tests/sparse_coding_test.cpp 2012-03-21 02:34:52 UTC (rev 11925)
@@ -49,14 +49,14 @@
BOOST_AUTO_TEST_CASE(SparseCodingTestCodingStepLasso)
{
double lambda1 = 0.1;
- u32 nAtoms = 25;
+ uword nAtoms = 25;
mat X;
X.load("mnist_first250_training_4s_and_9s.arm");
- u32 nPoints = X.n_cols;
+ uword nPoints = X.n_cols;
// normalize each point since these are images
- for(u32 i = 0; i < nPoints; i++) {
+ for(uword i = 0; i < nPoints; i++) {
X.col(i) /= norm(X.col(i), 2);
}
@@ -67,7 +67,7 @@
mat D = sc.MatD();
mat Z = sc.MatZ();
- for(u32 i = 0; i < nPoints; i++) {
+ for(uword i = 0; i < nPoints; i++) {
vec errCorr = trans(D) * (D * Z.unsafe_col(i) - X.unsafe_col(i));
VerifyCorrectness(Z.unsafe_col(i), errCorr, lambda1);
}
@@ -78,14 +78,14 @@
{
double lambda1 = 0.1;
double lambda2 = 0.2;
- u32 nAtoms = 25;
+ uword nAtoms = 25;
mat X;
X.load("mnist_first250_training_4s_and_9s.arm");
- u32 nPoints = X.n_cols;
+ uword nPoints = X.n_cols;
// normalize each point since these are images
- for(u32 i = 0; i < nPoints; i++) {
+ for(uword i = 0; i < nPoints; i++) {
X.col(i) /= norm(X.col(i), 2);
}
@@ -96,7 +96,7 @@
mat D = sc.MatD();
mat Z = sc.MatZ();
- for(u32 i = 0; i < nPoints; i++) {
+ for(uword i = 0; i < nPoints; i++) {
vec errCorr =
(trans(D) * D + lambda2 *
eye(nAtoms, nAtoms)) * Z.unsafe_col(i) - trans(D) * X.unsafe_col(i);
@@ -110,14 +110,14 @@
const double tol = 1e-12;
double lambda1 = 0.1;
- u32 nAtoms = 25;
+ uword nAtoms = 25;
mat X;
X.load("mnist_first250_training_4s_and_9s.arm");
- u32 nPoints = X.n_cols;
+ uword nPoints = X.n_cols;
// normalize each point since these are images
- for(u32 i = 0; i < nPoints; i++) {
+ for(uword i = 0; i < nPoints; i++) {
X.col(i) /= norm(X.col(i), 2);
}
More information about the mlpack-svn
mailing list