现在的位置: 首页 > 综合 > 正文

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

2018年04月13日 ⁄ 综合 ⁄ 共 1891字 ⁄ 字号 评论关闭

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>

抱歉!评论已关闭.