在本章节,我们将结合一小段python代码来进一步地说明神经网络的思想。(源代码可以从https://github.com/mnielsen/neural-networks-and-deep-learning 下载,本文只是对该代码进行了学习,未做任何修改)。关于神经网络的具体理论请参考上一章节进行学习。
核心代码是Network对象:
class Network(object):
sizes变量指明神经网络共多少层,每一层包含多少个神经元。神经网络参数bias和weight都以随机值作为迭代优化的初始点。
def __init__(self, sizes):
"""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 a Gaussian
distribution with mean 0, and variance 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.num_layers = len(sizes)
self.sizes = sizes
self.biases = [np.random.randn(y, 1) for y in sizes[1:]]
self.weights = [np.random.randn(y, x)
for x, y in zip(sizes[:-1], sizes[1:])]
前向传播代码:
根据公式,当前层神经元的激活值 al=σ(wlal−1+bl) ,由第一层神经元依次向后推算每一层神经元的激活值,直至输出层神经元。
def feedforward(self, a):
"""Return the output of the network if ``a`` is input."""
for b, w in zip(self.biases, self.weights):