类神经网路 Neural_Networks - Ex 1: Visualization of MLP weights on MNIST

优质
小牛编辑
136浏览
2023-12-01

http://scikit-learn.org/stable/auto_examples/neural_networks/plot_mnist_filters.html

此范例将使用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")

(二)将资料切割成训练集与测试集

  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:]

(三)设定分类器参数与训练网路并画出权重矩阵

  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. mlp.fit(X_train, y_train)
  6. #画出16个神经元的权重图,黑色表示负的权重,越深色表示数值越大,白色表示正的权重,越浅色表示数值越大
  7. fig, axes = plt.subplots(4, 4)
  8. # use global min / max to ensure all weights are shown on the same scale
  9. vmin, vmax = mlp.coefs_[0].min(), mlp.coefs_[0].max()
  10. for coef, ax in zip(mlp.coefs_[0].T, axes.ravel()):
  11. ax.matshow(coef.reshape(28, 28), cmap=plt.cm.gray, vmin=.5 * vmin,
  12. vmax=.5 * vmax)
  13. ax.set_xticks(())
  14. ax.set_yticks(())
  15. plt.show()

Ex 1: Visualization of MLP weights on MNIST - 图1

图1:16个神经元对于影像的权重图

Ex 1: Visualization of MLP weights on MNIST - 图2

图2:叠代计算时loss下降

(四)完整程式码

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