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

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Tue Feb 28 00:13:50 EST 2012


Author: rcurtin
Date: 2012-02-28 00:13:50 -0500 (Tue, 28 Feb 2012)
New Revision: 11619

Modified:
   mlpack/trunk/src/mlpack/core/kernels/gaussian_kernel.hpp
   mlpack/trunk/src/mlpack/core/kernels/hyperbolic_tangent_kernel.hpp
   mlpack/trunk/src/mlpack/core/kernels/laplacian_kernel.hpp
Log:
Some cleanup for kernels.


Modified: mlpack/trunk/src/mlpack/core/kernels/gaussian_kernel.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/gaussian_kernel.hpp	2012-02-28 04:07:51 UTC (rev 11618)
+++ mlpack/trunk/src/mlpack/core/kernels/gaussian_kernel.hpp	2012-02-28 05:13:50 UTC (rev 11619)
@@ -10,6 +10,7 @@
 #define __MLPACK_CORE_KERNELS_GAUSSIAN_KERNEL_HPP
 
 #include <mlpack/core.hpp>
+#include <mlpack/core/metrics/lmetric.hpp>
 
 namespace mlpack {
 namespace kernel {
@@ -39,8 +40,8 @@
    * @param bandwidth The bandwidth of the kernel (@f$\mu at f$).
    */
   GaussianKernel(double bandwidth) :
-    bandwidth(bandwidth),
-    gamma(-0.5 * pow(bandwidth, -2.0))
+      bandwidth(bandwidth),
+      gamma(-0.5 * pow(bandwidth, -2.0))
   { }
 
   /**
@@ -75,9 +76,17 @@
   }
 
   //! Get the bandwidth.
-  const double& Bandwidth() const { return bandwidth; }
+  double Bandwidth() const { return bandwidth; }
+  //! Modify the bandwidth.  This takes an argument because we must update the
+  //! precalculated constant (gamma).
+  void Bandwidth(const double bandwidth)
+  {
+    this->bandwidth = bandwidth;
+    this->gamma = -0.5 * pow(bandwidth, -2.0);
+  }
+
   //! Get the precalculated constant.
-  const double& Gamma() const { return gamma; }
+  double Gamma() const { return gamma; }
 
  private:
   //! Kernel bandwidth.

Modified: mlpack/trunk/src/mlpack/core/kernels/hyperbolic_tangent_kernel.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/hyperbolic_tangent_kernel.hpp	2012-02-28 04:07:51 UTC (rev 11618)
+++ mlpack/trunk/src/mlpack/core/kernels/hyperbolic_tangent_kernel.hpp	2012-02-28 05:13:50 UTC (rev 11619)
@@ -25,26 +25,24 @@
 {
  public:
   /**
-   * This constructor sets default scale to 1.0 and offset to 0.0
+   * This constructor sets the default scale to 1.0 and offset to 0.0.
    */
-  HyperbolicTangentKernel() :
-    scale(1.0),
-    offset(0.0)
+  HyperbolicTangentKernel() : scale(1.0), offset(0.0)
   { }
 
   /**
-   * Construct Hyperbolic Tangent Kernel with custom scale factor and offset
+   * Construct the hyperbolic tangent kernel with custom scale factor and
+   * offset.
    *
-   * @param scale scaling factor for <x, y>
-   * @param offset kernel offset
+   * @param scale Scaling factor for <x, y>.
+   * @param offset Kernel offset.
    */
   HyperbolicTangentKernel(double scale, double offset) :
-    scale(scale),
-    offset(offset)
+      scale(scale), offset(offset)
   { }
 
   /**
-   * Evaluation of Hyperbolic Tangent Kernel.  This evaluation uses Armadillo's
+   * Evaluate the hyperbolic tangent kernel.  This evaluation uses Armadillo's
    * dot() function.
    *
    * @tparam VecType Type of vector (should be arma::vec or arma::spvec).
@@ -58,11 +56,16 @@
     return tanh(scale * arma::dot(a, b) + offset);
   }
 
-  //Get scale factor
-  const double& Scale() const { return scale; }
-  //Get offset for the kernel
-  const double& Offset() const { return offset; }
+  //! Get scale factor.
+  double Scale() const { return scale; }
+  //! Modify scale factor.
+  double& Scale() { return scale; }
 
+  //! Get offset for the kernel.
+  double Offset() const { return offset; }
+  //! Modify offset for the kernel.
+  double& Offset() { return offset; }
+
  private:
   double scale;
   double offset;

Modified: mlpack/trunk/src/mlpack/core/kernels/laplacian_kernel.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/laplacian_kernel.hpp	2012-02-28 04:07:51 UTC (rev 11618)
+++ mlpack/trunk/src/mlpack/core/kernels/laplacian_kernel.hpp	2012-02-28 05:13:50 UTC (rev 11619)
@@ -17,7 +17,7 @@
  * bandwidth @f$ \mu @f$ (set in the constructor),
  *
  * @f[
- * K(x, y) = \exp(-\frac{|| x - y ||}{ \mu}).
+ * K(x, y) = \exp(-\frac{|| x - y ||}{\mu}).
  * @f]
  *
  * The implementation is all in the header file because it is so simple.
@@ -28,7 +28,7 @@
   /**
    * Default constructor; sets bandwidth to 1.0.
    */
-  LaplacianKernel() : bandwidth(1.0), gamma(-1.0)
+  LaplacianKernel() : bandwidth(1.0)
   { }
 
   /**
@@ -37,8 +37,7 @@
    * @param bandwidth The bandwidth of the kernel (@f$\mu at f$).
    */
   LaplacianKernel(double bandwidth) :
-    bandwidth(bandwidth),
-    gamma(-pow(bandwidth, -1.0))
+      bandwidth(bandwidth)
   { }
 
   /**
@@ -56,7 +55,7 @@
   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)));
+    return exp(-metric::EuclideanDistance::Evaluate(a, b) / bandwidth);
   }
 
   /**
@@ -69,21 +68,17 @@
   double Evaluate(double t) const
   {
     // The precalculation of gamma saves us a little computation time.
-    return exp(gamma * t);
+    return exp(-t / bandwidth);
   }
 
   //! Get the bandwidth.
-  const double& Bandwidth() const { return bandwidth; }
-  //! Get the precalculated constant.
-  const double& Gamma() const { return gamma; }
+  double Bandwidth() const { return bandwidth; }
+  //! Modify the bandwidth.
+  double& Bandwidth() { return bandwidth; }
 
  private:
   //! Kernel bandwidth.
   double bandwidth;
-
-  //! Precalculated constant depending on the bandwidth;
-  //! @f$ \gamma = -\frac{1}{ \mu} @f$.
-  double gamma;
 };
 
 }; // namespace kernel




More information about the mlpack-svn mailing list