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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48766{{"`Visualizing Weight Matrices With Hinton Diagrams`"}} matplotlib/importing_matplotlib -.-> lab-48766{{"`Visualizing Weight Matrices With Hinton Diagrams`"}} matplotlib/figures_axes -.-> lab-48766{{"`Visualizing Weight Matrices With Hinton Diagrams`"}} python/conditional_statements -.-> lab-48766{{"`Visualizing Weight Matrices With Hinton Diagrams`"}} python/for_loops -.-> lab-48766{{"`Visualizing Weight Matrices With Hinton Diagrams`"}} python/lists -.-> lab-48766{{"`Visualizing Weight Matrices With Hinton Diagrams`"}} python/tuples -.-> lab-48766{{"`Visualizing Weight Matrices With Hinton Diagrams`"}} python/function_definition -.-> lab-48766{{"`Visualizing Weight Matrices With Hinton Diagrams`"}} python/default_arguments -.-> lab-48766{{"`Visualizing Weight Matrices With Hinton Diagrams`"}} python/importing_modules -.-> lab-48766{{"`Visualizing Weight Matrices With Hinton Diagrams`"}} python/standard_libraries -.-> lab-48766{{"`Visualizing Weight Matrices With Hinton Diagrams`"}} python/math_random -.-> lab-48766{{"`Visualizing Weight Matrices With Hinton Diagrams`"}} python/numerical_computing -.-> lab-48766{{"`Visualizing Weight Matrices With Hinton Diagrams`"}} python/data_visualization -.-> lab-48766{{"`Visualizing Weight Matrices With Hinton Diagrams`"}} python/build_in_functions -.-> lab-48766{{"`Visualizing Weight Matrices With Hinton Diagrams`"}} end

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