[mlpack-svn] r10071 - mlpack/trunk/src/mlpack/methods/nca

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Sat Oct 29 01:40:51 EDT 2011


Author: rcurtin
Date: 2011-10-29 01:40:50 -0400 (Sat, 29 Oct 2011)
New Revision: 10071

Modified:
   mlpack/trunk/src/mlpack/methods/nca/nca_softmax_error_function.h
   mlpack/trunk/src/mlpack/methods/nca/nca_softmax_error_function_impl.h
Log:
Corner case: last_coordinates_ may happen to be initialized to what the user
passes in as the coordinates; and in that case, the Precalculate() doesn't
actually do its job.  So, make sure a precalculation is done if one has never
been done before.


Modified: mlpack/trunk/src/mlpack/methods/nca/nca_softmax_error_function.h
===================================================================
--- mlpack/trunk/src/mlpack/methods/nca/nca_softmax_error_function.h	2011-10-29 04:27:01 UTC (rev 10070)
+++ mlpack/trunk/src/mlpack/methods/nca/nca_softmax_error_function.h	2011-10-29 05:40:50 UTC (rev 10071)
@@ -77,6 +77,9 @@
   arma::vec p_; // Holds calculated p_i.
   arma::vec denominators_; // Holds denominators for calculation of p_ij.
 
+  //! False is nothing has ever been precalculated (only at construction time).
+  bool precalculated_;
+
   /**
    * Precalculate the denominators and numerators that will make up the p_ij,
    * but only if the coordinates matrix is different than the last coordinates

Modified: mlpack/trunk/src/mlpack/methods/nca/nca_softmax_error_function_impl.h
===================================================================
--- mlpack/trunk/src/mlpack/methods/nca/nca_softmax_error_function_impl.h	2011-10-29 04:27:01 UTC (rev 10070)
+++ mlpack/trunk/src/mlpack/methods/nca/nca_softmax_error_function_impl.h	2011-10-29 05:40:50 UTC (rev 10071)
@@ -18,7 +18,8 @@
                                                    const arma::uvec& labels,
                                                    Kernel kernel) :
   dataset_(dataset), labels_(labels), kernel_(kernel),
-  last_coordinates_(dataset.n_rows, dataset.n_rows)
+  last_coordinates_(dataset.n_rows, dataset.n_rows),
+  precalculated_(false)
 { /* nothing to do */ }
 
 template<typename Kernel>
@@ -82,7 +83,8 @@
 template<typename Kernel>
 void SoftmaxErrorFunction<Kernel>::Precalculate(const arma::mat& coordinates) {
   // Make sure the calculation is necessary.
-  if (accu(coordinates == last_coordinates_) == coordinates.n_elem)
+  if ((accu(coordinates == last_coordinates_) == coordinates.n_elem) &&
+      precalculated_)
     return; // No need to calculate; we already have this stuff saved.
 
   // Coordinates are different; save the new ones, and stretch the dataset.
@@ -128,6 +130,9 @@
       p_[i] = 0;
     }
   }
+
+  // We've done a precalculation.  Mark it as done.
+  precalculated_ = true;
 }
 
 }; // namespace nca




More information about the mlpack-svn mailing list