神经网络反向传播(3)--C++代码实现

这里代码实现的是专栏开始我们用到的神经网络模型,需要强调一下:本文实现的参数更新主要是根据一个输出节点的偏差来进行更新,并非所有的输出节点的误差。

根据计算需要,定义两个结构体储存权重参数、偏置参数和各节点激活前后的输出

struct net_param{   //正向传播参数结构
    vector<vector<double>> input_to_hidden_weight;
    vector<vector<double>> hidden_to_output_weight;
    vector<double> bias_of_hidden;
    vector<double> bias_of_output;
};

struct trans_val{
    vector<double> hid_out;//隐藏层激活后输出
    vector<double> hid_net;//隐藏层未激活输出
    vector<double> out_net;//输出层未激活输出
};

具体代码如下,函数中action变量指的是具体更新参数的输出节点,w_lr_alpha为权重参数更新时的学习率,b_lr_gamma为偏置参数更新时的学习率。

void neural_BP(double error, net_param param, trans_val temp_val, vector<double> output, double W_lr_alpha,double b_lr_gamma,int action)
{
    cout<<"BP_start"<<endl;

    for(int i=0;i<param.hidden_to_output_weight[0].size();i++){
        param.hidden_to_output_weight[action][i]=param.hidden_to_output_weight[action][i]-W_lr_alpha*error*output[action]*(1-output[action])*temp_val.out_net[action];//update weight Hid_to_output
    }
    param.bias_of_output[action]=param.bias_of_output[action]-b_lr_gamma*error*output[action]*(1-output[action]);//update bias Hid_to_output
    for(int i=0;i<param.input_to_hidden_weight.size();i++){
        for(int j=0;j<param.input_to_hidden_weight[i].size();j++){
            param.input_to_hidden_weight[i][j]=param.input_to_hidden_weight[i][j]-W_lr_alpha*error*output[action]*(1-output[action])*param.hidden_to_output_weight[action][i]*(1-pow(temp_val.hid_out[i],2))*temp_val.hid_net[i];//update weight input_to_hid
        }
        param.bias_of_hidden[i]=param.bias_of_hidden[i]-b_lr_gamma*error*output[action]*(1-output[action])*param.hidden_to_output_weight[action][i]*(1-pow(temp_val.hid_out[i],2));//update bias input_to_hid
    }

//打印输出各参数
    for(int i=0;i<param.input_to_hidden_weight.size();i++){
        for(int j=0;j<param.input_to_hidden_weight[i].size();j++){
            cout<<param.input_to_hidden_weight[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<"hidden to output weight: "<<endl;
    for(int i=0;i<param.hidden_to_output_weight.size();i++){
        for(int j=0;j<param.hidden_to_output_weight[i].size();j++){
            cout<<param.hidden_to_output_weight[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<"bias of hidden: "<<endl;
    for(int i=0;i<param.bias_of_hidden.size();i++){
        cout<<param.bias_of_hidden[i]<<" ";
    }
    cout<<endl;
    cout<<"bias of output: "<<endl;
    for(int i=0;i<param.bias_of_output.size();i++){
        cout<<param.bias_of_output[i]<<" ";
    }
    cout<<endl;

}

通过以上代码,即可实现神经网络反向传播的参数更新。

不过关于代码的实现方式可能略简单粗暴,如果有更好的方式欢迎一起交流~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值