Sometimes looking at the learned coefficients of a neural network can provide insight into the learning behavior. For example if weights look unstructured, maybe some were not used at all, or if very large coefficients exist, maybe regularization was too low or the learning rate too high.
This example shows how to plot some of the first layer weights in a MLPClassifier trained on the MNIST dataset. The input data consists of 28x28 pixel handwritten digits, leading to 784 features in the dataset. Therefore the first layer weight matrix have the shape (784, hidden_layer_sizes). We can therefore visualize a single column of the weight matrix as a 28x28 pixel image.
To make the example run faster, we use very few hidden units, and train only for a very short time. Training longer would result in weights with a much smoother spatial appearance.
import sklearn sklearn.__version__
print(__doc__) import plotly.plotly as py import plotly.graph_objs as go from plotly import tools import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import fetch_mldata from sklearn.neural_network import MLPClassifier
Automatically created module for IPython interactive environment
mnist = fetch_mldata("MNIST original") # rescale the data, use the traditional train/test split X, y = mnist.data / 255., mnist.target X_train, X_test = X[:60000], X[60000:] y_train, y_test = y[:60000], y[60000:] # mlp = MLPClassifier(hidden_layer_sizes=(100, 100), max_iter=400, alpha=1e-4, # solver='sgd', verbose=10, tol=1e-4, random_state=1) mlp = MLPClassifier(hidden_layer_sizes=(50,), max_iter=10, alpha=1e-4, solver='sgd', verbose=10, tol=1e-4, random_state=1, learning_rate_init=.1) mlp.fit(X_train, y_train) print("Training set score: %f" % mlp.score(X_train, y_train)) print("Test set score: %f" % mlp.score(X_test, y_test))
Iteration 1, loss = 0.32212731 Iteration 2, loss = 0.15738787 Iteration 3, loss = 0.11647274 Iteration 4, loss = 0.09631113 Iteration 5, loss = 0.08074513 Iteration 6, loss = 0.07163224 Iteration 7, loss = 0.06351392 Iteration 8, loss = 0.05694146 Iteration 9, loss = 0.05213487 Iteration 10, loss = 0.04708320 Training set score: 0.985733 Test set score: 0.971000
fig = tools.make_subplots(rows=4, cols=4, print_grid=False) def matplotlib_to_plotly(cmap, pl_entries): h = 1.0/(pl_entries-1) pl_colorscale =  for k in range(pl_entries): C = map(np.uint8, np.array(cmap(k*h)[:3])*255) pl_colorscale.append([k*h, 'rgb'+str((C, C, C))]) return pl_colorscale cmap = matplotlib_to_plotly(plt.cm.gray, 5)
# use global min / max to ensure all weights are shown on the same scale vmin, vmax = mlp.coefs_.min(), mlp.coefs_.max() order = np.linspace(0, 15, 16) for coef, i in zip(mlp.coefs_.T, order): p1 = go.Heatmap(z=coef.reshape(28, 28), colorscale=cmap, showscale=False) fig.append_trace(p1, int(i/4+1), int(i%4+1)) for i in map(str,range(1, 17)): y = 'yaxis'+ i x = 'xaxis' + i fig['layout'][y].update(autorange='reversed', showticklabels=False, ticks='') fig['layout'][x].update(showticklabels=False, ticks='') fig['layout'].update(height=700)