創(chuàng)建了神經(jīng)網(wǎng)絡后,我們需要進行權重和偏差的初始化。到現(xiàn)在,我們一直是根據(jù)在第一章中介紹的那樣進行初始化。提醒你一下,之前的方式就是根據(jù)獨立的均值為 $$0$$,標準差為 $$1$$ 的高斯隨機變量隨機采樣作為權重和偏差的初始值。這個方法工作的還不錯,但是非常 ad hoc,所以我們需要尋找一些更好的方式來設置我們網(wǎng)絡的初始化權重和偏差,這對于幫助網(wǎng)絡學習速度的提升很有價值。
結果表明,我們可以比使用正規(guī)化的高斯分布效果更好。為什么?假設我們使用一個很多的輸入神經(jīng)元,比如說 $$1000$$。假設,我們已經(jīng)使用正規(guī)化的高斯分布初始化了連接第一隱藏層的權重?,F(xiàn)在我將注意力集中在這一層的連接權重上,忽略網(wǎng)絡其他部分:
http://wiki.jikexueyuan.com/project/neural-networks-and-deep-learning-zh-cn/images/123.png" alt="" />
我們?yōu)榱撕喕?,假設,我們使用訓練樣本 x 其中一半的神經(jīng)元值為 $$0$$,另一半為 $$1$$。下面的觀點也是可以更加廣泛地應用,但是你可以從特例中獲得背后的思想。讓我們考慮帶權和 $$z=\sum_j w_j x_j + b$$ 的隱藏元輸入。其中 $$500$$ 個項消去了,因為對應的輸入 $$x_j=0$$。所以 $$z$$ 是 $$501$$ 個正規(guī)化的高斯隨機變量的和,包含 $$500$$ 個權重項和額外的 $$1$$ 個偏差項。因此 $$z$$ 本身是一個均值為 $$0$$ 標準差為 $$\sqrt{501}\approx 22.4$$ 的分布。$$z$$ 其實有一個非常寬的高斯分布,不是非常尖的形狀:
http://wiki.jikexueyuan.com/project/neural-networks-and-deep-learning-zh-cn/images/124.png" alt="" />
尤其是,我們可以從這幅圖中看出 $$|z|$$ 會變得非常的大,比如說 $$z\gg1$$ 或者 $$z\ll 1$$。如果是這樣,輸出 $$\sigma(z)$$ 就會接近 $$1$$ 或者 $$0$$。也就表示我們的隱藏元會飽和。所以當出現(xiàn)這樣的情況時,在權重中進行微小的調整僅僅會給隱藏元的激活值帶來極其微弱的改變。而這種微弱的改變也會影響網(wǎng)絡中剩下的神經(jīng)元,然后會帶來相應的代價函數(shù)的改變。結果就是,這些權重在我們進行梯度下降算法時會學習得非常緩慢。這其實和我們前面討論的問題差不多,前面的情況是輸出神經(jīng)元在錯誤的值上飽和導致學習的下降。我們之前通過代價函數(shù)的選擇解決了前面的問題。不幸的是,盡管那種方式在輸出神經(jīng)元上有效,但對于隱藏元的飽和卻一點作用都沒有。
我已經(jīng)研究了第一隱藏層的權重輸入。當然,類似的論斷也對后面的隱藏層有效:如果權重也是用正規(guī)化的高斯分布進行初始化,那么激活值將會接近 $$0$$ 或者 $$1$$,學習速度也會相當緩慢。
還有可以幫助我們進行更好地初始化么,能夠避免這種類型的飽和,最終避免學習速度的下降?假設我們有一個有 $$n{in}$$ 個輸入權重的神經(jīng)元。我們會使用均值為 $$0$$ 標準差為 $$1/\sqrt{n{in}}$$ 的高斯分布初始化這些權重。也就是說,我們會向下擠壓高斯分布,讓我們的神經(jīng)元更不可能飽和。我們會繼續(xù)使用均值為 $$0$$ 標準差為 $$1$$ 的高斯分布來對偏差進行初始化,后面會告訴你原因。有了這些設定,帶權和 $$z=\sum_j w_j x_j + b$$ 仍然是一個均值為 $$0$$ 不過有很陡的峰頂?shù)母咚狗植?。假設,我們有 $$500$$ 個值為 $$0$$ 的輸入和$$500$$ 個值為 $$1$$ 的輸入。那么很容證明 $$z$$ 是服從均值為 $$0$$ 標準差為 $$\sqrt{3/2} = 1.22$$ 的高斯分布。這圖像要比以前陡得多,所以即使我已經(jīng)對橫坐標進行壓縮為了進行更直觀的比較:
http://wiki.jikexueyuan.com/project/neural-networks-and-deep-learning-zh-cn/images/125.png" alt="" />
這樣的一個神經(jīng)元更不可能飽和,因此也不大可能遇到學習速度下降的問題。
我在上面提到,我們使用同樣的方式對偏差進行初始化,就是使用均值為 $$0$$ 標準差為 $$1$$ 的高斯分布來對偏差進行初始化。這其實是可行的,因為這樣并不會讓我們的神經(jīng)網(wǎng)絡更容易飽和。實際上,其實已經(jīng)避免了飽和的問題的話,如何初始化偏差影響不大。有些人將所有的偏差初始化為 $$0$$,依賴梯度下降來學習合適的偏差。但是因為差別不是很大,我們后面還會按照前面的方式來進行初始化。
讓我們在 MNIST 數(shù)字分類任務上比較一下新舊兩種權重初始化方式。同樣,還是使用 $$30$$ 個隱藏元,minibatch 的大小為 $$30$$,規(guī)范化參數(shù) $$\lambda=5.0$$,然后是交叉熵代價函數(shù)。我們將學習率從 $$\eta=0.5$$ 調整到 $$0.1$$,因為這樣會讓結果在圖像中表現(xiàn)得更加明顯。我們先使用舊的初始化方法訓練:
>>> import mnist_loader
>>> training_data, validation_data, test_data = \
... mnist_loader.load_data_wrapper()
>>> import network2
>>> net = network2.Network([784, 30, 10], cost=network2.CrossEntropyCost)
>>> net.large_weight_initializer()
>>> net.SGD(training_data, 30, 10, 0.1, lmbda = 5.0,
... evaluation_data=validation_data,
... monitor_evaluation_accuracy=True)
我們也使用新方法來進行權重的初始化。這實際上還要更簡單,因為 network2's 默認方式就是使用新的方法。這意味著我們可以丟掉 net.large_weight_initializer() 調用:
>>> net = network2.Network([784, 30, 10], cost=network2.CrossEntropyCost)
>>> net.SGD(training_data, 30, 10, 0.1, lmbda = 5.0,
... evaluation_data=validation_data,
... monitor_evaluation_accuracy=True)
將結果用圖展示出來,就是:
http://wiki.jikexueyuan.com/project/neural-networks-and-deep-learning-zh-cn/images/126.png" alt="" />
兩種情形下,我們在 96% 的準確度上重合了。最終的分類準確度幾乎完全一樣。但是新的初始化技術帶來了速度的提升。在第一種初始化方式的分類準確度在 87% 一下,而新的方法已經(jīng)幾乎達到了 93%??雌饋淼那闆r就是我們新的關于權重初始化的方式將訓練帶到了一個新的境界,讓我們能夠更加快速地得到好的結果。同樣的情況在 $$100$$ 個神經(jīng)元的設定中也出現(xiàn)了:
http://wiki.jikexueyuan.com/project/neural-networks-and-deep-learning-zh-cn/images/127.png" alt="" />
在這個情況下,兩個曲線并沒有重合。然而,我做的實驗發(fā)現(xiàn)了其實就在一些額外的回合后(這里沒有展示)準確度其實也是幾乎相同的。所以,基于這些實驗,看起來提升的權重初始化僅僅會加快訓練,不會改變網(wǎng)絡的性能。然而,在第四張,我們會看到一些例子里面使用 $$1/\sqrt{n_{in}}$$ 權重初始化的長期運行的結果要顯著更優(yōu)。因此,不僅僅能夠帶來訓練速度的加快,有時候在最終性能上也有很大的提升。
$$1/\sqrt{n{in}}$$ 的權重初始化方法幫助我們提升了神經(jīng)網(wǎng)絡學習的方式。其他的權重初始化技術同樣也有,很多都是基于這個基本的思想。我不會在這里給出其他的方法,因為 $$1/\sqrt{n{in}}$$ 已經(jīng)可以工作得很好了。如果你對另外的思想感興趣,我推薦你看看在 $$2012$$ 年的 Yoshua Bengio 的論文的 $$14$$ 和 $$15$$ 頁,以及相關的參考文獻。
Practical Recommendations for Gradient-Based Training of Deep Architectures, by Yoshua Bengio (2012).
讓我們實現(xiàn)本章討論過的這些想法。我們將寫出一個新的程序,network2.py,這是一個對第一章中開發(fā)的 network.py 的改進版本。如果你沒有仔細看過 network.py,那你可能會需要重讀前面關于這段代碼的討論。僅僅 $$74$$ 行代碼,也很易懂。
和 network.py 一樣,主要部分就是 Network 類了,我們用這個來表示神經(jīng)網(wǎng)絡。使用一個 sizes 的列表來對每個對應層進行初始化,默認使用交叉熵作為代價 cost 參數(shù):
class Network(object):
def __init__(self, sizes, cost=CrossEntropyCost):
self.num_layers = len(sizes)
self.sizes = sizes
self.default_weight_initializer()
self.cost=cost
__init__ 方法的和 network.py 中一樣,可以輕易弄懂。但是下面兩行是新的,我們需要知道他們到底做了什么。
我們先看看 default_weight_initializer 方法,使用了我們新式改進后的初始化權重方法。如我們已經(jīng)看到的,使用了均值為 $$0$$ 而標準差為 $$1/\sqrt{n}$$,$$n$$ 為對應的輸入連接個數(shù)。我們使用均值為 $$0$$ 而標準差為 $$1$$ 的高斯分布來初始化偏差。下面是代碼:
def default_weight_initializer(self):
self.biases = [np.random.randn(y, 1) for y in self.sizes[1:]]
self.weights = [np.random.randn(y, x)/np.sqrt(x)
for x, y in zip(self.sizes[:-1], self.sizes[1:])]
為了理解這段代碼,需要知道 np 就是進行線性代數(shù)運算的 Numpy 庫。我們在程序的開頭會 import Numpy。同樣我們沒有對第一層的神經(jīng)元的偏差進行初始化。因為第一層其實是輸入層,所以不需要引入任何的偏差。我們在 network.py 中做了完全一樣的事情。
作為 default_weight_initializer 的補充,我們同樣包含了一個 large_weight_initializer 方法。這個方法使用了第一章中的觀點初始化了權重和偏差。代碼也就僅僅是和default_weight_initializer差了一點點了:
def large_weight_initializer(self):
self.biases = [np.random.randn(y, 1) for y in self.sizes[1:]]
self.weights = [np.random.randn(y, x)
for x, y in zip(self.sizes[:-1], self.sizes[1:])]
我將 larger_weight_initializer 方法包含進來的原因也就是使得跟第一章的結果更容易比較。我并沒有考慮太多的推薦使用這個方法的實際情景。
初始化方法 __init__ 中的第二個新的東西就是我們初始化了 cost 屬性。為了理解這個工作的原理,讓我們看一下用來表示交叉熵代價的類:
class CrossEntropyCost(object):
@staticmethod
def fn(a, y):
return np.sum(np.nan_to_num(-y*np.log(a)-(1-y)*np.log(1-a)))
@staticmethod
def delta(z, a, y):
return (a-y)
讓我們分解一下。第一個看到的是:即使使用的是交叉熵,數(shù)學上看,就是一個函數(shù),這里我們用 Python 的類而不是 Python 函數(shù)實現(xiàn)了它。為什么這樣做呢?答案就是代價函數(shù)在我們的網(wǎng)絡中扮演了兩種不同的角色。明顯的角色就是代價是輸出激活值 $$a$$ 和目標輸出 $$y$$ 差距優(yōu)劣的度量。這個角色通過 CrossEntropyCost.fn 方法來扮演。(注意,np.nan_to_num 調用確保了 Numpy 正確處理接近 $$0$$ 的對數(shù)值)但是代價函數(shù)其實還有另一個角色?;叵氲诙轮羞\行反向傳播算法時,我們需要計算網(wǎng)絡輸出誤差,$$\delta^L$$。這種形式的輸出誤差依賴于代價函數(shù)的選擇:不同的代價函數(shù),輸出誤差的形式就不同。對于交叉熵函數(shù),輸出誤差就如公式(66)所示:
http://wiki.jikexueyuan.com/project/neural-networks-and-deep-learning-zh-cn/images/128.png" alt="" />
所以,我們定義了第二個方法,CrossEntropyCost.delta,目的就是讓網(wǎng)絡知道如何進行輸出誤差的計算。然后我們將這兩個組合在一個包含所有需要知道的有關代價函數(shù)信息的類中。
類似地,network2.py 還包含了一個表示二次代價函數(shù)的類。這個是用來和第一章的結果進行對比的,因為后面我們幾乎都在使用交叉函數(shù)。代碼如下。QuadraticCost.fn 方法是關于網(wǎng)絡輸出 $$a$$ 和目標輸出 $$y$$ 的二次代價函數(shù)的直接計算結果。由 QuadraticCost.delta 返回的值就是二次代價函數(shù)的誤差。
class QuadraticCost(object):
@staticmethod
def fn(a, y):
return 0.5*np.linalg.norm(a-y)**2
@staticmethod
def delta(z, a, y):
return (a-y) * sigmoid_prime(z)
現(xiàn)在,我們理解了 network2.py 和 network.py 兩個實現(xiàn)之間的主要差別。都是很簡單的東西。還有一些更小的變動,下面我們會進行介紹,包含 L2 規(guī)范化的實現(xiàn)。在講述規(guī)范化之前,我們看看 network2.py 完整的實現(xiàn)代碼。你不需要太仔細地讀遍這些代碼,但是對整個結構尤其是文檔中的內(nèi)容的理解是非常重要的,這樣,你就可以理解每段程序所做的工作。當然,你也可以隨自己意愿去深入研究!如果你迷失了理解,那么請讀讀下面的講解,然后再回到代碼中。不多說了,給代碼:
"""network2.py
~~~~~~~~~~~~~~
An improved version of network.py, implementing the stochastic
gradient descent learning algorithm for a feedforward neural network.
Improvements include the addition of the cross-entropy cost function,
regularization, and better initialization of network weights. Note
that I have focused on making the code simple, easily readable, and
easily modifiable. It is not optimized, and omits many desirable
features.
"""
#### Libraries
# Standard library
import json
import random
import sys
# Third-party libraries
import numpy as np
#### Define the quadratic and cross-entropy cost functions
class QuadraticCost(object):
@staticmethod
def fn(a, y):
"""Return the cost associated with an output ``a`` and desired output
``y``.
"""
return 0.5*np.linalg.norm(a-y)**2
@staticmethod
def delta(z, a, y):
"""Return the error delta from the output layer."""
return (a-y) * sigmoid_prime(z)
class CrossEntropyCost(object):
@staticmethod
def fn(a, y):
"""Return the cost associated with an output ``a`` and desired output
``y``. Note that np.nan_to_num is used to ensure numerical
stability. In particular, if both ``a`` and ``y`` have a 1.0
in the same slot, then the expression (1-y)*np.log(1-a)
returns nan. The np.nan_to_num ensures that that is converted
to the correct value (0.0).
"""
return np.sum(np.nan_to_num(-y*np.log(a)-(1-y)*np.log(1-a)))
@staticmethod
def delta(z, a, y):
"""Return the error delta from the output layer. Note that the
parameter ``z`` is not used by the method. It is included in
the method's parameters in order to make the interface
consistent with the delta method for other cost classes.
"""
return (a-y)
#### Main Network class
class Network(object):
def __init__(self, sizes, cost=CrossEntropyCost):
"""The list ``sizes`` contains the number of neurons in the respective
layers of the network. For example, if the list was [2, 3, 1]
then it would be a three-layer network, with the first layer
containing 2 neurons, the second layer 3 neurons, and the
third layer 1 neuron. The biases and weights for the network
are initialized randomly, using
``self.default_weight_initializer`` (see docstring for that
method).
"""
self.num_layers = len(sizes)
self.sizes = sizes
self.default_weight_initializer()
self.cost=cost
def default_weight_initializer(self):
"""Initialize each weight using a Gaussian distribution with mean 0
and standard deviation 1 over the square root of the number of
weights connecting to the same neuron. Initialize the biases
using a Gaussian distribution with mean 0 and standard
deviation 1.
Note that the first layer is assumed to be an input layer, and
by convention we won't set any biases for those neurons, since
biases are only ever used in computing the outputs from later
layers.
"""
self.biases = [np.random.randn(y, 1) for y in self.sizes[1:]]
self.weights = [np.random.randn(y, x)/np.sqrt(x)
for x, y in zip(self.sizes[:-1], self.sizes[1:])]
def large_weight_initializer(self):
"""Initialize the weights using a Gaussian distribution with mean 0
and standard deviation 1. Initialize the biases using a
Gaussian distribution with mean 0 and standard deviation 1.
Note that the first layer is assumed to be an input layer, and
by convention we won't set any biases for those neurons, since
biases are only ever used in computing the outputs from later
layers.
This weight and bias initializer uses the same approach as in
Chapter 1, and is included for purposes of comparison. It
will usually be better to use the default weight initializer
instead.
"""
self.biases = [np.random.randn(y, 1) for y in self.sizes[1:]]
self.weights = [np.random.randn(y, x)
for x, y in zip(self.sizes[:-1], self.sizes[1:])]
def feedforward(self, a):
"""Return the output of the network if ``a`` is input."""
for b, w in zip(self.biases, self.weights):
a = sigmoid(np.dot(w, a)+b)
return a
def SGD(self, training_data, epochs, mini_batch_size, eta,
lmbda = 0.0,
evaluation_data=None,
monitor_evaluation_cost=False,
monitor_evaluation_accuracy=False,
monitor_training_cost=False,
monitor_training_accuracy=False):
"""Train the neural network using mini-batch stochastic gradient
descent. The ``training_data`` is a list of tuples ``(x, y)``
representing the training inputs and the desired outputs. The
other non-optional parameters are self-explanatory, as is the
regularization parameter ``lmbda``. The method also accepts
``evaluation_data``, usually either the validation or test
data. We can monitor the cost and accuracy on either the
evaluation data or the training data, by setting the
appropriate flags. The method returns a tuple containing four
lists: the (per-epoch) costs on the evaluation data, the
accuracies on the evaluation data, the costs on the training
data, and the accuracies on the training data. All values are
evaluated at the end of each training epoch. So, for example,
if we train for 30 epochs, then the first element of the tuple
will be a 30-element list containing the cost on the
evaluation data at the end of each epoch. Note that the lists
are empty if the corresponding flag is not set.
"""
if evaluation_data: n_data = len(evaluation_data)
n = len(training_data)
evaluation_cost, evaluation_accuracy = [], []
training_cost, training_accuracy = [], []
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, lmbda, len(training_data))
print "Epoch %s training complete" % j
if monitor_training_cost:
cost = self.total_cost(training_data, lmbda)
training_cost.append(cost)
print "Cost on training data: {}".format(cost)
if monitor_training_accuracy:
accuracy = self.accuracy(training_data, convert=True)
training_accuracy.append(accuracy)
print "Accuracy on training data: {} / {}".format(
accuracy, n)
if monitor_evaluation_cost:
cost = self.total_cost(evaluation_data, lmbda, convert=True)
evaluation_cost.append(cost)
print "Cost on evaluation data: {}".format(cost)
if monitor_evaluation_accuracy:
accuracy = self.accuracy(evaluation_data)
evaluation_accuracy.append(accuracy)
print "Accuracy on evaluation data: {} / {}".format(
self.accuracy(evaluation_data), n_data)
print
return evaluation_cost, evaluation_accuracy, \
training_cost, training_accuracy
def update_mini_batch(self, mini_batch, eta, lmbda, n):
"""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)``, ``eta`` is the
learning rate, ``lmbda`` is the regularization parameter, and
``n`` is the total size of the training data set.
"""
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 = [(1-eta*(lmbda/n))*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):
"""Return a tuple ``(nabla_b, nabla_w)`` representing the
gradient for the cost function C_x. ``nabla_b`` and
``nabla_w`` are layer-by-layer lists of numpy arrays, similar
to ``self.biases`` and ``self.weights``."""
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
delta = (self.cost).delta(zs[-1], activations[-1], y)
nabla_b[-1] = delta
nabla_w[-1] = np.dot(delta, activations[-2].transpose())
# Note that the variable l in the loop below is used a little
# differently to the notation in Chapter 2 of the book. Here,
# l = 1 means the last layer of neurons, l = 2 is the
# second-last layer, and so on. It's a renumbering of the
# scheme in the book, used here to take advantage of the fact
# that Python can use negative indices in lists.
for l in xrange(2, self.num_layers):
z = zs[-l]
sp = sigmoid_prime(z)
delta = np.dot(self.weights[-l+1].transpose(), delta) * sp
nabla_b[-l] = delta
nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())
return (nabla_b, nabla_w)
def accuracy(self, data, convert=False):
"""Return the number of inputs in ``data`` for which the neural
network outputs the correct result. The neural network's
output is assumed to be the index of whichever neuron in the
final layer has the highest activation.
The flag ``convert`` should be set to False if the data set is
validation or test data (the usual case), and to True if the
data set is the training data. The need for this flag arises
due to differences in the way the results ``y`` are
represented in the different data sets. In particular, it
flags whether we need to convert between the different
representations. It may seem strange to use different
representations for the different data sets. Why not use the
same representation for all three data sets? It's done for
efficiency reasons -- the program usually evaluates the cost
on the training data and the accuracy on other data sets.
These are different types of computations, and using different
representations speeds things up. More details on the
representations can be found in
mnist_loader.load_data_wrapper.
"""
if convert:
results = [(np.argmax(self.feedforward(x)), np.argmax(y))
for (x, y) in data]
else:
results = [(np.argmax(self.feedforward(x)), y)
for (x, y) in data]
return sum(int(x == y) for (x, y) in results)
def total_cost(self, data, lmbda, convert=False):
"""Return the total cost for the data set ``data``. The flag
``convert`` should be set to False if the data set is the
training data (the usual case), and to True if the data set is
the validation or test data. See comments on the similar (but
reversed) convention for the ``accuracy`` method, above.
"""
cost = 0.0
for x, y in data:
a = self.feedforward(x)
if convert: y = vectorized_result(y)
cost += self.cost.fn(a, y)/len(data)
cost += 0.5*(lmbda/len(data))*sum(
np.linalg.norm(w)**2 for w in self.weights)
return cost
def save(self, filename):
"""Save the neural network to the file ``filename``."""
data = {"sizes": self.sizes,
"weights": [w.tolist() for w in self.weights],
"biases": [b.tolist() for b in self.biases],
"cost": str(self.cost.__name__)}
f = open(filename, "w")
json.dump(data, f)
f.close()
#### Loading a Network
def load(filename):
"""Load a neural network from the file ``filename``. Returns an
instance of Network.
"""
f = open(filename, "r")
data = json.load(f)
f.close()
cost = getattr(sys.modules[__name__], data["cost"])
net = Network(data["sizes"], cost=cost)
net.weights = [np.array(w) for w in data["weights"]]
net.biases = [np.array(b) for b in data["biases"]]
return net
#### Miscellaneous functions
def vectorized_result(j):
"""Return a 10-dimensional unit vector with a 1.0 in the j'th position
and zeroes elsewhere. This is used to convert a digit (0...9)
into a corresponding desired output from the neural network.
"""
e = np.zeros((10, 1))
e[j] = 1.0
return e
def sigmoid(z):
"""The sigmoid function."""
return 1.0/(1.0+np.exp(-z))
def sigmoid_prime(z):
"""Derivative of the sigmoid function."""
return sigmoid(z)*(1-sigmoid(z))
有個更加有趣的變動就是在代碼中增加了 L2 規(guī)范化。盡管這是一個主要的概念上的變動,在實現(xiàn)中其實相當簡單。對大部分情況,僅僅需要傳遞參數(shù) lmbda 到不同的方法中,主要是 Network.SGD 方法。實際上的工作就是一行代碼的事在 Network.update_mini_batch 的倒數(shù)第四行。這就是我們改動梯度下降規(guī)則來進行權重下降的地方。盡管改動很小,但其對結果影響卻很大!
其實這種情況在神經(jīng)網(wǎng)絡中實現(xiàn)一些新技術的常見現(xiàn)象。我們花費了近千字的篇幅來討論規(guī)范化。概念的理解非常微妙困難。但是添加到程序中的時候卻如此簡單。精妙復雜的技術可以通過微小的代碼改動就可以實現(xiàn)了。
另一個微小卻重要的改動是隨機梯度下降方法的幾個標志位的增加。這些標志位讓我們可以對在代價和準確度的監(jiān)控變得可能。這些標志位默認是 False 的,但是在我們例子中,已經(jīng)被置為 True 來監(jiān)控 Network 的性能。另外,network2.py 中的 Network.SGD 方法返回了一個四元組來表示監(jiān)控的結果。我們可以這樣使用:
>>> evaluation_cost, evaluation_accuracy,
... training_cost, training_accuracy = net.SGD(training_data, 30, 10, 0.5,
... lmbda = 5.0,
... evaluation_data=validation_data,
... monitor_evaluation_accuracy=True,
... monitor_evaluation_cost=True,
... monitor_training_accuracy=True,
... monitor_training_cost=True)
所以,比如 evaluation_cost 將會是一個 $$30$$ 個元素的列表其中包含了每個回合在驗證集合上的代價函數(shù)值。這種類型的信息在理解網(wǎng)絡行為的過程中特別有用。比如,它可以用來畫出展示網(wǎng)絡隨時間學習的狀態(tài)。其實,這也是我在前面的章節(jié)中展示性能的方式。然而要注意的是如果任何標志位都沒有設置的話,對應的元組中的元素就是空列表。
另一個增加項就是在 Network.save 方法中的代碼,用來將 Network 對象保存在磁盤上,還有一個載回內(nèi)存的函數(shù)。這兩個方法都是使用 JSON 進行的,而非 Python 的 pickle 或者 cPickle 模塊——這些通常是 Python 中常見的保存和裝載對象的方法。使用 JSON 的原因是,假設在未來某天,我們想改變 Network 類來允許非 sigmoid 的神經(jīng)元。對這個改變的實現(xiàn),我們最可能是改變在 Network.__init__ 方法中定義的屬性。如果我們簡單地 pickle 對象,會導致 load 函數(shù)出錯。使用 JSON 進行序列化可以顯式地讓老的 Network 仍然能夠 load。
其他也還有一些微小的變動。但是那些只是 network.py 的微調。結果就是把程序從 $$74$$ 行增長到了 $$152$$ 行。
network.py 中的 Network.cost_derivative 方法。這個方法是為二次代價函數(shù)寫的。怎樣修改可以用于交叉熵代價函數(shù)上?你能不能想到可能在交叉熵函數(shù)上遇到的問題?在 network2.py 中,我們已經(jīng)去掉了 Network.cost_derivative 方法,將其集成進了 CrossEntropyCost.delta 方法中。請問,這樣是如何解決你已經(jīng)發(fā)現(xiàn)的問題的?直到現(xiàn)在,我們還沒有解釋對諸如學習率 $$\eta$$,規(guī)范化參數(shù) $$\lambda$$ 等等超參數(shù)選擇的方法。我只是給出那些效果很好的值而已。實踐中,當你使用神經(jīng)網(wǎng)絡解決問題時,尋找好的超參數(shù)其實是很困難的一件事。例如,我們要解決 MNIST 問題,開始時對于選擇什么樣的超參數(shù)一無所知。假設,剛開始的實驗中選擇前面章節(jié)的參數(shù)都是運氣較好。但在使用學習率 $$\eta=10.0$$ 而規(guī)范化參數(shù) $$\lambda=1000.0$$。下面是我們的一個嘗試:
>>> import mnist_loader
>>> training_data, validation_data, test_data = \
... mnist_loader.load_data_wrapper()
>>> import network2
>>> net = network2.Network([784, 30, 10])
>>> net.SGD(training_data, 30, 10, 10.0, lmbda = 1000.0,
... evaluation_data=validation_data, monitor_evaluation_accuracy=True)
Epoch 0 training complete
Accuracy on evaluation data: 1030 / 10000
Epoch 1 training complete
Accuracy on evaluation data: 990 / 10000
Epoch 2 training complete
Accuracy on evaluation data: 1009 / 10000
...
Epoch 27 training complete
Accuracy on evaluation data: 1009 / 10000
Epoch 28 training complete
Accuracy on evaluation data: 983 / 10000
Epoch 29 training complete
Accuracy on evaluation data: 967 / 10000
我們分類準確度并不比隨機選擇更好。網(wǎng)絡就像隨機噪聲產(chǎn)生器一樣。
你可能會說,“這好辦,降低學習率和規(guī)范化參數(shù)就好了?!辈恍业氖?,你并不先驗地知道這些就是需要調整的超參數(shù)??赡苷嬲膯栴}出在 $$30$$ 個隱藏元中,本身就不能很有效,不管我們?nèi)绾握{整其他的超參數(shù)都沒有作用的?可能我們真的需要至少 $$100$$ 個隱藏神經(jīng)元?或者是 $$300$$ 個隱藏神經(jīng)元?或者更多層的網(wǎng)絡?或者不同輸出編碼方式?可能我們的網(wǎng)絡一直在學習,只是學習的回合還不夠?可能 minibatch 的太小了?可能我們需要切換成二次代價函數(shù)?可能我們需要嘗試不同的權重初始化方法?等等。很容易就在超參數(shù)的選擇中迷失了方向。如果你的網(wǎng)絡規(guī)模很大,或者使用了很多的訓練數(shù)據(jù),這種情況就很令人失望了,因為一次訓練可能就要幾個小時甚至幾天乃至幾周,最終什么都沒有獲得。如果這種情況一直發(fā)生,就會打擊你的自信心??赡苣銜岩缮窠?jīng)網(wǎng)絡是不是適合你所遇到的問題?可能就應該放棄這種嘗試了?
本節(jié),我會給出一些用于設定超參數(shù)的啟發(fā)式想法。目的是幫你發(fā)展出一套工作流來確保很好地設置超參數(shù)。當然,我不會覆蓋超參數(shù)優(yōu)化的每個方法。那是太繁重的問題,而且也不會是一個能夠完全解決的問題,也不存在一種通用的關于正確策略的共同認知??偸菚幸恍┬碌募记煽梢詭椭闾岣咭稽c性能。但是本節(jié)的啟發(fā)式想法能幫你開個好頭。
寬的策略:在使用神經(jīng)網(wǎng)絡來解決新的問題時,一個挑戰(zhàn)就是獲得任何一種非尋常的學習,也就是說,達到比隨機的情況更好的結果。這個實際上會很困難,尤其是遇到一種新類型的問題時。讓我們看看有哪些策略可以在面臨這類困難時候嘗試。
假設,我們第一次遇到 MNIST 分類問題。剛開始,你很有激情,但是當?shù)谝粋€神經(jīng)網(wǎng)絡完全失效時,你會就得有些沮喪。此時就可以將問題簡化。丟開訓練和驗證集合中的那些除了 $$0$$ 和 $$1$$ 的那些圖像。然后試著訓練一個網(wǎng)絡來區(qū)分 $$0$$ 和 $$1$$。不僅僅問題比 $$10$$ 個分類的情況簡化了,同樣也會減少 80% 的訓練數(shù)據(jù),這樣就給出了 $$5$$ 倍的加速。這樣可以保證更快的實驗,也能給予你關于如何構建好的網(wǎng)絡更快的洞察。
你通過簡化網(wǎng)絡來加速實驗進行更有意義的學習。如果你相信 $$[784, 10]$$ 的網(wǎng)絡更可能比隨機更加好的分類效果,那么就從這個網(wǎng)絡開始實驗。這會比訓練一個 $$[784, 30 ,10]$$ 的網(wǎng)絡更快,你可以進一步嘗試后一個。
你可以通過提高監(jiān)控的頻率來在試驗中獲得另一個加速了。在 network2.py 中,我們在每個訓練的回合的最后進行監(jiān)控。每回合 $$50,000$$,在接受到網(wǎng)絡學習狀況的反饋前需要等上一會兒——在我的筆記本上訓練 $$[784, 30, 10]$$ 網(wǎng)絡基本上每回合 $$10$$ 秒。當然,$$10$$ 秒并不太長,不過你希望嘗試幾十種超參數(shù)就很麻煩了,如果你想再嘗試更多地選擇,那就相當棘手了。我們可以通過更加頻繁地監(jiān)控驗證準確度來獲得反饋,比如說在每 $$1,000$$ 次訓練圖像后。而且,與其使用整個 $$10,000$$ 幅圖像的驗證集來監(jiān)控性能,我們可以使用 $$100$$ 幅圖像來進行驗證。真正重要的是網(wǎng)絡看到足夠多的圖像來做真正的學習,獲得足夠優(yōu)秀的估計性能。當然,我們的程序 network2.py 并沒有做這樣的監(jiān)控。但是作為一個湊合的能夠獲得類似效果的方案,我們將訓練數(shù)據(jù)減少到前 $$1,000$$ 幅 MNIST 訓練圖像。讓我們嘗試一下,看看結果。(為了讓代碼更加簡單,我并沒有取僅僅是 0 和 1 的圖像。當然,那樣也是很容易就可以實現(xiàn))。
>>> net = network2.Network([784, 10])
>>> net.SGD(training_data[:1000], 30, 10, 10.0, lmbda = 1000.0, \
... evaluation_data=validation_data[:100], \
... monitor_evaluation_accuracy=True)
Epoch 0 training complete
Accuracy on evaluation data: 10 / 100
Epoch 1 training complete
Accuracy on evaluation data: 10 / 100
Epoch 2 training complete
Accuracy on evaluation data: 10 / 100
...
我們?nèi)匀猾@得完全的噪聲!但是有一個進步:現(xiàn)在我們每一秒鐘可以得到反饋,而不是之前每 10 秒鐘才可以。這意味著你可以更加快速地實驗其他的超參數(shù),或者甚至近同步地進行不同參數(shù)的組合的評比。
在上面的例子中,我設置 $$\lambda=1000.0$$,跟我們之前一樣。但是因為這里改變了訓練樣本的個數(shù),我們必須對 $$\lambda$$ 進行調整以保證權重下降的同步性。這意味著改變 $$\lambda = 20.0$$。如果我們這樣設置,則有:
>>> net = network2.Network([784, 10])
>>> net.SGD(training_data[:1000], 30, 10, 10.0, lmbda = 20.0, \
... evaluation_data=validation_data[:100], \
... monitor_evaluation_accuracy=True)
Epoch 0 training complete
Accuracy on evaluation data: 12 / 100
Epoch 1 training complete
Accuracy on evaluation data: 14 / 100
Epoch 2 training complete
Accuracy on evaluation data: 25 / 100
Epoch 3 training complete
Accuracy on evaluation data: 18 / 100
...
哦也!現(xiàn)在有了信號了。不是非常糟糕的信號,卻真是一個信號。我們可以基于這點,來改變超參數(shù)從而獲得更多的提升。可能我們猜測學習率需要增加(你可以能會發(fā)現(xiàn),這只是一個不大好的猜測,原因后面會講,但是相信我)所以為了測試我們的猜測就將 $$\eta$$ 調整至 $$100.0$$:
>>> net = network2.Network([784, 10])
>>> net.SGD(training_data[:1000], 30, 10, 100.0, lmbda = 20.0, \
... evaluation_data=validation_data[:100], \
... monitor_evaluation_accuracy=True)
Epoch 0 training complete
Accuracy on evaluation data: 10 / 100
Epoch 1 training complete
Accuracy on evaluation data: 10 / 100
Epoch 2 training complete
Accuracy on evaluation data: 10 / 100
Epoch 3 training complete
Accuracy on evaluation data: 10 / 100
...
這并不好!告訴我們之前的猜測是錯誤的,問題并不是學習率太低了。所以,我們試著將 $$\eta$$ 將至 $$\eta=1.0$$:
>>> net = network2.Network([784, 10])
>>> net.SGD(training_data[:1000], 30, 10, 1.0, lmbda = 20.0, \
... evaluation_data=validation_data[:100], \
... monitor_evaluation_accuracy=True)
Epoch 0 training complete
Accuracy on evaluation data: 62 / 100
Epoch 1 training complete
Accuracy on evaluation data: 42 / 100
Epoch 2 training complete
Accuracy on evaluation data: 43 / 100
Epoch 3 training complete
Accuracy on evaluation data: 61 / 100
...
這樣好點了!所以我們可以繼續(xù),逐個調整每個超參數(shù),慢慢提升性能。一旦我們找到一種提升性能的 $$\eta$$ 值,我們就可以嘗試尋找好的值。然后按照一個更加復雜的網(wǎng)絡架構進行實驗,假設是一個有 $$10$$ 個隱藏元的網(wǎng)絡。然后繼續(xù)調整 $$\eta$$ 和 $$\lambda$$。接著調整成 $$20$$ 個隱藏元。然后將其他的超參數(shù)調整再調整。如此進行,在每一步使用我們 hold out 驗證數(shù)據(jù)集來評價性能,使用這些度量來找到越來越好的超參數(shù)。當我們這么做的時候,一般都需要花費更多時間來發(fā)現(xiàn)由于超參數(shù)改變帶來的影響,這樣就可以一步步減少監(jiān)控的頻率。
所有這些作為一種寬泛的策略看起來很有前途。然而,我想要回到尋找超參數(shù)的原點。實際上,即使是上面的討論也傳達出過于樂觀的觀點。實際上,很容易會遇到神經(jīng)網(wǎng)絡學習不到任何知識的情況。你可能要花費若干天在調整參數(shù)上,仍然沒有進展。所以我想要再重申一下在前期你應該從實驗中盡可能早的獲得快速反饋。直覺上看,這看起來簡化問題和架構僅僅會降低你的效率。實際上,這樣能夠將進度加快,因為你能夠更快地找到傳達出有意義的信號的網(wǎng)絡。一旦你獲得這些信號,你可以嘗嘗通過微調超參數(shù)獲得快速的性能提升。這和人生中很多情況一樣——萬事開頭難。
好了,上面就是寬泛的策略?,F(xiàn)在我們看看一些具體的設置超參數(shù)的推薦。我會聚焦在學習率 $$\eta$$,L2 規(guī)范化參數(shù) $$\lambda$$,和 minibatch 大小。然而,很多的觀點同樣可以應用在其他的超參數(shù)的選擇上,包括一些關于網(wǎng)絡架構的、其他類型的規(guī)范化和一些本書后面遇到的如 momentum co-efficient 這樣的超參數(shù)。
學習率:假設我們運行了三個不同學習率($$\eta=0.025$$、$$\eta=0.25$$、$$\eta=2.5$$)的 MNIST 網(wǎng)絡。我們會像前面介紹的實驗那樣設置其他的超參數(shù),進行 $$30$$ 回合,minibatch 大小為 $$10$$,然后 $$\lambda = 5.0$$。我們同樣會使用整個 $$50,000$$ 幅訓練圖像。下面是一副展示了訓練代價的變化情況的圖:
http://wiki.jikexueyuan.com/project/neural-networks-and-deep-learning-zh-cn/images/129.png" alt="" />
使用 $$\eta=0.025$$,代價函數(shù)平滑下降到最后的回合。使用 $$\eta=0.25$$,代價剛開始下降,在大約 $$20$$ 回合后接近飽和狀態(tài),后面就是微小的震蕩和隨機抖動。最終使用 $$\eta=2.5$$ 代價從始至終都震蕩得非常明顯。為了理解震蕩的原因,回想一下隨機梯度下降其實是期望我們能夠逐漸地抵達代價函數(shù)的谷底的,
http://wiki.jikexueyuan.com/project/neural-networks-and-deep-learning-zh-cn/images/130.png" alt="" />
然而,如果 $$\eta$$ 太大的話,步長也會變大可能會使得算法在接近最小值時候又越過了谷底。這在 $$\eta=2.5$$ 時非常可能發(fā)生。當我們選擇 $$\eta=0.25$$ 時,初始幾步將我們帶到了谷底附近,但一旦到達了谷底,又很容易跨越過去。而在我們選擇 $$\eta=0.025$$ 時,在前 $$30$$ 回合的訓練中不再受到這個情況的影響。當然,選擇太小的學習率,也會帶來另一個問題——隨機梯度下降算法變慢了。一種更加好的策略其實是,在開始時使用 $$\eta=0.25$$,隨著越來越接近谷底,就換成 $$\eta=0.025$$。這種可變學習率的方法我們后面會介紹?,F(xiàn)在,我們就聚焦在找出一個單獨的好的學習率的選擇,$$\eta$$。
所以,有了這樣的想法,我們可以如下設置 $$\eta$$。首先,我們選擇在訓練數(shù)據(jù)上的代價立即開始下降而非震蕩或者增加時作為 $$\eta$$ 的閾值的估計。這個估計并不需要太過精確。你可以估計這個值的量級,比如說從 $$\eta=0.01$$ 開始。如果代價在訓練的前面若干回合開始下降,你就可以逐步地嘗試 $$\eta=0.1, 1.0,...$$,直到你找到一個 $$\eta$$ 的值使得在開始若干回合代價就開始震蕩或者增加。相反,如果代價在 $$\eta=0.01$$ 時就開始震蕩或者增加,那就嘗試 $$\eta=0.001, 0.0001,...$$ 直到你找到代價在開始回合就下降的設定。按照這樣的方法,我們可以掌握學習率的閾值的量級的估計。你可以選擇性地優(yōu)化估計,選擇那些最大的 $$\eta$$,比方說 $$\eta=0.5$$ 或者 $$\eta=0.2$$(這里也不需要過于精確)。
顯然,$$\eta$$ 實際值不應該比閾值大。實際上,如果 $$\eta$$ 的值重復使用很多回合的話,你更應該使用稍微小一點的值,例如,閾值的一半這樣的選擇。這樣的選擇能夠允許你訓練更多的回合,不會減慢學習的速度。
在 MNIST 數(shù)據(jù)中,使用這樣的策略會給出一個關于學習率 $$\eta$$ 的一個量級的估計,大概是 $$0.1$$。在一些改良后,我們得到了閾值 $$\eta=0.5$$。所以,我們按照剛剛的取一半的策略就確定了學習率為 $$\eta=0.25$$。實際上,我發(fā)現(xiàn)使用 $$\eta=0.5$$ 在 $$30$$ 回合內(nèi)表現(xiàn)是很好的,所以選擇更低的學習率,也沒有什么問題。
這看起來相當直接。然而,使用訓練代價函數(shù)來選擇 $$\eta$$ 看起來和我們之前提到的通過驗證集來確定超參數(shù)的觀點有點矛盾。實際上,我們會使用驗證準確度來選擇規(guī)范化超參數(shù),minibatch 大小,和層數(shù)及隱藏元個數(shù)這些網(wǎng)絡參數(shù),等等。為何對學習率要用不同的方法呢?坦白地說,這些選擇其實是我個人美學偏好,個人習慣罷了。原因就是其他的超參數(shù)傾向于提升最終的測試集上的分類準確度,所以將他們通過驗證準確度來選擇更合理一些。然而,學習率僅僅是偶然地影響最終的分類準確度的。學習率主要的目的是控制梯度下降的步長,監(jiān)控訓練代價是最好的檢測步長過大的方法。所以,這其實就是個人的偏好。在學習的前期,如果驗證準確度提升,訓練代價通常都在下降。所以在實踐中使用那種衡量方式并不會對判斷的影響太大。
使用 Early stopping 來確定訓練的回合數(shù):正如我們在本章前面討論的那樣,Early stopping 表示在每個回合的最后,我們都要計算驗證集上的分類準確度。當準確度不再提升,就終止它。這讓選擇回合數(shù)變得很簡單。特別地,也意味著我們不再需要擔心顯式地掌握回合數(shù)和其他超參數(shù)的關聯(lián)。而且,這個過程還是自動的。另外,Early stopping 也能夠幫助我們避免過匹配。盡管在實驗前期不采用 Early stopping,這樣可以看到任何過匹配的信號,使用這些來選擇規(guī)范化方法,但 early stopping 仍然是一件很棒的事。
我們需要再明確一下什么叫做分類準確度不再提升,這樣方可實現(xiàn) Early stopping。正如我們已經(jīng)看到的,分類準確度在整體趨勢下降的時候仍舊會抖動或者震蕩。如果我們在準確度剛開始下降的時候就停止,那么肯定會錯過更好的選擇。一種不錯的解決方案是如果分類準確度在一段時間內(nèi)不再提升的時候終止。例如,我們要解決 MNIST 問題。如果分類準確度在近 $$10$$ 個回合都沒有提升的時候,我們將其終止。這樣不僅可以