-
Notifications
You must be signed in to change notification settings - Fork 0
/
coauthor_link.py
188 lines (152 loc) · 7.35 KB
/
coauthor_link.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
import numpy as np
import scipy.sparse as sp
import torch
import argparse
import os
import warnings
warnings.filterwarnings("ignore")
from utils import process
from utils import aug
from scipy.sparse import csr_matrix
from utils.utils import str_to_bool
from modules.encoder import GraphEncoder
from modules.model import GRCCA
from test import link_eva
from modules.clustering import init_memory
from train import train
from utils.other_process import get_train_val_test_split
parser = argparse.ArgumentParser()
parser.add_argument('--device', type=str, default='cuda:0')
parser.add_argument('--seed', type=int, default=1908)
parser.add_argument('--data', type=str, default='amazon_electronics_photo')
parser.add_argument('--runs', type=int, default=5)
parser.add_argument('--epochs', type=int, default=1000)
parser.add_argument('--lr', type=float, default=0.001)
parser.add_argument('--weight_decay', type=float, default=0.00001)
parser.add_argument('--patience', type=int, default=100)
parser.add_argument('--sparse', type=str_to_bool, default=True)
parser.add_argument('--gnn_dim', type=int, default=256)
parser.add_argument('--proj_dim', type=int, default=256)
parser.add_argument('--proj_hid', type=int, default=256)
parser.add_argument('--proj_out', type=int, default=256)
parser.add_argument('--proto_dim', type=int, default=256)
parser.add_argument('--act', type=str, default='relu')
parser.add_argument("--nmb_prototypes", default=[24, 24, 24, 24], type=int, nargs="+",
help="number of prototypes - it can be multihead")
parser.add_argument("--crops_for_assign", type=int, nargs="+", default=[0, 1],
help="list of crops id used for computing assignments")
parser.add_argument('--alpha', type=float, default=0.05)
parser.add_argument('--drop_edge', type=float, default=0.4)
parser.add_argument('--drop_feat1', type=float, default=0.2)
parser.add_argument('--drop_feat2', type=float, default=0.2)
parser.add_argument("--temperature", default=0.1, type=float,
help="temperature parameter in training loss")
parser.add_argument("--nmb_crops", type=int, default=[2], nargs="+",
help="list of number of crops (example: [2, 6])")
parser.add_argument('--test_rate', dest='test_rate', type=float, default=0.1, help='')
args = parser.parse_args()
torch.set_num_threads(4)
def main():
# Set the seed
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
# Set the device
device = torch.device(args.device if torch.cuda.is_available() else 'cpu')
# Set hyper-parameters
dataset = args.data
gnn_output_size = args.gnn_dim
projection_size = args.proj_dim
projection_hidden_size = args.proj_hid
projection_out_size = args.proj_out
nmb_prototpyes = args.nmb_prototypes
alpha = args.alpha
epochs = args.epochs
lr = args.lr
weight_decay = args.weight_decay
patience = args.patience
sparse = args.sparse
act = args.act
# Load datasets
adj, features, labels, idx_train, idx_val, idx_test = get_train_val_test_split(0, 30, dataset)
adj_sparse = adj
if os.path.exists('process_dataset/split_{}_adj_train.npz'.format(dataset)):
print('the split existed, so we directly load this splited data')
mtr = np.load('process_dataset/split_{}_adj_train.npz'.format(dataset), allow_pickle=True)
adj_train = csr_matrix((mtr['data'], (mtr['row'], mtr['col'])))
test_edges = np.load('process_dataset/split_{}_test_edges.npy'.format(dataset), allow_pickle=True)
test_edges_false = np.load('process_dataset/split_{}_test_edges_false.npy'.format(dataset), allow_pickle=True)
else:
print('we contruct this splited data')
adj_train, train_edges, train_edges_false, val_edges, val_edges_false, \
test_edges, test_edges_false = process.mask_test_edges(adj, test_frac=args.test_rate, val_frac=0.05)
mtr = adj_train.tocoo()
np.savez('process_dataset/split_{}_adj_train'.format(dataset), data=mtr.data, row=mtr.row, col=mtr.col)
np.save('process_dataset/split_{}_test_edges'.format(dataset), test_edges)
np.save('process_dataset/split_{}_test_edges_false'.format(dataset), test_edges_false)
adj = adj_train
if os.path.exists('data/diff_{}_{}_link.npy'.format(dataset, alpha)):
diff = np.load('data/diff_{}_{}_link.npy'.format(dataset, alpha), allow_pickle=True)
else:
diff = aug.gdc(adj, alpha=alpha, eps=0.0001)
np.save('data/diff_{}_{}_link.npy'.format(dataset, alpha), diff)
# features, _ = process.preprocess_features(features)
features = features.todense()
features = torch.FloatTensor(features[np.newaxis]) # 将tensor扩展一个维度
input_size = features.shape[-1]
norm_adj = process.normalize_adj(adj + sp.eye(adj.shape[0]))
norm_diff = sp.csr_matrix(diff)
if sparse:
eval_adj = process.sparse_mx_to_torch_sparse_tensor(norm_adj)
eval_diff = process.sparse_mx_to_torch_sparse_tensor(norm_diff)
else:
eval_adj = (norm_adj + sp.eye(norm_adj.shape[0])).todense()
eval_diff = (norm_diff + sp.eye(norm_diff.shape[0])).todense()
eval_adj = torch.FloatTensor(eval_adj[np.newaxis])
eval_diff = torch.FloatTensor(eval_diff[np.newaxis])
# Record results over each run
AUC = []
AP = []
for i in range(args.runs):
# Bulid model
model = GraphEncoder(input_size, gnn_output_size, act='relu')
cgcn = GRCCA(encoder=model,
projection_input_size=projection_size,
projection_hidden_size=projection_hidden_size,
projection_output_size=projection_out_size,
nmb_prototypes=nmb_prototpyes).to(device)
# Set optimizer
opt = torch.optim.Adam(cgcn.parameters(), lr=lr, weight_decay=weight_decay)
# Initialize memory
local_memory_embeddings = init_memory(args, model, adj, diff, features, sparse, device)
# Training
best = 1e9
patience_count = 0
for epoch in range(epochs):
loss, local_memory_embeddings = train(args, cgcn, adj, diff, features, local_memory_embeddings, sparse,
device, opt)
print(f'(T) | Run={i:03d}, Epoch={epoch:03d}, loss={loss:.4f}')
if loss < best:
best = loss
patience_count = 0
# print("hell")
torch.save(model.state_dict(), dataset + '-link.pkl')
else:
patience_count += 1
if patience_count == patience:
# print('Early stopping!')
break
# 输出最优结果
sc_roc, sc_ap = link_eva(eval_adj, eval_diff, features, dataset, sparse, input_size, gnn_output_size, test_edges, test_edges_false, adj_sparse, act)
AUC.append(sc_roc * 100)
AP.append(sc_ap * 100)
print('-----------------------------------------')
print('Average AUC:')
print(np.mean(AUC))
print(np.std(AUC))
print('-----------------------------------------')
print('Average AP:')
print(np.mean(AP))
print(np.std(AP))
if __name__ == '__main__':
main()