# coding=utf-8import numpy as np
import randomclassNetwork(object):def__init__(self, sizes):"""構(gòu)造函數(shù):param sizes: 每層神經(jīng)元的個數(shù),net = Network([2,3,1])"""self.num_layers =len(sizes)self.sizes = sizes# np.random.randn(y, 1) 隨機從正太分布(均值0,方差1)中生成self.biases =[np.random.randn(y,1)for y in sizes[1:]]# weights[1]存儲連接第二層和第三層的權(quán)重self.weights =[np.random.randn(x, y)for x, y inzip(sizes[:-1], sizes[1:])]deffeedforward(self, a):"""return the output of the network if 'a' is input:param a::return:"""for b, w inzip(self.biases, self.weights):a = sigmoid(np.dot(w, a)+ b)return adefSGD(self, training_data, epochs, mini_batch_size, eta, test_data=None):"""Train the neural network using mini-batch stochastic gradient descent.隨機梯度下降算法:param training_data: 訓(xùn)練集training_data is a list of tuples "(x,y)" representing the training inputs and the desired outputs:param epochs: 迭代次數(shù):param mini_batch_size: 每一小塊包含多少個實例:param eta: 學(xué)習(xí)率:param test_data: 測試集:return:"""if test_data: n_test =len(test_data)n =len(training_data)for j inxrange(epochs):random.shuffle(training_data)# 隨機打亂mini_batches =[training_data[k:k + mini_batch_size]for k inxrange(0, n, mini_batch_size)]# 從0到n,間隔為mini_batch_sizefor 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)defupdate_mini_batch(self, mini_batch, eta):"""Update the network's weights and biases by applying gradient descentusing back propagation to a single mini batch.:param mini_batch: list of tuples "(x,y)":param eta: learning rate:return:"""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 inzip(nabla_b, delta_nabla_b)]nabla_w =[nw + dnw for nw, dnw inzip(nabla_w, delta_nabla_w)]self.weights =[w -(eta /len(mini_batch))* nw for w, nw inzip(self.weights, nabla_w)]self.biases =[b -(eta /len(mini_batch))* nb for b, nb inzip(self.biases, nabla_b)]# sizes = [2, 3, 1]# print sizes[1:]# bias = [np.random.randn(y, 1) for y in sizes[1:]]# print bias# for x, y in zip(sizes[:-1], sizes[1:]):# print (x, y)net = Network([2,3,1])print net.num_layers
print net.sizes
print net.biases
print net.weights
from collections import defaultdict# My librariesimport mnist_loaderdefmain():training_data, validation_data, test_data = mnist_loader.load_data()# training phase: compute the average darknesses for each digit,# based on the training dataavgs = avg_darknesses(training_data)# testing phase: see how many of the test images are classified# correctlynum_correct =sum(int(guess_digit(image, avgs)== digit)for image, digit inzip(test_data[0], test_data[1]))print"Baseline classifier using average darkness of image."print"%s of %s values correct."%(num_correct,len(test_data[1]))defavg_darknesses(training_data):""" Return a defaultdict whose keys are the digits 0 through 9.For each digit we compute a value which is the average darkness oftraining images containing that digit. The darkness for anyparticular image is just the sum of the darknesses for each pixel."""digit_counts = defaultdict(int)darknesses = defaultdict(float)for image, digit inzip(training_data[0], training_data[1]):digit_counts[digit]+=1darknesses[digit]+=sum(image)avgs = defaultdict(float)for digit, n in digit_counts.iteritems():avgs[digit]= darknesses[digit]/ nreturn avgsdefguess_digit(image, avgs):"""Return the digit whose average darkness in the training data isclosest to the darkness of ``image``. Note that ``avgs`` isassumed to be a defaultdict whose keys are 0...9, and whose valuesare the corresponding average darknesses across the training data."""darkness =sum(image)distances ={k:abs(v - darkness)for k, v in avgs.iteritems()}returnmin(distances, key=distances.get)if __name__ =="__main__":main()
識別的準確率只有22.5%
第二種 基于SVM手寫數(shù)字識別
核心代碼
defsvm_baseline():training_data, validation_data, test_data = mnist_loader.load_data()# trainclf = svm.SVC()clf.fit(training_data[0], training_data[1])# testpredictions =[int(a)for a in clf.predict(test_data[0])]num_correct =sum(int(a == y)for a, y inzip(predictions, test_data[1]))print"Baseline classifier using an SVM."print"%s of %s values correct."%(num_correct,len(test_data[1]))if __name__ =="__main__":svm_baseline()