觀察卷積
檢視卷積層的活化值是件很酷的事。至少要體驗一次!
為了做到這點,你會使用 Keras 的 Model 物件來建立一個新模型。它會接收輸入清單與輸出清單。你要提供給這個新模型的輸出,是在輸入 MNIST 數字影像時,第一個卷積層的輸出。
你在前一個練習中建立的卷積 model 已經幫你訓練好了。它現在可以正確分類 MNIST 的手寫影像。你可以在主控台用 model.summary() 來檢查。
現在就來看看這個模型第一個卷積層所學到的卷積遮罩吧!
本練習屬於課程
Keras 深度學習入門
練習說明
- 取得模型中第一個卷積層輸出的參照。
- 使用模型第一層的輸入與
first_layer_output作為輸出,建立一個新模型。 - 使用這個
first_layer_model對X_test進行預測。 - 繪製
X_test第 1 個數字的活化值中,第 15 與第 18 個神經元過濾器的結果。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Obtain a reference to the outputs of the first layer
first_layer_output = model.layers[____].output
# Build a model using the model's input and the first layer output
first_layer_model = Model(inputs = model.layers[0].input, outputs = ____)
# Use this model to predict on X_test
activations = ____.____(____)
# Plot the activations of first digit of X_test for the 15th filter
axs[0].matshow(activations[____,:,:,14], cmap = 'viridis')
# Do the same but for the 18th filter now
axs[1].matshow(activations[0,:,:,____], cmap = 'viridis')
plt.show()