Ex 1: Visualization of MLP weights on MNIST
此範例將使用MNIST dataset的訓練資料集去訓練MLPClassifier,資料集中每張圖片都是28*28,對於第一層的每個神經元都會有28*28個特徵,輸出結果是將訓練資料的每個像素點對於神經元的權重畫成28*28的圖,用來表示圖片上每個像素點對於神經元的權重多寡。

(一)引入函式庫與資料

1.matplotlib.pyplot:用來繪製影像 2.sklearn.datasets:引入內建的手寫數字資料庫 3.sklearn.neural_network:引入類神經網路的套件
1
import matplotlib.pyplot as plt
2
from sklearn.datasets import fetch_mldata
3
from sklearn.neural_network import MLPClassifier
4
mnist = fetch_mldata("MNIST original")
Copied!

(二)將資料切割成訓練集與測試集

1
# 將灰階影像降尺度降到[0,1]
2
X, y = mnist.data / 255., mnist.target
3
X_train, X_test = X[:60000], X[60000:]
4
y_train, y_test = y[:60000], y[60000:]
Copied!

(三)設定分類器參數與訓練網路並畫出權重矩陣

1
#hidden_layer_sizes=(50)此處使用1層隱藏層,只有50個神經元,max_iter=10疊代訓練10次
2
mlp = MLPClassifier(hidden_layer_sizes=(50), max_iter=10, alpha=1e-4,
3
solver='sgd', verbose=10, tol=1e-4, random_state=1,
4
learning_rate_init=.1)
5
6
mlp.fit(X_train, y_train)
7
#畫出16個神經元的權重圖,黑色表示負的權重,越深色表示數值越大,白色表示正的權重,越淺色表示數值越大
8
fig, axes = plt.subplots(4, 4)
9
# use global min / max to ensure all weights are shown on the same scale
10
vmin, vmax = mlp.coefs_[0].min(), mlp.coefs_[0].max()
11
for coef, ax in zip(mlp.coefs_[0].T, axes.ravel()):
12
ax.matshow(coef.reshape(28, 28), cmap=plt.cm.gray, vmin=.5 * vmin,
13
vmax=.5 * vmax)
14
ax.set_xticks(())
15
ax.set_yticks(())
16
17
plt.show()
Copied!
圖1:16個神經元對於影像的權重圖
圖2:疊代計算時loss下降

(四)完整程式碼

1
print(__doc__)
2
3
import matplotlib.pyplot as plt
4
from sklearn.datasets import fetch_mldata
5
from sklearn.neural_network import MLPClassifier
6
7
mnist = fetch_mldata("MNIST original")
8
9
X, y = mnist.data / 255., mnist.target
10
X_train, X_test = X[:60000], X[60000:]
11
y_train, y_test = y[:60000], y[60000:]
12
13
14
mlp = MLPClassifier(hidden_layer_sizes=(50), max_iter=10, alpha=1e-4,
15
solver='sgd', verbose=10, tol=1e-4, random_state=1,
16
learning_rate_init=.1)
17
18
mlp.fit(X_train, y_train)
19
print("Training set score: %f" % mlp.score(X_train, y_train))
20
print("Test set score: %f" % mlp.score(X_test, y_test))
21
22
fig, axes = plt.subplots(4, 4)
23
24
vmin, vmax = mlp.coefs_[0].min(), mlp.coefs_[0].max()
25
for coef, ax in zip(mlp.coefs_[0].T, axes.ravel()):
26
ax.matshow(coef.reshape(28, 28), cmap=plt.cm.gray, vmin=.5 * vmin,
27
vmax=.5 * vmax)
28
ax.set_xticks(())
29
ax.set_yticks(())
30
31
plt.show()
Copied!