[mlpack-svn] r10923 - mlpack/trunk/src/mlpack/core/kernels

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Dec 21 14:24:10 EST 2011


Author: ajinkya
Date: 2011-12-21 14:24:09 -0500 (Wed, 21 Dec 2011)
New Revision: 10923

Added:
   mlpack/trunk/src/mlpack/core/kernels/laplacian_kernel.hpp
Modified:
   mlpack/trunk/src/mlpack/core/kernels/CMakeLists.txt
Log:
laplacian kernel


Modified: mlpack/trunk/src/mlpack/core/kernels/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/CMakeLists.txt	2011-12-21 19:17:38 UTC (rev 10922)
+++ mlpack/trunk/src/mlpack/core/kernels/CMakeLists.txt	2011-12-21 19:24:09 UTC (rev 10923)
@@ -8,6 +8,7 @@
   cosine_distance.hpp
   cosine_distance_impl.hpp
   exponential_kernel.hpp
+  laplacian_kernel.hpp
 )
 
 # add directory name to sources

Added: mlpack/trunk/src/mlpack/core/kernels/laplacian_kernel.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/laplacian_kernel.hpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/core/kernels/laplacian_kernel.hpp	2011-12-21 19:24:09 UTC (rev 10923)
@@ -0,0 +1,92 @@
+/**
+ * @file laplacian_kernel.hpp
+ * @author Ajinkya Kale <kaleajinkya at gmail.com>
+ *
+ * Implementation of the Laplacian kernel (LaplacianKernel).
+ */
+#ifndef __MLPACK_CORE_KERNELS_LAPLACIAN_KERNEL_HPP
+#define __MLPACK_CORE_KERNELS_LAPLACIAN_KERNEL_HPP
+
+#include <mlpack/core.hpp>
+
+namespace mlpack {
+namespace kernel {
+
+/**
+ * The standard Laplacian kernel.  Given two vectors @f$ x @f$, @f$ y @f$, and a
+ * bandwidth @f$ \mu @f$ (set in the constructor),
+ *
+ * @f[
+ * K(x, y) = \exp(-\frac{|| x - y ||}{ \mu}).
+ * @f]
+ *
+ * The implementation is all in the header file because it is so simple.
+ */
+class LaplacianKernel
+{
+ public:
+  /**
+   * Default constructor; sets bandwidth to 1.0.
+   */
+  LaplacianKernel() : bandwidth(1.0), gamma(-1.0)
+  { }
+
+  /**
+   * Construct the Laplacian kernel with a custom bandwidth.
+   *
+   * @param bandwidth The bandwidth of the kernel (@f$\mu at f$).
+   */
+  LaplacianKernel(double bandwidth) :
+    bandwidth(bandwidth),
+    gamma(-pow(bandwidth, -1.0))
+  { }
+
+  /**
+   * Evaluation of the Laplacian kernel.  This could be generalized to use any
+   * distance metric, not the Euclidean distance, but for now, the Euclidean
+   * distance is used.
+   *
+   * @tparam VecType Type of vector (likely arma::vec or arma::spvec).
+   * @param a First vector.
+   * @param b Second vector.
+   * @return K(a, b) using the bandwidth (@f$\mu at f$) specified in the
+   *   constructor.
+   */
+  template<typename VecType>
+  double Evaluate(const VecType& a, const VecType& b) const
+  {
+    // The precalculation of gamma saves us a little computation time.
+    return exp(gamma * sqrt(metric::SquaredEuclideanDistance::Evaluate(a, b)));
+  }
+
+  /**
+   * Evaluation of the Laplacian kernel using a double precision argument.
+   *
+   * @param t double value.
+   * @return K(t) using the bandwidth (@f$\mu at f$) specified in the
+   *     constructor.
+   */
+  double Evaluate(double t) const
+  {
+    // The precalculation of gamma saves us a little computation time.
+    return exp(gamma * t);
+  }
+
+  //! Get the bandwidth.
+  const double& Bandwidth() const { return bandwidth; }
+  //! Get the precalculated constant.
+  const double& Gamma() const { return gamma; }
+
+ private:
+  //! Kernel bandwidth.
+  double bandwidth;
+
+  //! Precalculated constant depending on the bandwidth;
+  //! @f$ \gamma = -\frac{1}{ \mu} @f$.
+  double gamma;
+};
+
+}; // namespace kernel
+}; // namespace mlpack
+
+#endif




More information about the mlpack-svn mailing list