[mlpack-svn] r10740 - mlpack/trunk/src/mlpack/core/kernels
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Tue Dec 13 04:20:15 EST 2011
Author: rcurtin
Date: 2011-12-13 04:20:14 -0500 (Tue, 13 Dec 2011)
New Revision: 10740
Added:
mlpack/trunk/src/mlpack/core/kernels/cosine_distance_impl.hpp
Removed:
mlpack/trunk/src/mlpack/core/kernels/cosine_distance.cpp
Modified:
mlpack/trunk/src/mlpack/core/kernels/CMakeLists.txt
mlpack/trunk/src/mlpack/core/kernels/cosine_distance.hpp
mlpack/trunk/src/mlpack/core/kernels/example_kernel.hpp
mlpack/trunk/src/mlpack/core/kernels/gaussian_kernel.hpp
mlpack/trunk/src/mlpack/core/kernels/linear_kernel.hpp
Log:
Abstractize vector type.
Modified: mlpack/trunk/src/mlpack/core/kernels/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/CMakeLists.txt 2011-12-13 08:59:08 UTC (rev 10739)
+++ mlpack/trunk/src/mlpack/core/kernels/CMakeLists.txt 2011-12-13 09:20:14 UTC (rev 10740)
@@ -6,7 +6,7 @@
gaussian_kernel.hpp
linear_kernel.hpp
cosine_distance.hpp
- cosine_distance.cpp
+ cosine_distance_impl.hpp
)
# add directory name to sources
Deleted: mlpack/trunk/src/mlpack/core/kernels/cosine_distance.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/cosine_distance.cpp 2011-12-13 08:59:08 UTC (rev 10739)
+++ mlpack/trunk/src/mlpack/core/kernels/cosine_distance.cpp 2011-12-13 09:20:14 UTC (rev 10740)
@@ -1,17 +0,0 @@
-/**
- * @file cosine_distance.cpp
- * @author Ryan Curtin
- *
- * This implements the cosine distance.
- */
-#include "cosine_distance.hpp"
-
-using namespace mlpack;
-using namespace mlpack::kernel;
-using namespace arma;
-
-double CosineDistance::Evaluate(const arma::vec& a, const arma::vec& b)
-{
- // Since we are using the L2 inner product, this is easy.
- return 1 - dot(a, b) / (norm(a, 2) * norm(b, 2));
-}
Modified: mlpack/trunk/src/mlpack/core/kernels/cosine_distance.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/cosine_distance.hpp 2011-12-13 08:59:08 UTC (rev 10739)
+++ mlpack/trunk/src/mlpack/core/kernels/cosine_distance.hpp 2011-12-13 09:20:14 UTC (rev 10740)
@@ -39,10 +39,14 @@
* @param b Second vector.
* @return d(a, b).
*/
- static double Evaluate(const arma::vec& a, const arma::vec& b);
+ template<typename VecType>
+ static double Evaluate(const VecType& a, const VecType& b);
};
}; // namespace kernel
}; // namespace mlpack
+// Include implementation.
+#include "cosine_distance_impl.hpp"
+
#endif
Copied: mlpack/trunk/src/mlpack/core/kernels/cosine_distance_impl.hpp (from rev 10737, mlpack/trunk/src/mlpack/core/kernels/cosine_distance.cpp)
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/cosine_distance_impl.hpp (rev 0)
+++ mlpack/trunk/src/mlpack/core/kernels/cosine_distance_impl.hpp 2011-12-13 09:20:14 UTC (rev 10740)
@@ -0,0 +1,25 @@
+/**
+ * @file cosine_distance_impl.hpp
+ * @author Ryan Curtin
+ *
+ * This implements the cosine distance.
+ */
+#ifndef __MLPACK_CORE_KERNELS_COSINE_DISTANCE_IMPL_HPP
+#define __MLPACK_CORE_KERNELS_COSINE_DISTANCE_IMPL_HPP
+
+#include "cosine_distance.hpp"
+
+namespace mlpack {
+namespace kernel {
+
+template<typename VecType>
+double CosineDistance::Evaluate(const VecType& a, const VecType& b)
+{
+ // Since we are using the L2 inner product, this is easy.
+ return 1 - dot(a, b) / (norm(a, 2) * norm(b, 2));
+}
+
+}; // namespace kernel
+}; // namespace mlpack
+
+#endif
Modified: mlpack/trunk/src/mlpack/core/kernels/example_kernel.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/example_kernel.hpp 2011-12-13 08:59:08 UTC (rev 10739)
+++ mlpack/trunk/src/mlpack/core/kernels/example_kernel.hpp 2011-12-13 09:20:14 UTC (rev 10740)
@@ -93,11 +93,13 @@
* function static. For a more complex example which cannot be declared
* static, see the GaussianKernel, which stores an internal parameter.
*
+ * @tparam VecType Type of vector (arma::vec, arma::spvec should be expected).
* @param a First vector.
* @param b Second vector.
* @return K(a, b).
*/
- static double Evaluate(const arma::vec& a, const arma::vec& b) { return 0; }
+ template<typename VecType>
+ static double Evaluate(const VecType& a, const VecType& b) { return 0; }
};
}; // namespace kernel
Modified: mlpack/trunk/src/mlpack/core/kernels/gaussian_kernel.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/gaussian_kernel.hpp 2011-12-13 08:59:08 UTC (rev 10739)
+++ mlpack/trunk/src/mlpack/core/kernels/gaussian_kernel.hpp 2011-12-13 09:20:14 UTC (rev 10740)
@@ -48,16 +48,17 @@
* 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$\bandwidth at f$) specified in the
* constructor.
*/
- double Evaluate(const arma::vec& a, const arma::vec& b) const
+ template<typename VecType>
+ double Evaluate(const VecType& a, const VecType& b) const
{
- // The precalculation of gamma saves us some little computation time.
- arma::vec diff = b - a;
- return exp(gamma * arma::dot(diff, diff));
+ // The precalculation of gamma saves us a little computation time.
+ return exp(gamma * metric::SquaredEuclideanDistance::Evaluate(a, b));
}
/**
* Evaluation of the Gaussian kernel using a double precision argument
@@ -66,8 +67,9 @@
* @return K(t) using the bandwidth (@f$\bandwidth at f$) specified in the
* constructor.
*/
- double Evaluate(double t) const {
- // The precalculation of gamma saves us some little computation time.
+ double Evaluate(double t) const
+ {
+ // The precalculation of gamma saves us a little computation time.
return exp(gamma * t * t);
}
@@ -75,18 +77,14 @@
const double& Bandwidth() { return bandwidth; }
private:
- /**
- * kernel bandwidth
- * */
+ //! Kernel bandwidth.
double bandwidth;
- /**
- * normalizing constant
- */
+
+ //! Normalizing constant.
double normalizer;
- /**
- * Precalculated constant depending on the bandwidth; @f$ \gamma =
- * -\frac{1}{2 \bandwidth^2} @f$.
- */
+
+ //! Precalculated constant depending on the bandwidth;
+ //! @f$ \gamma = -\frac{1}{2 \bandwidth^2} @f$.
double gamma;
};
Modified: mlpack/trunk/src/mlpack/core/kernels/linear_kernel.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/linear_kernel.hpp 2011-12-13 08:59:08 UTC (rev 10739)
+++ mlpack/trunk/src/mlpack/core/kernels/linear_kernel.hpp 2011-12-13 09:20:14 UTC (rev 10740)
@@ -37,11 +37,13 @@
* Simple evaluation of the dot product. This evaluation uses Armadillo's
* dot() function.
*
+ * @tparam VecType Type of vector (should be arma::vec or arma::spvec).
* @param a First vector.
* @param b Second vector.
* @return K(a, b).
*/
- static double Evaluate(const arma::vec& a, const arma::vec& b)
+ template<typename VecType>
+ static double Evaluate(const VecType& a, const VecType& b)
{
return arma::dot(a, b);
}
More information about the mlpack-svn
mailing list