{"id":981,"date":"2019-01-22T10:46:12","date_gmt":"2019-01-22T02:46:12","guid":{"rendered":"http:\/\/www.max-shu.com\/blog\/?p=981"},"modified":"2019-01-22T10:46:12","modified_gmt":"2019-01-22T02:46:12","slug":"%e3%80%8a%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c%e4%b8%8e%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0%e3%80%8b%e4%b8%ad%e7%9a%84python-3-x-%e4%bb%a3%e7%a0%81network1-py","status":"publish","type":"post","link":"http:\/\/www.max-shu.com\/blog\/?p=981","title":{"rendered":"\u300a\u795e\u7ecf\u7f51\u7edc\u4e0e\u6df1\u5ea6\u5b66\u4e60\u300b\u4e2d\u7684Python 3.x \u4ee3\u7801network1.py"},"content":{"rendered":"<p>\u300a\u795e\u7ecf\u7f51\u7edc\u4e0e\u6df1\u5ea6\u5b66\u4e60\u300b\uff08<span class=\"fontstyle2\">Michael Nielsen<\/span><span class=\"fontstyle0\">\u8457<\/span>\uff09\u4e2d\u7684\u4ee3\u7801\u662f\u57fa\u4e8epython2.7\u7684\uff0c\u4e0b\u9762\u4e3a\u79fb\u690d\u5230python3\u4e0b\u7684\u4ee3\u7801\uff1a<\/p>\n<p><span style=\"color: #ff0000;\"><strong>mnist_loader.py\u4ee3\u7801\uff1a<\/strong><\/span><\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u70b9\u51fb\u4e0b\u8f7d\uff1a<\/strong><\/span><a href=\"http:\/\/www.max-shu.com\/blog\/wp-content\/uploads\/2019\/01\/mnist_loader.zip\">mnist_loader<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&#8220;&#8221;&#8221;<br \/>\nmnist_loader<br \/>\n~~~~~~~~~~~~<\/p>\n<p>A library to load the MNIST image data. For details of the data<br \/>\nstructures that are returned, see the doc strings for &#8220;load_data&#8220;<br \/>\nand &#8220;load_data_wrapper&#8220;. In practice, &#8220;load_data_wrapper&#8220; is the<br \/>\nfunction usually called by our neural network code.<br \/>\n&#8220;&#8221;&#8221;<\/p>\n<p>#### Libraries<br \/>\n# Standard library<br \/>\ntry:<br \/>\nimport cPickle as pickle<br \/>\nexcept ImportError:<br \/>\nimport pickle<\/p>\n<p># import cPickle<br \/>\nimport gzip<\/p>\n<p># Third-party libraries<br \/>\nimport numpy as np<\/p>\n<p>def load_data():<br \/>\n&#8220;&#8221;&#8221;Return the MNIST data as a tuple containing the training data,<br \/>\nthe validation data, and the test data.<\/p>\n<p>The &#8220;training_data&#8220; is returned as a tuple with two entries.<br \/>\nThe first entry contains the actual training images. This is a<br \/>\nnumpy ndarray with 50,000 entries. Each entry is, in turn, a<br \/>\nnumpy ndarray with 784 values, representing the 28 * 28 = 784<br \/>\npixels in a single MNIST image.<\/p>\n<p>The second entry in the &#8220;training_data&#8220; tuple is a numpy ndarray<br \/>\ncontaining 50,000 entries. Those entries are just the digit<br \/>\nvalues (0&#8230;9) for the corresponding images contained in the first<br \/>\nentry of the tuple.<\/p>\n<p>The &#8220;validation_data&#8220; and &#8220;test_data&#8220; are similar, except<br \/>\neach contains only 10,000 images.<\/p>\n<p>This is a nice data format, but for use in neural networks it&#8217;s<br \/>\nhelpful to modify the format of the &#8220;training_data&#8220; a little.<br \/>\nThat&#8217;s done in the wrapper function &#8220;load_data_wrapper()&#8220;, see<br \/>\nbelow.<br \/>\n&#8220;&#8221;&#8221;<br \/>\nwith gzip.open(&#8216;.\/mnist.pkl.gz&#8217;, &#8216;rb&#8217;) as f:<br \/>\ntraining_data, validation_data, test_data = pickle.load(f, encoding=&#8217;latin1&#8242;)<br \/>\n#show_mnist_data(training_data, validation_data, test_data)<\/p>\n<p>#f = gzip.open(&#8216;.\/mnist.pkl.gz&#8217;, &#8216;rb&#8217;)<br \/>\n#training_data, validation_data, test_data = cPickle.load(f, encoding=&#8221;latin1&#8243;)<br \/>\n#f.close()<\/p>\n<p>return (training_data, validation_data, test_data)<\/p>\n<p>def show_mnist_data(training_data, validation_data, test_data):<br \/>\nfw = open(&#8220;.\/log.txt&#8221;, &#8216;w&#8217;)<br \/>\nnp.set_printoptions(precision=4, linewidth=800)<br \/>\nnum = 0<br \/>\nfw.write(&#8220;\u4e0b\u9762\u4e3aMNIST\u6240\u6709\u56fe\u7247\u7070\u5ea6\u5316\u4e4b\u540e\u518d\u6570\u5b57\u5316\u62bd\u6837\u4e4b\u540e\u7684\u6570\u636e\u3002\\n&#8221;)<br \/>\nfw.write(&#8220;====== TRAINING DATA Start&#8230; ======\\n&#8221;)<br \/>\nfor x,y in training_data:<br \/>\nnum += 1<br \/>\nif(num &gt; 10):<br \/>\nbreak<br \/>\nfw.write(&#8220;++++++ &#8220;+str(num)+ &#8221; ++++++\\n&#8221;)<br \/>\nfw.write(&#8220;28&#215;28\u7684\u56fe\u7247\u6570\u636e\uff1a\\n&#8221;)<br \/>\nfor i in x.reshape(28, 28):<br \/>\nfw.write(str(i)+&#8221;\\n&#8221;)<br \/>\nfw.write(&#8220;==&gt;&#8221;+ &#8220;\u56fe\u7247\u5bf9\u5e940~9\u517110\u4e2a\u6570\u5b57\u7684\u6743\u91cd\uff1a&#8221;+ str(y.tolist())+&#8221;\\n&#8221;)<br \/>\nif num &gt; 10:<br \/>\nfw.write(&#8220;&#8230;&#8230;&#8230;.\\n&#8221;)<br \/>\nfw.write(&#8220;====== TRAINING DATA End. ======\\n&#8221;)<br \/>\nfw.write(&#8221; \\n&#8221;)<br \/>\nfw.write(&#8221; \\n&#8221;)<\/p>\n<p>num = 0<br \/>\nfw.write(&#8220;====== VALIDATION DATA Start&#8230; ======\\n&#8221;)<br \/>\nfor x,y in validation_data:<br \/>\nnum += 1<br \/>\nif(num &gt; 10):<br \/>\nbreak<br \/>\nfw.write(&#8220;++++++ &#8220;+str(num)+ &#8221; ++++++\\n&#8221;)<br \/>\nfw.write(&#8220;28&#215;28\u7684\u56fe\u7247\u6570\u636e\uff1a\\n&#8221;)<br \/>\nfor i in x.reshape(28, 28):<br \/>\nfw.write(str(i)+&#8221;\\n&#8221;)<br \/>\nfw.write(&#8220;==&gt;&#8221;+ &#8220;\u56fe\u7247\u6240\u5bf9\u5e94\u7684\u6570\u5b57\uff1a&#8221;+ str(y.tolist())+&#8221;\\n&#8221;)<br \/>\nif num &gt; 10:<br \/>\nfw.write(&#8220;&#8230;&#8230;&#8230;.\\n&#8221;)<br \/>\nfw.write(&#8220;====== VALIDATION DATA End. ======\\n&#8221;)<br \/>\nfw.write(&#8221; \\n&#8221;)<br \/>\nfw.write(&#8221; \\n&#8221;)<\/p>\n<p>num = 0<br \/>\nfw.write(&#8220;====== TEST DATA Start&#8230; ======\\n&#8221;)<br \/>\nfor x,y in test_data:<br \/>\nnum += 1<br \/>\nif(num &gt; 10):<br \/>\nbreak<br \/>\nfw.write(&#8220;++++++ &#8220;+str(num)+ &#8221; ++++++\\n&#8221;)<br \/>\nfw.write(&#8220;28&#215;28\u7684\u56fe\u7247\u6570\u636e\uff1a\\n&#8221;)<br \/>\nfor i in x.reshape(28, 28):<br \/>\nfw.write(str(i)+&#8221;\\n&#8221;)<br \/>\nfw.write(&#8220;==&gt;&#8221;+ &#8220;\u56fe\u7247\u6240\u5bf9\u5e94\u7684\u6570\u5b57\uff1a&#8221;+ str(y.tolist())+&#8221;\\n&#8221;)<br \/>\nif num &gt; 10:<br \/>\nfw.write(&#8220;&#8230;&#8230;&#8230;.\\n&#8221;)<br \/>\nfw.write(&#8220;====== TEST DATA End. ======\\n&#8221;)<br \/>\nfw.write(&#8221; \\n&#8221;)<br \/>\nfw.write(&#8221; \\n&#8221;)<br \/>\nfw.close()<\/p>\n<p>def load_data_wrapper():<br \/>\n&#8220;&#8221;&#8221;Return a tuple containing &#8220;(training_data, validation_data,<br \/>\ntest_data)&#8220;. Based on &#8220;load_data&#8220;, but the format is more<br \/>\nconvenient for use in our implementation of neural networks.<\/p>\n<p>In particular, &#8220;training_data&#8220; is a list containing 50,000<br \/>\n2-tuples &#8220;(x, y)&#8220;. &#8220;x&#8220; is a 784-dimensional numpy.ndarray<br \/>\ncontaining the input image. &#8220;y&#8220; is a 10-dimensional<br \/>\nnumpy.ndarray representing the unit vector corresponding to the<br \/>\ncorrect digit for &#8220;x&#8220;.<\/p>\n<p>&#8220;validation_data&#8220; and &#8220;test_data&#8220; are lists containing 10,000<br \/>\n2-tuples &#8220;(x, y)&#8220;. In each case, &#8220;x&#8220; is a 784-dimensional<br \/>\nnumpy.ndarry containing the input image, and &#8220;y&#8220; is the<br \/>\ncorresponding classification, i.e., the digit values (integers)<br \/>\ncorresponding to &#8220;x&#8220;.<\/p>\n<p>Obviously, this means we&#8217;re using slightly different formats for<br \/>\nthe training data and the validation \/ test data. These formats<br \/>\nturn out to be the most convenient for use in our neural network<br \/>\ncode.&#8221;&#8221;&#8221;<br \/>\ntr_d, va_d, te_d = load_data()<br \/>\ntraining_inputs = [np.reshape(x, (784, 1)) for x in tr_d[0]]<br \/>\ntraining_results = [vectorized_result(y) for y in tr_d[1]]<br \/>\ntraining_data = zip(training_inputs, training_results)<br \/>\nvalidation_inputs = [np.reshape(x, (784, 1)) for x in va_d[0]]<br \/>\nvalidation_data = zip(validation_inputs, va_d[1])<br \/>\ntest_inputs = [np.reshape(x, (784, 1)) for x in te_d[0]]<br \/>\ntest_data = zip(test_inputs, te_d[1])<\/p>\n<p># show_mnist_data(training_data, validation_data, test_data) #\u8f93\u51fa\uff0c\u4f46\u662fzip\u7684iter\u53d8\u91cf\u53ea\u80fd\u7528\u4e00\u6b21\uff0c\u8f93\u51fa\u4e4b\u540ezip\u5c31\u53d8\u7a7a\u4e86<br \/>\nreturn (training_data, validation_data, test_data)<\/p>\n<p>def vectorized_result(j):<br \/>\n&#8220;&#8221;&#8221;Return a 10-dimensional unit vector with a 1.0 in the jth<br \/>\nposition and zeroes elsewhere. This is used to convert a digit<br \/>\n(0&#8230;9) into a corresponding desired output from the neural<br \/>\nnetwork.&#8221;&#8221;&#8221;<br \/>\ne = np.zeros((10, 1))<br \/>\ne[j] = 1.0<br \/>\nreturn e<\/p>\n<div><\/div>\n<div><\/div>\n<div><span style=\"color: #ff0000;\"><strong>network1.py\u4ee3\u7801\uff1a<\/strong><\/span><\/div>\n<div><span style=\"color: #ff0000;\"><strong>\u70b9\u51fb\u4e0b\u8f7d\uff1a<a href=\"http:\/\/www.max-shu.com\/blog\/wp-content\/uploads\/2019\/01\/network1.zip\">network1<\/a><\/strong><\/span><\/div>\n<div><\/div>\n<p>#!\/usr\/bin\/python<br \/>\n# -*- coding: UTF-8 -*-<\/p>\n<p>import random<br \/>\nimport numpy as np<\/p>\n<p>def sigmoid(z):<br \/>\n&#8220;&#8221;&#8221;The simgoid fnction.&#8221;&#8221;&#8221;<br \/>\nreturn 1.0\/(1.0+np.exp(-z))<\/p>\n<p>def sigmoid_prime(z):<br \/>\n&#8220;&#8221;&#8221;Derivative of the sigmoid function.&#8221;&#8221;&#8221;<br \/>\nreturn sigmoid(z)*(1-sigmoid(z))<\/p>\n<p>class Network(object):<br \/>\ndef __init__(self, sizes):<br \/>\nself.num_layers = len(sizes)<br \/>\nself.sizes = sizes<br \/>\nself.biases = [np.random.randn(y, 1) for y in sizes[1:]]<br \/>\nself.weights = [np.random.randn(y, x) for x, y in zip(sizes[:-1], sizes[1:])]<\/p>\n<p>print(&#8220;\u8f93\u5165\u56fe\u7247\u50cf\u7d20\u70b9\u6570\uff1a&#8221;+str(self.sizes[0]) +&#8221;, \u8f93\u5165\u795e\u7ecf\u7f51\u7edc\u5c42\u6570\uff1a&#8221;+str(self.num_layers))<br \/>\nprint(&#8220;\u7b2c 1 \u5c42\u4e3a\u8f93\u5165\u5c42\uff08\u5171 &#8220;+str(self.sizes[0])+&#8221; \u4e2a\u795e\u7ecf\u5143\uff09\uff0c\u6743\u91cd\u548c\u504f\u7f6e\u90fd\u4e0d\u9700\u8981\u3002&#8221;)<br \/>\nfor i in range(1, self.num_layers):<br \/>\nprint(&#8220;\u7b2c &#8220;+str(i+1)+&#8221; \u5c42\u5171\u6709 &#8220;+str(self.sizes[i])+&#8221; \u4e2a\u795e\u7ecf\u5143\uff0c\u521d\u59cb\u6743\u91cd\u548c\u504f\u7f6e\u5982\u4e0b\u3002&#8221;)<br \/>\nprint(&#8221; &#8220;)<br \/>\nlayer = 1<br \/>\nprint(&#8220;\u8bad\u7ec3\u524d\u7684\u6743\u91cd\u548c\u504f\u7f6e\uff1a&#8221;)<br \/>\nfor lw, lb in zip(self.weights, self.biases):<br \/>\nlayer += 1<br \/>\nnum_nw = 0<br \/>\nfor w, b in zip(lw, lb):<br \/>\nnum_nw += 1<br \/>\nprint(&#8220;\u7b2c &#8220;+str(layer)+&#8221; \u5c42\u7b2c &#8220;+str(num_nw)+&#8221; \u4e2a\u795e\u7ecf\u5143\u7684\u6743\u91cd(\u5171&#8221;+str(len(w))+&#8221;\u4e2a\u8f93\u5165)\uff1a&#8221;)<br \/>\nprint(str(w))<br \/>\nprint(&#8220;\u7b2c &#8220;+str(layer)+&#8221; \u5c42\u7b2c &#8220;+str(num_nw)+&#8221; \u4e2a\u795e\u7ecf\u5143\u7684\u504f\u7f6e(\u5171&#8221;+str(len(b))+&#8221;\u4e2a\u8f93\u51fa)\uff1a&#8221;)<br \/>\nprint(str(b))<br \/>\nprint(&#8221; &#8220;)<br \/>\nprint(&#8221; &#8220;)<\/p>\n<p>def feedforward(self ,a):<br \/>\n&#8220;&#8221;&#8221;return the output of the network if &#8220;a&#8221; is input.&#8221;&#8221;&#8221;<br \/>\nfor b, w in zip(self.biases, self.weights):<br \/>\na = sigmoid(np.dot(w, a)+b)<br \/>\nreturn a<\/p>\n<p>def SGD(self, training_data, epochs, mini_batch_size, eta, test_data=None):<br \/>\n&#8220;&#8221;&#8221;Train the neural network using mini-batch stochastic gradient descent.<br \/>\nThe &#8220;training_data&#8221; is a list of tuples &#8220;(x, y)&#8221; representing the training inputs and the desired outputs.<br \/>\nThe other non-optional parameters are self-explanatory.<br \/>\nIf &#8220;test_data&#8221; is provided then the network will be evaluated against the test data after each epoch, and partial progress printed out.<br \/>\nThis is useful for tracking progress, but slows things down substantially.<br \/>\n&#8220;&#8221;&#8221;<br \/>\ntraining_data_list = list(training_data)<br \/>\ntest_data_list = list(test_data)<br \/>\nif test_data:<br \/>\nn_test = len(test_data_list)<br \/>\nn = len(training_data_list)<br \/>\nprint(&#8220;=================================================================&#8221;)<br \/>\nprint(&#8220;=================================================================&#8221;)<br \/>\nprintStr = &#8220;&#8221;<br \/>\nloop = 0<br \/>\nfor j in range(epochs):<br \/>\nloop += 1<br \/>\nrandom.shuffle(training_data_list)<br \/>\ntraining_data = training_data_list<br \/>\nmini_batches = [training_data[k:k+mini_batch_size] for k in range(0, n, mini_batch_size)]<br \/>\nfor mini_batch in mini_batches:<br \/>\nself.update_mini_batch(mini_batch, eta)<br \/>\nif test_data:<br \/>\ntest_data = test_data_list<br \/>\nprintStr += (&#8220;Epoch {0}: \u6210\u529f\u8bc6\u522b\uff1a{1} \/ \u603b\u5171\uff1a{2}&#8221;.format(j, self.evaluate(test_data), n_test)+&#8221;\\n&#8221;)<br \/>\nprint(&#8220;Epoch {0}: \u6210\u529f\u8bc6\u522b\uff1a{1} \/ \u603b\u5171\uff1a{2}&#8221;.format(j, self.evaluate(test_data), n_test))<br \/>\nelse:<br \/>\nprint(&#8220;Loop &#8220;+str(loop))<br \/>\nprintStr += (&#8220;Epoch {0} complete&#8221;.format(j)+&#8221;\\n&#8221;)<br \/>\nprint(&#8220;Epoch {0} complete&#8221;.format(j))<\/p>\n<p>print(&#8220;=================================================================&#8221;)<br \/>\nprint(&#8220;=================================================================&#8221;)<\/p>\n<p>print(&#8221; &#8220;)<br \/>\nlayer = 1<br \/>\nprint(&#8220;\u8bad\u7ec3\u540e\u7684\u6743\u91cd\u548c\u504f\u7f6e\uff1a&#8221;)<br \/>\nfor lw, lb in zip(self.weights, self.biases):<br \/>\nlayer += 1<br \/>\nnum_nw = 0<br \/>\nfor w, b in zip(lw, lb):<br \/>\nnum_nw += 1<br \/>\nprint(&#8220;\u7b2c &#8220;+str(layer)+&#8221; \u5c42\u7b2c &#8220;+str(num_nw)+&#8221; \u4e2a\u795e\u7ecf\u5143\u7684\u6743\u91cd(\u5171&#8221;+str(len(w))+&#8221;\u4e2a\u8f93\u5165)\uff1a&#8221;)<br \/>\nprint(str(w))<br \/>\nprint(&#8220;\u7b2c &#8220;+str(layer)+&#8221; \u5c42\u7b2c &#8220;+str(num_nw)+&#8221; \u4e2a\u795e\u7ecf\u5143\u7684\u504f\u7f6e(\u5171&#8221;+str(len(b))+&#8221;\u4e2a\u8f93\u51fa)\uff1a&#8221;)<br \/>\nprint(str(b))<br \/>\nprint(&#8221; &#8220;)<br \/>\nprint(&#8221; &#8220;)<\/p>\n<p>print(&#8220;=================================================================&#8221;)<br \/>\nprint(&#8220;=================================================================&#8221;)<br \/>\nprint(printStr)<br \/>\nprint(&#8220;\u6bcf\u5c42\u6d4b\u8bd5\u7ed3\u679c\u7684\u8ba1\u7b97\u516c\u5f0f\uff1a (\u8f93\u51651*\u6743\u91cd1 + \u8f93\u51652*\u6743\u91cd2 + \u8f93\u51653*\u6743\u91cd3 + &#8230;) + \u504f\u7f6e &#8220;)<br \/>\nprint(&#8220;a1*w11&#8212;|&#8221;)<br \/>\nprint(&#8220;a2*w12&#8212;|+b1&#8212;|&#8221;)<br \/>\nprint(&#8220;a3*w13&#8212;| |&#8221;)<br \/>\nprint(&#8220;&#8230;&#8230;&#8212;| |-output&#8221;)<br \/>\nprint(&#8220;a1*w21&#8212;| |&#8221;)<br \/>\nprint(&#8220;a2*w22&#8212;|+b2&#8212;|&#8221;)<br \/>\nprint(&#8220;a3*w23&#8212;|&#8221;)<br \/>\nprint(&#8220;&#8230;&#8230;&#8212;|&#8221;)<\/p>\n<p>def update_mini_batch(self, mini_batch, eta):<br \/>\n&#8220;&#8221;&#8221;Update the network&#8217;s weights and biases by applying gradient descent using backpropagation to a single mini batch.<br \/>\nThe &#8220;mini_batch&#8221; is a list of tuples &#8220;(x, y)&#8221;, and &#8220;eta&#8221; is the learning rate.<br \/>\n&#8220;&#8221;&#8221;<br \/>\nnabla_b = [np.zeros(b.shape) for b in self.biases]<br \/>\nnabla_w = [np.zeros(w.shape) for w in self.weights]<br \/>\nfor x, y in mini_batch:<br \/>\ndelta_nabla_b, delta_nabla_w = self.backprop(x, y)<br \/>\nnabla_b = [nb + dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]<br \/>\nnabla_w = [nw + dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]<br \/>\nself.weights = [w &#8211; (eta\/len(mini_batch))*nw for w, nw in zip(self.weights, nabla_w)]<br \/>\nself.biases = [b &#8211; (eta\/len(mini_batch))*nb for b, nb in zip(self.biases, nabla_b)]<\/p>\n<p>def backprop(self, x, y):<br \/>\n&#8220;&#8221;&#8221;Return a tuple &#8220;(nabla_b, nabla_w)&#8220; representing the gradient for the cost function C_x.<br \/>\n&#8220;nabla_b&#8220; and &#8220;nabla_w&#8220; are layer-by-layer lists of numpy arrays, similar to &#8220;self.biases&#8220; and &#8220;self.weights&#8220;.<br \/>\n&#8220;&#8221;&#8221;<br \/>\nnabla_b = [np.zeros(b.shape) for b in self.biases]<br \/>\nnabla_w = [np.zeros(w.shape) for w in self.weights]<br \/>\n#feedforward<br \/>\nactivation = x<br \/>\nactivations = [x] # list to store all the activations, layer by layer<br \/>\nzs = [] # list to store all the z vectors, layer by layer<br \/>\nfor b, w in zip(self.biases, self.weights):<br \/>\nz = np.dot(w, activation) + b<br \/>\nzs.append(z)<br \/>\nactivation = sigmoid(z)<br \/>\nactivations.append(activation)<br \/>\n# backward pass<br \/>\ndelta = self.cost_derivative(activations[-1], y) * sigmoid_prime(zs[-1])<br \/>\nnabla_b[-1] = delta<br \/>\nnabla_w[-1] = np.dot(delta, activations[-2].transpose())<br \/>\n# Note that the variable l in the loop below is used a little<br \/>\n# differently to the notation in Chapter 2 of the book. Here,<br \/>\n# l = 1 means the last layer of neurons, l = 2 is the<br \/>\n# second-last layer, and so on. It&#8217;s a renumbering of the<br \/>\n# scheme in the book, used here to take advantage of the fact<br \/>\n# that Python can use negative indices in lists.<br \/>\nfor l in range(2, self.num_layers):<br \/>\nz = zs[-l]<br \/>\nsp = sigmoid_prime(z)<br \/>\ndelta = np.dot(self.weights[-l+1].transpose(), delta) * sp<br \/>\nnabla_b[-l] = delta<br \/>\nnabla_w[-l] = np.dot(delta, activations[-l-1].transpose())<br \/>\nreturn (nabla_b, nabla_w)<\/p>\n<p>def evaluate(self, test_data):<br \/>\n&#8220;&#8221;&#8221;Return the number of test inputs for which the neural network outputs the correct result.<br \/>\nNote that the neural network&#8217;s output is assumed to be the index of whichever neuron in the final layer has the highest activation.<br \/>\n&#8220;&#8221;&#8221;<br \/>\ntest_results = [(np.argmax(self.feedforward(x)), y) for (x, y) in test_data]<br \/>\nreturn sum(int(x == y) for (x, y) in test_results)<\/p>\n<p>def cost_derivative(self, output_activations, y):<br \/>\n&#8220;&#8221;&#8221;Return the vector of partial derivatives \\partial C_x \/ \\partial a for the output activations.<br \/>\n&#8220;&#8221;&#8221;<br \/>\nreturn (output_activations &#8211; y)<\/p>\n<p>def test():<br \/>\nimport mnist_loader<br \/>\ntraining_data, validation_data, test_data = mnist_loader.load_data_wrapper()<br \/>\nnet = Network([784, 30, 10])<br \/>\nnet.SGD(training_data, 30, 10, 3.0, test_data=test_data)<\/p>\n<p>if __name__ == &#8216;__main__&#8217;:<br \/>\ntest()<\/p>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u300a\u795e\u7ecf\u7f51\u7edc\u4e0e\u6df1\u5ea6\u5b66\u4e60\u300b\uff08Michael Nielsen\u8457\uff09\u4e2d\u7684\u4ee3\u7801\u662f\u57fa\u4e8epython2.7\u7684\uff0c\u4e0b\u9762\u4e3a\u79fb\u690d\u5230py &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[443],"tags":[726,108,446,447],"class_list":["post-981","post","type-post","status-publish","format-standard","hentry","category-443","tag-network1-py","tag-python","tag-446","tag-447"],"views":2114,"_links":{"self":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=981"}],"version-history":[{"count":2,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/981\/revisions"}],"predecessor-version":[{"id":985,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/981\/revisions\/985"}],"wp:attachment":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=981"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}