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

激活函数实现–1 接口设计

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

1 使用语言和库

库:mingw编译的OpenCV2.4.9.0

开发语言:C++
IDE:Qt Creator
版本控制:git




2 主要接口

2.1 功能

激活函数的主要功能是输入一个矩阵,计算矩阵中的元素的激活值,同时,也包含有输入一个矩阵,计算矩阵中的元素关于激活函数的导数值,这是为了后续进行神经网络实现所进行的一部分。




2.2 主要接口

2.2.1 激活计算接口

cv::Mat Activating(const cv::Mat& InputMat)



2.2.2 导数计算接口

cv::Mat Derivating(const cv::Mat& InputMat)




3  实现过程

3.1  接口实现

   接口主要通过AbstractActivationFunction类来实现。
AbstractActivationFunction类的定义如下:

#ifndef ABSTRACTACTIVATIONFUNCTION_H
#define ABSTRACTACTIVATIONFUNCTION_H

#include "Headerfiles.h"


/**
 * @brief The AbstractActivationFunction class is the abstract class used to
 *        define the interface of the 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 AbstractActivationFunction
{
    public:
        AbstractActivationFunction();

        cv::Mat ConvertToFloatMat(const cv::Mat& InputMat);

        virtual cv::Mat Activating(const cv::Mat& InputMat) = 0;

        virtual cv::Mat Derivating(const cv::Mat& InputMat) = 0;

        virtual ~AbstractActivationFunction();
};

#endif // ABSTRACTACTIVATIONFUNCTION_H

函数的定义如下:

#include "abstractactivationfunction.h"



/**
* @brief The default constructor of the AbstractActivationFunction class.
* 
* @author sheng
* @version 0.1
* @date 2014-07-18
* 
* @history
*     <author>       <date>         <version>        <description>
*      sheng       2014-07-18          0.1         build the function
* 
* 
*/
AbstractActivationFunction::AbstractActivationFunction()
{

}


/**
* @brief Convert the InputMat to be the float mat.
* @param InputMat  The input mat 
* @return The float mat which is at the same size of the InputMat.
* @exception 
* 
* @TODO without throw exception.
* 
* 
* @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 AbstractActivationFunction::ConvertToFloatMat(const cv::Mat &InputMat)
{
    // assert if the InputMat is empty.
    // TODO: without throw exception
    assert(InputMat.data);
    if (!InputMat.data)
    {
        std::cout << "The input Mat is empty." << std::endl;
        return InputMat;
    }


    cv::Mat FloatInput;

    // convert the inputmat to float
    if (InputMat.depth() != CV_32F)
    {
        switch (InputMat.channels())
        {
            case 1:
                InputMat.convertTo(FloatInput, CV_32FC1);
                break;

            case 2:
                InputMat.convertTo(FloatInput, CV_32FC2);
                break;

            case 3:
                InputMat.convertTo(FloatInput, CV_32FC3);
                break;

            case 4:
                InputMat.convertTo(FloatInput, CV_32FC4);
                break;

            default:
                break;
        }
    }
    else
    {
        FloatInput = InputMat;
    }

    return FloatInput;
}






/**
* @brief The deconstructor of the AbstractActivationFunction class.
* 
* @author sheng
* @version 0.1
* @date 2014-07-18
* 
* @history
*     <author>       <date>         <version>        <description>
*      sheng       2014-07-18          0.1         build the function
* 
* 
*/
AbstractActivationFunction::~AbstractActivationFunction()
{
    
}

抱歉!评论已关闭.