-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_graph.py
209 lines (169 loc) · 7 KB
/
main_graph.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
import logging
from tqdm import tqdm
import numpy as np
import dgl
from dgl.nn.pytorch.glob import SumPooling, AvgPooling, MaxPooling
from dgl.dataloading import GraphDataLoader
import torch
from sklearn.model_selection import StratifiedKFold, GridSearchCV
from sklearn.svm import SVC
from sklearn.metrics import f1_score
from augmae.utils import (
build_args,
create_optimizer,
set_random_seed,
TBLogger,
get_current_lr,
load_best_configs,
)
from augmae.datasets.data_util import load_graph_classification_dataset
from augmae.models import build_model
def graph_classification_evaluation(model, pooler, dataloader, num_classes, lr_f, weight_decay_f, max_epoch_f, device, mute=False):
model.eval()
x_list = []
y_list = []
with torch.no_grad():
for i, (batch_g, labels) in enumerate(dataloader):
batch_g = batch_g.to(device)
feat = batch_g.ndata["attr"]
out = model.embed(batch_g, feat)
out = pooler(batch_g, out)
y_list.append(labels.numpy())
x_list.append(out.cpu().numpy())
x = np.concatenate(x_list, axis=0)
y = np.concatenate(y_list, axis=0)
test_f1, test_std = evaluate_graph_embeddings_using_svm(x, y)
print(f"#Test_f1: {test_f1:.4f}±{test_std:.4f}")
return test_f1
def evaluate_graph_embeddings_using_svm(embeddings, labels):
result = []
kf = StratifiedKFold(n_splits=10, shuffle=True, random_state=0)
for train_index, test_index in kf.split(embeddings, labels):
x_train = embeddings[train_index]
x_test = embeddings[test_index]
y_train = labels[train_index]
y_test = labels[test_index]
params = {"C": [1e-3, 1e-2, 1e-1, 1, 10]}
svc = SVC(random_state=42)
clf = GridSearchCV(svc, params)
clf.fit(x_train, y_train)
preds = clf.predict(x_test)
f1 = f1_score(y_test, preds, average="micro")
result.append(f1)
test_f1 = np.mean(result)
test_std = np.std(result)
return test_f1, test_std
def pretrain(model, pooler, dataloaders, optimizer, max_epoch, device, scheduler, num_classes, lr_f, weight_decay_f, max_epoch_f,args,mask_module,optimizer_mask, linear_prob=True, logger=None):
train_loader, eval_loader = dataloaders
epoch_iter = tqdm(range(max_epoch))
for epoch in epoch_iter:
model.train()
loss_list = []
for batch in train_loader:
batch_g, _ = batch
batch_g = batch_g.to(device)
feat = batch_g.ndata["attr"]
model.train()
mask_module.train()
mask_prob = mask_module.forward(batch_g, feat,args)
loss, loss_mask, loss_dict = model(batch_g, feat,epoch,args,mask_prob,pooler)
optimizer_mask.zero_grad()
loss_mask.backward()
optimizer_mask.step()
mask_prob = mask_module.forward(batch_g, feat,args)
loss, loss_mask, loss_dict = model(batch_g, feat,epoch,args,mask_prob,pooler)
optimizer.zero_grad()
loss.backward()
optimizer.step()
loss_list.append(loss.item())
if logger is not None:
loss_dict["lr"] = get_current_lr(optimizer)
logger.note(loss_dict, step=epoch)
if scheduler is not None:
scheduler.step()
epoch_iter.set_description(f"Epoch {epoch} | train_loss: {np.mean(loss_list):.4f}")
return model
def collate_fn(batch):
graphs = [x[0] for x in batch]
labels = [x[1] for x in batch]
batch_g = dgl.batch(graphs)
labels = torch.cat(labels, dim=0)
return batch_g, labels
def main(args):
device = args.device if args.device >= 0 else "cpu"
seeds = args.seeds
dataset_name = args.dataset
max_epoch = args.max_epoch
max_epoch_f = args.max_epoch_f
num_hidden = args.num_hidden
num_layers = args.num_layers
encoder_type = args.encoder
decoder_type = args.decoder
replace_rate = args.replace_rate
optim_type = args.optimizer
loss_fn = args.loss_fn
lr = args.lr
weight_decay = args.weight_decay
lr_f = args.lr_f
weight_decay_f = args.weight_decay_f
linear_prob = args.linear_prob
load_model = args.load_model
save_model = args.save_model
logs = args.logging
use_scheduler = args.scheduler
pooling = args.pooling
deg4feat = args.deg4feat
batch_size = args.batch_size
lr_mask = args.lr_mask
graphs, (num_features, num_classes) = load_graph_classification_dataset(dataset_name, deg4feat=deg4feat)
args.num_features = num_features
train_loader = GraphDataLoader(graphs, collate_fn=collate_fn, batch_size=batch_size, pin_memory=True,shuffle=True)
eval_loader = GraphDataLoader(graphs, collate_fn=collate_fn, batch_size=batch_size, shuffle=False)
if pooling == "mean":
pooler = AvgPooling()
elif pooling == "max":
pooler = MaxPooling()
elif pooling == "sum":
pooler = SumPooling()
else:
raise NotImplementedError
acc_list = []
for i, seed in enumerate(seeds):
print(f"####### Run {i} for seed {seed}")
set_random_seed(seed)
if logs:
logger = TBLogger(name=f"{dataset_name}_loss_{loss_fn}_rpr_{replace_rate}_nh_{num_hidden}_nl_{num_layers}_lr_{lr}_mp_{max_epoch}_mpf_{max_epoch_f}_wd_{weight_decay}_wdf_{weight_decay_f}_{encoder_type}_{decoder_type}")
else:
logger = None
model, mask_module = build_model(args)
model.to(device)
mask_module.to(device)
optimizer = create_optimizer(optim_type, model, lr, weight_decay)
optimizer_mask = create_optimizer(optim_type, mask_module, lr_mask, weight_decay)
if use_scheduler:
logging.info("Use schedular")
scheduler = lambda epoch: ( 1 + np.cos((epoch) * np.pi / max_epoch) ) * 0.5
scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=scheduler)
else:
scheduler = None
if not load_model:
model = pretrain(model, pooler, (train_loader, eval_loader), optimizer, max_epoch, device, scheduler, num_classes, lr_f, weight_decay_f, max_epoch_f,args,mask_module,optimizer_mask,linear_prob, logger)
model = model.cpu()
if load_model:
logging.info("Loading Model ... ")
model.load_state_dict(torch.load("checkpoint.pt"))
if save_model:
logging.info("Saveing Model ...")
torch.save(model.state_dict(), "checkpoint.pt")
model = model.to(device)
model.eval()
test_f1 = graph_classification_evaluation(model, pooler, eval_loader, num_classes, lr_f, weight_decay_f, max_epoch_f, device, mute=False)
acc_list.append(test_f1)
final_acc, final_acc_std = np.mean(acc_list), np.std(acc_list)
print(f"# final_acc: {final_acc:.4f}±{final_acc_std:.4f}")
if __name__ == "__main__":
args = build_args()
if args.use_cfg:
args = load_best_configs(args, "configs.yml")
print(args)
main(args)