.. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here <sphx_glr_download_auto_examples_pytorch_plot_isomorphic_graphs_pytorch.py>` to download the full example code
.. rst-class:: sphx-glr-example-title
This example is an introduction to pygmtools
which shows how to match isomorphic graphs.
Isomorphic graphs mean graphs whose structures are identical, but the node correspondence is unknown.
# Author: Runzhong Wang <runzhong.wang@sjtu.edu.cn>
#
# License: Mulan PSL v2 License
Note
The following solvers support QAP formulation, and are included in this example:
- :func:`~pygmtools.classic_solvers.rrwm` (classic solver)
- :func:`~pygmtools.classic_solvers.ipfp` (classic solver)
- :func:`~pygmtools.classic_solvers.sm` (classic solver)
- :func:`~pygmtools.neural_solvers.ngm` (neural network solver)
import torch # pytorch backend
import pygmtools as pygm
import matplotlib.pyplot as plt # for plotting
from matplotlib.patches import ConnectionPatch # for plotting matching result
import networkx as nx # for plotting graphs
pygm.BACKEND = 'pytorch' # set default backend for pygmtools
_ = torch.manual_seed(1) # fix random seed
num_nodes = 10
X_gt = torch.zeros(num_nodes, num_nodes)
X_gt[torch.arange(0, num_nodes, dtype=torch.int64), torch.randperm(num_nodes)] = 1
A1 = torch.rand(num_nodes, num_nodes)
A1 = (A1 + A1.t() > 1.) * (A1 + A1.t()) / 2
torch.diagonal(A1)[:] = 0
A2 = torch.mm(torch.mm(X_gt.t(), A1), X_gt)
n1 = torch.tensor([num_nodes])
n2 = torch.tensor([num_nodes])
plt.figure(figsize=(8, 4))
G1 = nx.from_numpy_array(A1.numpy())
G2 = nx.from_numpy_array(A2.numpy())
pos1 = nx.spring_layout(G1)
pos2 = nx.spring_layout(G2)
plt.subplot(1, 2, 1)
plt.title('Graph 1')
nx.draw_networkx(G1, pos=pos1)
plt.subplot(1, 2, 2)
plt.title('Graph 2')
nx.draw_networkx(G2, pos=pos2)
.. image-sg:: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_001.png :alt: Graph 1, Graph 2 :srcset: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_001.png :class: sphx-glr-single-img
These two graphs look dissimilar because they are not aligned. We then align these two graphs by graph matching.
To match isomorphic graphs by graph matching, we follow the formulation of Quadratic Assignment Problem (QAP):
&\max_{\mathbf{X}} \ \texttt{vec}(\mathbf{X})^\top \mathbf{K} \texttt{vec}(\mathbf{X})\\ s.t. \quad &\mathbf{X} \in \{0, 1\}^{n_1\times n_2}, \ \mathbf{X}\mathbf{1} = \mathbf{1}, \ \mathbf{X}^\top\mathbf{1} \leq \mathbf{1}
where the first step is to build the affinity matrix (\mathbf{K})
conn1, edge1 = pygm.utils.dense_to_sparse(A1)
conn2, edge2 = pygm.utils.dense_to_sparse(A2)
import functools
gaussian_aff = functools.partial(pygm.utils.gaussian_aff_fn, sigma=.1) # set affinity function
K = pygm.utils.build_aff_mat(None, edge1, conn1, None, edge2, conn2, n1, None, n2, None, edge_aff_fn=gaussian_aff)
Visualization of the affinity matrix. For graph matching problem with N nodes, the affinity matrix has N^2\times N^2 elements because there are N^2 edges in each graph.
Note
The diagonal elements of the affinity matrix are empty because there is no node features in this example.
plt.figure(figsize=(4, 4))
plt.title(f'Affinity Matrix (size: {K.shape[0]}$\\times${K.shape[1]})')
plt.imshow(K.numpy(), cmap='Blues')
.. image-sg:: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_002.png :alt: Affinity Matrix (size: 100$\times$100) :srcset: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_002.png :class: sphx-glr-single-img
.. rst-class:: sphx-glr-script-out .. code-block:: none <matplotlib.image.AxesImage object at 0x7fd867606220>
See :func:`~pygmtools.classic_solvers.rrwm` for the API reference.
X = pygm.rrwm(K, n1, n2)
The output of RRWM is a soft matching matrix. Visualization:
plt.figure(figsize=(8, 4))
plt.subplot(1, 2, 1)
plt.title('RRWM Soft Matching Matrix')
plt.imshow(X.numpy(), cmap='Blues')
plt.subplot(1, 2, 2)
plt.title('Ground Truth Matching Matrix')
plt.imshow(X_gt.numpy(), cmap='Blues')
.. image-sg:: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_003.png :alt: RRWM Soft Matching Matrix, Ground Truth Matching Matrix :srcset: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_003.png :class: sphx-glr-single-img
.. rst-class:: sphx-glr-script-out .. code-block:: none <matplotlib.image.AxesImage object at 0x7fd867b2c8e0>
Hungarian algorithm is then adopted to reach a discrete matching matrix
X = pygm.hungarian(X)
Visualization of the discrete matching matrix:
plt.figure(figsize=(8, 4))
plt.subplot(1, 2, 1)
plt.title(f'RRWM Matching Matrix (acc={(X * X_gt).sum()/ X_gt.sum():.2f})')
plt.imshow(X.numpy(), cmap='Blues')
plt.subplot(1, 2, 2)
plt.title('Ground Truth Matching Matrix')
plt.imshow(X_gt.numpy(), cmap='Blues')
.. image-sg:: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_004.png :alt: RRWM Matching Matrix (acc=1.00), Ground Truth Matching Matrix :srcset: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_004.png :class: sphx-glr-single-img
.. rst-class:: sphx-glr-script-out .. code-block:: none <matplotlib.image.AxesImage object at 0x7fd8679e0b20>
Draw the matching (green lines for correct matching, red lines for wrong matching):
plt.figure(figsize=(8, 4))
ax1 = plt.subplot(1, 2, 1)
plt.title('Graph 1')
nx.draw_networkx(G1, pos=pos1)
ax2 = plt.subplot(1, 2, 2)
plt.title('Graph 2')
nx.draw_networkx(G2, pos=pos2)
for i in range(num_nodes):
j = torch.argmax(X[i]).item()
con = ConnectionPatch(xyA=pos1[i], xyB=pos2[j], coordsA="data", coordsB="data",
axesA=ax1, axesB=ax2, color="green" if X_gt[i, j] else "red")
plt.gca().add_artist(con)
.. image-sg:: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_005.png :alt: Graph 1, Graph 2 :srcset: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_005.png :class: sphx-glr-single-img
Align the nodes:
align_A2 = torch.mm(torch.mm(X, A2), X.t())
plt.figure(figsize=(8, 4))
ax1 = plt.subplot(1, 2, 1)
plt.title('Graph 1')
nx.draw_networkx(G1, pos=pos1)
ax2 = plt.subplot(1, 2, 2)
plt.title('Aligned Graph 2')
align_pos2 = {}
for i in range(num_nodes):
j = torch.argmax(X[i]).item()
align_pos2[j] = pos1[i]
con = ConnectionPatch(xyA=pos1[i], xyB=align_pos2[j], coordsA="data", coordsB="data",
axesA=ax1, axesB=ax2, color="green" if X_gt[i, j] else "red")
plt.gca().add_artist(con)
nx.draw_networkx(G2, pos=align_pos2)
.. image-sg:: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_006.png :alt: Graph 1, Aligned Graph 2 :srcset: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_006.png :class: sphx-glr-single-img
See :func:`~pygmtools.classic_solvers.ipfp` for the API reference.
X = pygm.ipfp(K, n1, n2)
Visualization of IPFP matching result:
plt.figure(figsize=(8, 4))
plt.subplot(1, 2, 1)
plt.title(f'IPFP Matching Matrix (acc={(X * X_gt).sum()/ X_gt.sum():.2f})')
plt.imshow(X.numpy(), cmap='Blues')
plt.subplot(1, 2, 2)
plt.title('Ground Truth Matching Matrix')
plt.imshow(X_gt.numpy(), cmap='Blues')
.. image-sg:: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_007.png :alt: IPFP Matching Matrix (acc=1.00), Ground Truth Matching Matrix :srcset: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_007.png :class: sphx-glr-single-img
.. rst-class:: sphx-glr-script-out .. code-block:: none <matplotlib.image.AxesImage object at 0x7fd868243940>
See :func:`~pygmtools.classic_solvers.sm` for the API reference.
X = pygm.sm(K, n1, n2)
X = pygm.hungarian(X)
Visualization of SM matching result:
plt.figure(figsize=(8, 4))
plt.subplot(1, 2, 1)
plt.title(f'SM Matching Matrix (acc={(X * X_gt).sum()/ X_gt.sum():.2f})')
plt.imshow(X.numpy(), cmap='Blues')
plt.subplot(1, 2, 2)
plt.title('Ground Truth Matching Matrix')
plt.imshow(X_gt.numpy(), cmap='Blues')
.. image-sg:: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_008.png :alt: SM Matching Matrix (acc=1.00), Ground Truth Matching Matrix :srcset: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_008.png :class: sphx-glr-single-img
.. rst-class:: sphx-glr-script-out .. code-block:: none <matplotlib.image.AxesImage object at 0x7fd86838c850>
See :func:`~pygmtools.neural_solvers.ngm` for the API reference.
with torch.set_grad_enabled(False):
X = pygm.ngm(K, n1, n2, pretrain='voc')
X = pygm.hungarian(X)
.. rst-class:: sphx-glr-script-out .. code-block:: none Downloading to /Users/guoziao/Library/Caches/pygmtools/ngm_voc_pytorch.pt... Downloading to /Users/guoziao/Library/Caches/pygmtools/ngm_voc_pytorch.pt... Warning: Network error. Retrying... HTTPSConnectionPool(host='doc-0o-cc-docs.googleusercontent.com', port=443): Max retries exceeded with url: /docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/bvf212d39oou09ftriha793afe8790s1/1678197975000/06552463500435400376/*/1POvy6J-9UDNy93qJCKu-czh2FCYkykMK?e=download&uuid=d8fb3ca2-24d4-4a1a-aacb-fd9773fedefe (Caused by ProxyError('Cannot connect to proxy.', OSError(0, 'Error'))) Downloading to /Users/guoziao/Library/Caches/pygmtools/ngm_voc_pytorch.pt... Downloading to /Users/guoziao/Library/Caches/pygmtools/ngm_voc_pytorch.pt... Warning: Network error. Retrying... HTTPSConnectionPool(host='drive.google.com', port=443): Max retries exceeded with url: /u/0/uc?export=download&confirm=Z-AR&id=1POvy6J-9UDNy93qJCKu-czh2FCYkykMK (Caused by ProxyError('Cannot connect to proxy.', OSError(0, 'Error'))) Downloading to /Users/guoziao/Library/Caches/pygmtools/ngm_voc_pytorch.pt... Downloading to /Users/guoziao/Library/Caches/pygmtools/ngm_voc_pytorch.pt... Warning: Network error. Retrying... HTTPSConnectionPool(host='drive.google.com', port=443): Max retries exceeded with url: /u/0/uc?export=download&confirm=Z-AR&id=1POvy6J-9UDNy93qJCKu-czh2FCYkykMK (Caused by ProxyError('Cannot connect to proxy.', OSError(0, 'Error'))) Downloading to /Users/guoziao/Library/Caches/pygmtools/ngm_voc_pytorch.pt... Downloading to /Users/guoziao/Library/Caches/pygmtools/ngm_voc_pytorch.pt... 0%| | 0/23119 [00:00<?, ?it/s] 100%|##########| 22.6k/22.6k [00:00<00:00, 60.9kB/s] 100%|##########| 22.6k/22.6k [00:00<00:00, 60.7kB/s]
Visualization of NGM matching result:
plt.figure(figsize=(8, 4))
plt.subplot(1, 2, 1)
plt.title(f'NGM Matching Matrix (acc={(X * X_gt).sum()/ X_gt.sum():.2f})')
plt.imshow(X.numpy(), cmap='Blues')
plt.subplot(1, 2, 2)
plt.title('Ground Truth Matching Matrix')
plt.imshow(X_gt.numpy(), cmap='Blues')
.. image-sg:: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_009.png :alt: NGM Matching Matrix (acc=1.00), Ground Truth Matching Matrix :srcset: /auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_pytorch_009.png :class: sphx-glr-single-img
.. rst-class:: sphx-glr-script-out .. code-block:: none <matplotlib.image.AxesImage object at 0x7fd8682f2820>
.. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 32.533 seconds)
.. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_isomorphic_graphs_pytorch.py <plot_isomorphic_graphs_pytorch.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_isomorphic_graphs_pytorch.ipynb <plot_isomorphic_graphs_pytorch.ipynb>`
.. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_