-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.py
229 lines (189 loc) · 8.95 KB
/
main.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import argparse
import time
import torch
import torch.nn as nn
from sklearn import metrics
from torch.utils.data import DataLoader
from tqdm import tqdm
from spikenet import dataset, neuron
from spikenet.layers import SAGEAggregator
from spikenet.utils import (RandomWalkSampler, Sampler, add_selfloops,
set_seed, tab_printer)
class SpikeNet(nn.Module):
def __init__(self, in_features, out_features, hids=[32], alpha=1.0, p=0.5,
dropout=0.7, bias=True, aggr='mean', sampler='sage',
surrogate='triangle', sizes=[5, 2], concat=False, act='LIF'):
super().__init__()
tau = 1.0
if sampler == 'rw':
self.sampler = [RandomWalkSampler(
add_selfloops(adj_matrix)) for adj_matrix in data.adj]
self.sampler_t = [RandomWalkSampler(add_selfloops(
adj_matrix)) for adj_matrix in data.adj_evolve]
elif sampler == 'sage':
self.sampler = [Sampler(add_selfloops(adj_matrix))
for adj_matrix in data.adj]
self.sampler_t = [Sampler(add_selfloops(adj_matrix))
for adj_matrix in data.adj_evolve]
else:
raise ValueError(sampler)
aggregators, snn = nn.ModuleList(), nn.ModuleList()
for hid in hids:
aggregators.append(SAGEAggregator(in_features, hid,
concat=concat, bias=bias,
aggr=aggr))
if act == "IF":
snn.append(neuron.IF(alpha=alpha, surrogate=surrogate))
elif act == 'LIF':
snn.append(neuron.LIF(tau, alpha=alpha, surrogate=surrogate))
elif act == 'PLIF':
snn.append(neuron.PLIF(tau, alpha=alpha, surrogate=surrogate))
else:
raise ValueError(act)
in_features = hid * 2 if concat else hid
self.aggregators = aggregators
self.dropout = nn.Dropout(dropout)
self.snn = snn
self.sizes = sizes
self.p = p
self.pooling = nn.Linear(len(data) * in_features, out_features)
def encode(self, nodes):
spikes = []
sizes = self.sizes
for time_step in range(len(data)):
snapshot = data[time_step]
sampler = self.sampler[time_step]
sampler_t = self.sampler_t[time_step]
x = snapshot.x
h = [x[nodes].to(device)]
num_nodes = [nodes.size(0)]
nbr = nodes
for size in sizes:
size_1 = max(int(size * self.p), 1)
size_2 = size - size_1
if size_2 > 0:
nbr_1 = sampler(nbr, size_1).view(nbr.size(0), size_1)
nbr_2 = sampler_t(nbr, size_2).view(nbr.size(0), size_2)
nbr = torch.cat([nbr_1, nbr_2], dim=1).flatten()
else:
nbr = sampler(nbr, size_1).view(-1)
num_nodes.append(nbr.size(0))
h.append(x[nbr].to(device))
for i, aggregator in enumerate(self.aggregators):
self_x = h[:-1]
neigh_x = []
for j, n_x in enumerate(h[1:]):
neigh_x.append(n_x.view(-1, sizes[j], h[0].size(-1)))
out = self.snn[i](aggregator(self_x, neigh_x))
if i != len(sizes) - 1:
out = self.dropout(out)
h = torch.split(out, num_nodes[:-(i + 1)])
spikes.append(out)
spikes = torch.cat(spikes, dim=1)
neuron.reset_net(self)
return spikes
def forward(self, nodes):
spikes = self.encode(nodes)
return self.pooling(spikes)
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", nargs="?", default="DBLP",
help="Datasets (DBLP, Tmall, Patent). (default: DBLP)")
parser.add_argument('--sizes', type=int, nargs='+', default=[5, 2], help='Neighborhood sampling size for each layer. (default: [5, 2])')
parser.add_argument('--hids', type=int, nargs='+',
default=[128, 10], help='Hidden units for each layer. (default: [128, 10])')
parser.add_argument("--aggr", nargs="?", default="mean",
help="Aggregate function ('mean', 'sum'). (default: 'mean')")
parser.add_argument("--sampler", nargs="?", default="sage",
help="Neighborhood Sampler, including uniform sampler from GraphSAGE ('sage') and random walk sampler ('rw'). (default: 'sage')")
parser.add_argument("--surrogate", nargs="?", default="sigmoid",
help="Surrogate function ('sigmoid', 'triangle', 'arctan', 'mg', 'super'). (default: 'sigmoid')")
parser.add_argument("--neuron", nargs="?", default="LIF",
help="Spiking neuron used for training. (IF, LIF, PLIF). (default: LIF")
parser.add_argument('--batch_size', type=int, default=1024,
help='Batch size for training. (default: 1024)')
parser.add_argument('--lr', type=float, default=5e-3,
help='Learning rate for training. (default: 5e-3)')
parser.add_argument('--train_size', type=float, default=0.4,
help='Ratio of nodes for training. (default: 0.4)')
parser.add_argument('--alpha', type=float, default=1.0,
help='Smooth factor for surrogate learning. (default: 1.0)')
parser.add_argument('--p', type=float, default=0.5,
help='Percentage of sampled neighborhoods for g_t. (default: 0.5)')
parser.add_argument('--dropout', type=float, default=0.7,
help='Dropout probability. (default: 0.7)')
parser.add_argument('--epochs', type=int, default=100,
help='Number of training epochs. (default: 100)')
parser.add_argument('--concat', action='store_true',
help='Whether to concat node representation and neighborhood representations. (default: False)')
parser.add_argument('--seed', type=int, default=2022,
help='Random seed for model. (default: 2022)')
try:
args = parser.parse_args()
args.test_size = 1 - args.train_size
args.train_size = args.train_size - 0.05
args.val_size = 0.05
args.split_seed = 42
tab_printer(args)
except:
parser.print_help()
exit(0)
assert len(args.hids) == len(args.sizes), "must be equal!"
if args.dataset.lower() == "dblp":
data = dataset.DBLP()
elif args.dataset.lower() == "tmall":
data = dataset.Tmall()
elif args.dataset.lower() == "patent":
data = dataset.Patent()
else:
raise ValueError(
f"{args.dataset} is invalid. Only datasets (dblp, tmall, patent) are available.")
# train:val:test
data.split_nodes(train_size=args.train_size, val_size=args.val_size,
test_size=args.test_size, random_state=args.split_seed)
set_seed(args.seed)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
y = data.y.to(device)
train_loader = DataLoader(data.train_nodes.tolist(), pin_memory=False, batch_size=args.batch_size, shuffle=True)
val_loader = DataLoader(data.test_nodes.tolist() if data.val_nodes is None else data.val_nodes.tolist(),
pin_memory=False, batch_size=200000, shuffle=False)
test_loader = DataLoader(data.test_nodes.tolist(), pin_memory=False, batch_size=200000, shuffle=False)
model = SpikeNet(data.num_features, data.num_classes, alpha=args.alpha,
dropout=args.dropout, sampler=args.sampler, p=args.p,
aggr=args.aggr, concat=args.concat, sizes=args.sizes, surrogate=args.surrogate,
hids=args.hids, act=args.neuron, bias=True).to(device)
optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr)
loss_fn = nn.CrossEntropyLoss()
def train():
model.train()
for nodes in tqdm(train_loader, desc='Training'):
optimizer.zero_grad()
loss_fn(model(nodes), y[nodes]).backward()
optimizer.step()
@torch.no_grad()
def test(loader):
model.eval()
logits = []
labels = []
for nodes in loader:
logits.append(model(nodes))
labels.append(y[nodes])
logits = torch.cat(logits, dim=0).cpu()
labels = torch.cat(labels, dim=0).cpu()
logits = logits.argmax(1)
metric_macro = metrics.f1_score(labels, logits, average='macro')
metric_micro = metrics.f1_score(labels, logits, average='micro')
return metric_macro, metric_micro
best_val_metric = test_metric = 0
start = time.time()
for epoch in range(1, args.epochs + 1):
train()
val_metric, test_metric = test(val_loader), test(test_loader)
if val_metric[1] > best_val_metric:
best_val_metric = val_metric[1]
best_test_metric = test_metric
end = time.time()
print(
f'Epoch: {epoch:03d}, Val: {val_metric[1]:.4f}, Test: {test_metric[1]:.4f}, Best: Macro-{best_test_metric[0]:.4f}, Micro-{best_test_metric[1]:.4f}, Time elapsed {end-start:.2f}s')
# save bianry node embeddings (spikes)
# emb = model.encode(torch.arange(data.num_nodes)).cpu()
# torch.save(emb, 'emb.pth')