001 [deep learning] mnist handwritten identification number set - fully connected neural network - Based python3.7

Part of the code taken from the "deep learning portal - python based on the theory and practice" with the book code

After reading the "deep learning of mathematics' own use c ++ realize a bit, but failed, to buy a new reading of " deep learning portal - python based on the theory and practice " , above the code comes with the book, bring your own realization to learn about.

For the realization of the first layer:

. 1  # Coding: UTF. 8- 
2  Import SYS, OS
 . 3 sys.path.append (os.pardir)   # is set to the parent of the file to import carried out 
. 4  Import numpy AS NP
 . 5  from common.layers Import *
 . 6  from Common .gradient Import numerical_gradient
 . 7  from Collections Import OrderedDict
 . 8  
. 9  
10  class TwoLayerNet:
 . 11  
12 is      DEF  the __init__ (Self, input_size, hidden_size, output_size, weight_init_std = 0.01 ):
 13 is         # 初始化权重
14         self.params = {}
15         self.params['W1'] = weight_init_std * np.random.randn(input_size, hidden_size)
16         self.params['b1'] = np.zeros(hidden_size)
17         self.params['W2'] = weight_init_std * np.random.randn(hidden_size, output_size) 
18         self.params['b2'] = np.zeros(output_size)
19 
20         # 生成层
21         self.layers = OrderedDict()
22         self.layers['Affine1'] = Affine(self.params['W1'], self.params['b1'])
23         self.layers['Relu1'] = Relu()
24         self.layers['Affine2'] = Affine(self.params['W2'], self.params['b2'])
25 
26         self.lastLayer = SoftmaxWithLoss()
27         
28     def predict(self, x):
29         for layer in self.layers.values():
30             x = layer.forward(x)
31         
32         return x
33         
34     # x:输入数据, t:监督数据
35     def loss(self, x, t):
36         y = self.predict(x)
37         return self.lastLayer.forward(y, t)
38     
39     def accuracy(self, x, t):
40         y = self.predict(x)
41         y = np.argmax(y, axis=1)
42         if t.ndim != 1 : t = np.argmax(t, axis=1)
43         
44         accuracy = np.sum(y == t) / float(x.shape[0])
45         return accuracy
46         
47     # x:输入数据, t:监督数据
48     def numerical_gradient(self, x, t):
49         loss_W = lambda W: self.loss(x, t)
50         
51         grads = {}
52         grads['W1'] = numerical_gradient(loss_W, self.params['W1'])
53         grads['b1'] = numerical_gradient(loss_W, self.params['b1'])
54         grads['W2'] = numerical_gradient(loss_W, self.params['W2'])
55         grads['b2'] = numerical_gradient(loss_W, self.params['b2'])
56         
57         return grads
58         
59     def gradient(self, x, t):
60         # forward
61         self.loss(x, t)
62 
63         # backward
64         dout = 1
65         dout = self.lastLayer.backward(dout)
66         
67         layers = list(self.layers.values())
68         layers.reverse()
69         for layer in layers:
70             dout = layer.backward(dout)
71 
72         # 设定
73         grads = {}
74         grads['W1'], grads['b1'] = self.layers['Affine1'].dW, self.layers['Affine1'].db
75         grads['W2'], grads['b2'] = self.layers['Affine2'].dW, self.layers['Affine2'].db
76 
77         return grads

Some of these libraries can be downloaded with the book code Anaconda and Turing communities get (do not buy can also download)

Then the most important main program code:

 1 # coding: utf-8
 2 import sys, os
 3 sys.path.append(os.pardir)
 4 
 5 import numpy as np
 6 from dataset.mnist import load_mnist
 7 from two_layer_net import TwoLayerNet
 8 
 9 # 读入数据
10 (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, one_hot_label=True)
11 
12 network = TwoLayerNet(input_size=784, hidden_size=50, output_size=10)
13 
14 iters_num = 10000
15 train_size = x_train.shape[0]
16 batch_size = 100
17 learning_rate = 0.1
18 
19 train_loss_list = []
20 train_acc_list = []
21 test_acc_list = []
22 
23 iter_per_epoch = max(train_size / batch_size, 1)
24 
25 for i in range(iters_num):
26     batch_mask = np.random.choice(train_size, batch_size)
27     x_batch = x_train[batch_mask]
28     t_batch = t_train[batch_mask]
29     
30     # 梯度
31     #grad = network.numerical_gradient(x_batch, t_batch)
32     grad = network.gradient(x_batch, t_batch)
33     
34     # 更新
35     for key in ('W1', 'b1', 'W2', 'b2'):
36         network.params[key] -= learning_rate * grad[key]
37     
38     loss = network.loss(x_batch, t_batch)
39     train_loss_list.append(loss)
40     
41     if i % iter_per_epoch == 0:
42         train_acc = network.accuracy(x_train, t_train)
43         test_acc = network.accuracy(x_test, t_test)
44         train_acc_list.append(train_acc)
45         test_acc_list.append(test_acc)
46         print(train_acc, test_acc)

Running about (the first one is trained in the proper ratio, the second is to test the correct rate):

 

 The results can be seen by running the correct rate of 97%;

But just look at the correct rate and given him feel this program is really complete digital identification, we would like to see his identification of individual numbers

The following code is added at the main program:

# Reread without normalizing 
(x_train, t_train), (x_test, t_test) = load_mnist (Flatten = True, the normalize = False) 
batch_mask = np.random.choice (train_size,. 1 ) 
x_batch = x_train [batch_mask] 
t_batch = t_train [batch_mask] 

IMG = x_batch [0] 
label = t_batch [0] 


Y = network.predict (x_batch) 
Y = np.argmax (Y, = Axis. 1 )
 Print ( " the I Think IT IS " , Y) # computer thinks the answer 

Print ( " answer iS " , label)# Correct answer 
IMG = img.reshape (28, 28)   # the shape of the image into the original size 
img_show (IMG) # display image

It should be noted that the following program reads in a different program.

After running you can see:

 

 

 

 By the naked eye with our True image recognition we can also see that this is indeed 3

Some deviations can be recognized:

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/charainland/p/11972560.html