-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_transductive.py
145 lines (120 loc) · 4.92 KB
/
main_transductive.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
import logging
import numpy as np
from tqdm import tqdm
import torch
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_dataset
from augmae.evaluation import node_classification_evaluation
from augmae.models import build_model
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO)
def pretrain(model, graph, feat, optimizer, max_epoch, device, scheduler, num_classes, lr_f, weight_decay_f,
max_epoch_f, linear_prob, args, mask_module, optimizer_mask, logger=None):
logging.info("start training..")
graph = graph.to(device)
x = feat.to(device)
epoch_iter = tqdm(range(max_epoch))
for epoch in epoch_iter:
model.train()
mask_module.train()
mask_prob = mask_module.forward(graph, x,args)
loss, loss_mask, loss_dict = model(graph, x, epoch, args, mask_prob,0.)
optimizer_mask.zero_grad()
loss_mask.backward()
optimizer_mask.step()
mask_prob = mask_module.forward(graph, x,args)
loss, loss_mask, loss_dict = model(graph, x, epoch, args, mask_prob,0.)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if scheduler is not None:
scheduler.step()
epoch_iter.set_description(f"# Epoch {epoch}: train_loss: {loss.item():.4f}")
if logger is not None:
loss_dict["lr"] = get_current_lr(optimizer)
logger.note(loss_dict, step=epoch)
return model
def main(args):
device = args.device
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
lr_mask = args.lr_mask
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
graph, (num_features, num_classes) = load_dataset(dataset_name)
args.num_features = num_features
args.max_degree = graph.in_degrees().max() + 1
acc_list = []
estp_acc_list = []
train_mask = graph.ndata["train_mask"]
val_mask = graph.ndata["val_mask"]
for i, seed in enumerate(seeds):
print(f"####### Run {i} for seed {seed}")
set_random_seed(seed)
if dataset_name == "wikics":
graph.ndata['train_mask'] = train_mask[:, i]
graph.ndata['val_mask'] = val_mask[:, i]
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
x = graph.ndata["feat"]
if not load_model:
model = pretrain(model, graph, x, optimizer, max_epoch, device, scheduler, num_classes, lr_f, weight_decay_f, max_epoch_f, linear_prob, args,mask_module,optimizer_mask,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()
final_acc, estp_acc = node_classification_evaluation(model, graph, x, num_classes, lr_f, weight_decay_f, max_epoch_f, device, linear_prob)
acc_list.append(final_acc)
estp_acc_list.append(estp_acc)
if logger is not None:
logger.finish()
final_acc, final_acc_std = np.mean(acc_list), np.std(acc_list)
estp_acc, estp_acc_std = np.mean(estp_acc_list), np.std(estp_acc_list)
print(f"# final_acc: {final_acc:.4f}±{final_acc_std:.4f}")
print(f"# early-stopping_acc: {estp_acc:.4f}±{estp_acc_std:.4f}")
if __name__ == "__main__":
args = build_args()
if args.use_cfg:
args = load_best_configs(args, "configs.yml")
print(args)
main(args)