1.TANH函数的定义
2. TANH函数的导数
3.TANH函数的实现
Tanh主要是通过类Tanh来实现的,该类继承自AbstrcactActivation类,主要实现Activating接口和Derivating接口。
Tanh类的定义如下:
<span style="font-size:24px;">#ifndef TANH_H #define TANH_H #include "abstractactivationfunction.h" /** * @brief The tanh class used to calculate the tanh activation function. * * @author sheng * @date 2014-07-18 * @version 0.1 * * @history * <author> <date> <version> <description> * sheng 2014-07-18 0.1 build the class * */ class Tanh : public AbstractActivationFunction { public: Tanh(); cv::Mat Activating(const cv::Mat &InputMat); cv::Mat Derivating(const cv::Mat &InputMat); }; #endif // TANH_H </span>
函数的定义如下:
<span style="font-size:24px;">#include "tanh.h" /** * @brief The default constructor. * * @author sheng * @version 0.1 * @date 2014-07-18 * * @history * <author> <date> <version> <description> * sheng 2014-07-18 0.1 build the function * * */ Tanh::Tanh() { } /** * @brief Calculating the tanh of the inputmat. * @param InputMat The input mat * @return the result of tanh(inputmat), whose size is the same of the * InputMat. * * * @author sheng * @version 0.1 * @date 2014-07-18 * * @history * <author> <date> <version> <description> * sheng 2014-07-18 0.1 build the function * * * */ cv::Mat Tanh::Activating(const cv::Mat &InputMat) { // convert the inputmat to the float mat cv::Mat Floatmat = ConvertToFloatMat(InputMat); // calculating the exp(x) cv::Mat EXP_X; cv::exp(Floatmat, EXP_X); // calculating the exp(-x) cv::Mat EXP_X_; cv::exp(-Floatmat, EXP_X_); // calculating the tanh(x) cv::Mat Result = (EXP_X - EXP_X_) / (EXP_X + EXP_X_); return Result; } /** * @brief Calculating the derivative of the tanh. * @param InputMat The input mat * @return the result of the derivative of the tanh, whose size is the same of * the InputMat. * * * @author sheng * @version 0.1 * @date 2014-07-18 * * @history * <author> <date> <version> <description> * sheng 2014-07-18 0.1 build the function * * * */ cv::Mat Tanh::Derivating(const cv::Mat &InputMat) { // convert the inputmat to float mat cv::Mat FloatMat = ConvertToFloatMat(InputMat); // calculating the tanh(x) cv::Mat TanhX = Activating(FloatMat); // calculting the tanh(x) * tanh(x) cv::Mat SquareTanhX = TanhX.mul(TanhX); // calculating the derivative of the tanh cv::Mat Result = 1 - SquareTanhX; return Result; }</span>