使用神經網絡進行樣本訓練,要實現隨機梯度下降算法。這里我根據麥子學院彭亮老師的講解,總結如下,(神經網絡的結構在另一篇博客中已經定義):
def SGD(self, training_data, epochs, mini_batch_size, eta, test_data=None): if test_data: n_test = len(test_data)#有多少個測試集 n = len(training_data) for j in xrange(epochs): random.shuffle(training_data) mini_batches = [ training_data[k:k+mini_batch_size] for k in xrange(0,n,mini_batch_size)] for mini_batch in mini_batches: self.update_mini_batch(mini_batch, eta) if test_data: print "Epoch {0}: {1}/{2}".format(j, self.evaluate(test_data),n_test) else: print "Epoch {0} complete".format(j) 其中training_data是訓練集,是由很多的tuples(元組)組成。每一個元組(x,y)代表一個實例,x是圖像的向量表示,y是圖像的類別。
epochs表示訓練多少輪。
mini_batch_size表示每一次訓練的實例個數。
eta表示學習率。
test_data表示測試集。
比較重要的函數是self.update_mini_batch,他是更新權重和偏置的關鍵函數,接下來就定義這個函數。
def update_mini_batch(self, mini_batch,eta): 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_nable_w = self.backprop(x,y)#目標函數對b和w的偏導數 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)]#累加b和w #最終更新權重為 self.weights = [w-(eta/len(mini_batch))*nw for w, nw in zip(self.weights, nabla_w)] self.baises = [b-(eta/len(mini_batch))*nb for b, nb in zip(self.baises, nabla_b)]
這個update_mini_batch函數根據你傳入的一些數據進行更新神經網絡的權重和偏置。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。
新聞熱點
疑難解答