-1

我在網上找到一個示例,其中包含一個返回傳播錯誤並調整權重的方法。我想知道這是如何工作的,以及使用什麼樣的權重更新算法。它可能是梯度下降?反向傳播算法

/** 
    * all output propagate back 
    * 
    * @param expectedOutput 
    *   first calculate the partial derivative of the error with 
    *   respect to each of the weight leading into the output neurons 
    *   bias is also updated here 
    */ 
    public void applyBackpropagation(double expectedOutput[]) { 

    // error check, normalize value ]0;1[ 
    for (int i = 0; i < expectedOutput.length; i++) { 
     double d = expectedOutput[i]; 
     if (d < 0 || d > 1) { 
      if (d < 0) 
       expectedOutput[i] = 0 + epsilon; 
      else 
       expectedOutput[i] = 1 - epsilon; 
     } 
    } 

    int i = 0; 
    for (Neuron n : outputLayer) { 
     ArrayList<Connection> connections = n.getAllInConnections(); 
     for (Connection con : connections) { 
      double ak = n.getOutput(); 
      double ai = con.leftNeuron.getOutput(); 
      double desiredOutput = expectedOutput[i]; 

      double partialDerivative = -ak * (1 - ak) * ai 
        * (desiredOutput - ak); 
      double deltaWeight = -learningRate * partialDerivative; 
      double newWeight = con.getWeight() + deltaWeight; 
      con.setDeltaWeight(deltaWeight); 
      con.setWeight(newWeight + momentum * con.getPrevDeltaWeight()); 
     } 
     i++; 
    } 

    // update weights for the hidden layer 
    for (Neuron n : hiddenLayer) { 
     ArrayList<Connection> connections = n.getAllInConnections(); 
     for (Connection con : connections) { 
      double aj = n.getOutput(); 
      double ai = con.leftNeuron.getOutput(); 
      double sumKoutputs = 0; 
      int j = 0; 
      for (Neuron out_neu : outputLayer) { 
       double wjk = out_neu.getConnection(n.id).getWeight(); 
       double desiredOutput = (double) expectedOutput[j]; 
       double ak = out_neu.getOutput(); 
       j++; 
       sumKoutputs = sumKoutputs 
         + (-(desiredOutput - ak) * ak * (1 - ak) * wjk); 
      } 

      double partialDerivative = aj * (1 - aj) * ai * sumKoutputs; 
      double deltaWeight = -learningRate * partialDerivative; 
      double newWeight = con.getWeight() + deltaWeight; 
      con.setDeltaWeight(deltaWeight); 
      con.setWeight(newWeight + momentum * con.getPrevDeltaWeight()); 
     } 
    } 
} 
+1

據我所知,這代表了一個隱藏層的MLP。如果沒有能夠遵循這個代碼,我猜測反向傳播會導致梯度下降。你的問題到底是什麼?如果你可以使用這個代碼或者梯度下降是什麼? – Tim 2012-02-24 13:44:03

回答

1

這個看起來醜陋的文章似乎在描述算法的完全相同版本:http://www.speech.sri.com/people/anand/771/html/node37.html。我在大學的論文中使用了相同的公式,但遺憾的是:a)他們沒有在線提供; b)他們的語言你不會理解。

至於梯度下降,該算法類似於梯度下降,但不能保證達到最佳位置。在每個步驟中,通過改變網絡邊緣的值來改變訓練樣本值的概率。

2

在我看來,這個解決方案使用stochastic gradient descent。它與常規梯度下降之間的主要區別在於,對於每個示例,梯度都是近似的,而不是對所有示例計算梯度,然後選擇最佳方向。這是實施反向傳播的常用方法,甚至對梯度下降有一定優勢(可避免一些局部最小值)。我相信這篇文章也會解釋什麼是想法,還有很多其他文章解釋了後向傳播背後的主要思想。

+0

如果這是SGD,那麼訓練樣本的循環在哪裏?在輸出單元上有一個循環,使'i'增加,就好像輸出單元的數量等於訓練樣本的數量,這看起來完全荒謬。 – 2012-02-24 13:52:48

+1

我認爲應該有一個循環調用這個函數,實際上函數會將權重調整爲只有一個樣本。這可以從該函數的唯一輸入是所有輸出感知器的預期值(每個神經元只有一個值)的事實中得到推導。 – 2012-02-24 14:02:19

+0

啊,對!我期待'expectedOutput'有長度'n_samples'。 – 2012-02-24 14:30:55