-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypergraph_wl_subtree_kernel.py
148 lines (140 loc) · 5.92 KB
/
hypergraph_wl_subtree_kernel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from itertools import combinations
from collections import defaultdict
import torch
import numpy as np
from dhg import Hypergraph, Graph, DiGraph
# Hypergraph WL Subtree Kernel from "Feng et al. 2024 IEEE TPAMI Hypergraph Isomorphism Computation"
class HypergraphSubtreeKernel:
def __init__(self, n_iter=4, degree_as_label=True, normalize=True):
self.n_iter = n_iter
self.normalize = normalize
self.degree_as_label = degree_as_label
self._subtree_map = {}
def remap_v(self, hg_list, cnt, drop=False):
for hg_idx, hg in enumerate(hg_list):
for v_idx in range(hg["num_v"]):
cur_lbl = hg["v_lbl"][v_idx]
cur_lbl = "v" + str(cur_lbl)
if cur_lbl not in self._subtree_map:
if drop:
hg["v_lbl"][v_idx] = -1
continue
else:
self._subtree_map[cur_lbl] = len(self._subtree_map)
hg["v_lbl"][v_idx] = self._subtree_map[cur_lbl]
cnt[hg_idx][self._subtree_map[cur_lbl]] += 1
return hg_list, cnt
def remap_e(self, hg_list, cnt, drop=False):
for hg_idx, hg in enumerate(hg_list):
for e_idx in range(hg["dhg"].num_e):
cur_lbl = hg["e_lbl"][e_idx]
cur_lbl = "e" + str(cur_lbl)
if cur_lbl not in self._subtree_map:
if drop:
hg["e_lbl"][e_idx] = -1
continue
else:
self._subtree_map[cur_lbl] = len(self._subtree_map)
hg["e_lbl"][e_idx] = self._subtree_map[cur_lbl]
cnt[hg_idx][self._subtree_map[cur_lbl]] += 1
return hg_list, cnt
def cnt2mat(self, raw_cnt):
# filter count
cnt = []
valid_id_set = set(
[v for k, v in self._subtree_map.items() if k.startswith("v")]
)
id_map = {k: v for v, k in enumerate(sorted(valid_id_set))}
for c in raw_cnt:
cnt.append({id_map[k]: v for k, v in c.items() if k in valid_id_set})
# count
row_idx, col_idx, data = [], [], []
for idx, g in enumerate(cnt):
for lbl, c in g.items():
row_idx.append(idx)
col_idx.append(lbl)
data.append(c)
return (
torch.sparse_coo_tensor(
torch.tensor([row_idx, col_idx]),
torch.tensor(data),
size=(len(cnt), len(self._subtree_map)),
)
.coalesce()
.float()
)
def fit_transform(self, hg_list):
# if self.degree_as_label:
# for hg in hg_list:
# hg["v_lbl"] = [int(v) for v in hg["dhg"].deg_v]
# hg["e_lbl"] = [int(e) for e in hg["dhg"].deg_e]
self._cnt = [defaultdict(int) for _ in range(len(hg_list))]
self.remap_v(hg_list, self._cnt)
self.remap_e(hg_list, self._cnt)
for _ in range(self.n_iter):
for hg in hg_list:
tmp = []
for e_idx in range(hg["dhg"].num_e):
cur_lbl = hg["e_lbl"][e_idx]
nbr_lbl = sorted(
hg["v_lbl"][v_idx] for v_idx in hg["dhg"].nbr_v(e_idx)
)
tmp.append(f"{cur_lbl},{nbr_lbl}")
hg["e_lbl"] = tmp
self.remap_e(hg_list, self._cnt)
for hg in hg_list:
tmp = []
for v_idx in range(hg["dhg"].num_v):
cur_lbl = hg["v_lbl"][v_idx]
nbr_lbl = sorted(
hg["e_lbl"][e_idx] for e_idx in hg["dhg"].nbr_e(v_idx)
)
tmp.append(f"{cur_lbl},{nbr_lbl}")
hg["v_lbl"] = tmp
self.remap_v(hg_list, self._cnt)
self.train_cnt = self.cnt2mat(self._cnt)
self.train_ft = self.train_cnt.mm(self.train_cnt.t()).to_dense()
if self.normalize:
self.train_ft_diag = torch.diag(self.train_ft)
self.train_ft = (
self.train_ft
/ torch.outer(self.train_ft_diag, self.train_ft_diag).sqrt()
)
self.train_ft[torch.isnan(self.train_ft)] = 0
return self.train_ft
def transform(self, hg_list):
# if self.degree_as_label:
# for hg in hg_list:
# hg["v_lbl"] = [int(v) for v in hg["dhg"].deg_v]
# hg["e_lbl"] = [int(e) for e in hg["dhg"].deg_e]
cnt = [defaultdict(int) for _ in range(len(hg_list))]
self.remap_v(hg_list, cnt, drop=True)
self.remap_e(hg_list, cnt, drop=True)
for _ in range(self.n_iter):
for hg in hg_list:
tmp = []
for e_idx in range(hg["dhg"].num_e):
cur_lbl = hg["e_lbl"][e_idx]
nbr_lbl = sorted(
hg["v_lbl"][v_idx] for v_idx in hg["dhg"].nbr_v(e_idx)
)
tmp.append(f"{cur_lbl},{nbr_lbl}")
hg["e_lbl"] = tmp
self.remap_e(hg_list, cnt, drop=True)
for hg in hg_list:
tmp = []
for v_idx in range(hg["dhg"].num_v):
cur_lbl = hg["v_lbl"][v_idx]
nbr_lbl = sorted(
hg["e_lbl"][e_idx] for e_idx in hg["dhg"].nbr_e(v_idx)
)
tmp.append(f"{cur_lbl},{nbr_lbl}")
hg["v_lbl"] = tmp
self.remap_v(hg_list, cnt, drop=True)
test_cnt = self.cnt2mat(cnt)
test_ft = test_cnt.mm(self.train_cnt.t()).to_dense()
if self.normalize:
test_ft_diag = torch.sparse.sum(test_cnt * test_cnt, dim=1).to_dense()
test_ft = test_ft / torch.outer(test_ft_diag, self.train_ft_diag).sqrt()
test_ft[torch.isnan(test_ft)] = 0
return test_ft