forked from yang-zhang/deep-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspect_keras_model.py
129 lines (59 loc) · 1.5 KB
/
inspect_keras_model.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# coding: utf-8
# In[1]:
from ds_utils.imports import *
# In[33]:
iris = sklearn.datasets.load_iris()
X = iris.data
y = iris.target
y_cat = keras.utils.to_categorical(y)
X.shape, y.shape, y_cat.shape
# In[39]:
model = keras.models.Sequential()
model.add(keras.layers.Dense(3, activation='relu', input_dim=X.shape[1]))
model.add(keras.layers.Dense(5, activation='relu'))
model.add(keras.layers.Dense(y_cat.shape[1], activation='softmax'))
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# In[47]:
model.fit(X, y_cat, epochs=10, batch_size=1, verbose=0)
# ### Summary
# In[40]:
model.summary()
# ### Layers
# In[137]:
layers = model.layers
# In[138]:
layer = layers[2]
# In[139]:
layer.get_config()
# In[140]:
layer.get_weights()
# In[ ]:
layer.
# ### Model Config
# In[46]:
config = model.get_config()
config
# In[90]:
pd.DataFrame([layer['config']['kernel_initializer']['config'] for layer in config])
# In[89]:
pd.DataFrame([layer['config']['bias_initializer']['config'] for layer in config])
# In[23]:
pd.DataFrame([layer['config'] for layer in config])
# ### Model Weights
# In[123]:
[w.shape for w in weights]
# In[126]:
for layer in model.layers:
print(layer.get_config())
print(layer.get_weights())
# In[127]:
weights[0]
# In[128]:
np.dot(X[0], weights[0][:,0])
# In[129]:
np.dot(X[0], weights[0])
# In[93]:
assert np.dot(X[0], weights[0][:,0]) == (np.dot(X[0], weights[0]))[0]
# In[ ]: