12345678910111213141516171819202122232425262728293031323334353637383940 |
- from sklearn.neural_network import MLPClassifier
- from sklearn.datasets import fetch_openml
- import matplotlib.pyplot as plt
- def main():
- mnist_data = fetch_openml("mnist_784")
- x, y = mnist_data.data / 255., mnist_data.target
- x_train, x_test = x[:60000], x[60000:]
- y_train, y_test = y[:60000], y[60000:]
- mlp = MLPClassifier(hidden_layer_sizes=(50,), max_iter=10, alpha=1e-4, solver='sgd', tol=1e-4, random_state=1,
- learning_rate_init=.1)
-
-
-
-
-
-
-
-
- mlp.fit(x_train, y_train)
- print(mlp.score(x_train, y_train))
- print(mlp.score(x_test, y_test))
- fig, axes = plt.subplots(4, 4)
-
- vmin, vmax = mlp.coefs_[0].min(), mlp.coefs_[0].max()
- for coef, ax in zip(mlp.coefs_[0].T, axes.ravel()):
- ax.matshow(coef.reshape(28, 28), cmap=plt.cm.gray, vmin=.5 * vmin, vmax=.5 * vmax)
- ax.set_xticks(())
- ax.set_yticks(())
- plt.show()
- if __name__ == "__main__":
- main()
|