1
下面的代碼提供in the matplotlib documentation創建Hinton的圖:PdfPages在Matplotlib保存同一圖兩次
def hinton(matrix, max_weight=None, ax=None):
"""Draw Hinton diagram for visualizing a weight matrix."""
ax = ax if ax is not None else plt.gca()
if not max_weight:
max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max())/np.log(2))
ax.patch.set_facecolor('gray')
ax.set_aspect('equal', 'box')
ax.xaxis.set_major_locator(pl.NullLocator())
ax.yaxis.set_major_locator(pl.NullLocator())
for (x, y), w in np.ndenumerate(matrix):
color = 'white' if w > 0 else 'black'
size = np.sqrt(np.abs(w)/max_weight)
rect = pl.Rectangle([x - size/2, y - size/2], size, size,
facecolor=color, edgecolor=color)
ax.add_patch(rect)
ax.autoscale_view()
ax.invert_yaxis()
我想創建兩個Hinton的圖表:一個用於權重從輸入要隱藏層,和一個從我的單層MLP中的隱藏層到輸出層。我曾嘗試(based on this jupyter notebook):
W = model_created.layers[0].kernel.get_value(borrow=True)
W = np.squeeze(W)
print("W shape : ", W.shape) # (153, 15)
W_out = model_created.layers[1].kernel.get_value(borrow=True)
W_out = np.squeeze(W_out)
print('W_out shape : ', W_out.shape) # (15, 8)
with PdfPages('hinton_again.pdf') as pdf:
h1 = hinton(W)
h2 = hinton(W_out)
pdf.savefig()
我也曾嘗試(based on this answer):
h1 = hinton(W)
h2 = hinton(W_out)
pp = PdfPages('hinton_both.pdf')
pp.savefig(h1)
pp.savefig(h2)
pp.close()
無論如何,結局都是一樣的:W的韓丁圖繪製得到兩次。我怎樣才能在我的第一套權重中加入欣頓圖,並在同一pdf中加入我的第二組權重的欣頓圖(如果我能得到兩張欣頓圖,我還將解決兩個單獨的pdf)?
輝煌!非常感謝你。我添加的唯一更改是使用pl.figure(1)和pl.figure(2),因爲我導入pylot作爲pl,而不是plt – StatsSorceress
雖然您當然可以將matplotlib.pyplot導入爲xasepgoah或任何您喜歡的,我認爲最好在提問和回答關於SO的問題時保持使用'plt'的matplotlib風格。另外請注意,[推薦的顯示你的感謝的方式](https://stackoverflow.com/help/someone-answers)只是提供你收到的問題的答案。 – ImportanceOfBeingErnest