激活函数实现--3 Tanh函数实现

本文详细介绍了TANH函数的定义、导数及其在计算机科学领域的实现方式,包括如何通过类Tanh进行计算激活函数和导数,以及在神经网络中的应用。

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值