mlpack 3.4.2
gaussian_kernel.hpp
Go to the documentation of this file.
1
14#ifndef MLPACK_CORE_KERNELS_GAUSSIAN_KERNEL_HPP
15#define MLPACK_CORE_KERNELS_GAUSSIAN_KERNEL_HPP
16
17#include <mlpack/prereqs.hpp>
20
21namespace mlpack {
22namespace kernel {
23
35{
36 public:
40 GaussianKernel() : bandwidth(1.0), gamma(-0.5)
41 { }
42
48 GaussianKernel(const double bandwidth) :
49 bandwidth(bandwidth),
50 gamma(-0.5 * pow(bandwidth, -2.0))
51 { }
52
64 template<typename VecTypeA, typename VecTypeB>
65 double Evaluate(const VecTypeA& a, const VecTypeB& b) const
66 {
67 // The precalculation of gamma saves us a little computation time.
68 return exp(gamma * metric::SquaredEuclideanDistance::Evaluate(a, b));
69 }
70
78 double Evaluate(const double t) const
79 {
80 // The precalculation of gamma saves us a little computation time.
81 return exp(gamma * std::pow(t, 2.0));
82 }
83
92 double Gradient(const double t) const {
93 return 2 * t * gamma * exp(gamma * std::pow(t, 2.0));
94 }
95
104 double GradientForSquaredDistance(const double t) const {
105 return gamma * exp(gamma * t);
106 }
107
114 double Normalizer(const size_t dimension)
115 {
116 return pow(sqrt(2.0 * M_PI) * bandwidth, (double) dimension);
117 }
118
126 template<typename VecTypeA, typename VecTypeB>
127 double ConvolutionIntegral(const VecTypeA& a, const VecTypeB& b)
128 {
130 2.0)) / (Normalizer(a.n_rows) * pow(2.0, (double) a.n_rows / 2.0));
131 }
132
133
135 double Bandwidth() const { return bandwidth; }
136
139 void Bandwidth(const double bandwidth)
140 {
141 this->bandwidth = bandwidth;
142 this->gamma = -0.5 * pow(bandwidth, -2.0);
143 }
144
146 double Gamma() const { return gamma; }
147
149 template<typename Archive>
150 void serialize(Archive& ar, const unsigned int /* version */)
151 {
152 ar & BOOST_SERIALIZATION_NVP(bandwidth);
153 ar & BOOST_SERIALIZATION_NVP(gamma);
154 }
155
156 private:
158 double bandwidth;
159
162 double gamma;
163};
164
166template<>
168{
169 public:
171 static const bool IsNormalized = true;
173 static const bool UsesSquaredDistance = true;
174};
175
176} // namespace kernel
177} // namespace mlpack
178
179#endif
The standard Gaussian kernel.
double Evaluate(const double t) const
Evaluation of the Gaussian kernel given the distance between two points.
GaussianKernel()
Default constructor; sets bandwidth to 1.0.
double Gradient(const double t) const
Evaluation of the gradient of Gaussian kernel given the distance between two points.
void Bandwidth(const double bandwidth)
Modify the bandwidth.
double GradientForSquaredDistance(const double t) const
Evaluation of the gradient of Gaussian kernel given the squared distance between two points.
double Evaluate(const VecTypeA &a, const VecTypeB &b) const
Evaluation of the Gaussian kernel.
GaussianKernel(const double bandwidth)
Construct the Gaussian kernel with a custom bandwidth.
double Normalizer(const size_t dimension)
Obtain the normalization constant of the Gaussian kernel.
double Bandwidth() const
Get the bandwidth.
double Gamma() const
Get the precalculated constant.
void serialize(Archive &ar, const unsigned int)
Serialize the kernel.
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.
static const bool UsesSquaredDistance
If true, then the kernel include a squared distance, ||x - y||^2 .
static const bool IsNormalized
If true, then the kernel is normalized: K(x, x) = K(y, y) = 1 for all x.
static VecTypeA::elem_type Evaluate(const VecTypeA &a, const VecTypeB &b)
Computes the distance between two points.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The core includes that mlpack expects; standard C++ includes and Armadillo.
#define M_PI
Definition: prereqs.hpp:39