[mlpack-svn] master: Add implementation of the tanh activation function. (c935252)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Wed Dec 31 15:54:38 EST 2014


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/8316fefddb7b2e23679babf77c90647e529988eb...c935252ea3134025d7d0df05afa4a1501dad4d59

>---------------------------------------------------------------

commit c935252ea3134025d7d0df05afa4a1501dad4d59
Author: Marcus Edel <marcus.edel at fu-berlin.de>
Date:   Wed Dec 31 21:54:33 2014 +0100

    Add implementation of the tanh activation function.


>---------------------------------------------------------------

c935252ea3134025d7d0df05afa4a1501dad4d59
 .../ann/activation_functions/tanh_function.hpp     | 100 +++++++++++++++++++++
 1 file changed, 100 insertions(+)

diff --git a/src/mlpack/methods/ann/activation_functions/tanh_function.hpp b/src/mlpack/methods/ann/activation_functions/tanh_function.hpp
new file mode 100644
index 0000000..8987c0b
--- /dev/null
+++ b/src/mlpack/methods/ann/activation_functions/tanh_function.hpp
@@ -0,0 +1,100 @@
+/**
+ * @file tanh_function.hpp
+ * @author Marcus Edel
+ *
+ * Definition and implementation of the Tangens Hyperbolic function.
+ */
+#ifndef __MLPACK_METHODS_ANN_ACTIVATION_FUNCTIONS_TANH_FUNCTION_HPP
+#define __MLPACK_METHODS_ANN_ACTIVATION_FUNCTIONS_TANH_FUNCTION_HPP
+
+#include <mlpack/core.hpp>
+
+namespace mlpack {
+namespace ann /** Artificial Neural Network. */ {
+
+/**
+ * The tanh function, defined by
+ *
+ * @f[
+ * f(x) &=& \frac{e^x - e^{-x}{e^x + e^{-x}}} \\
+ * f'(x) &=& 1 - tanh^2(x)
+ * f^{-1}(x) &=& atan(x)
+ * @f]
+ */
+class TanhFunction
+{
+  public:
+  /**
+   * Computes the tanh function.
+   *
+   * @param x Input data.
+   * @return f(x).
+   */
+  static double fn(const double x)
+  {
+    return std::tanh(x);
+  }
+
+  /**
+   * Computes the tanh function.
+   *
+   * @param x Input data.
+   * @param y The resulting output activation.
+   */
+  template<typename InputVecType, typename OutputVecType>
+  static void fn(const InputVecType& x, OutputVecType& y)
+  {
+    y = arma::tanh(x);
+  }
+
+  /**
+   * Computes the first derivative of the tanh function.
+   *
+   * @param y Input data.
+   * @return f'(x)
+   */
+  static double deriv(const double y)
+  {
+    return 1 - std::pow(y, 2);
+  }
+
+  /**
+   * Computes the first derivatives of the tanh function.
+   *
+   * @param y Input data.
+   * @param x The resulting derivatives.
+   */
+  template<typename InputVecType, typename OutputVecType>
+  static void deriv(const InputVecType& y, OutputVecType& x)
+  {
+    x = 1 - arma::pow(y, 2);
+  }
+
+  /**
+   * Computes the inverse of the tanh function.
+   *
+   * @param y Input data.
+   * @return f^{-1}(x)
+   */
+  static double inv(const double y)
+  {
+    return std::atanh(y);
+  }
+
+  /**
+   * Computes the inverse of the tanh function.
+   *
+   * @param y Input data.
+   * @param x The resulting inverse of the input data.
+   */
+  template<typename InputVecType, typename OutputVecType>
+  static void inv(const InputVecType& y, OutputVecType& x)
+  {
+    x = arma::atanh(y);
+  }
+}; // class TanhFunction
+
+}; // namespace ann
+}; // namespace mlpack
+
+#endif




More information about the mlpack-svn mailing list