-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
289 lines (249 loc) · 10.2 KB
/
train.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import argparse
import os.path as osp
import time
import pdb
import hashlib
import sys
import torch
import torch.nn.functional as F
from torch_geometric.datasets import TUDataset
from torch_geometric.loader import DataLoader
from torch_geometric.logging import init_wandb, log
import wandb
from torch_geometric.data import Data
import torch_geometric.transforms as T
from model import GIN, DiffGIN, DenseGAT, DeepDiff, DeepSet, DeepCount
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, default='MUTAG')
parser.add_argument('--transform', type=str, default=None, help='random, hash, diff, diff_one, counts')
parser.add_argument('--online_trans', action='store_true', help='perform hash/random transformations online')
parser.add_argument('--model', type=str, default='DenseGAT', help='DenseGAT, GIN, DiffGIN, DeepSet, DeepCount')
parser.add_argument('--batch_size', type=int, default=128)
parser.add_argument('--hidden_channels', type=int, default=32)
parser.add_argument('--num_layers', type=int, default=5)
parser.add_argument('--lr', type=float, default=0.01)
parser.add_argument('--epochs', type=int, default=100)
args = parser.parse_args()
if torch.cuda.is_available():
device = torch.device('cuda')
elif hasattr(torch.backends, 'mps') and torch.backends.mps.is_available():
# MPS is currently slower than CPU due to missing int64 min/max ops
device = torch.device('cpu')
else:
device = torch.device('cpu')
wandb.init(
project="lexiinv_9_16",
name=f'{args.dataset}-{args.transform}-{args.model}' + args.online_trans * '-online_trans',
config={
"batch_size": args.batch_size,
"lr": args.lr,
"epochs": args.epochs,
"hidden_channels": args.hidden_channels,
"num_layers": args.num_layers,
"device": device,
"transform": args.transform,
"online_trans": args.online_trans,
}
)
print("Command line: ", 'python ' + " ".join(sys.argv))
class ReplaceNodeAttributesWithRandomVectors(object):
def __init__(self, vector_dim):
self.vector_dim = vector_dim
def __call__(self, data):
num_nodes = data.num_nodes
# Generate random vectors for each node
random_vectors = torch.rand((num_nodes, self.vector_dim))
# Replace the node attributes with the random vectors
data.x = random_vectors
return data
class ReplaceNodeAttributesWithHash(object):
def __init__(self, hash_func=hashlib.sha256, output_dim=16):
self.hash_func = hash_func
self.output_dim = output_dim
def vector_to_hash(self, vector):
# Convert the vector to a string and encode it
vector_str = vector.numpy().tobytes()
# Create the hash
hash_value = self.hash_func(vector_str).hexdigest()
# Convert the hash to a fixed-size tensor (e.g., first 64 characters)
hash_tensor = torch.tensor([int(hash_value[i:i+2], 16) for i in range(0, self.output_dim, 2)], dtype=torch.float32)
return hash_tensor
def __call__(self, data):
# Append a fixed random vector to each node
rand_vec = torch.randn(8).to(data.x.device)
# Apply hashing to each node attribute vector
hashed_vectors = torch.stack([self.vector_to_hash(torch.cat([vec, rand_vec])) for vec in data.x])
# Replace the node attributes with hashed vectors
data.x = hashed_vectors
return data
class ReplaceNodeAttributesWithOne(object):
def __call__(self, data):
num_nodes = data.num_nodes
data.x = torch.ones([num_nodes, 1])
return data
class CreateSparseDifferenceMatrix(object):
def __call__(self, data):
diff = (data.x.unsqueeze(1) == data.x).all(-1)
edge_index_diff = torch.nonzero(diff, as_tuple=False).t()
data.edge_index_diff = edge_index_diff
return data
class CreateAdjacencyDifferenceMatrix(object):
def __init__(self, max_node_num):
self.max_node_num = max_node_num
def __call__(self, data):
n = data.x.size(0)
# Initialize the matrices
diff = torch.zeros((1, self.max_node_num, self.max_node_num), dtype=torch.float32)
adj = torch.zeros((1, self.max_node_num, self.max_node_num), dtype=torch.float32)
# Fill the matrices
for i in range(n):
for j in range(n):
if torch.equal(data.x[i], data.x[j]):
diff[0, i, j] = 1.0
for k in range(data.edge_index.size(1)):
i, j = data.edge_index[0, k], data.edge_index[1, k]
adj[0, i, j] = 1.0
data.diff = diff
data.adj = adj
data.pad_feat = torch.cat((data.x, torch.zeros((self.max_node_num - n, data.x.size(1)))), 0).unsqueeze(0)
return data
class CreateUniqueCounts(object):
def __call__(self, data):
_, data.x = data.x.unique(return_counts=True, dim=0)
data.x = data.x.unsqueeze(1).to(torch.float32)
data.num_nodes = data.x.shape[0]
data.edge_index = None
return data
# maximum node number
if True:
path = osp.join(osp.dirname(osp.realpath(__file__)), '.', 'data', 'TU_tmp')
dataset = TUDataset(path, name=args.dataset)
max_node_num = max([data.x.size(0) for data in dataset])
print('Maximum node number is ' + str(max_node_num))
# Define the pre_transform
# todo: still transform to random hash, then handle that zeros are still zeros
if args.transform == 'random':
transform = None
pre_transform = ReplaceNodeAttributesWithRandomVectors(vector_dim=16)
#pre_transform = CreateAdjacencyDifferenceMatrix(max_node_num)
pre_transform = T.Compose([pre_transform, CreateAdjacencyDifferenceMatrix(max_node_num)])
if args.online_trans:
transform, pre_transform = pre_transform, None
elif args.transform == 'hash':
transform = None
pre_transform = ReplaceNodeAttributesWithHash()
if args.online_trans:
transform, pre_transform = pre_transform, None
#pre_transform = CreateAdjacencyDifferenceMatrix(max_node_num)
#pre_transform = T.Compose([pre_transform, CreateAdjacencyDifferenceMatrix(max_node_num)])
elif args.transform == 'one':
transform = None
pre_transform = ReplaceNodeAttributesWithOne()
elif args.transform == 'diff':
transform = None
pre_transform = CreateAdjacencyDifferenceMatrix(max_node_num)
elif args.transform == 'diff_one':
transform = None
pre_transform = T.Compose([CreateSparseDifferenceMatrix(), ReplaceNodeAttributesWithOne()])
elif args.transform == 'counts':
transform = None
pre_transform = CreateUniqueCounts()
path = osp.join(osp.dirname(osp.realpath(__file__)), '.', 'data', 'TU_' + args.transform)
if args.online_trans:
path = osp.join(osp.dirname(osp.realpath(__file__)), '.', 'data', 'TU')
dataset = TUDataset(path, name=args.dataset, transform=transform,
pre_transform=pre_transform, use_node_attr=True).shuffle()
train_loader = DataLoader(dataset[:0.9], args.batch_size, shuffle=True)
test_loader = DataLoader(dataset[0.9:], args.batch_size)
if args.model == 'GIN':
model = GIN(
in_channels=dataset.num_features,
hidden_channels=args.hidden_channels,
out_channels=dataset.num_classes,
num_layers=args.num_layers,
).to(device)
if args.model == 'DiffGIN':
model = DiffGIN(
in_channels=dataset.num_features,
hidden_channels=args.hidden_channels,
out_channels=dataset.num_classes,
num_layers=args.num_layers,
).to(device)
elif args.model == 'DenseGAT':
model = DenseGAT(
in_channels=dataset[0].pad_feat.shape[-1],
hidden_channels=args.hidden_channels,
out_channels=dataset.num_classes,
num_layers=args.num_layers,
).to(device)
elif args.model == 'DeepDiff':
model = DeepDiff(
hidden_channels=args.hidden_channels,
out_channels=dataset.num_classes,
num_layers=args.num_layers,
).to(device)
elif args.model == 'DeepSet':
model = DeepSet(
in_channels=dataset.num_features,
hidden_channels=args.hidden_channels,
out_channels=dataset.num_classes,
num_layers=args.num_layers,
).to(device)
elif args.model == 'DeepCount':
model = DeepCount(
hidden_channels=args.hidden_channels,
out_channels=dataset.num_classes,
num_layers=args.num_layers,
).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
def train():
model.train()
total_loss = 0
for data in train_loader:
data = data.to(device)
#pdb.set_trace()
optimizer.zero_grad()
if args.model == 'GIN':
out = model(data.x, data.edge_index, data.batch)
elif args.model == 'DiffGIN':
out = model(data.x, data.edge_index, data.edge_index_diff, data.batch)
elif args.model == 'DenseGAT':
out = model(data.pad_feat, data.adj, data.diff)
elif args.model == 'DeepDiff':
out = model(data.adj, data.diff)
elif args.model in ['DeepSet', 'DeepCount']:
out = model(data.x, data.batch)
loss = F.cross_entropy(out, data.y)
loss.backward()
optimizer.step()
total_loss += float(loss) * data.num_graphs
return total_loss / len(train_loader.dataset)
@torch.no_grad()
def test(loader):
model.eval()
total_correct = 0
for data in loader:
data = data.to(device)
if args.model == 'GIN':
out = model(data.x, data.edge_index, data.batch)
elif args.model == 'DiffGIN':
out = model(data.x, data.edge_index, data.edge_index_diff, data.batch)
elif args.model == 'DenseGAT':
out = model(data.pad_feat, data.adj, data.diff)
elif args.model == 'DeepDiff':
out = model(data.adj, data.diff)
elif args.model in ['DeepSet', 'DeepCount']:
out = model(data.x, data.batch)
pred = out.argmax(dim=-1)
total_correct += int((pred == data.y).sum())
return total_correct / len(loader.dataset)
times = []
for epoch in range(1, args.epochs + 1):
start = time.time()
loss = train()
train_acc = test(train_loader)
test_acc = test(test_loader)
wandb.log({"Loss": loss, "Train": train_acc, "Test": test_acc})
log(Epoch = epoch, Loss = loss, Train = train_acc, Test = test_acc)
times.append(time.time() - start)
print(f'Median time per epoch: {torch.tensor(times).median():.4f}s')