DL之NN:利用(本地数据集50000张数据集)调用自定义神经网络network.py实现手写数字图片识别94%准确率

举报
一个处女座的程序猿 发表于 2021/03/28 02:59:24 2021/03/28
【摘要】 DL之NN:利用(本地数据集50000张数据集)调用自定义神经网络network.py实现手写数字图片识别94%准确率     目录 输出结果 代码设计         输出结果 更新……     代码设计 import mnist_loaderimport net...

DL之NN:利用(本地数据集50000张数据集)调用自定义神经网络network.py实现手写数字图片识别94%准确率

 

 

目录

输出结果

代码设计


 

 

 

 

输出结果

更新……

 

 

代码设计


  
  1. import mnist_loader
  2. import network
  3. training_data, validation_data, test_data = mnist_loader.load_data_wrapper()
  4. print("training_data")
  5. print(type(training_data))
  6. print(list(training_data))
  7. print(training_data[0][0].shape)
  8. print(training_data[0][1].shape)
  9. net = network.Network([784, 30, 10])
  10. net.SGD(training_data, 30, 10, 3.0, test_data=test_data)

  
  1. import random
  2. import numpy as np
  3. class Network(object):
  4. def __init__(self, sizes):
  5. """The list ``sizes`` contains the number of neurons in the
  6. respective layers of the network. For example, if the list
  7. was [2, 3, 1] then it would be a three-layer network, with the
  8. first layer containing 2 neurons, the second layer 3 neurons,
  9. and the third layer 1 neuron. The biases and weights for the
  10. network are initialized randomly, using a Gaussian
  11. distribution with mean 0, and variance 1. Note that the first
  12. layer is assumed to be an input layer, and by convention we
  13. won't set any biases for those neurons, since biases are only
  14. ever used in computing the outputs from later layers."""
  15. self.num_layers = len(sizes)
  16. self.sizes = sizes
  17. self.biases = [np.random.randn(y, 1) for y in sizes[1:]]
  18. self.weights = [np.random.randn(y, x) for x, y in zip(sizes[:-1], sizes[1:])]
  19. def feedforward(self, a):
  20. """Return the output of the network if ``a`` is input."""
  21. for b, w in zip(self.biases, self.weights):
  22. a = sigmoid(np.dot(w, a)+b)
  23. return a
  24. def SGD(self, training_data, epochs, mini_batch_size, eta, test_data=None):
  25. """Train the neural network using mini-batch stochastic
  26. gradient descent. The ``training_data`` is a list of tuples
  27. ``(x, y)`` representing the training inputs and the desired
  28. outputs. The other non-optional parameters are
  29. self-explanatory. If ``test_data`` is provided then the
  30. network will be evaluated against the test data after each
  31. epoch, and partial progress printed out. This is useful for
  32. tracking progress, but slows things down substantially."""
  33. if test_data:
  34. n_test = len(test_data)
  35. n = len(training_data)
  36. for j in xrange(epochs):
  37. random.shuffle(training_data)
  38. mini_batches = [training_data[k:k+mini_batch_size]
  39. for k in xrange(0, n, mini_batch_size)]
  40. for mini_batch in mini_batches:
  41. self.update_mini_batch(mini_batch, eta)
  42. if test_data:
  43. print ("Epoch {0}: {1} / {2}".format(j, self.evaluate(test_data), n_test))
  44. else:
  45. print ("Epoch {0} complete".format(j))
  46. def update_mini_batch(self, mini_batch, eta):
  47. """Update the network's weights and biases by applying
  48. gradient descent using backpropagation to a single mini batch.
  49. The ``mini_batch`` is a list of tuples ``(x, y)``, and ``eta``
  50. is the learning rate."""
  51. nabla_b = [np.zeros(b.shape) for b in self.biases]
  52. nabla_w = [np.zeros(w.shape) for w in self.weights]
  53. for x, y in mini_batch:
  54. delta_nabla_b, delta_nabla_w = self.backprop(x, y)
  55. nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
  56. nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
  57. self.weights = [w-(eta/len(mini_batch))*nw for w, nw in zip(self.weights, nabla_w)]
  58. self.biases = [b-(eta/len(mini_batch))*nb for b, nb in zip(self.biases, nabla_b)]
  59. def backprop(self, x, y):
  60. """Return a tuple ``(nabla_b, nabla_w)`` representing the
  61. gradient for the cost function C_x. ``nabla_b`` and
  62. ``nabla_w`` are layer-by-layer lists of numpy arrays, similar
  63. to ``self.biases`` and ``self.weights``."""
  64. nabla_b = [np.zeros(b.shape) for b in self.biases]
  65. nabla_w = [np.zeros(w.shape) for w in self.weights]
  66. # feedforward
  67. activation = x
  68. activations = [x] # list to store all the activations, layer by layer
  69. zs = [] # list to store all the z vectors, layer by layer
  70. for b, w in zip(self.biases, self.weights):
  71. z = np.dot(w, activation)+b
  72. zs.append(z)
  73. activation = sigmoid(z)
  74. activations.append(activation)
  75. # backward pass
  76. delta = self.cost_derivative(activations[-1], y) * \
  77. sigmoid_prime(zs[-1])
  78. nabla_b[-1] = delta
  79. nabla_w[-1] = np.dot(delta, activations[-2].transpose())
  80. # Note that the variable l in the loop below is used a little
  81. # differently to the notation in Chapter 2 of the book. Here,
  82. # l = 1 means the last layer of neurons, l = 2 is the
  83. # second-last layer, and so on. It's a renumbering of the
  84. # scheme in the book, used here to take advantage of the fact
  85. # that Python can use negative indices in lists.
  86. for l in xrange(2, self.num_layers):
  87. z = zs[-l]
  88. sp = sigmoid_prime(z)
  89. delta = np.dot(self.weights[-l+1].transpose(), delta) * sp
  90. nabla_b[-l] = delta
  91. nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())
  92. return (nabla_b, nabla_w)
  93. def evaluate(self, test_data):#评估,
  94. """Return the number of test inputs for which the neural
  95. network outputs the correct result. Note that the neural
  96. network's output is assumed to be the index of whichever
  97. neuron in the final layer has the highest activation."""
  98. test_results = [(np.argmax(self.feedforward(x)), y)
  99. for (x, y) in test_data]
  100. return sum(int(x == y) for (x, y) in test_results)
  101. def cost_derivative(self, output_activations, y):
  102. """Return the vector of partial derivatives \partial C_x /
  103. \partial a for the output activations."""
  104. return (output_activations-y)
  105. def sigmoid(z):
  106. """The sigmoid function."""
  107. return 1.0/(1.0+np.exp(-z))
  108. def sigmoid_prime(z):
  109. """Derivative of the sigmoid function."""
  110. return sigmoid(z)*(1-sigmoid(z))

  
  1. import pickle as cPickle
  2. import gzip
  3. import numpy as np
  4. def load_data():
  5. """Return the MNIST data as a tuple containing the training data,
  6. the validation data, and the test data.
  7. The ``training_data`` is returned as a tuple with two entries.
  8. The first entry contains the actual training images. This is a
  9. numpy ndarray with 50,000 entries. Each entry is, in turn, a
  10. numpy ndarray with 784 values, representing the 28 * 28 = 784
  11. pixels in a single MNIST image.
  12. The second entry in the ``training_data`` tuple is a numpy ndarray
  13. containing 50,000 entries. Those entries are just the digit
  14. values (0...9) for the corresponding images contained in the first
  15. entry of the tuple.
  16. The ``validation_data`` and ``test_data`` are similar, except
  17. each contains only 10,000 images.
  18. This is a nice data format, but for use in neural networks it's
  19. helpful to modify the format of the ``training_data`` a little.
  20. That's done in the wrapper function ``load_data_wrapper()``, see
  21. below.
  22. """
  23. f = gzip.open('../data/mnist.pkl.gz', 'rb')
  24. training_data, validation_data, test_data = cPickle.load(f,encoding='bytes') #(f,encoding='bytes')
  25. f.close()
  26. return (training_data, validation_data, test_data)
  27. def load_data_wrapper():
  28. """Return a tuple containing ``(training_data, validation_data,
  29. test_data)``. Based on ``load_data``, but the format is more
  30. convenient for use in our implementation of neural networks.
  31. In particular, ``training_data`` is a list containing 50,000
  32. 2-tuples ``(x, y)``. ``x`` is a 784-dimensional numpy.ndarray
  33. containing the input image. ``y`` is a 10-dimensional
  34. numpy.ndarray representing the unit vector corresponding to the
  35. correct digit for ``x``.
  36. ``validation_data`` and ``test_data`` are lists containing 10,000
  37. 2-tuples ``(x, y)``. In each case, ``x`` is a 784-dimensional
  38. numpy.ndarry containing the input image, and ``y`` is the
  39. corresponding classification, i.e., the digit values (integers)
  40. corresponding to ``x``.
  41. Obviously, this means we're using slightly different formats for
  42. the training data and the validation / test data. These formats
  43. turn out to be the most convenient for use in our neural network
  44. code."""
  45. tr_d, va_d, te_d = load_data()
  46. training_inputs = [np.reshape(x, (784, 1)) for x in tr_d[0]]
  47. training_results = [vectorized_result(y) for y in tr_d[1]]
  48. training_data = zip(training_inputs, training_results)
  49. validation_inputs = [np.reshape(x, (784, 1)) for x in va_d[0]]
  50. validation_data = zip(validation_inputs, va_d[1])
  51. test_inputs = [np.reshape(x, (784, 1)) for x in te_d[0]]
  52. test_data = zip(test_inputs, te_d[1])
  53. return (training_data, validation_data, test_data)
  54. def vectorized_result(j):
  55. """Return a 10-dimensional unit vector with a 1.0 in the jth
  56. position and zeroes elsewhere. This is used to convert a digit
  57. (0...9) into a corresponding desired output from the neural
  58. network."""
  59. e = np.zeros((10, 1))
  60. e[j] = 1.0
  61. return e

 

DL之NN:利用(本地数据集50000张数据集)调用自定义神经网络network.py实现手写图片识别94%

文章来源: yunyaniu.blog.csdn.net,作者:一个处女座的程序猿,版权归原作者所有,如需转载,请联系作者。

原文链接:yunyaniu.blog.csdn.net/article/details/80022781

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。