此文章是介紹MNIST手寫辨識的作法及知識,分為兩個版本,,一個為Tensorflow版本,另一個為Pytorch版本,觀念的部分大致相同,比較不一樣的地會是在實作的部分,那廢話不多說,開始吧!
Contents
我會一步一步的講解每一個步驟,有些步驟是幫助我們做理解的,但對最後實作結果沒有明顯幫助,在文章最後面會有完整程式碼,而某些步驟只是讓我們更了解實作過程的那些程式碼不會被包含在內。
## 1. 引用第三方套件 1
2
3
4
5
6
7
8
9
10import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from tensorflow.keras.utils import to_categorical # one-hot encoding
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.datasets import mnist
# from tensorflow.keras.datasets import fashion_mnist
# (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()1
2
3
4
5
6
7
8
9(x_train, y_train), (x_test, y_test) = mnist.load_data()
n = 9487
plt.imshow(x_train[n], cmap='Greys')
plt.show()
x_train = x_train.reshape(60000, 784)/255
x_test = x_test.reshape(10000, 784)/255
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)1
2
3
4
5
6
7
8model = Sequential()
model.add(Dense(100, input_dim=784, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(10, activation='softmax')) # output
model.compile(loss='mse', optimizer=SGD(lr=0.09),
metrics=['accuracy'])
model.summary()1
model.fit(x_train, y_train, batch_size=100, epochs=20)
1
2
3
4
5
6
7
8
9predict = model.predict_classes(x_test)
n = 9999
score = model.evaluate(x_test, y_test)
print('test loss:', score[0])
print('test accuracy:', score[1])
print('神經網路預測是:', predict[n])
plt.imshow(x_test[n].reshape(28,28), cmap='Greys');
model.save("my_nn.h5")
plt.show()1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from tensorflow.keras.utils import to_categorical # one-hot encoding
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.datasets import mnist
# from tensorflow.keras.datasets import fashion_mnist
# (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
(x_train, y_train), (x_test, y_test) = mnist.load_data()
n = 9487
plt.imshow(x_train[n], cmap='Greys')
plt.show()
x_train = x_train.reshape(60000, 784)/255
x_test = x_test.reshape(10000, 784)/255
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
model = Sequential()
model.add(Dense(100, input_dim=784, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(10, activation='softmax')) # output
model.compile(loss='mse', optimizer=SGD(lr=0.09), metrics=['accuracy'])
model.summary()
model.fit(x_train, y_train, batch_size=100, epochs=20)
predict = model.predict_classes(x_test)
n = 9999
score = model.evaluate(x_test, y_test)
print('test loss:', score[0])
print('test accuracy:', score[1])
print('神經網路預測是:', predict[n])
plt.imshow(x_test[n].reshape(28,28), cmap='Greys');
model.save("my_nn.h5")
plt.show()