Visualizing Weight Matrices with Hinton Diagrams

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn how to use Hinton diagrams for visualizing weight matrices. Hinton diagrams are very useful when you want to visualize a 2D array, such as 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.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Importing Libraries

We will start by importing the necessary libraries for this lab. In this case, we will need matplotlib and numpy.

import matplotlib.pyplot as plt
import numpy as np

Defining the Hinton Function

Next, we will define a function called hinton that will generate the Hinton diagram. This function takes in a matrix, which is the weight matrix that we want to visualize, and a max_weight parameter, which is an optional parameter that specifies the maximum weight value for normalization purposes.

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.log2(np.abs(matrix).max()))

    ax.patch.set_facecolor('gray')
    ax.set_aspect('equal', 'box')
    ax.xaxis.set_major_locator(plt.NullLocator())
    ax.yaxis.set_major_locator(plt.NullLocator())

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

    ax.autoscale_view()
    ax.invert_yaxis()

Generating a Hinton Diagram

Now, we will generate a random weight matrix using numpy and then use the hinton function to generate the Hinton diagram.

if __name__ == '__main__':
    ## Fixing random state for reproducibility
    np.random.seed(19680801)

    hinton(np.random.rand(20, 20) - 0.5)
    plt.show()

Summary

In this lab, we learned how to use Hinton diagrams for visualizing weight matrices. We defined a function called hinton that generates the Hinton diagram and then used it to generate a random weight matrix. Hinton diagrams are very useful for visualizing 2D arrays, such as weight matrices, and can be used to quickly identify patterns and trends in the data.

Other Python Tutorials you may like