簡體   English   中英

深度學習:Python中的反向傳播代碼

[英]Deep learning: the code for backpropagation in Python

我正在閱讀免費的在線書籍,並且正在為某些代碼而苦苦掙扎。

(代碼來自 Michael Nielsen)

class Network(object):

def update_mini_batch(self, mini_batch, eta):
        """Update the network's weights and biases by applying
        gradient descent using backpropagation to a single mini batch.
        The "mini_batch" is a list of tuples "(x, y)", and "eta"
        is the learning rate."""
        nabla_b = [np.zeros(b.shape) for b in self.biases]
        nabla_w = [np.zeros(w.shape) for w in self.weights]
        for x, y in mini_batch:
            delta_nabla_b, delta_nabla_w = self.backprop(x, y)
            nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
            nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
        self.weights = [w-(eta/len(mini_batch))*nw 
                        for w, nw in zip(self.weights, nabla_w)]
        self.biases = [b-(eta/len(mini_batch))*nb 
                       for b, nb in zip(self.biases, nabla_b)]


def backprop(self, x, y):
        nabla_b = [np.zeros(b.shape) for b in self.biases]
        nabla_w = [np.zeros(w.shape) for w in self.weights]
        # feedforward
        activation = x
        activations = [x] # list to store all the activations, layer by layer
        zs = [] # list to store all the z vectors, layer by layer
        for b, w in zip(self.biases, self.weights):
            z = np.dot(w, activation)+b
            zs.append(z)
            activation = sigmoid(z)
            activations.append(activation)
        # backward pass

因為它說mini_batch是一個元組列表(x, y) ,所以 function backpropx的參數是一個標量,對吧? 如果是這樣,因為w (權重)是一個矩陣(比如它的維度是n*p ),它的行在第l層有n神經元,列在l-1層有p個神經元。 那么, x必須是nx 1向量。 我感到困惑。

在本書的示例中,它使用了[2,3,1] ,即三層分別具有 2,3 和 1 個神經元。 因為第一層輸入,它有兩個元素。 所以第 2 層的權重矩陣有 3*2 維。 似乎x應該是一個長度為 2 的向量來與w進行矩陣乘法。

此外,關於C_x關於激活 a 的偏導數的代碼如下:

def cost_derivative(self, output_activations, y):
        """Return the vector of partial derivatives \partial C_x /
        \partial a for the output activations."""
        return (output_activations-y) 

我檢查了我理解的公式( output_activations-y )意味着成本的變化。 但這應該除以激活的變化嗎?

你可以幫幫我嗎?

因為它說“mini_batch”是元組“(x,y)”的列表,所以function反向傳播中x的參數是一個標量,對吧?

不,“批次”一詞對應於 python 列表。 在批處理/python 列表中有像(x, y)這樣的對,其中x表示輸入向量, y是 label。 x的形狀取決於您如何創建網絡 object。 [2, 3, 1]的情況下,x 應該是形狀為(2,)的向量。

但這應該除以激活的變化嗎?

不。

首先,您正在考慮的內容稱為“數值微分”。 既然你沒有成本的變化,你不應該通過激活的變化來分割它。

其次,作者使用的東西叫做“解析微分”。 比如說,你有一個 function f(x, y) = 0.5*(xy)^2 f w.r.t 的偏導數。 xxy 因此,您不需要將它除以x的變化。 但是,您需要注意實際使用的成本 function 以便導出正確的導數。 有時,如何計算這些值並不明顯。 在這種情況下,損失是均方誤差,如在線書籍中所述。


回復評論:

輸入為vector_x = (1,2,3)的訓練集

訓練集應該是一個包含一組訓練樣本的容器,其中每個訓練樣本由一個輸入和對應的 label 組成。 因此,小批量的示例可能是 python 列表,例如: [([0, 1], [1, 2]), ([2, 1], [3, 2])] ,這表明存在是兩個訓練樣本。 第一個是([0, 1], [1, 2]) ,第二個是([2, 1], [3, 2]) 以第一個為例,它的輸入是[0, 1] (形狀為(2,)的向量),它的 output 是[1, 2] ,也就是說,輸入和期望的 output 都是單次訓練樣本可以是向量。 您的問題( [(1,a),(2,b),(3,c)] )有一些含糊之處,所以我更喜歡我的解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM