-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_substructure.py
140 lines (107 loc) · 4.57 KB
/
main_substructure.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
from src.utils import *
from src.model import *
import os
# --------------------------------- ARGPARSE --------------------------------- #
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--model", type=str, required=True, help="type of GNN layer")
parser.add_argument("--outdir", type=str, default="result", help="dataset directory")
parser.add_argument("--data", type=str, required=True, help="dataset name")
parser.add_argument("--task", type=str, required=True, help="dataset task")
parser.add_argument("--device", type=int, default=0, help="CUDA device")
parser.add_argument("--seed", type=int, default=19260817, help="random seed")
parser.add_argument("--max_dis", type=int, default=5, help="distance encoding")
parser.add_argument("--num_layer", type=int, default=6, help="number of layers")
parser.add_argument("--dim_embed", type=int, default=96, help="embedding dimension")
parser.add_argument("--bs", type=int, default=128, help="batch size")
parser.add_argument("--lr", type=float, default=1e-3, help="learning rate")
parser.add_argument("--decay_rate", type=float, default=0.5, help="lr decay rate")
parser.add_argument("--epochs", type=int, default=400, help="training epochs")
args = parser.parse_args()
print(f"""Run:
model: {args.model}
data: {args.data}
task: {args.task}
seed: {args.seed}
""")
# ----------------------------------- MODEL ---------------------------------- #
if args.model == "SWL_SV": layer, pool = ["uL"], "uG"
if args.model == "SWL_VS": layer, pool = ["uL"], "vG"
if args.model == "PSWL_SV": layer, pool = ["uL", "vv"], "uG"
if args.model == "PSWL_VS": layer, pool = ["uL", "vv"], "uG"
if args.model == "GSWL" : layer, pool = ["uL", "vG"], "uG"
if args.model == "SSWL" : layer, pool = ["uL", "vL"], "uG"
if args.model == "SSWL_P" : layer, pool = ["uL", "vL", "vv"], "uG"
torch.manual_seed(args.seed)
device = torch.device(f"cuda:{args.device}")
from src import dataset
dataloader = {
name: data.DataLoader(
dataset.GraphCount(
split=name,
root=args.data,
task=args.task,
transform=subgraph(layer + [pool]),
),
batch_size=args.bs,
num_workers=2,
shuffle=True
)
for name in ["train", "val", "test"]
}
model = GNN(idim=args.dim_embed, odim=1,
max_dis=args.max_dis, encode=False,
As=[(Agg(layer), args.dim_embed)] * args.num_layer \
+[(Agg([pool], gin=False), args.dim_embed)])
# ----------------------------------- ITER ----------------------------------- #
def train(model, loader, critn, optim):
model.train()
losses = []
for batch in loader:
batch = batch.to(device)
pred = model(batch) \
.view(batch.y.shape)
optim.zero_grad()
loss = critn(pred, batch.y)
loss.backward()
optim.step()
losses.append(loss.item())
return np.array(losses)
def eval(model, loader, critn):
model.eval()
pred, true = [], []
for batch in loader:
batch = batch.to(device)
with torch.no_grad():
true.append(batch.y)
pred.append(model(batch) \
.view(batch.y.shape))
return critn(torch.cat(pred), torch.cat(true))
# ---------------------------------------------------------------------------- #
# MAIN #
# ---------------------------------------------------------------------------- #
model = model.to(device)
critn = torch.nn.L1Loss()
optim = torch.optim.Adam(model.parameters(), lr=args.lr)
# ------------------------------------ RUN ----------------------------------- #
import numpy as np
record = np.zeros((args.epochs, 4))
from tqdm import tqdm
pbar = tqdm(range(args.epochs))
output_dir = args.outdir
if not os.path.exists(output_dir):
os.makedirs(output_dir)
filename = f"{output_dir}/{args.model}-{args.data}-{args.task}-{args.dim_embed}-{args.bs}-{args.lr}-{args.epochs}-{args.max_dis}-{args.seed}.txt"
for epoch in pbar:
for group in optim.param_groups:
group['lr'] = (1 + np.cos(np.pi * epoch / args.epochs)) / 2 * args.lr
losses = train(model, dataloader["train"], critn, optim)
val_metric = eval(model, dataloader["val"], critn)
test_metric = eval(model, dataloader["test"], critn)
record[epoch] = np.array([optim.param_groups[0]['lr'], losses.mean(), val_metric.item(), test_metric.item()])
pbar.set_postfix({
"loss": losses.mean(),
"val": val_metric.item(),
"test": test_metric.item()
})
np.savetxt(filename, record, delimiter='\t')