如何实现批量反向传播

时间:2018-12-30 22:40:52

标签: c++ machine-learning backpropagation

我已经成功实现了随机反向传播,并且我试图提高其准确性。我注意到批量反向传播似乎更受欢迎,我想尝试一下是否可以提高网络的准确性,但是我似乎还不知道如何实现它。 “分批反向传播”是指反向传播,其中权重和偏差仅在完成小批量或历元之后才更新,而不是在每次输入后进行更新。

我的理解是,您总结需要对每个权重和偏见进行的更改,并在一批培训示例的末尾应用更改。我基本上没有改变最初的随机反向支持代码,而是将更改直接应用到权重和偏差上,而不是直接将更改应用到权重和偏差上,然后再使用该缓冲区更新权重和偏差。还是我应该总结每个训练示例的成本,然后在批处理的最后进行反向传播?如果是这种情况,那么如果成本是一批输入的成本的总和,我该如何使用中间结果(每一层的输出矢量)?

//Called after each calculation on a training example
void ML::NeuralNetwork::learnBatch(const Matrix & calc, const Matrix & real) const {
  ML::Matrix cost = 2 * (calc - real);
  for (int i = weights.size() - 1; i >= 0; --i) {
      //Each element in results is the column vector output for each layer
      //ElementMultiply() returns Hadamard Product
      ML::Matrix dCdB = cost.elementMultiply(ML::sigDerivative(weights[i] * results[i] + biases[i]));
      ML::Matrix dCdW = dCdB * results[i].transpose();
      cost = weights[i].transpose() * dCdB;

      sumWeights[i] += learningRate * dCdW; //Scalar multiplication
      sumBiases[i] += learningRate * dCdB;
      /* Original Code:
       * weights[i] -= learningRate * dCdW;
       * biases[i] -= learningRate * dCdB;
      */ 
  }

}
//Called at the end of a batch
void ML::NeuralNetwork::update() {

  for (int i = 0; i < weights.size(); ++i) {
      weights[i] -= sumWeights[i];
      biases[i] -= sumBiases[i];

      //Sets all elements in the matrix to 0
      sumWeights[i].zero();
      sumBiases[i].zero();

  }
}

除了添加update()函数外,我对工作中的随机反向支持代码的确没有多大改变。使用我当前的批次反向传播代码,即使迭代200多个批次,神经网络也永远不会学习并且始终获得0个正确的输出。有我不明白的东西吗?

所有帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

在批次反向传播中,您对每个样本的反向传播的贡献求和。

换句话说,结果梯度就是每个样本的梯度之和。

相关问题