1 /**
2  * @file gaussian_function.hpp
3  * @author Himanshu Pathak
4  *
5  * Definition and implementation of the gaussian function.
6  *
7  * mlpack is free software; you may redistribute it and/or modify it under the
8  * terms of the 3-clause BSD license.  You should have received a copy of the
9  * 3-clause BSD license along with mlpack.  If not, see
10  * http://www.opensource.org/licenses/BSD-3-Clause for more information.
11  */
12 #ifndef MLPACK_METHODS_ANN_ACTIVATION_FUNCTIONS_GAUSSIAN_FUNCTION_HPP
13 #define MLPACK_METHODS_ANN_ACTIVATION_FUNCTIONS_GAUSSIAN_FUNCTION_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 namespace mlpack {
18 namespace ann /** Artificial Neural Network. */ {
19 
20 /**
21  * The gaussian function, defined by
22  *
23  * @f{eqnarray*}{
24  * f(x) &=& e^{-1 * x^2} \\
25  * f'(x) &=& 2 * -x * f(x)
26  * @f}
27  */
28 class GaussianFunction
29 {
30  public:
31   /**
32    * Computes the gaussian function.
33    *
34    * @param x Input data.
35    * @return f(x).
36    */
37   template<typename eT>
Fn(const eT x)38   static double Fn(const eT x)
39   {
40     return std::exp(-1 * std::pow(x, 2));
41   }
42 
43   /**
44    * Computes the gaussian function.
45    *
46    * @param x Input data.
47    * @param y The resulting output activation.
48    */
49   template<typename InputVecType, typename OutputVecType>
Fn(const InputVecType & x,OutputVecType & y)50   static void Fn(const InputVecType& x, OutputVecType& y)
51   {
52     y = arma::exp(-1 * arma::pow(x, 2));
53   }
54 
55   /**
56    * Computes the first derivative of the gaussian function.
57    *
58    * @param y Input data.
59    * @return f'(x)
60    */
Deriv(const double y)61   static double Deriv(const double y)
62   {
63     return 2 * -y * std::exp(-1 * std::pow(y, 2));
64   }
65 
66   /**
67    * Computes the first derivatives of the gaussian function.
68    *
69    * @param y Input activations.
70    * @param x The resulting derivatives.
71    */
72   template<typename InputVecType, typename OutputVecType>
Deriv(const InputVecType & y,OutputVecType & x)73   static void Deriv(const InputVecType& y, OutputVecType& x)
74   {
75     x = 2 * -y % arma::exp(-1 * arma::pow(y, 2));
76   }
77 }; // class GaussianFunction
78 
79 } // namespace ann
80 } // namespace mlpack
81 
82 #endif
83