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