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

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Thu Sep 12 14:29:42 EDT 2013


Author: rcurtin
Date: Thu Sep 12 14:29:42 2013
New Revision: 15766

Log:
Add KernelTraits, a useful template class that can tell you about things, like
whether or not your kernel is normalized.  This is useful for FastMKS, and will
probably be useful for other things later too.


Added:
   mlpack/trunk/src/mlpack/core/kernels/kernel_traits.hpp
Modified:
   mlpack/trunk/src/mlpack/core/kernels/CMakeLists.txt
   mlpack/trunk/src/mlpack/core/kernels/cosine_distance.hpp
   mlpack/trunk/src/mlpack/core/kernels/epanechnikov_kernel.hpp
   mlpack/trunk/src/mlpack/core/kernels/gaussian_kernel.hpp
   mlpack/trunk/src/mlpack/core/kernels/laplacian_kernel.hpp
   mlpack/trunk/src/mlpack/core/kernels/spherical_kernel.hpp
   mlpack/trunk/src/mlpack/core/kernels/triangular_kernel.hpp

Modified: mlpack/trunk/src/mlpack/core/kernels/CMakeLists.txt
==============================================================================
--- mlpack/trunk/src/mlpack/core/kernels/CMakeLists.txt	(original)
+++ mlpack/trunk/src/mlpack/core/kernels/CMakeLists.txt	Thu Sep 12 14:29:42 2013
@@ -9,6 +9,7 @@
   example_kernel.hpp
   gaussian_kernel.hpp
   hyperbolic_tangent_kernel.hpp
+  kernel_traits.hpp
   laplacian_kernel.hpp
   linear_kernel.hpp
   polynomial_kernel.hpp

Modified: mlpack/trunk/src/mlpack/core/kernels/cosine_distance.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/kernels/cosine_distance.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/kernels/cosine_distance.hpp	Thu Sep 12 14:29:42 2013
@@ -36,6 +36,15 @@
   static double Evaluate(const VecType& a, const VecType& b);
 };
 
+//! Kernel traits for the cosine distance.
+template<>
+class KernelTraits<CosineDistance>
+{
+ public:
+  //! The cosine kernel is normalized: K(x, x) = 1 for all x.
+  static const bool IsNormalized = true;
+};
+
 }; // namespace kernel
 }; // namespace mlpack
 

Modified: mlpack/trunk/src/mlpack/core/kernels/epanechnikov_kernel.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/kernels/epanechnikov_kernel.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/kernels/epanechnikov_kernel.hpp	Thu Sep 12 14:29:42 2013
@@ -74,6 +74,15 @@
   double inverseBandwidthSquared;
 };
 
+//! Kernel traits for the Epanechnikov kernel.
+template<>
+class KernelTraits<EpanechnikovKernel>
+{
+ public:
+  //! The Epanechnikov kernel is normalized: K(x, x) = 1 for all x.
+  static const bool IsNormalized = true;
+};
+
 }; // namespace kernel
 }; // namespace mlpack
 

Modified: mlpack/trunk/src/mlpack/core/kernels/gaussian_kernel.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/kernels/gaussian_kernel.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/kernels/gaussian_kernel.hpp	Thu Sep 12 14:29:42 2013
@@ -122,6 +122,15 @@
   double gamma;
 };
 
+//! Kernel traits for the Gaussian kernel.
+template<>
+class KernelTraits<GaussianKernel>
+{
+ public:
+  //! The Gaussian kernel is normalized: K(x, x) = 1 for all x.
+  static const bool IsNormalized = true;
+};
+
 }; // namespace kernel
 }; // namespace mlpack
 

Added: mlpack/trunk/src/mlpack/core/kernels/kernel_traits.hpp
==============================================================================
--- (empty file)
+++ mlpack/trunk/src/mlpack/core/kernels/kernel_traits.hpp	Thu Sep 12 14:29:42 2013
@@ -0,0 +1,34 @@
+/**
+ * @file kernel_traits.hpp
+ * @author Ryan Curtin
+ *
+ * This provides the KernelTraits class, a template class to get information
+ * about various kernels.
+ */
+#ifndef __MLPACK_CORE_KERNELS_KERNEL_TRAITS_HPP
+#define __MLPACK_CORE_KERNELS_KERNEL_TRAITS_HPP
+
+namespace mlpack {
+namespace kernel {
+
+/**
+ * This is a template class that can provide information about various kernels.
+ * By default, this class will provide the weakest possible assumptions on
+ * kernels, and each kernel should override values as necessary.  If a kernel
+ * doesn't need to override a value, then there's no need to write a
+ * KernelTraits specialization for that class.
+ */
+template<typename KernelType>
+class KernelTraits
+{
+ public:
+  /**
+   * If true, then the kernel is normalized: K(x, x) = K(y, y) = 1 for all x.
+   */
+  static const bool IsNormalized = false;
+};
+
+}; // namespace kernel
+}; // namespace mlpack
+
+#endif

Modified: mlpack/trunk/src/mlpack/core/kernels/laplacian_kernel.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/kernels/laplacian_kernel.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/kernels/laplacian_kernel.hpp	Thu Sep 12 14:29:42 2013
@@ -81,6 +81,15 @@
   double bandwidth;
 };
 
+//! Kernel traits of the Laplacian kernel.
+template<>
+class KernelTraits<LaplacianKernel>
+{
+ public:
+  //! The Laplacian kernel is normalized: K(x, x) = 1 for all x.
+  static const bool IsNormalized = true;
+};
+
 }; // namespace kernel
 }; // namespace mlpack
 

Modified: mlpack/trunk/src/mlpack/core/kernels/spherical_kernel.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/kernels/spherical_kernel.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/kernels/spherical_kernel.hpp	Thu Sep 12 14:29:42 2013
@@ -79,11 +79,21 @@
   {
     return (t <= bandwidth) ? 1.0 : 0.0;
   }
+
  private:
   double bandwidth;
   double bandwidthSquared;
 };
 
+//! Kernel traits for the spherical kernel.
+template<>
+class KernelTraits<SphericalKernel>
+{
+ public:
+  //! The spherical kernel is normalized: K(x, x) = 1 for all x.
+  static const bool IsNormalized = true;
+};
+
 }; // namespace kernel
 }; // namespace mlpack
 

Modified: mlpack/trunk/src/mlpack/core/kernels/triangular_kernel.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/kernels/triangular_kernel.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/kernels/triangular_kernel.hpp	Thu Sep 12 14:29:42 2013
@@ -55,6 +55,15 @@
   double bandwidth;
 };
 
+//! Kernel traits for the triangular kernel.
+template<>
+class KernelTraits<TriangularKernel>
+{
+ public:
+  //! The triangular kernel is normalized: K(x, x) = 1 for all x.
+  static const bool IsNormalized = true;
+};
+
 }; // namespace kernel
 }; // namespace mlpack
 



More information about the mlpack-svn mailing list