我們直接來產生 MNIST CNN 模型特徵圖,使用 Keras 和 Matplotlib 來構建模型。
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Model
from keras.layers import Conv2D, Flatten, Dense, Input
from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = np.expand_dims(x_train, -1) / 255.0
x_test = np.expand_dims(x_test, -1) / 255.0
x_train = np.repeat(x_train, 3, axis=-1)
x_test = np.repeat(x_test, 3, axis=-1)
input_shape = (28, 28, 3)
inputs = Input(shape=input_shape)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
x = Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = Flatten()(x)
outputs = Dense(10, activation='softmax')(x)
model = Model(inputs, outputs)
feature_model = Model(inputs=inputs, outputs=model.layers[1].output)
sample = np.expand_dims(x_test[0], axis=0)
feature_map = feature_model.predict(sample)
def plot_feature_maps(feature_maps):
num_filters = feature_maps.shape[-1]
plt.figure(figsize=(15, 15))
for i in range(num_filters):
plt.subplot(8, 8, i + 1)
plt.imshow(feature_maps[0, :, :, i], cmap='viridis')
plt.axis('off')
plt.tight_layout()
plt.show()
plot_feature_maps(feature_map)
feature_model.predict 用來獲取特徵圖,以下這張圖為輸出結果,特別注意它是未經訓練前的圖:
要訓練模型只需要加上這兩行程式碼:
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5, verbose=1)
就會得到:
要對比訓練模型前後差異,我們需要先了解什麼是反向傳播演算法。
反向傳播(Backpropagation)是一種用於訓練人工神經網路的算法,主要用來計算網絡中每一層的梯度,從而更新權重以最小化損失函數。以下是反向傳播的基本步驟:
以下附上每一步驟的數學公式:
假設有一個簡單的神經網路: