建立 CNN 模型
在 Keras 中建立 CNN 模型並不比你在本課程先前建立的模型更困難!你只需要使用卷積層即可。
你將建立一個淺層的卷積式 model,用來分類 MNIST 手寫數字資料集。這就是你之前用自動編碼器去雜訊過的那個資料集!影像大小為 28 x 28 像素,且因為是黑白圖片,只有 1 個通道。
現在就動手建立這個小型的卷積模型吧!
本練習屬於課程
Keras 深度學習入門
練習說明
- 匯入
Conv2D與Flatten層,並建立你的模型實例。 - 新增第一個卷積層,包含 32 個大小為 3x3 的濾波器,並以對應的 3D tuple 作為
input_shape。 - 新增第二個卷積層,包含 16 個大小為 3x3 的濾波器,啟用函式使用 relu。
- 將前一層的輸出攤平成一維向量。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the Conv2D and Flatten layers and instantiate model
from tensorflow.keras.____ import ____,____
model = ____
# Add a convolutional layer of 32 filters of size 3x3
model.add(Conv2D(____, kernel_size = ____, input_shape = (____, ____, 1), activation = 'relu'))
# Add a convolutional layer of 16 filters of size 3x3
model.add(____(____, ____ = ____, activation = ____))
# Flatten the previous layer output
model.add(____)
# Add as many outputs as classes with softmax activation
model.add(Dense(10, activation = 'softmax'))