1

The following code provided in the matplotlib documentation creates Hinton diagrams:

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

I would like to create two Hinton diagrams: one for the weights going from the input to the hidden layer, and one going from the hidden layer to the output layer in my one-layer MLP. I have tried (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()

I have also tried (based on this answer):

h1 = hinton(W)
h2 = hinton(W_out)

pp = PdfPages('hinton_both.pdf')
pp.savefig(h1)
pp.savefig(h2)
pp.close()

Regardless, the outcome is the same: the Hinton diagram for W gets plotted twice. How can I include a Hinton diagram for my first set of weights and a Hinton diagram for my second set of weights in the same pdf (I would also settle for two separate pdfs, as long as I can get both Hinton diagrams)?

StatsSorceress
  • 3,019
  • 7
  • 41
  • 82

1 Answers1

1

The pdf.savefig() command saves the current figure. As there is only one current figure, it will save it twice. In order to get two figures, they need to be created, e.g. via plt.figure(1) and 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()

There are of course tons of different ways to save the two plots, anotherone might be

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)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Brilliant! Thank you very much. The only change I added was to use pl.figure(1) and pl.figure(2), as I imported pylot as pl, not plt – StatsSorceress Jul 16 '17 at 23:07
  • While you may of course `import matplotlib.pyplot as xasepgoah` or whatever you like, I think it's best to stay with the matplotlib style of using `plt` when asking and answering questions on SO. Also mind that the [recommended way of showing your gratitude](https://stackoverflow.com/help/someone-answers) would be to just upvote the answers you receive to your questions. – ImportanceOfBeingErnest Jul 16 '17 at 23:23