-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
153 lines (108 loc) · 4.82 KB
/
main.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
import argparse
from model import CCA_SSG, LogReg
from aug import random_aug
from dataset import load
import numpy as np
import torch as th
import torch.nn as nn
import warnings
warnings.filterwarnings('ignore')
parser = argparse.ArgumentParser(description='CCA-SSG')
parser.add_argument('--dataname', type=str, default='cora', help='Name of dataset.')
parser.add_argument('--gpu', type=int, default=0, help='GPU index.')
parser.add_argument('--epochs', type=int, default=100, help='Training epochs.')
parser.add_argument('--lr1', type=float, default=1e-3, help='Learning rate of CCA-SSG.')
parser.add_argument('--lr2', type=float, default=1e-2, help='Learning rate of linear evaluator.')
parser.add_argument('--wd1', type=float, default=0, help='Weight decay of CCA-SSG.')
parser.add_argument('--wd2', type=float, default=1e-4, help='Weight decay of linear evaluator.')
parser.add_argument('--lambd', type=float, default=1e-3, help='trade-off ratio.')
parser.add_argument('--n_layers', type=int, default=2, help='Number of GNN layers')
parser.add_argument('--use_mlp', action='store_true', default=False, help='Use MLP instead of GNN')
parser.add_argument('--der', type=float, default=0.2, help='Drop edge ratio.')
parser.add_argument('--dfr', type=float, default=0.2, help='Drop feature ratio.')
parser.add_argument("--hid_dim", type=int, default=512, help='Hidden layer dim.')
parser.add_argument("--out_dim", type=int, default=512, help='Output layer dim.')
args = parser.parse_args()
# check cuda
if args.gpu != -1 and th.cuda.is_available():
args.device = 'cuda:{}'.format(args.gpu)
else:
args.device = 'cpu'
if __name__ == '__main__':
print(args)
graph, feat, labels, num_class, train_idx, val_idx, test_idx = load(args.dataname)
in_dim = feat.shape[1]
model = CCA_SSG(in_dim, args.hid_dim, args.out_dim, args.n_layers, args.use_mlp)
model = model.to(args.device)
optimizer = th.optim.Adam(model.parameters(), lr=args.lr1, weight_decay=args.wd1)
N = graph.number_of_nodes()
for epoch in range(args.epochs):
model.train()
optimizer.zero_grad()
graph1, feat1 = random_aug(graph, feat, args.dfr, args.der)
graph2, feat2 = random_aug(graph, feat, args.dfr, args.der)
graph1 = graph1.add_self_loop()
graph2 = graph2.add_self_loop()
graph1 = graph1.to(args.device)
graph2 = graph2.to(args.device)
feat1 = feat1.to(args.device)
feat2 = feat2.to(args.device)
z1, z2 = model(graph1, feat1, graph2, feat2)
c = th.mm(z1.T, z2)
c1 = th.mm(z1.T, z1)
c2 = th.mm(z2.T, z2)
c = c / N
c1 = c1 / N
c2 = c2 / N
loss_inv = -th.diagonal(c).sum()
iden = th.tensor(np.eye(c.shape[0])).to(args.device)
loss_dec1 = (iden - c1).pow(2).sum()
loss_dec2 = (iden - c2).pow(2).sum()
loss = loss_inv + args.lambd * (loss_dec1 + loss_dec2)
loss.backward()
optimizer.step()
print('Epoch={:03d}, loss={:.4f}'.format(epoch, loss.item()))
print("=== Evaluation ===")
graph = graph.to(args.device)
graph = graph.remove_self_loop().add_self_loop()
feat = feat.to(args.device)
embeds = model.get_embedding(graph, feat)
train_embs = embeds[train_idx]
val_embs = embeds[val_idx]
test_embs = embeds[test_idx]
label = labels.to(args.device)
train_labels = label[train_idx]
val_labels = label[val_idx]
test_labels = label[test_idx]
train_feat = feat[train_idx]
val_feat = feat[val_idx]
test_feat = feat[test_idx]
''' Linear Evaluation '''
logreg = LogReg(train_embs.shape[1], num_class)
opt = th.optim.Adam(logreg.parameters(), lr=args.lr2, weight_decay=args.wd2)
logreg = logreg.to(args.device)
loss_fn = nn.CrossEntropyLoss()
best_val_acc = 0
eval_acc = 0
for epoch in range(2000):
logreg.train()
opt.zero_grad()
logits = logreg(train_embs)
preds = th.argmax(logits, dim=1)
train_acc = th.sum(preds == train_labels).float() / train_labels.shape[0]
loss = loss_fn(logits, train_labels)
loss.backward()
opt.step()
logreg.eval()
with th.no_grad():
val_logits = logreg(val_embs)
test_logits = logreg(test_embs)
val_preds = th.argmax(val_logits, dim=1)
test_preds = th.argmax(test_logits, dim=1)
val_acc = th.sum(val_preds == val_labels).float() / val_labels.shape[0]
test_acc = th.sum(test_preds == test_labels).float() / test_labels.shape[0]
if val_acc > best_val_acc:
best_val_acc = val_acc
eval_acc = test_acc
print('Epoch:{}, train_acc:{:.4f}, val_acc:{:4f}, test_acc:{:4f}'.format(epoch, train_acc, val_acc, test_acc))
print('Linear evaluation accuracy:{:.4f}'.format(eval_acc))