[mlpack-svn] r13015 - mlpack/trunk/src/mlpack/core/data

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Jun 11 13:35:52 EDT 2012


Author: rcurtin
Date: 2012-06-11 13:35:52 -0400 (Mon, 11 Jun 2012)
New Revision: 13015

Modified:
   mlpack/trunk/src/mlpack/core/data/load.hpp
   mlpack/trunk/src/mlpack/core/data/load_impl.hpp
   mlpack/trunk/src/mlpack/core/data/save.hpp
   mlpack/trunk/src/mlpack/core/data/save_impl.hpp
Log:
Add option allowing user to specify if they want the data transposed on input;
this is useful for LARS.


Modified: mlpack/trunk/src/mlpack/core/data/load.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/data/load.hpp	2012-06-11 15:30:11 UTC (rev 13014)
+++ mlpack/trunk/src/mlpack/core/data/load.hpp	2012-06-11 17:35:52 UTC (rev 13015)
@@ -35,15 +35,23 @@
  * This is preferable to Armadillo's default behavior of loading an unknown
  * filetype as raw_binary, which can have very confusing effects.
  *
+ * If the parameter 'fatal' is set to true, the program will exit with an error
+ * if the matrix does not load successfully.  The parameter 'transpose' controls
+ * whether or not the matrix is transposed after loading.  In most cases,
+ * because data is generally stored in a row-major format and MLPACK requires
+ * column-major matrices, this should be left at its default value of 'true'.
+ *
  * @param filename Name of file to load.
  * @param matrix Matrix to load contents of file into.
  * @param fatal If an error should be reported as fatal (default false).
+ * @param transpose If true, transpose the matrix after loading.
  * @return Boolean value indicating success or failure of load.
  */
 template<typename eT>
 bool Load(const std::string& filename,
           arma::Mat<eT>& matrix,
-          bool fatal = false);
+          bool fatal = false,
+          bool transpose = true);
 
 }; // namespace data
 }; // namespace mlpack

Modified: mlpack/trunk/src/mlpack/core/data/load_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/data/load_impl.hpp	2012-06-11 15:30:11 UTC (rev 13014)
+++ mlpack/trunk/src/mlpack/core/data/load_impl.hpp	2012-06-11 17:35:52 UTC (rev 13015)
@@ -14,7 +14,10 @@
 namespace data {
 
 template<typename eT>
-bool Load(const std::string& filename, arma::Mat<eT>& matrix, bool fatal)
+bool Load(const std::string& filename,
+          arma::Mat<eT>& matrix,
+          bool fatal,
+          bool transpose)
 {
   // First we will try to discriminate by file extension.
   size_t ext = filename.rfind('.');
@@ -161,8 +164,9 @@
       Log::Warn << "Loading from '" << filename << "' failed." << std::endl;
   }
 
-  // Now transpose the matrix.
-  matrix = trans(matrix);
+  // Now transpose the matrix, if necessary.
+  if (transpose)
+    matrix = trans(matrix);
 
   // Finally, return the success indicator.
   return success;

Modified: mlpack/trunk/src/mlpack/core/data/save.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/data/save.hpp	2012-06-11 15:30:11 UTC (rev 13014)
+++ mlpack/trunk/src/mlpack/core/data/save.hpp	2012-06-11 17:35:52 UTC (rev 13015)
@@ -31,17 +31,24 @@
  *  - Raw binary (raw_binary), denoted by .bin
  *  - Armadillo binary (arma_binary), denoted by .bin
  *
- * If the file extension is not one of those types, an error will be given.
+ * If the file extension is not one of those types, an error will be given.  If
+ * the 'fatal' parameter is set to true, an error will cause the program to
+ * exit.  If the 'transpose' parameter is set to true, the matrix will be
+ * transposed before saving.  Generally, because MLPACK stores matrices in a
+ * column-major format and most datasets are stored on disk as row-major, this
+ * parameter should be left at its default value of 'true'.
  *
  * @param filename Name of file to save to.
  * @param matrix Matrix to save into file.
  * @param fatal If an error should be reported as fatal (default false).
+ * @param transpose If true, transpose the matrix before saving.
  * @return Boolean value indicating success or failure of save.
  */
 template<typename eT>
 bool Save(const std::string& filename,
           const arma::Mat<eT>& matrix,
-          bool fatal = false);
+          bool fatal = false,
+          bool transpose = true);
 
 }; // namespace data
 }; // namespace mlpack

Modified: mlpack/trunk/src/mlpack/core/data/save_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/data/save_impl.hpp	2012-06-11 15:30:11 UTC (rev 13014)
+++ mlpack/trunk/src/mlpack/core/data/save_impl.hpp	2012-06-11 17:35:52 UTC (rev 13015)
@@ -14,7 +14,10 @@
 namespace data {
 
 template<typename eT>
-bool Save(const std::string& filename, const arma::Mat<eT>& matrix, bool fatal)
+bool Save(const std::string& filename,
+          const arma::Mat<eT>& matrix,
+          bool fatal,
+          bool transpose)
 {
   // First we will try to discriminate by file extension.
   size_t ext = filename.rfind('.');
@@ -96,16 +99,31 @@
       << std::endl;
 
   // Transpose the matrix.
-  arma::Mat<eT> tmp = trans(matrix);
+  if (transpose)
+  {
+    arma::Mat<eT> tmp = trans(matrix);
 
-  if (!tmp.quiet_save(stream, saveType))
+    if (!tmp.quiet_save(stream, saveType))
+    {
+      if (fatal)
+        Log::Fatal << "Save to '" << filename << "' failed." << std::endl;
+      else
+        Log::Warn << "Save to '" << filename << "' failed." << std::endl;
+
+      return false;
+    }
+  }
+  else
   {
-    if (fatal)
-      Log::Fatal << "Save to '" << filename << "' failed." << std::endl;
-    else
-      Log::Warn << "Save to '" << filename << "' failed." << std::endl;
+    if (!matrix.quiet_save(stream, saveType))
+    {
+      if (fatal)
+        Log::Fatal << "Save to '" << filename << "' failed." << std::endl;
+      else
+        Log::Warn << "Save to '" << filename << "' failed." << std::endl;
 
-    return false;
+      return false;
+    }
   }
 
   // Finally return success.




More information about the mlpack-svn mailing list