[mlpack-svn] r13112 - mlpack/trunk/src/mlpack/methods/sparse_coding

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Tue Jun 26 16:00:21 EDT 2012


Author: rcurtin
Date: 2012-06-26 16:00:21 -0400 (Tue, 26 Jun 2012)
New Revision: 13112

Modified:
   mlpack/trunk/src/mlpack/methods/sparse_coding/sparse_coding_main.cpp
Log:
Avoid unnecessary initialization of dictionary when the user passes a dictionary
already.


Modified: mlpack/trunk/src/mlpack/methods/sparse_coding/sparse_coding_main.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/sparse_coding/sparse_coding_main.cpp	2012-06-26 19:55:44 UTC (rev 13111)
+++ mlpack/trunk/src/mlpack/methods/sparse_coding/sparse_coding_main.cpp	2012-06-26 20:00:21 UTC (rev 13112)
@@ -95,7 +95,7 @@
   Log::Info << "Loaded " << matX.n_cols << " points in " << matX.n_rows <<
       " dimensions." << endl;
 
-  // Normalize each point since these are images.
+  // Normalize each point if the user asked for it.
   if (normalize)
   {
     Log::Info << "Normalizing data before coding..." << std::endl;
@@ -103,14 +103,15 @@
       matX.col(i) /= norm(matX.col(i), 2);
   }
 
-  // Run the sparse coding algorithm.
-  SparseCoding<> sc(matX, atoms, lambda1, lambda2);
-
+  // If there is an initial dictionary, be sure we do not initialize one.
   if (initialDictionaryFile != "")
   {
+    SparseCoding<NothingInitializer> sc(matX, atoms, lambda1, lambda2);
+
     // Load initial dictionary directly into sparse coding object.
     data::Load(initialDictionaryFile, sc.Dictionary(), true);
 
+    // Validate size of initial dictionary.
     if (sc.Dictionary().n_cols != atoms)
     {
       Log::Fatal << "The specified initial dictionary to load has "
@@ -124,18 +125,32 @@
           << sc.Dictionary().n_rows << " dimensions, but the specified data "
           << "has " << matX.n_rows << " dimensions!" << endl;
     }
-  }
 
-  Timer::Start("sparse_coding");
-  sc.Encode(maxIterations);
-  Timer::Stop("sparse_coding");
+    // Run sparse coding.
+    Timer::Start("sparse_coding");
+    sc.Encode(maxIterations);
+    Timer::Stop("sparse_coding");
 
-  // Save the results.
-  Log::Info << "Saving dictionary matrix to '" << dictionaryFile << "'.\n";
+    // Save the results.
+    Log::Info << "Saving dictionary matrix to '" << dictionaryFile << "'.\n";
+    data::Save(dictionaryFile, sc.Dictionary());
+    Log::Info << "Saving sparse codes to '" << codesFile << "'.\n";
+    data::Save(codesFile, sc.Codes());
+  }
+  else
+  {
+    // No initial dictionary.
+    SparseCoding<> sc(matX, atoms, lambda1, lambda2);
 
-  data::Save(dictionaryFile, sc.Dictionary());
+    // Run sparse coding.
+    Timer::Start("sparse_coding");
+    sc.Encode(maxIterations);
+    Timer::Stop("sparse_coding");
 
-  Log::Info << "Saving sparse codes to '" << codesFile << "'.\n";
-
-  data::Save(codesFile, sc.Codes());
+    // Save the results.
+    Log::Info << "Saving dictionary matrix to '" << dictionaryFile << "'.\n";
+    data::Save(dictionaryFile, sc.Dictionary());
+    Log::Info << "Saving sparse codes to '" << codesFile << "'.\n";
+    data::Save(codesFile, sc.Codes());
+  }
 }




More information about the mlpack-svn mailing list