-
Notifications
You must be signed in to change notification settings - Fork 76
/
main.py
222 lines (201 loc) · 12.1 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from sklearn.metrics import roc_auc_score
from tensorboardX import SummaryWriter
from args import *
from model import *
from utils import *
from dataset import *
if not os.path.isdir('results'):
os.mkdir('results')
# args
args = make_args()
print(args)
np.random.seed(123)
np.random.seed()
writer_train = SummaryWriter(comment=args.task+'_'+args.model+'_'+args.comment+'_train')
writer_val = SummaryWriter(comment=args.task+'_'+args.model+'_'+args.comment+'_val')
writer_test = SummaryWriter(comment=args.task+'_'+args.model+'_'+args.comment+'_test')
# set up gpu
if args.gpu:
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.cuda)
print('Using GPU {}'.format(os.environ['CUDA_VISIBLE_DEVICES']))
else:
print('Using CPU')
device = torch.device('cuda:'+str(args.cuda) if args.gpu else 'cpu')
for task in ['link', 'link_pair']:
args.task = task
if args.dataset=='All':
if task == 'link':
datasets_name = ['grid','communities','ppi']
else:
datasets_name = ['communities', 'email', 'protein']
else:
datasets_name = [args.dataset]
for dataset_name in datasets_name:
# if dataset_name in ['communities','grid']:
# args.cache = False
# else:
# args.epoch_num = 401
# args.cache = True
results = []
for repeat in range(args.repeat_num):
result_val = []
result_test = []
time1 = time.time()
data_list = get_tg_dataset(args, dataset_name, use_cache=args.cache, remove_feature=args.rm_feature)
time2 = time.time()
print(dataset_name, 'load time', time2-time1)
num_features = data_list[0].x.shape[1]
num_node_classes = None
num_graph_classes = None
if 'y' in data_list[0].__dict__ and data_list[0].y is not None:
num_node_classes = max([data.y.max().item() for data in data_list])+1
if 'y_graph' in data_list[0].__dict__ and data_list[0].y_graph is not None:
num_graph_classes = max([data.y_graph.numpy()[0] for data in data_list])+1
print('Dataset', dataset_name, 'Graph', len(data_list), 'Feature', num_features, 'Node Class', num_node_classes, 'Graph Class', num_graph_classes)
nodes = [data.num_nodes for data in data_list]
edges = [data.num_edges for data in data_list]
print('Node: max{}, min{}, mean{}'.format(max(nodes), min(nodes), sum(nodes)/len(nodes)))
print('Edge: max{}, min{}, mean{}'.format(max(edges), min(edges), sum(edges)/len(edges)))
args.batch_size = min(args.batch_size, len(data_list))
print('Anchor num {}, Batch size {}'.format(args.anchor_num, args.batch_size))
# data
for i,data in enumerate(data_list):
preselect_anchor(data, layer_num=args.layer_num, anchor_num=args.anchor_num, device='cpu')
data = data.to(device)
data_list[i] = data
# model
input_dim = num_features
output_dim = args.output_dim
model = locals()[args.model](input_dim=input_dim, feature_dim=args.feature_dim,
hidden_dim=args.hidden_dim, output_dim=output_dim,
feature_pre=args.feature_pre, layer_num=args.layer_num, dropout=args.dropout).to(device)
# loss
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=5e-4)
if 'link' in args.task:
loss_func = nn.BCEWithLogitsLoss()
out_act = nn.Sigmoid()
for epoch in range(args.epoch_num):
if epoch==200:
for param_group in optimizer.param_groups:
param_group['lr'] /= 10
model.train()
optimizer.zero_grad()
shuffle(data_list)
effective_len = len(data_list)//args.batch_size*len(data_list)
for id, data in enumerate(data_list[:effective_len]):
if args.permute:
preselect_anchor(data, layer_num=args.layer_num, anchor_num=args.anchor_num, device=device)
out = model(data)
# get_link_mask(data,resplit=False) # resample negative links
edge_mask_train = np.concatenate((data.mask_link_positive_train, data.mask_link_negative_train), axis=-1)
nodes_first = torch.index_select(out, 0, torch.from_numpy(edge_mask_train[0,:]).long().to(device))
nodes_second = torch.index_select(out, 0, torch.from_numpy(edge_mask_train[1,:]).long().to(device))
pred = torch.sum(nodes_first * nodes_second, dim=-1)
label_positive = torch.ones([data.mask_link_positive_train.shape[1],], dtype=pred.dtype)
label_negative = torch.zeros([data.mask_link_negative_train.shape[1],], dtype=pred.dtype)
label = torch.cat((label_positive,label_negative)).to(device)
loss = loss_func(pred, label)
# update
loss.backward()
if id % args.batch_size == args.batch_size-1:
if args.batch_size>1:
# if this is slow, no need to do this normalization
for p in model.parameters():
if p.grad is not None:
p.grad /= args.batch_size
optimizer.step()
optimizer.zero_grad()
if epoch % args.epoch_log == 0:
# evaluate
model.eval()
loss_train = 0
loss_val = 0
loss_test = 0
correct_train = 0
all_train = 0
correct_val = 0
all_val = 0
correct_test = 0
all_test = 0
auc_train = 0
auc_val = 0
auc_test = 0
emb_norm_min = 0
emb_norm_max = 0
emb_norm_mean = 0
for id, data in enumerate(data_list):
out = model(data)
emb_norm_min += torch.norm(out.data, dim=1).min().cpu().numpy()
emb_norm_max += torch.norm(out.data, dim=1).max().cpu().numpy()
emb_norm_mean += torch.norm(out.data, dim=1).mean().cpu().numpy()
# train
# get_link_mask(data, resplit=False) # resample negative links
edge_mask_train = np.concatenate((data.mask_link_positive_train, data.mask_link_negative_train), axis=-1)
nodes_first = torch.index_select(out, 0, torch.from_numpy(edge_mask_train[0, :]).long().to(device))
nodes_second = torch.index_select(out, 0, torch.from_numpy(edge_mask_train[1, :]).long().to(device))
pred = torch.sum(nodes_first * nodes_second, dim=-1)
label_positive = torch.ones([data.mask_link_positive_train.shape[1], ], dtype=pred.dtype)
label_negative = torch.zeros([data.mask_link_negative_train.shape[1], ], dtype=pred.dtype)
label = torch.cat((label_positive, label_negative)).to(device)
loss_train += loss_func(pred, label).cpu().data.numpy()
auc_train += roc_auc_score(label.flatten().cpu().numpy(), out_act(pred).flatten().data.cpu().numpy())
# val
edge_mask_val = np.concatenate((data.mask_link_positive_val, data.mask_link_negative_val), axis=-1)
nodes_first = torch.index_select(out, 0, torch.from_numpy(edge_mask_val[0, :]).long().to(device))
nodes_second = torch.index_select(out, 0, torch.from_numpy(edge_mask_val[1, :]).long().to(device))
pred = torch.sum(nodes_first * nodes_second, dim=-1)
label_positive = torch.ones([data.mask_link_positive_val.shape[1], ], dtype=pred.dtype)
label_negative = torch.zeros([data.mask_link_negative_val.shape[1], ], dtype=pred.dtype)
label = torch.cat((label_positive, label_negative)).to(device)
loss_val += loss_func(pred, label).cpu().data.numpy()
auc_val += roc_auc_score(label.flatten().cpu().numpy(), out_act(pred).flatten().data.cpu().numpy())
# test
edge_mask_test = np.concatenate((data.mask_link_positive_test, data.mask_link_negative_test), axis=-1)
nodes_first = torch.index_select(out, 0, torch.from_numpy(edge_mask_test[0, :]).long().to(device))
nodes_second = torch.index_select(out, 0, torch.from_numpy(edge_mask_test[1, :]).long().to(device))
pred = torch.sum(nodes_first * nodes_second, dim=-1)
label_positive = torch.ones([data.mask_link_positive_test.shape[1], ], dtype=pred.dtype)
label_negative = torch.zeros([data.mask_link_negative_test.shape[1], ], dtype=pred.dtype)
label = torch.cat((label_positive, label_negative)).to(device)
loss_test += loss_func(pred, label).cpu().data.numpy()
auc_test += roc_auc_score(label.flatten().cpu().numpy(), out_act(pred).flatten().data.cpu().numpy())
loss_train /= id+1
loss_val /= id+1
loss_test /= id+1
emb_norm_min /= id+1
emb_norm_max /= id+1
emb_norm_mean /= id+1
auc_train /= id+1
auc_val /= id+1
auc_test /= id+1
print(repeat, epoch, 'Loss {:.4f}'.format(loss_train), 'Train AUC: {:.4f}'.format(auc_train),
'Val AUC: {:.4f}'.format(auc_val), 'Test AUC: {:.4f}'.format(auc_test))
writer_train.add_scalar('repeat_' + str(repeat) + '/auc_'+dataset_name, auc_train, epoch)
writer_train.add_scalar('repeat_' + str(repeat) + '/loss_'+dataset_name, loss_train, epoch)
writer_val.add_scalar('repeat_' + str(repeat) + '/auc_'+dataset_name, auc_val, epoch)
writer_train.add_scalar('repeat_' + str(repeat) + '/loss_'+dataset_name, loss_val, epoch)
writer_test.add_scalar('repeat_' + str(repeat) + '/auc_'+dataset_name, auc_test, epoch)
writer_test.add_scalar('repeat_' + str(repeat) + '/loss_'+dataset_name, loss_test, epoch)
writer_test.add_scalar('repeat_' + str(repeat) + '/emb_min_'+dataset_name, emb_norm_min, epoch)
writer_test.add_scalar('repeat_' + str(repeat) + '/emb_max_'+dataset_name, emb_norm_max, epoch)
writer_test.add_scalar('repeat_' + str(repeat) + '/emb_mean_'+dataset_name, emb_norm_mean, epoch)
result_val.append(auc_val)
result_test.append(auc_test)
result_val = np.array(result_val)
result_test = np.array(result_test)
results.append(result_test[np.argmax(result_val)])
results = np.array(results)
results_mean = np.mean(results).round(6)
results_std = np.std(results).round(6)
print('-----------------Final-------------------')
print(results_mean, results_std)
with open('results/{}_{}_{}_layer{}_approximate{}.txt'.format(args.task,args.model,dataset_name,args.layer_num,args.approximate), 'w') as f:
f.write('{}, {}\n'.format(results_mean, results_std))
# export scalar data to JSON for external processing
writer_train.export_scalars_to_json("./all_scalars.json")
writer_train.close()
writer_val.export_scalars_to_json("./all_scalars.json")
writer_val.close()
writer_test.export_scalars_to_json("./all_scalars.json")
writer_test.close()