-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtrain_node.py
58 lines (45 loc) · 1.84 KB
/
train_node.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
import os
import wandb
import pickle
import torch
from torch_geometric.seed import seed_everything
from torch_geometric.utils import to_undirected, is_undirected
import torch_geometric.transforms as T
from torch_geometric.datasets import CitationFull, Coauthor, Flickr, RelLinkPredDataset, WordNet18, WordNet18RR
from torch_geometric.seed import seed_everything
from framework import get_model, get_trainer
from framework.training_args import parse_args
from framework.trainer.base import NodeClassificationTrainer
from framework.utils import negative_sampling_kg
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def main():
args = parse_args()
args.checkpoint_dir = 'checkpoint_node'
args.dataset = 'DBLP'
args.unlearning_model = 'original'
args.checkpoint_dir = os.path.join(args.checkpoint_dir, args.dataset, args.gnn, args.unlearning_model, str(args.random_seed))
os.makedirs(args.checkpoint_dir, exist_ok=True)
seed_everything(args.random_seed)
# Dataset
dataset = CitationFull(os.path.join(args.data_dir, args.dataset), args.dataset, transform=T.NormalizeFeatures())
data = dataset[0]
print('Original data', data)
split = T.RandomNodeSplit()
data = split(data)
assert is_undirected(data.edge_index)
print('Split data', data)
args.in_dim = data.x.shape[1]
args.out_dim = dataset.num_classes
wandb.init(config=args)
# Model
model = get_model(args, num_nodes=data.num_nodes, num_edge_type=args.num_edge_type).to(device)
wandb.watch(model, log_freq=100)
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)#, weight_decay=args.weight_decay)
# Train
trainer = NodeClassificationTrainer(args)
trainer.train(model, data, optimizer, args)
# Test
trainer.test(model, data)
trainer.save_log()
if __name__ == "__main__":
main()