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

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Tue Dec 13 14:04:20 EST 2011


Author: rcurtin
Date: 2011-12-13 14:04:19 -0500 (Tue, 13 Dec 2011)
New Revision: 10753

Modified:
   mlpack/trunk/src/mlpack/methods/nca/nca.hpp
   mlpack/trunk/src/mlpack/methods/nca/nca_impl.hpp
   mlpack/trunk/src/mlpack/methods/nca/nca_softmax_error_function.hpp
   mlpack/trunk/src/mlpack/methods/nca/nca_softmax_error_function_impl.hpp
Log:
Sparse NCA doesn't actually make sense.


Modified: mlpack/trunk/src/mlpack/methods/nca/nca.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/nca/nca.hpp	2011-12-13 19:00:15 UTC (rev 10752)
+++ mlpack/trunk/src/mlpack/methods/nca/nca.hpp	2011-12-13 19:04:19 UTC (rev 10753)
@@ -33,11 +33,8 @@
  *   title = {{Neighbourhood Components Analysis}},
  *   year = {2004}
  * }
- *
- * @tparam MetricType Distance metric to use.
- * @tparam MatType Type of matrix (arma::mat or arma::spmat).
  */
-template<typename MetricType, typename MatType = arma::mat>
+template<typename Kernel>
 class NCA
 {
  public:
@@ -47,7 +44,7 @@
    *
    * @param dataset Input dataset.
    */
-  NCA(const MatType& dataset, const arma::uvec& labels);
+  NCA(const arma::mat& dataset, const arma::uvec& labels);
 
   /**
    * Perform Neighborhood Components Analysis.  The output distance learning
@@ -55,10 +52,10 @@
    *
    * @param output_matrix Covariance matrix of Mahalanobis distance.
    */
-  void LearnDistance(MatType& output_matrix);
+  void LearnDistance(arma::mat& output_matrix);
 
  private:
-  const MatType& dataset_;
+  const arma::mat& dataset_;
   const arma::uvec& labels_;
 };
 

