[mlpack-svn] r16172 - in mlpack/trunk/src/mlpack: core/kernels core/math core/metrics tests
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Jan 27 16:08:41 EST 2014
Author: birm
Date: Mon Jan 27 16:08:40 2014
New Revision: 16172
Log:
Added ToString() to core/dists, core/kernels, and core/metrics
Added a test for the ToString() methods
Added:
mlpack/trunk/src/mlpack/tests/To_String_Test.cpp
Modified:
mlpack/trunk/src/mlpack/core/kernels/cosine_distance.hpp
mlpack/trunk/src/mlpack/core/kernels/epanechnikov_kernel.cpp
mlpack/trunk/src/mlpack/core/kernels/epanechnikov_kernel.hpp
mlpack/trunk/src/mlpack/core/kernels/epanechnikov_kernel_impl.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/hyperbolic_tangent_kernel.hpp
mlpack/trunk/src/mlpack/core/kernels/laplacian_kernel.hpp
mlpack/trunk/src/mlpack/core/kernels/linear_kernel.hpp
mlpack/trunk/src/mlpack/core/kernels/polynomial_kernel.hpp
mlpack/trunk/src/mlpack/core/kernels/pspectrum_string_kernel.hpp
mlpack/trunk/src/mlpack/core/kernels/pspectrum_string_kernel_impl.hpp
mlpack/trunk/src/mlpack/core/kernels/spherical_kernel.hpp
mlpack/trunk/src/mlpack/core/kernels/triangular_kernel.hpp
mlpack/trunk/src/mlpack/core/math/range_impl.hpp
mlpack/trunk/src/mlpack/core/metrics/ip_metric.hpp
mlpack/trunk/src/mlpack/core/metrics/ip_metric_impl.hpp
mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp
mlpack/trunk/src/mlpack/core/metrics/lmetric_impl.hpp
mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp
mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp
mlpack/trunk/src/mlpack/tests/CMakeLists.txt
mlpack/trunk/src/mlpack/tests/sgd_test.cpp
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 Mon Jan 27 16:08:40 2014
@@ -34,6 +34,14 @@
*/
template<typename VecType>
static double Evaluate(const VecType& a, const VecType& b);
+ /**
+ * Returns a string representation of this object.
+ */
+ std::string ToString() const{
+ std::ostringstream convert;
+ convert << "CosineDistance [" << this << "]" << std::endl;
+ return convert.str();
+}
};
//! Kernel traits for the cosine distance.
Modified: mlpack/trunk/src/mlpack/core/kernels/epanechnikov_kernel.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/kernels/epanechnikov_kernel.cpp (original)
+++ mlpack/trunk/src/mlpack/core/kernels/epanechnikov_kernel.cpp Mon Jan 27 16:08:40 2014
@@ -30,3 +30,13 @@
{
return std::max(0.0, 1 - std::pow(distance, 2.0) * inverseBandwidthSquared);
}
+
+// Return String of Object
+std::string EpanechnikovKernel::ToString() const{
+ std::ostringstream convert;
+ convert << "EpanechnikovKernel [" << this << "]" << std::endl;
+ convert << "Bandwidth: " << bandwidth << std::endl;
+ convert << "Inverse Bandwidth Squared: ";
+ convert << inverseBandwidthSquared << std::endl;
+ return convert.str();
+}
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 Mon Jan 27 16:08:40 2014
@@ -58,6 +58,7 @@
* @param b Second vector.
* @return the convolution integral value.
*/
+
template<typename VecType>
double ConvolutionIntegral(const VecType& a, const VecType& b);
@@ -67,12 +68,16 @@
* @param dimension Dimension to calculate the normalizer for.
*/
double Normalizer(const size_t dimension);
+
+ // Returns String of O bject
+ std::string ToString() const;
private:
//! Bandwidth of the kernel.
double bandwidth;
//! Cached value of the inverse bandwidth squared (to speed up computation).
double inverseBandwidthSquared;
+
};
//! Kernel traits for the Epanechnikov kernel.
Modified: mlpack/trunk/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp (original)
+++ mlpack/trunk/src/mlpack/core/kernels/epanechnikov_kernel_impl.hpp Mon Jan 27 16:08:40 2014
@@ -23,6 +23,8 @@
* inverseBandwidthSquared);
}
+
+
/**
* Obtains the convolution integral [integral of K(||x-a||) K(||b-x||) dx]
* for the two vectors.
Modified: mlpack/trunk/src/mlpack/core/kernels/example_kernel.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/kernels/example_kernel.hpp (original)
+++ mlpack/trunk/src/mlpack/core/kernels/example_kernel.hpp Mon Jan 27 16:08:40 2014
@@ -98,6 +98,16 @@
* @param b Second vector.
* @return K(a, b).
*/
+ std::string ToString() const{
+ std::ostringstream convert;
+ convert << "ExampleKernel [" << this << "]" << std::endl;
+ return convert.str();
+ }
+ /**
+ * Returns a String for the Kernel Object; in this case, with only the memory
+ * address for the Kernel. If your kernel has any members, your ToString
+ * method should include those as neccessary as well.
+ **/
template<typename VecType>
static double Evaluate(const VecType& a, const VecType& b) { return 0; }
@@ -127,7 +137,8 @@
* @param dimension the dimension of the space.
* @return the normalization constant.
*/
- static double Normalizer(size_t dimension) { return 0; }
+ static double Normalizer(size_t dimension=1) { return 0; }
+
};
}; // namespace kernel
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 Mon Jan 27 16:08:40 2014
@@ -115,6 +115,13 @@
//! Get the precalculated constant.
double Gamma() const { return gamma; }
+ // convert object to string
+ std::string ToString() const{
+ std::ostringstream convert;
+ convert << "GaussianKernel [" << this << "]" << std::endl;
+ return convert.str();
+ }
+
private:
//! Kernel bandwidth.
double bandwidth;
Modified: mlpack/trunk/src/mlpack/core/kernels/hyperbolic_tangent_kernel.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/kernels/hyperbolic_tangent_kernel.hpp (original)
+++ mlpack/trunk/src/mlpack/core/kernels/hyperbolic_tangent_kernel.hpp Mon Jan 27 16:08:40 2014
@@ -65,6 +65,13 @@
//! Modify offset for the kernel.
double& Offset() { return offset; }
+ // Convert Object to String
+ std::string ToString() const{
+ std::ostringstream convert;
+ convert << "HyperbolicTangentKernel [" << this << "]" << std::endl;
+ return convert.str();
+ }
+
private:
double scale;
double offset;
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 Mon Jan 27 16:08:40 2014
@@ -76,7 +76,11 @@
double Bandwidth() const { return bandwidth; }
//! Modify the bandwidth.
double& Bandwidth() { return bandwidth; }
-
+ std::string ToString() const{
+ std::ostringstream convert;
+ convert << "LaplacianKernel [" << this << "]" << std::endl;
+ return convert.str();
+ }
private:
//! Kernel bandwidth.
double bandwidth;
Modified: mlpack/trunk/src/mlpack/core/kernels/linear_kernel.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/kernels/linear_kernel.hpp (original)
+++ mlpack/trunk/src/mlpack/core/kernels/linear_kernel.hpp Mon Jan 27 16:08:40 2014
@@ -47,6 +47,12 @@
{
return arma::dot(a, b);
}
+
+ std::string ToString() const{
+ std::ostringstream convert;
+ convert << "Linear Kernel [" << this << "]" << std::endl;
+ return convert.str();
+ }
};
}; // namespace kernel
Modified: mlpack/trunk/src/mlpack/core/kernels/polynomial_kernel.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/kernels/polynomial_kernel.hpp (original)
+++ mlpack/trunk/src/mlpack/core/kernels/polynomial_kernel.hpp Mon Jan 27 16:08:40 2014
@@ -59,7 +59,14 @@
const double& Offset() const { return offset; }
//! Modify the offset of the dot product of the arguments.
double& Offset() { return offset; }
-
+
+ // Return String of Object
+ std::string ToString() const{
+ std::ostringstream convert;
+ convert << "PolynomialKernel [" << this << "]" << std::endl;
+ return convert.str();
+ }
+
private:
//! The degree of the polynomial.
double degree;
Modified: mlpack/trunk/src/mlpack/core/kernels/pspectrum_string_kernel.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/kernels/pspectrum_string_kernel.hpp (original)
+++ mlpack/trunk/src/mlpack/core/kernels/pspectrum_string_kernel.hpp Mon Jan 27 16:08:40 2014
@@ -95,6 +95,14 @@
//! Modify the value of p.
size_t& P() { return p; }
+ /*
+ * Returns a string representation of this object.
+ */
+ std::string ToString() const{
+ std::ostringstream convert;
+ convert << "PSpectrumStringKernel [" << this << "]" << std::endl;
+ return convert.str();
+ }
private:
//! The datasets.
const std::vector<std::vector<std::string> >& datasets;
Modified: mlpack/trunk/src/mlpack/core/kernels/pspectrum_string_kernel_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/kernels/pspectrum_string_kernel_impl.hpp (original)
+++ mlpack/trunk/src/mlpack/core/kernels/pspectrum_string_kernel_impl.hpp Mon Jan 27 16:08:40 2014
@@ -69,7 +69,6 @@
return eval;
}
-
}; // 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 Mon Jan 27 16:08:40 2014
@@ -80,6 +80,13 @@
return (t <= bandwidth) ? 1.0 : 0.0;
}
+ // convert object to string
+ std::string ToString() const{
+ std::ostringstream convert;
+ convert << "SphericalKernel [" << this << "]" << std::endl;
+ return convert.str();
+ }
+
private:
double bandwidth;
double bandwidthSquared;
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 Mon Jan 27 16:08:40 2014
@@ -61,6 +61,13 @@
//! Modify the bandwidth of the kernel.
double& Bandwidth() { return bandwidth; }
+ // convert object to string
+ std::string ToString() const{
+ std::ostringstream convert;
+ convert << "TriangularKernel [" << this << "]" << std::endl;
+ return convert.str();
+ }
+
private:
//! The bandwidth of the kernel.
double bandwidth;
Modified: mlpack/trunk/src/mlpack/core/math/range_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/math/range_impl.hpp (original)
+++ mlpack/trunk/src/mlpack/core/math/range_impl.hpp Mon Jan 27 16:08:40 2014
@@ -183,7 +183,6 @@
convert << "[" << lo << ", " << hi << "]";
return convert.str();
}
-
}; // namespace math
}; // namespace mlpack
Modified: mlpack/trunk/src/mlpack/core/metrics/ip_metric.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/metrics/ip_metric.hpp (original)
+++ mlpack/trunk/src/mlpack/core/metrics/ip_metric.hpp Mon Jan 27 16:08:40 2014
@@ -34,7 +34,10 @@
const KernelType& Kernel() const { return kernel; }
//! Modify the kernel.
KernelType& Kernel() { return kernel; }
-
+ /**
+ * Returns a string representation of this object.
+ */
+ std::string ToString() const;
private:
//! The locally stored kernel, if it is necessary.
KernelType* localKernel;
Modified: mlpack/trunk/src/mlpack/core/metrics/ip_metric_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/metrics/ip_metric_impl.hpp (original)
+++ mlpack/trunk/src/mlpack/core/metrics/ip_metric_impl.hpp Mon Jan 27 16:08:40 2014
@@ -53,6 +53,15 @@
2 * kernel.Evaluate(a, b));
}
+// Convert Object To String
+template<typename KernelType>
+std::string IPMetric<KernelType>::ToString() const{
+ std::ostringstream convert;
+ convert << "IPMetric [" << this << "]" << std::endl;
+ convert << "Kernel: " << localKernel << std::endl;
+ return convert.str();
+}
+
// A specialization for the linear kernel, which actually just turns out to be
// the Euclidean distance.
template<>
Modified: mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp (original)
+++ mlpack/trunk/src/mlpack/core/metrics/lmetric.hpp Mon Jan 27 16:08:40 2014
@@ -69,6 +69,7 @@
*/
template<typename VecType1, typename VecType2>
static double Evaluate(const VecType1& a, const VecType2& b);
+ std::string ToString() const;
};
// Convenience typedefs.
@@ -93,6 +94,7 @@
*/
typedef LMetric<INT_MAX, false> ChebyshevDistance;
+
}; // namespace metric
}; // namespace mlpack
Modified: mlpack/trunk/src/mlpack/core/metrics/lmetric_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/metrics/lmetric_impl.hpp (original)
+++ mlpack/trunk/src/mlpack/core/metrics/lmetric_impl.hpp Mon Jan 27 16:08:40 2014
@@ -29,6 +29,15 @@
return pow(sum, (1.0 / Power));
}
+// String Conversion
+template<int Power, bool TakeRoot>
+std::string LMetric<Power,TakeRoot>::ToString() const
+{
+ std::ostringstream convert;
+ convert << "LMetric [" << this << "]" << std::endl;;
+ return convert.str();
+}
+
// L1-metric specializations; the root doesn't matter.
template<>
template<typename VecType1, typename VecType2>
Modified: mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp (original)
+++ mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance.hpp Mon Jan 27 16:08:40 2014
@@ -30,7 +30,7 @@
* itself before performing any evaluations. However, this class is provided
* for convenience.
*
- * Similar to the LMetric class, this offers a template parameter t_take_root
+ * Similar to the LMetric class, this offers a template parameter TakeRoot
* which, when set to false, will instead evaluate the distance
*
* @f[
@@ -39,10 +39,10 @@
*
* which is faster to evaluate.
*
- * @tparam t_take_root If true, takes the root of the output. It is slightly
+ * @tparam TakeRoot If true, takes the root of the output. It is slightly
* faster to leave this at the default of false.
*/
-template<bool t_take_root = false>
+template<bool TakeRoot = false>
class MahalanobisDistance
{
public:
@@ -78,6 +78,9 @@
* @param a First vector.
* @param b Second vector.
*/
+
+ // Return String of Object
+ std::string ToString() const;
template<typename VecType1, typename VecType2>
double Evaluate(const VecType1& a, const VecType2& b);
@@ -94,7 +97,6 @@
* @return Reference to the covariance matrix.
*/
arma::mat& Covariance() { return covariance; }
-
private:
//! The covariance matrix associated with this distance.
arma::mat covariance;
Modified: mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp (original)
+++ mlpack/trunk/src/mlpack/core/metrics/mahalanobis_distance_impl.hpp Mon Jan 27 16:08:40 2014
@@ -12,6 +12,15 @@
namespace mlpack {
namespace metric {
+
+// Convert Object into String
+template<bool TakeRoot>
+std::string MahalanobisDistance<TakeRoot>::ToString() const{
+ std::ostringstream convert;
+ convert << "MahalanobisDistance [" << this << "]" << std::endl;
+ return convert.str();
+}
+
/**
* Specialization for non-rooted case.
*/
@@ -24,7 +33,6 @@
arma::mat out = trans(m) * covariance * m; // 1x1
return out[0];
}
-
/**
* Specialization for rooted case. This requires one extra evaluation of
* sqrt().
Modified: mlpack/trunk/src/mlpack/tests/CMakeLists.txt
==============================================================================
--- mlpack/trunk/src/mlpack/tests/CMakeLists.txt (original)
+++ mlpack/trunk/src/mlpack/tests/CMakeLists.txt Mon Jan 27 16:08:40 2014
@@ -39,6 +39,7 @@
sgd_test.cpp
sort_policy_test.cpp
sparse_coding_test.cpp
+ To_String_Test.cpp
tree_test.cpp
tree_traits_test.cpp
union_find_test.cpp
Added: mlpack/trunk/src/mlpack/tests/To_String_Test.cpp
==============================================================================
--- (empty file)
+++ mlpack/trunk/src/mlpack/tests/To_String_Test.cpp Mon Jan 27 16:08:40 2014
@@ -0,0 +1,128 @@
+/**
+ * @file ToStringTest.cpp
+ * @author Ryan Birmingham
+ *
+ * Test of the AugmentedLagrangian class using the test functions defined in
+ * aug_lagrangian_test_functions.hpp.
+ **/
+
+#include <mlpack/core.hpp>
+#include <mlpack/core.hpp>
+#include <boost/test/unit_test.hpp>
+#include "old_boost_test_definitions.hpp"
+
+#include <mlpack/core/metrics/ip_metric.hpp>
+#include <mlpack/core/metrics/lmetric.hpp>
+#include <mlpack/core/metrics/mahalanobis_distance.hpp>
+#include <mlpack/core/kernels/pspectrum_string_kernel.hpp>
+
+#include <mlpack/core/kernels/pspectrum_string_kernel.hpp>
+
+#include <mlpack/core/kernels/example_kernel.hpp>
+
+using namespace mlpack;
+using namespace mlpack::kernel;
+using namespace mlpack::distribution;
+using namespace mlpack::metric;
+
+BOOST_AUTO_TEST_SUITE(ToStringTest);
+
+BOOST_AUTO_TEST_CASE(DiscreteDistributionString)
+{
+ DiscreteDistribution d;
+ Log::Info << d;
+}
+
+BOOST_AUTO_TEST_CASE(GaussianDistributionString)
+{
+ GaussianDistribution d;
+ Log::Info << d;
+}
+
+BOOST_AUTO_TEST_CASE(CosineDistanceString)
+{
+ CosineDistance d;
+ Log::Info << d;
+}
+
+BOOST_AUTO_TEST_CASE(EpanechnikovKernelString)
+{
+ EpanechnikovKernel d;
+ Log::Info << d;
+}
+
+BOOST_AUTO_TEST_CASE(ExampleKernelString)
+{
+ ExampleKernel d;
+ Log::Info << d;
+}
+
+BOOST_AUTO_TEST_CASE(GaussianKernelString)
+{
+ GaussianKernel d;
+ Log::Info << d;
+}
+
+BOOST_AUTO_TEST_CASE(HyperbolicTangentKernelString)
+{
+ HyperbolicTangentKernel d;
+ Log::Info << d;
+}
+
+BOOST_AUTO_TEST_CASE(LaplacianKernelString)
+{
+ LaplacianKernel d;
+ Log::Info << d;
+}
+
+BOOST_AUTO_TEST_CASE(LinearKernelString)
+{
+ LinearKernel d;
+ Log::Info << d;
+}
+
+BOOST_AUTO_TEST_CASE(PolynomialKernelString)
+{
+ PolynomialKernel d;
+ Log::Info << d;
+}
+
+BOOST_AUTO_TEST_CASE(PSpectrumStringKernelString)
+{
+ const std::vector<std::vector<std::string> > s;
+ const size_t t=1;
+ PSpectrumStringKernel d(s,t);
+ Log::Info << d;
+}
+
+BOOST_AUTO_TEST_CASE(SphericalKernelString)
+{
+ SphericalKernel d;
+ Log::Info << d;
+}
+
+BOOST_AUTO_TEST_CASE(TriangularKernelString)
+{
+ TriangularKernel d;
+ Log::Info << d;
+}
+
+BOOST_AUTO_TEST_CASE(IPMetricString)
+{
+ IPMetric<TriangularKernel> d;
+ Log::Info << d;
+}
+
+BOOST_AUTO_TEST_CASE(LMetricString)
+{
+ LMetric<1> d;
+ Log::Info << d;
+}
+
+BOOST_AUTO_TEST_CASE(MahalanobisDistanceString)
+{
+ MahalanobisDistance<> d;
+ Log::Info << d;
+}
+
+BOOST_AUTO_TEST_SUITE_END();
Modified: mlpack/trunk/src/mlpack/tests/sgd_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/sgd_test.cpp (original)
+++ mlpack/trunk/src/mlpack/tests/sgd_test.cpp Mon Jan 27 16:08:40 2014
@@ -9,6 +9,10 @@
#include <mlpack/core/optimizers/lbfgs/test_functions.hpp>
#include <mlpack/core/optimizers/sgd/test_function.hpp>
+#include <mlpack/core/metrics/ip_metric.hpp>
+#include <mlpack/core/metrics/lmetric.hpp>
+#include <mlpack/core/metrics/mahalanobis_distance.hpp>
+
#include <boost/test/unit_test.hpp>
#include "old_boost_test_definitions.hpp"
@@ -17,6 +21,7 @@
using namespace mlpack;
using namespace mlpack::optimization;
using namespace mlpack::optimization::test;
+using namespace mlpack::metric;
BOOST_AUTO_TEST_SUITE(SGDTest);
More information about the mlpack-svn
mailing list