forked from intoli/intoli-article-materials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot-activation-layers.py
97 lines (79 loc) · 2.39 KB
/
plot-activation-layers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import keras
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from keras import initializers
from keras.datasets import mnist
from utils import (
compile_model,
create_mlp_model,
get_activations,
grid_axes_it,
)
seed = 10
# Number of points to plot
n_train = 1000
n_test = 100
n_classes = 10
# Network params
n_hidden_layers = 5
dim_layer = 100
batch_size = n_train
epochs = 1
# Load and prepare MNIST dataset.
n_train = 60000
n_test = 10000
(x_train, y_train), (x_test, y_test) = mnist.load_data()
num_classes = len(np.unique(y_test))
data_dim = 28 * 28
x_train = x_train.reshape(60000, 784).astype('float32')[:n_train]
x_test = x_test.reshape(10000, 784).astype('float32')[:n_train]
x_train /= 255
x_test /= 255
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
# Run the data through a few MLP models and save the activations from
# each layer into a Pandas DataFrame.
rows = []
sigmas = [0.10, 0.14, 0.28]
for stddev in sigmas:
init = initializers.RandomNormal(mean=0.0, stddev=stddev, seed=seed)
activation = 'relu'
model = create_mlp_model(
n_hidden_layers,
dim_layer,
(data_dim,),
n_classes,
init,
'zeros',
activation
)
compile_model(model)
output_elts = get_activations(model, x_test)
n_layers = len(model.layers)
i_output_layer = n_layers - 1
for i, out in enumerate(output_elts[:-1]):
if i > 0 and i != i_output_layer:
for out_i in out.ravel()[::20]:
rows.append([i, stddev, out_i])
df = pd.DataFrame(rows, columns=['Hidden Layer', 'Standard Deviation', 'Output'])
# Plot previously saved activations from the 5 hidden layers
# using different initialization schemes.
fig = plt.figure(figsize=(12, 6))
axes = grid_axes_it(len(sigmas), 1, fig=fig)
for sig in sigmas:
ax = next(axes)
ddf = df[df['Standard Deviation'] == sig]
sns.violinplot(x='Hidden Layer', y='Output', data=ddf, ax=ax, scale='count', inner=None)
ax.set_xlabel('')
ax.set_ylabel('')
ax.set_title('Weights Drawn from $N(\mu = 0, \sigma = {%.2f})$' % sig, fontsize=13)
if sig == sigmas[1]:
ax.set_ylabel("ReLu Neuron Outputs")
if sig != sigmas[-1]:
ax.set_xticklabels(())
else:
ax.set_xlabel("Hidden Layer")
plt.tight_layout()
plt.show()