Modified: mlpack/trunk/src/mlpack/methods/nca/nca_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/nca/nca_impl.hpp	2011-12-13 19:00:15 UTC (rev 10752)
+++ mlpack/trunk/src/mlpack/methods/nca/nca_impl.hpp	2011-12-13 19:04:19 UTC (rev 10753)
@@ -18,25 +18,19 @@
 namespace nca {
 
 // Just set the internal matrix reference.
-template<typename MetricType, typename MatType>
-NCA<MetricType, MatType>::NCA(const MatType& dataset,
-                              const arma::uvec& labels) :
-    dataset_(dataset),
-    labels_(labels)
-{
-  /* nothing to do */
-}
+template<typename Kernel>
+NCA<Kernel>::NCA(const arma::mat& dataset, const arma::uvec& labels) :
+    dataset_(dataset), labels_(labels) { /* nothing to do */ }
 
-template<typename MetricType, typename MatType>
-void NCA<MetricType, MatType>::LearnDistance(MatType& output_matrix)
+template<typename Kernel>
+void NCA<Kernel>::LearnDistance(arma::mat& output_matrix)
 {
-  output_matrix = arma::eye<MatType>(dataset_.n_rows, dataset_.n_rows);
+  output_matrix = arma::eye<arma::mat>(dataset_.n_rows, dataset_.n_rows);
 
-  SoftmaxErrorFunction<MetricType> error_func(dataset_, labels_);
+  SoftmaxErrorFunction<Kernel> error_func(dataset_, labels_);
 
   // We will use the L-BFGS optimizer to optimize the stretching matrix.
-  optimization::L_BFGS<SoftmaxErrorFunction<MetricType, MatType> >
-      lbfgs(error_func, 10);
+  optimization::L_BFGS<SoftmaxErrorFunction<Kernel> > lbfgs(error_func, 10);
 
   lbfgs.Optimize(0, output_matrix);
 }

Modified: mlpack/trunk/src/mlpack/methods/nca/nca_softmax_error_function.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/nca/nca_softmax_error_function.hpp	2011-12-13 19:00:15 UTC (rev 10752)
+++ mlpack/trunk/src/mlpack/methods/nca/nca_softmax_error_function.hpp	2011-12-13 19:04:19 UTC (rev 10753)
@@ -24,30 +24,27 @@
  * where x_n represents a point and A is the current scaling matrix.
  *
  * This class is more flexible than the original paper, allowing an arbitrary
- * metric function to be used, meaning that the Mahalanobis distance is not the
+ * kernel function to be used, meaning that the Mahalanobis distance is not the
  * only allowed way to run NCA.  However, the Mahalanobis distance is probably
  * the best way to use this.
- *
- * @tparam MetricType Type of metric to be used.
- * @tparam MatType Type of matrix (arma::mat or arma::spmat).
  */
-template<typename MetricType, typename MatType = arma::mat>
+template<typename Kernel>
 class SoftmaxErrorFunction
 {
  public:
   /**
-   * Initialize with the given metric; useful when the metric has some state to
-   * store, which is set elsewhere.  If no metric is given, an empty metric is
+   * Initialize with the given kernel; useful when the kernel has some state to
+   * store, which is set elsewhere.  If no kernel is given, an empty kernel is
    * used; this way, you can call the constructor with no arguments.  A
    * reference to the dataset we will be optimizing over is also required.
    *
    * @param dataset Matrix containing the dataset.
    * @param labels Vector of class labels for each point in the dataset.
-   * @param metric Instantiated metric (optional).
+   * @param kernel Instantiated kernel (optional).
    */
-  SoftmaxErrorFunction(const MatType& dataset,
+  SoftmaxErrorFunction(const arma::mat& dataset,
                        const arma::uvec& labels,
-                       MetricType metric = MetricType());
+                       Kernel kernel = Kernel());
 
   /**
    * Evaluate the softmax function for the given covariance matrix.
@@ -71,13 +68,13 @@
   const arma::mat GetInitialPoint() const;
 
  private:
-  const MatType& dataset_;
+  const arma::mat& dataset_;
   const arma::uvec& labels_;
 
-  MetricType metric_;
+  Kernel kernel_;
 
   arma::mat last_coordinates_;
-  MatType stretched_dataset_;
+  arma::mat stretched_dataset_;
   arma::vec p_; // Holds calculated p_i.
   arma::vec denominators_; // Holds denominators for calculation of p_ij.
 

Modified: mlpack/trunk/src/mlpack/methods/nca/nca_softmax_error_function_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/nca/nca_softmax_error_function_impl.hpp	2011-12-13 19:00:15 UTC (rev 10752)
+++ mlpack/trunk/src/mlpack/methods/nca/nca_softmax_error_function_impl.hpp	2011-12-13 19:04:19 UTC (rev 10753)
@@ -13,24 +13,18 @@
 namespace mlpack {
 namespace nca {
 
-// Initialize with the given metric.
-template<typename MetricType, typename MatType>
-SoftmaxErrorFunction<MetricType, MatType>::SoftmaxErrorFunction(
-    const MatType& dataset,
-    const arma::uvec& labels,
-    MetricType metric) :
-    dataset_(dataset),
-    labels_(labels),
-    metric_(metric),
-    last_coordinates_(dataset.n_rows, dataset.n_rows),
-    precalculated_(false)
-{
-  /* nothing to do */
-}
+// Initialize with the given kernel.
+template<typename Kernel>
+SoftmaxErrorFunction<Kernel>::SoftmaxErrorFunction(const arma::mat& dataset,
+                                                   const arma::uvec& labels,
+                                                   Kernel kernel) :
+  dataset_(dataset), labels_(labels), kernel_(kernel),
+  last_coordinates_(dataset.n_rows, dataset.n_rows),
+  precalculated_(false)
+{ /* nothing to do */ }
 
-template<typename MetricType, typename MatType>
-double SoftmaxErrorFunction<MetricType, MatType>::Evaluate(
-    const arma::mat& coordinates)
+template<typename Kernel>
+double SoftmaxErrorFunction<Kernel>::Evaluate(const arma::mat& coordinates)
 {
   // Calculate the denominators and numerators, if necessary.
   Precalculate(coordinates);
@@ -39,10 +33,9 @@
                     // minimizes, not maximizes.
 };
 
-template<typename MetricType, typename MatType>
-void SoftmaxErrorFunction<MetricType, MatType>::Gradient(
-    const arma::mat& coordinates,
-    arma::mat& gradient)
+template<typename Kernel>
+void SoftmaxErrorFunction<Kernel>::Gradient(const arma::mat& coordinates,
+                                            arma::mat& gradient)
 {
   // Calculate the denominators and numerators, if necessary.
   Precalculate(coordinates);
@@ -66,8 +59,8 @@
     for (size_t k = (i + 1); k < stretched_dataset_.n_cols; k++)
     {
       // Calculate p_ik and p_ki first.
-      double eval = exp(-metric_.Evaluate(stretched_dataset_.col(i),
-                                          stretched_dataset_.col(k)));
+      double eval = exp(-kernel_.Evaluate(stretched_dataset_.unsafe_col(i),
+                                          stretched_dataset_.unsafe_col(k)));
       double p_ik = 0, p_ki = 0;
       p_ik = eval / denominators_(i);
       p_ki = eval / denominators_(k);
@@ -87,16 +80,14 @@
   gradient = -2 * coordinates * sum;
 }
 
-template<typename MetricType, typename MatType>
-const arma::mat SoftmaxErrorFunction<MetricType, MatType>::GetInitialPoint()
-    const
+template<typename Kernel>
+const arma::mat SoftmaxErrorFunction<Kernel>::GetInitialPoint() const
 {
   return arma::eye<arma::mat>(dataset_.n_rows, dataset_.n_rows);
 }
 
-template<typename MetricType, typename MatType>
-void SoftmaxErrorFunction<MetricType, MatType>::Precalculate(
-    const arma::mat& coordinates)
+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) &&
@@ -120,8 +111,8 @@
     for (size_t j = (i + 1); j < stretched_dataset_.n_cols; j++)
     {
       // Evaluate exp(-K(x_i, x_j)).
-      double eval = exp(-metric_.Evaluate(stretched_dataset_.col(i),
-                                          stretched_dataset_.col(j)));
+      double eval = exp(-kernel_.Evaluate(stretched_dataset_.unsafe_col(i),
+                                          stretched_dataset_.unsafe_col(j)));
 
       // Add this to the denominators of both i and j: p_ij = p_ji.
       denominators_[i] += eval;




More information about the mlpack-svn mailing list