2017-07-16 8 views
1

次のコードは、in the matplotlib documentationを提供ヒントン図を作成します。PdfPagesは同じ図を保存し二回

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() 

私は2つのヒントン図を作成したいと思います:中間層への入力から行くの重みのための1つを、そして1つは、私の1層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の2番目のウェイトセットのヒントンダイアグラムを含めるにはどうすればいいですか(私はヒントンダイアグラムを両方取得できる限り、2つの別々のpdfを解決します)。

答えて

1

pdf.savefig()コマンドは、現在のFigureを保存します。現在の数字は1つだけなので、2度保存します。 2つの数字を取得するには、それらを作成する必要があります(例: plt.figure(1)およびplt.figure(2)を介して。

with PdfPages('hinton_again.pdf') as pdf: 
    plt.figure(1) 
    h1 = hinton(W) 
    pdf.savefig() 
    plt.figure(2) 
    h2 = hinton(W_out) 
    pdf.savefig() 

2つのプロットを保存するためのさまざまな方法のコーストンであり、anotheroneは

fig, ax = plt.subplots() 
hinton(W, ax=ax) 

fig2, ax2 = plt.subplots() 
hinton(W_out, ax=ax2) 

with PdfPages('hinton_again.pdf') as pdf: 
    pdf.savefig(fig) 
    pdf.savefig(fig2) 
+0

ブリリアントかもしれません!どうもありがとうございました。私が追加した唯一の変更は、plotでなくpplとしてインポートしたので、pl.figure(1)とpl.figure(2)を使用することでした。 – StatsSorceress

+0

もちろん、import matplotlib.pyplot as xasepgoah'や好きなことがあれば、私は、あなたがSOの質問をしたり答えたりするときに、 'plt'を使ってmatplotlibスタイルをとっておくのが最善だと思います。また、[あなたの感謝の気持ちを示すための推奨される方法](https://stackoverflow.com/help/someone-answers)は、あなたがあなたの質問に受け取った回答をアップアップするだけであることに気づくでしょう。 – ImportanceOfBeingErnest

関連する問題