我是靠谱客的博主 甜甜学姐,最近开发中收集的这篇文章主要介绍python bp神经网络 mnist_BP神经网络在python下的自主搭建梳理,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1 #!/usr/bin/env python

2 #coding: utf-8

3

4 #In[1]:

5

6

7 importnumpy8 importscipy.special9 importmatplotlib.pyplot10

11

12 #In[2]:

13

14

15 classneuralNetwork:16 def __init__(self, inputNodes, hiddenNodes, outputNodes,learningRate):17 self.iNodes =inputNodes18 self.oNodes =outputNodes19 self.hNodes =hiddenNodes20 self.lr =learningRate21 self.wih = numpy.random.normal (0.0, pow(self.hNodes,-0.5), (self.hNodes, self.iNodes))22 self.who = numpy.random.normal (0.0, pow(self.oNodes,-0.5), (self.oNodes, self.hNodes))23

24 self.activation_function = lambdax: scipy.special.expit(x)25 #print(self.wih)

26 pass

27

28 deftrain(self,inputs_list, target_list):29 inputs = numpy.array(inputs_list, ndmin=2).T30 targets = numpy.array(target_list, ndmin=2).T31 #print(inputs)

32 #print(targets)

33 hidden_inputs =numpy.dot(self.wih,inputs)34 #print(self.wih.shape)

35 #print(inputs.shape)

36 hidden_outputs =self.activation_function(hidden_inputs)37 #print(hidden_inputs)

38 final_inputs =numpy.dot(self.who,hidden_outputs)39 #print(hidden_outputs)

40 final_outputs =self.activation_function(final_inputs)41

42 output_errors = targets -final_outputs43 hidden_errors =numpy.dot(self.who.T,output_errors)44 self.who += self.lr * numpy.dot((output_errors * final_outputs * (1.0 -final_outputs)),numpy.transpose(hidden_outputs))45 self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 -hidden_outputs)),numpy.transpose(inputs))46 pass

47

48 defquery(self, inputs_list):49 inputs = numpy.array(inputs_list, ndmin=2).T50 hidden_inputs =numpy.dot(self.wih,inputs)51 hidden_outputs =self.activation_function(hidden_inputs)52 final_inputs =numpy.dot(self.who,hidden_outputs)53 final_outpus =self.activation_function(final_inputs)54 returnfinal_outpus55 pass

56

57

58

59 #In[3]:

60

61

62 inputNodes = 784

63 outputNodes = 10

64 hiddenNodes = 100

65 learningRate = 0.1

66 nN =neuralNetwork(inputNodes, hiddenNodes, outputNodes, learningRate)67

68

69 #In[4]:

70

71

72 data_file = open("mnist_train.csv",'r')73 data_list =data_file.readlines()74 data_file.close()75

76

77 #In[5]:

78

79

80 epochs = 1

81 for e inrange(epochs) :82 for record indata_list:83 all_values = record.split(',')84 inputs = numpy.asfarray( all_values [1:])/255.0*0.99+0.01

85 targets = numpy.zeros(outputNodes) + 0.01

86 targets[int (all_values[0])] = 0.99

87 nN.train(inputs,targets)88 pass

89 pass

90

91

92 #In[6]:

93

94

95 test_data_file = open("mnist_test.csv",'r')96 test_data_list =test_data_file.readlines()97 test_data_file.close()98

99

100 #In[7]:

101

102

103 scorecard =[]104 for record intest_data_list:105 all_values = record.split(',')106 correct_label =int(all_values[0])107 inputs = numpy.asfarray( all_values [1:])/255.0*0.99+0.01

108 outputs =nN.query(inputs)109 label =numpy.argmax(outputs)110 if(label ==correct_label):111 scorecard.append(1)112 else:113 scorecard.append(0)114 pass

115 pass

116

117

118 #In[8]:

119

120

121 scorecard_array =numpy.asarray(scorecard)122 print ("performance =" ,scorecard_array.sum()/scorecard_array.size)123

124

125 #In[9]:

126

127

128 importscipy.misc129 img_array = scipy.misc.imread('test.png',flatten="True")130 img_data = 255.0 - img_array . reshape(784)131 img_data = (img_data /255.0 * 0.99 ) + 0.01

132 op=nN.query(img_data)133 print(op)134 print(numpy.argmax(op))135

136

137 #In[10]:

138

139

140 all_values = data_list[1].split(',')141 image_array = numpy.asfarray( all_values [1:]).reshape((28,28))142 matplotlib.pyplot.imshow(image_array, cmap = 'Greys',interpolation='None')

最后

以上就是甜甜学姐为你收集整理的python bp神经网络 mnist_BP神经网络在python下的自主搭建梳理的全部内容,希望文章能够帮你解决python bp神经网络 mnist_BP神经网络在python下的自主搭建梳理所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部