mlpack: a scalable c++ machine learning library
mlpack  2.0.2
gaussian_kernel.hpp
Go to the documentation of this file.
1 
16 #ifndef mlpack_CORE_KERNELS_GAUSSIAN_KERNEL_HPP
17 #define mlpack_CORE_KERNELS_GAUSSIAN_KERNEL_HPP
18 
19 #include <mlpack/core.hpp>
21 
22 namespace mlpack {
23 namespace kernel {
24 
36 {
37  public:
41  GaussianKernel() : bandwidth(1.0), gamma(-0.5)
42  { }
43 
49  GaussianKernel(const double bandwidth) :
50  bandwidth(bandwidth),
51  gamma(-0.5 * pow(bandwidth, -2.0))
52  { }
53 
65  template<typename VecTypeA, typename VecTypeB>
66  double Evaluate(const VecTypeA& a, const VecTypeB& b) const
67  {
68  // The precalculation of gamma saves us a little computation time.
70  }
71 
79  double Evaluate(const double t) const
80  {
81  // The precalculation of gamma saves us a little computation time.
82  return exp(gamma * std::pow(t, 2.0));
83  }
84 
93  double Gradient(const double t) const {
94  return 2 * t * gamma * exp(gamma * std::pow(t, 2.0));
95  }
96 
105  double GradientForSquaredDistance(const double t) const {
106  return gamma * exp(gamma * t);
107  }
108 
115  double Normalizer(const size_t dimension)
116  {
117  return pow(sqrt(2.0 * M_PI) * bandwidth, (double) dimension);
118  }
119 
127  template<typename VecTypeA, typename VecTypeB>
128  double ConvolutionIntegral(const VecTypeA& a, const VecTypeB& b)
129  {
130  return Evaluate(sqrt(metric::SquaredEuclideanDistance::Evaluate(a, b) / 2.0)) /
131  (Normalizer(a.n_rows) * pow(2.0, (double) a.n_rows / 2.0));
132  }
133 
134 
136  double Bandwidth() const { return bandwidth; }
137 
140  void Bandwidth(const double bandwidth)
141  {
142  this->bandwidth = bandwidth;
143  this->gamma = -0.5 * pow(bandwidth, -2.0);
144  }
145 
147  double Gamma() const { return gamma; }
148 
150  template<typename Archive>
151  void Serialize(Archive& ar, const unsigned int /* version */)
152  {
153  ar & data::CreateNVP(bandwidth, "bandwidth");
154  ar & data::CreateNVP(gamma, "gamma");
155  }
156 
157  private:
159  double bandwidth;
160 
163  double gamma;
164 };
165 
167 template<>
169 {
170  public:
172  static const bool IsNormalized = true;
174  static const bool UsesSquaredDistance = true;
175 };
176 
177 } // namespace kernel
178 } // namespace mlpack
179 
180 #endif
GaussianKernel()
Default constructor; sets bandwidth to 1.0.
double ConvolutionIntegral(const VecTypeA &a, const VecTypeB &b)
Obtain a convolution integral of the Gaussian kernel.
This is a template class that can provide information about various kernels.
Linear algebra utility functions, generally performed on matrices or vectors.
FirstShim< T > CreateNVP(T &t, const std::string &name, typename boost::enable_if< HasSerialize< T >>::type *=0)
Call this function to produce a name-value pair; this is similar to BOOST_SERIALIZATION_NVP(), but should be used for types that have a Serialize() function (or contain a type that has a Serialize() function) instead of a serialize() function.
double Gamma() const
Get the precalculated constant.
void Bandwidth(const double bandwidth)
Modify the bandwidth.
double gamma
Precalculated constant depending on the bandwidth; .
#define M_PI
Definition: prereqs.hpp:44
double Evaluate(const double t) const
Evaluation of the Gaussian kernel given the distance between two points.
void Serialize(Archive &ar, const unsigned int)
Serialize the kernel.
static VecTypeA::elem_type Evaluate(const VecTypeA &a, const VecTypeB &b)
Computes the distance between two points.
GaussianKernel(const double bandwidth)
Construct the Gaussian kernel with a custom bandwidth.
double GradientForSquaredDistance(const double t) const
Evaluation of the gradient of Gaussian kernel given the squared distance between two points...
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
double Normalizer(const size_t dimension)
Obtain the normalization constant of the Gaussian kernel.
The standard Gaussian kernel.
double Bandwidth() const
Get the bandwidth.
double Evaluate(const VecTypeA &a, const VecTypeB &b) const
Evaluation of the Gaussian kernel.
double Gradient(const double t) const
Evaluation of the gradient of Gaussian kernel given the distance between two points.
double bandwidth
Kernel bandwidth.