-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtrain_linkpred.py
152 lines (118 loc) · 6.94 KB
/
train_linkpred.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
import argparse
import numpy as np
import torch
import torch_geometric.transforms as T
from tqdm.auto import tqdm
# custom modules
from maskgae.utils import set_seed, tab_printer, get_dataset
from maskgae.model import MaskGAE, DegreeDecoder, EdgeDecoder, GNNEncoder, DotEdgeDecoder
from maskgae.mask import MaskEdge, MaskPath
def train_linkpred(model, splits, args, device="cpu"):
optimizer = torch.optim.Adam(model.parameters(),
lr=args.lr,
weight_decay=args.weight_decay)
best_valid = 0
batch_size = args.batch_size
train_data = splits['train'].to(device)
valid_data = splits['valid'].to(device)
test_data = splits['test'].to(device)
for epoch in tqdm(range(1, 1 + args.epochs)):
loss = model.train_step(train_data, optimizer,
alpha=args.alpha,
batch_size=args.batch_size)
if epoch % args.eval_period == 0:
valid_auc, valid_ap = model.test_step(valid_data,
valid_data.pos_edge_label_index,
valid_data.neg_edge_label_index,
batch_size=batch_size)
if valid_auc > best_valid:
best_valid = valid_auc
best_epoch = epoch
torch.save(model.state_dict(), args.save_path)
model.load_state_dict(torch.load(args.save_path))
test_auc, test_ap = model.test_step(test_data,
test_data.pos_edge_label_index,
test_data.neg_edge_label_index,
batch_size=batch_size)
return test_auc, test_ap
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", nargs="?", default="Cora", help="Datasets. (default: Cora)")
parser.add_argument("--mask", nargs="?", default="Path", help="Masking stractegy, `Path`, `Edge` or `None` (default: Path)")
parser.add_argument('--seed', type=int, default=2022, help='Random seed for model and dataset. (default: 2022)')
parser.add_argument('--bn', action='store_true', help='Whether to use batch normalization for GNN encoder. (default: False)')
parser.add_argument("--layer", nargs="?", default="gcn", help="GNN layer, (default: gcn)")
parser.add_argument("--encoder_activation", nargs="?", default="elu", help="Activation function for GNN encoder, (default: elu)")
parser.add_argument('--encoder_channels', type=int, default=128, help='Channels of GNN encoder. (default: 128)')
parser.add_argument('--hidden_channels', type=int, default=128, help='Channels of hidden representation. (default: 128)')
parser.add_argument('--decoder_channels', type=int, default=64, help='Channels of decoder. (default: 64)')
parser.add_argument('--encoder_layers', type=int, default=1, help='Number of layers of encoder. (default: 1)')
parser.add_argument('--decoder_layers', type=int, default=2, help='Number of layers for decoders. (default: 2)')
parser.add_argument('--encoder_dropout', type=float, default=0.8, help='Dropout probability of encoder. (default: 0.7)')
parser.add_argument('--decoder_dropout', type=float, default=0.2, help='Dropout probability of decoder. (default: 0.3)')
parser.add_argument('--alpha', type=float, default=0.003, help='loss weight for degree prediction. (default: 2e-3)')
parser.add_argument('--lr', type=float, default=1e-2, help='Learning rate for training. (default: 1e-2)')
parser.add_argument('--weight_decay', type=float, default=5e-5, help='weight_decay for training. (default: 5e-5)')
parser.add_argument('--grad_norm', type=float, default=1.0, help='grad_norm for training. (default: 1.0.)')
parser.add_argument('--batch_size', type=int, default=2**16, help='Number of batch size. (default: 2**16)')
parser.add_argument("--start", nargs="?", default="edge", help="Which Type to sample starting nodes for random walks, (default: edge)")
parser.add_argument('--p', type=float, default=0.7, help='Mask ratio or sample ratio for MaskEdge/MaskPath')
parser.add_argument('--epochs', type=int, default=500, help='Number of training epochs. (default: 300)')
parser.add_argument('--runs', type=int, default=10, help='Number of runs. (default: 10)')
parser.add_argument('--eval_period', type=int, default=10, help='(default: 10)')
parser.add_argument("--save_path", nargs="?", default="MaskGAE-LinkPred.pt", help="save path for model. (default: MaskGAE-LinkPred.pt)")
parser.add_argument("--device", type=int, default=0)
try:
args = parser.parse_args()
print(tab_printer(args))
except:
parser.print_help()
exit(0)
set_seed(args.seed)
if args.device < 0:
device = "cpu"
else:
device = f"cuda:{args.device}" if torch.cuda.is_available() else "cpu"
transform = T.Compose([
T.ToUndirected(),
T.ToDevice(device),
])
# (!IMPORTANT) Specify the path to your dataset directory ##############
# root = '~/public_data/pyg_data' # my root directory
root = 'data/'
########################################################################
data = get_dataset(root, args.dataset, transform=transform)
train_data, val_data, test_data = T.RandomLinkSplit(num_val=0.05, num_test=0.1,
is_undirected=True,
split_labels=True,
add_negative_train_samples=False)(data)
splits = dict(train=train_data, valid=val_data, test=test_data)
if args.mask == 'Path':
mask = MaskPath(p=args.p, num_nodes=data.num_nodes,
start=args.start,
walk_length=args.encoder_layers+1)
elif args.mask == 'Edge':
mask = MaskEdge(p=args.p)
else:
mask = None # vanilla GAE
encoder = GNNEncoder(data.num_features, args.encoder_channels, args.hidden_channels,
num_layers=args.encoder_layers, dropout=args.encoder_dropout,
bn=args.bn, layer=args.layer, activation=args.encoder_activation)
if args.decoder_layers == 0:
edge_decoder = DotEdgeDecoder()
else:
edge_decoder = EdgeDecoder(args.hidden_channels, args.decoder_channels,
num_layers=args.decoder_layers, dropout=args.decoder_dropout)
degree_decoder = DegreeDecoder(args.hidden_channels, args.decoder_channels,
num_layers=args.decoder_layers, dropout=args.decoder_dropout)
model = MaskGAE(encoder, edge_decoder, degree_decoder, mask).to(device)
auc_results = []
ap_results = []
for run in range(1, args.runs+1):
test_auc, test_ap = train_linkpred(model, splits, args, device=device)
auc_results.append(test_auc)
ap_results.append(test_ap)
print(f'Runs {run} - AUC: {test_auc:.2%}', f'AP: {test_ap:.2%}')
print(f'Link Prediction Results ({args.runs} runs):\n'
f'AUC: {np.mean(auc_results):.2%} ± {np.std(auc_results):.2%}',
f'AP: {np.mean(ap_results):.2%} ± {np.std(ap_results):.2%}',
)