0%

network2.py

network2.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""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)

#********************************************************
class LogLikelihoodCost(object):

@staticmethod
def fn(a, y):
"""
C = -log(a[i])

a(10,1) y(10,1)
y = [0,0,1,0,0,0,0,0,0,0,0]
i = 2
C = -log a[2,0]
"""
i = np.argmax(y)
return -np.log(a[i,0])

@staticmethod
def delta(z, a, y):
"""
delta_j = aj-yj
"""
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.cost=cost
# init use_softmax with cost type
if self.cost == LogLikelihoodCost:
self.use_softmax = True
else:
self.use_softmax = False

# init weight initializer and feedforward method
if self.use_softmax:
self.default_weight_initializer = self.default_weight_initializer_with_softmax
self.feedforward = self.feedforward_with_softmax
else:
self.default_weight_initializer = self.default_weight_initializer_1
self.feedforward = self.feedforward_1
# init weights and biases
self.default_weight_initializer()
# at least an input and output layers
assert self.num_layers>=2

def default_weight_initializer_with_softmax(self):
# sigmoid neurons: w(0,1/sqrt(n_in)) b(0,1)
# softmax neurons: w = b = 0
# len(sizes)>=2
# (1) for sigmoid neuros
self.biases = [np.random.randn(y, 1) for y in self.sizes[1:-1]]
self.weights = [np.random.randn(y, x)/np.sqrt(x)
for x, y in zip(self.sizes[:-1], self.sizes[1:-1])]
#(2) for last somtmax neurons
x = self.sizes[-2]
y = self.sizes[-1]
last_b = np.zeros((y, 1))
last_w = np.zeros((y, x))

self.biases.append(last_b)
self.weights.append(last_w)


def default_weight_initializer_1(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_with_softmax(self, a):
"""Return the output of the network if ``a`` is input."""
for b, w in zip(self.biases[:-1], self.weights[:-1]):
a = sigmoid(np.dot(w, a)+b)
# last layer
b,w = self.biases[-1],self.weights[-1]
last_z = np.dot(w, a)+b
last_a = softmax(last_z)
return last_a

def feedforward_1(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)
num_batches = n/mini_batch_size
evaluation_cost, evaluation_accuracy = [], []
training_cost, training_accuracy = [], []
for j in xrange(epochs):
random.shuffle(training_data)
for k in xrange(0,num_batches):
mini_batch = training_data[k*mini_batch_size : (k+1)*mini_batch_size]
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 calculate_sum_derivatives_of_mini_batch(self,mini_batch):
"""
计算m个样本的总梯度和。
利用反向传播计算每一个样本(x,y)对应的梯度。
"""
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:
# 给定一个样本X,利用反向传播算法计算对应w,b的梯度
delta_nabla_b, delta_nabla_w = self.backprop(x, y)
# 对m个样本的梯度进行累计求和
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)]
return nabla_b,nabla_w

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.

"""
m = len(mini_batch)
nabla_b,nabla_w = self.calculate_sum_derivatives_of_mini_batch(mini_batch)

#self.weights = [w-(eta/m)*nw for w, nw in zip(self.weights, nabla_w)]
#self.biases = [b-(eta/m)*nb for b, nb in zip(self.biases, nabla_b)]

# L2 regularization
weight_decay = 1-eta*(lmbda/n)
self.weights = [weight_decay*w-(eta/m)*nw
for w, nw in zip(self.weights, nabla_w)]
self.biases = [b-(eta/m)*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"."""
# 初始化nb,nw,结构和b,w一样
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]

# feedforward
# 执行算法的feedforward阶段
# (1)初始化x作为a_1
activation = x
activations = [x] # list to store all the activations, layer by layer
zs = [] # list to store all the z vectors, layer by layer
# (2)l=2,....L层,分别计算z_l,a_l并且保存下来。
#*************************************************************
if self.use_softmax:
for b, w in zip(self.biases[:-1], self.weights[:-1]):
z = np.dot(w, activation)+b
zs.append(z)
activation = sigmoid(z)
activations.append(activation)
#last layer
b,w = self.biases[-1],self.weights[-1]
last_z = np.dot(w, activation)+b
last_a = softmax(last_z)
zs.append(last_z)
activations.append(last_a)
#*************************************************************
else:
for b, w in zip(self.biases, self.weights):
z = np.dot(w, activation)+b
zs.append(z)
activation = sigmoid(z)
activations.append(activation)

#========================================================================
# 先计算所有的误差delta,最后计算所有层的梯度nb,nw,代码可读性更高一些
#========================================================================
# method2
# backward pass
# 执行算法的backward阶段
# (3)初始化第L层的误差,delta_L = cost(z,a,y)
l = -1
#***************************************************
delta = self.cost.delta(zs[l],activations[l], y)
#***************************************************
deltas = [delta] # list to store all the errors,layer by layer
# (4)初始化l=L-1,....2层的误差,delta_l = np.dot(w_l+1^T,delta_l+1)* sigmoid_prime(z_l)
for i in xrange(2, self.num_layers):
l = -i #(-2代表L-1,-3代表L-2,-(L-1)代表2)
delta = np.dot(self.weights[l+1].transpose(), deltas[l+1]) * sigmoid_prime(zs[l])
deltas.insert(0,delta) # 确保误差的顺序,从后往前计算,所以需要insert在数组的最前面

#(5)l=L,L-1,....2层,计算所有的梯度向量nb,nw
for i in xrange(1, self.num_layers):
l = -i #(-1,-2,....-(L-1))
nabla_b[l] = deltas[l]
nabla_w[l] = np.dot(deltas[l], 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: # train data
results = [(np.argmax(self.feedforward(x)), np.argmax(y))
for (x, y) in data]
else: # val/test data
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
n = len(data)
for x, y in data:
a = self.feedforward(x)
if convert: # val/test data
y = vectorized_result(y)
cost += self.cost.fn(a, y)/n
# L2 term = lmbda/(2n)*sum(w**2)
l2_term = 0.5*(lmbda/n)*sum(np.linalg.norm(w)**2 for w in self.weights)
cost += l2_term
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()
name = sys.modules[__name__]
cost = getattr(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))

def softmax(z):
#e^z/ sum(e^z)
ez = np.exp(z)
sum_ez = sum(ez)
return ez/sum_ez
1
2
import mnist_loader
training_data, validation_data, test_data = mnist_loader.load_data_wrapper()
1
2
net = load("./1.json")
net.accuracy(validation_data)
9385
1
2
net = load("./0.json")
net.accuracy(validation_data)
8904

Reference

History

  • 20180807: created.