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

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()
{
    
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值