Searching for tutorials and software about Deep Learning and Neural Nets? Be sure to look at my Resource Page!
Looking for Octave? Go to my Easy Octave on Mac page!

Wednesday, November 11, 2015

Hinton diagrams with iPython and Matplotlib

When you're an old geek, learning new stuff is a pain. So here I am, stuck with the problem of learning the various Python science tools and the environment. Here is a simple plot with iPython. The first line is a "magic" that  loads in np and matplotlib and sets up graphics to display in the notebook.

Hinton diagrams are cell mosaics to visualize weight matrices. Each cell contains a black or white tile  set on a grey background - the size of the tile represents the magnitude of the weight value, the color the sign. Here is a twisty and psychedelic example:


  1. Nice visualization for the Hinton diagram. What Python code does this?

  2. I think I googled Hinton diagram, ripped whatever code I found and ran it in iPython or Jupyter or whatever the notebook interface is called today. I do NOT think that I just copied the image off the web. If I find the code again I'll post it here, but I guess Google is your friend. I'm truly sorry, but this a result of my terminal disorganisation. In fact I think I remember I did this twice, losing the code between times.

  3. Ok, here's a link.

  4. """
    Demo of a function to create Hinton diagrams.

    Hinton diagrams are useful for visualizing the values of a 2D array (e.g.
    a weight matrix): Positive and negative values are represented by white and
    black squares, respectively, and the size of each square represents the
    magnitude of each value.

    Initial idea from David Warde-Farley on the SciPy Cookbook
    import numpy as np
    import matplotlib.pyplot as plt

    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.set_aspect('equal', 'box')

    for (x, y), w in np.ndenumerate(matrix):
    color = 'white' if w > 0 else 'black'
    size = np.sqrt(np.abs(w))
    rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
    facecolor=color, edgecolor=color)


    if __name__ == '__main__':
    hinton(np.random.rand(20, 20) - 0.5)


Hey, let me know what you think of my blog, and what material I should add!