1. 程式人生 > 其它 >深度學習入門(魚書)學習筆記:第3章 神經網路

深度學習入門(魚書)學習筆記:第3章 神經網路

目錄導航

第3章 神經網路

3.1 從感知機到神經網路

3.2 啟用函式

3.3 多維陣列運算

3.4 3層神經網路的實現

3.5 輸出層設計

3.6 手寫數字識別

ch03/mnist_show.py的實現:

點選mnist_show.py程式碼
# coding: utf-8
import sys, os
sys.path.append(os.pardir)  # 為了匯入父目錄的檔案而進行的設定
import numpy as np
from dataset_all.mnist import load_mnist
from PIL import Image


def img_show(img):
    pil_img = Image.fromarray(np.uint8(img))
    pil_img.show()


(x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False)

index = 0
img = x_train[index]
label = t_train[index]
print(label)  # 5

print(img.shape)  # (784,)
img = img.reshape(28, 28)  # 把影象的形狀變為原來的尺寸
print(img.shape)  # (28, 28)


if __name__ == '__main__':
    img_show(img)

執行ch03/mnist_show.py的結果

點選檢視執行輸出
"D:\Program Files\Python\Python37\python.exe" D:/Code/CodePython/02_dl_from_scratch_demo/ch03/mnist_show.py
Converting train-images-idx3-ubyte.gz to NumPy Array ...
Done
Converting train-labels-idx1-ubyte.gz to NumPy Array ...
Done
Converting t10k-images-idx3-ubyte.gz to NumPy Array ...
Done
Converting t10k-labels-idx1-ubyte.gz to NumPy Array ...
Done
Creating pickle file ...
Done!
0
(784,)
(28, 28)

Process finished with exit code 0

目錄dataset_all裡會儲存一個mnist.pkl檔案。注意儲存和讀取pkl檔案的Python版本必須保持一致,否則會報錯。比如,在一臺電腦上使用Python3.7生成並儲存pkl檔案,而在另一臺電腦上讀取pkl檔案就會報錯。一個解決辦法就是不把pkl檔案新增到git中。

3.7 小結