Skip to content

Commit c745b5c

Browse files
dheyaymikeheddes
andauthored
Add similarity plotting utilities (#67)
* Initial functions for plotting added * update * Adding matplotlib dependency * Added utils module and updated functions to include matplotlib axes * remove matplotlib dependency from functional * Added examples and updates * fix * minor updates * Update plotting functions Co-authored-by: mikeheddes <mikeheddes@gmail.com>
1 parent bb72ce4 commit c745b5c

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ Torchhd is a Python library dedicated to *Hyperdimensional Computing* (also knwo
1919
embeddings
2020
structures
2121
datasets
22+
utils
2223

2324

docs/utils.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.. _utils:
2+
3+
torchhd.utils
4+
==================
5+
6+
.. currentmodule:: torchhd.utils
7+
8+
This module provides plotting functions for hypervector similarities.
9+
10+
.. autosummary::
11+
:toctree: generated/
12+
:template: function.rst
13+
14+
plot_similarity
15+
plot_pair_similarity
16+

torchhd/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import torchhd.embeddings as embeddings
33
import torchhd.structures as structures
44
import torchhd.datasets as datasets
5+
import torchhd.utils as utils
56

67
from torchhd.functional import (
78
identity_hv,
@@ -20,6 +21,7 @@
2021
"embeddings",
2122
"structures",
2223
"datasets",
24+
"utils",
2325
"identity_hv",
2426
"random_hv",
2527
"level_hv",

torchhd/utils.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import torch
2+
from torch import Tensor
3+
4+
import torchhd.functional as functional
5+
6+
7+
def plot_pair_similarity(memory: Tensor, ax=None, **kwargs):
8+
"""Plots the pair-wise similarity of a hypervector set.
9+
10+
Args:
11+
memory (Tensor): The set of :math:`n` hypervectors whose pair-wise similarity is to be displayed.
12+
ax (matplotlib.axes, optional): Axes in which to draw the plot.
13+
14+
Other Parameters:
15+
**kwargs: `matplotlib.axes.Axes.pcolormesh <https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.pcolormesh.html>`_ arguments.
16+
17+
Returns:
18+
matplotlib.collections.QuadMesh: `matplotlib.collections.QuadMesh <https://matplotlib.org/stable/api/collections_api.html#matplotlib.collections.QuadMesh>`_.
19+
20+
Shapes:
21+
- Memory: :math:`(n, d)`
22+
23+
Examples::
24+
25+
>>> import matplotlib.pyplot as plt
26+
>>> hv = torchhd.level_hv(10, 10000)
27+
>>> utils.plot_pair_similarity(hv)
28+
>>> plt.show()
29+
"""
30+
try:
31+
import matplotlib.pyplot as plt
32+
except ImportError:
33+
raise ImportError(
34+
"Install matplotlib to use plotting functionality. \
35+
See https://matplotlib.org/stable/users/installing/index.html for more information."
36+
)
37+
38+
similarity = []
39+
for vector in memory:
40+
similarity.append(functional.cosine_similarity(vector, memory).tolist())
41+
42+
if ax is None:
43+
ax = plt.gca()
44+
45+
xy = torch.arange(memory.size(-2))
46+
x, y = torch.meshgrid(xy, xy)
47+
48+
ax.set_aspect("equal", adjustable="box")
49+
return ax.pcolormesh(x, y, similarity, **kwargs)
50+
51+
52+
def plot_similarity(input: Tensor, memory: Tensor, ax=None, **kwargs):
53+
"""Plots the similarity of an one hypervector with a set of hypervectors.
54+
55+
Args:
56+
input (torch.Tensor): Hypervector to compare against the memory.
57+
memory (torch.Tensor): Set of :math:`n` hypervectors.
58+
ax (matplotlib.axes, optional): Axes in which to draw the plot.
59+
60+
Other Parameters:
61+
**kwargs: `matplotlib.axes.Axes.stem <https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.stem.html?highlight=stem#matplotlib.axes.Axes.stem>`_ arguments.
62+
63+
Returns:
64+
StemContainer: `matplotlib.container.StemContainer <https://matplotlib.org/stable/api/container_api.html#matplotlib.container.StemContainer>`_.
65+
66+
Shapes:
67+
- Input: :math:`(d)`
68+
- Memory: :math:`(n, d)`
69+
70+
Examples::
71+
72+
>>> import matplotlib.pyplot as plt
73+
>>> hv = torchhd.level_hv(10, 10000)
74+
>>> utils.plot_similarity(hv[4], hv)
75+
>>> plt.show()
76+
77+
"""
78+
try:
79+
import matplotlib.pyplot as plt
80+
except ImportError:
81+
raise ImportError(
82+
"Install matplotlib to use plotting functionality. \
83+
See https://matplotlib.org/stable/users/installing/index.html for more information."
84+
)
85+
86+
similarity = functional.cosine_similarity(input, memory).tolist()
87+
88+
if ax is None:
89+
ax = plt.gca()
90+
91+
return ax.stem(similarity, **kwargs)

0 commit comments

Comments
 (0)