激活函数实现--2 Sigmoid函数实现

1.Sigmoid函数的定义









2.Sigmoid函数的导数




3.SIGMOID激活的实现过程

Sigmoid主要是通过类Sigmoid来实现的,该类继承自AbstrcactActivation类,主要实现Activating接口和Derivating接口。
Sigmoid类的定义如下:
<span style="font-size:24px;">#ifndef SIGMOID_H
#define SIGMOID_H

#include "abstractactivationfunction.h"


/**
 * @brief The Sigmoid class used to calculate the sigmoid 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 Sigmoid : public AbstractActivationFunction
{
    public:
        Sigmoid();

        cv::Mat Activating(const cv::Mat& InputMat);
        cv::Mat Derivating(const cv::Mat& InputMat);
};

#endif // SIGMOID_H
</span>




函数的实现如下:
<span style="font-size:24px;">#include "sigmoid.h"

#include <iostream>


/**
* @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
*
*
*/
Sigmoid::Sigmoid()
{
}



/**
* @brief Calculating the sigmoid of the inputmat.
* @param InputMat  The input mat
* @return the result of sigmoid(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 Sigmoid::Activating(const cv::Mat& InputMat)
{
    // convert the mat to be float
    cv::Mat FloatInput = ConvertToFloatMat(InputMat);


    // calculate the exp(-x)
    cv::Mat TmpExp;
    cv::exp(-FloatInput, TmpExp);


    // calculate the sigmoid
    cv::Mat ResultMat = 1.0 / (1.0 + TmpExp);


    return ResultMat;
}







/**
* @brief Calculating the derivating of the sigmoid function.
* @param InputMat The input mat.
* @return The result of the derivative of the sigmoid function, whose size is
*         the same of the input mat.
*
* @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 Sigmoid::Derivating(const cv::Mat &InputMat)
{
    // calculating the sigmoid
    cv::Mat TmpMat = Activating(InputMat);


    // calculating the derivate of sigmoid function
    cv::Mat ResultMat = TmpMat.mul(1.0 - TmpMat);


    return ResultMat;
}</span>





  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值