C++实现人工神经网络的类

这个学期我学习是神经网络课程,有很多的知识国内都不是很完善,而国外就有很大的进步,下面就是来自一本
<AI for Game>的电子版英文书.其中我就拿用面向对象写的C++类进行说明怎样编写神经网络程序.
神经网络的基本思想就是怎样去改变权值.
神经网络层类
class NeuralNetworkLayer
{
 
public:
     int               NumberOfNodes;
     int               NumberOfChildNodes;
     int               NumberOfParentNodes;
     double**          Weights;
     double**          WeightChanges;
     double*           NeuronValues;
     double*           DesiredValues;
     double*           Errors;
     double*           BiasWeights;
     double*           BiasValues;
     double            LearningRate;
     bool              LinearOutput;
     bool              UseMomentum;
     double            MomentumFactor;
     NeuralNetworkLayer*     ParentLayer;
     NeuralNetworkLayer*     ChildLayer;
     NeuralNetworkLayer();
     void     Initialize(int NumNodes,
                            NeuralNetworkLayer* parent,
                            NeuralNetworkLayer* child);
     void     CleanUp(void);
     void     RandomizeWeights(void);
     void     CalculateErrors(void);
     void     AdjustWeights(void);
     void     CalculateNeuronValues(void);
};
NumberOfNodes 层中神经元数目
NumberOfChildNodes     子层神经元数目
NumberOfParentNodes    父层神经元数目
Weights权值数组
WeightChanges 权值改变数组
NeuronValues   神经元值
DesiredValues 导师信号
Errors 误差
BiasWeights    偏差权值
LearningRate   学习效率
LinearOutput   是否线性输出
UseMomentum    是否有动力因素
MomentumFactor有动力因素的话,则动力因素大小值
ParentLayer    父层
ChildLayer     子层
构造函数
NeuralNetworkLayer::NeuralNetworkLayer()
{
 
     ParentLayer = NULL;
     ChildLayer = NULL;
     LinearOutput = false;
     UseMomentum = false;
      MomentumFactor = 0.9;
}
初始化类
void NeuralNetworkLayer::Initialize(int NumNodes,
                                               NeuralNetworkLayer* parent,
                                               NeuralNetworkLayer* child)
{
 
     int     i, j;
     // 分配内存
     NeuronValues = (double*) malloc(sizeof(double) *
                                               NumberOfNodes);
     DesiredValues = (double*) malloc(sizeof(double) *
                                               NumberOfNodes);
     Errors = (double*) malloc(sizeof(double) * NumberOfNodes);
     if(parent != NULL)
     {
 
          ParentLayer = parent;
     }
     if(child != NULL)
     {
 
          ChildLayer = child;
          Weights = (double**) malloc(sizeof(double*) *
                                              NumberOfNodes);
          WeightChanges = (double**) malloc(sizeof(double*) *
                                              NumberOfNodes);
          for(i = 0; i<NumberOfNodes; i++)
          {
 
               Weights[i] = (double*) malloc(sizeof(double) *                                                     NumberOfChildNodes);
               WeightChanges[i] = (double*) malloc(sizeof(double) *                                                         NumberOfChildNodes);
          }
          BiasValues = (double*) malloc(sizeof(double) *
                                        NumberOfChildNodes);
          BiasWeights = (double*) malloc(sizeof(double) *
                                         NumberOfChildNodes);
     } else {
 
          Weights = NULL;
          BiasValues = NULL;
          BiasWeights = NULL;
          WeightChanges = NULL;
     }
     // 确保所有归 0
     for(i=0; i<NumberOfNodes; i++)
     {
 
          NeuronValues[i] = 0;
          DesiredValues[i] = 0;
          Errors[i] = 0;
          if(ChildLayer != NULL)
               for(j=0; j<NumberOfChildNodes; j++)
               {
 
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值