基于Eigen库的C++深度神经网络类

本文介绍了如何使用Eigen库在C++中构建一个深度神经网络类,包括构造函数(随机初始化权重和从文件加载权重)、前向传播、反向传播、参数更新、Sigmoid激活函数及其导数、训练接口、网络参数存储和二值化等关键功能。通过实例展示了如何使用该类训练MNIST数据集,并提供了训练和预测的代码片段。
摘要由CSDN通过智能技术生成

都在用Python做深度学习,懒人的福利。

手写一个基于Eigen的C++深度神经网络类,对于弄懂其中的过程还是很有好处的。代码分享给大家。

一、构造函数

类构造函数两个

(1)DeepLearn::DeepLearn(Eigen::MatrixXi layer_structure)

参数是一个1行多列的向量,各列数据即为每层神经元个数;

DeepLearn::DeepLearn(Eigen::MatrixXi layer_structure):mLayerStructure(layer_structure)
{
    int nLayers = layer_structure.size() - 1;

    for (int i = 0; i < nLayers; i++)
    {
        Eigen::MatrixXd w = Eigen::MatrixXd::Random(layer_structure(i + 1), layer_structure(i));
        Eigen::MatrixXd b = Eigen::MatrixXd::Random(layer_structure(i + 1), 1);
        Eigen::MatrixXd z = Eigen::MatrixXd::Zero(layer_structure(i + 1), 1);
        Eigen::MatrixXd a = Eigen::MatrixXd::Zero(layer_structure(i + 1), 1);
        Eigen::MatrixXd d = Eigen::MatrixXd::Zero(layer_structure(i + 1), 1);
        Eigen::MatrixXd dw = Eigen::MatrixXd::Zero(layer_structure(i + 1), layer_structure(i));
        Eigen::MatrixXd db = Eigen::MatrixXd::Zero(layer_structure(i + 1), 1);

        vLayerWeights.push_back(w);
        vLayerBiases.push_back(b);
        vLayerZetas.push_back(z);
        vLayerActivators.push_back(a);
        vLayerDeltas.push_back(d);
        vAccumulateLayerWeights.push_back(dw);
        vAccumulateLayerBiases.push_back(db);
    }
    vSampleInputs.clear();
    vSampleExpectations.clear();
    fAccumulateError = 0.0;
}

(2)DeepLearn::DeepLearn(std::string param_file)

参数是文件名,该文件存储已经训练了若干次的神经网络数据。

即,首次训练使用(1)构造函数,训练完毕后,网络参数会自动保存为文件,后续如果想接着训练,则可以传入文件名,使用(2)构造函数构造网络。

DeepLearn::DeepLearn(std::string param_file)
{
    std::ifstream f;
    f.open(param_file, std::ios::binary | std::ios::in);
    int row, col;
    f.read((char*)&row, sizeof(int));
    f.read((char*)&col, sizeof(int));
    mLayerStructure = Eigen::MatrixXi::Zero(row, col);
    f.read((char*)mLayerStructure.data(), sizeof(int)*row*col);
    int nLayers = mLayerStructure.size() - 1;
    for (int i = 0; i < nLayers; i++)
    {
        int r = mLayerStructure(i + 1);
        int c = mLayerStructure(i);
        Eigen::MatrixXd w = Eigen::MatrixXd::Zero(r, c);
        Eigen::MatrixXd b = Eigen::MatrixXd::Zero(r, 1);
        f.read((char*)w.data(), sizeof(double)*r*c);
        f.read((char*)b.data(), sizeof(double)*r);
        Eigen::MatrixXd z = Eigen::MatrixXd::Zero(r, 1);
        Eigen::MatrixXd a = Eigen::MatrixXd::Zero(r, 1);
        Eigen::MatrixXd d = Eigen::MatrixXd::Zero(r, 1);
        Eigen::MatrixXd dw = Eigen::MatrixXd::Zero(r, c);
        Eigen::MatrixXd db = Eigen::MatrixXd::Zero(r, 1);

        vLayerWeights.push_back(w);
        vLayerBiases.push_back(b);
        vLayerZetas.push_back(z);
        vLayerActivators.push_back(a);
        vLayerDeltas.push_back(d);
        vAccumulateLayerWeights.push_back(dw);
        vAccumulateLayerBiases.push_back(db);

    }
    f.close();
    vSampleInputs.clear();
    vSampleE

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值