-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.py
287 lines (216 loc) · 8.34 KB
/
utils.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from __future__ import division
import torch
import numpy as np
import random
import subprocess
from torch_scatter import scatter_add
import pdb
from torch_geometric.utils import degree
import torch.nn.functional as F
from torch.distributions.uniform import Uniform
from tqdm import tqdm
from torch_geometric.data import DataLoader
import math
from torch_geometric.utils import (negative_sampling, add_self_loops,
train_test_split_edges)
def accuracy(pred, target):
r"""Computes the accuracy of correct predictions.
Args:
pred (Tensor): The predictions.
target (Tensor): The targets.
:rtype: int
"""
return (pred == target).sum().item() / target.numel()
def true_positive(pred, target, num_classes):
r"""Computes the number of true positive predictions.
Args:
pred (Tensor): The predictions.
target (Tensor): The targets.
num_classes (int): The number of classes.
:rtype: :class:`LongTensor`
"""
out = []
for i in range(num_classes):
out.append(((pred == i) & (target == i)).sum())
return torch.tensor(out)
def true_negative(pred, target, num_classes):
r"""Computes the number of true negative predictions.
Args:
pred (Tensor): The predictions.
target (Tensor): The targets.
num_classes (int): The number of classes.
:rtype: :class:`LongTensor`
"""
out = []
for i in range(num_classes):
out.append(((pred != i) & (target != i)).sum())
return torch.tensor(out)
def false_positive(pred, target, num_classes):
r"""Computes the number of false positive predictions.
Args:
pred (Tensor): The predictions.
target (Tensor): The targets.
num_classes (int): The number of classes.
:rtype: :class:`LongTensor`
"""
out = []
for i in range(num_classes):
out.append(((pred == i) & (target != i)).sum())
return torch.tensor(out)
def false_negative(pred, target, num_classes):
r"""Computes the number of false negative predictions.
Args:
pred (Tensor): The predictions.
target (Tensor): The targets.
num_classes (int): The number of classes.
:rtype: :class:`LongTensor`
"""
out = []
for i in range(num_classes):
out.append(((pred != i) & (target == i)).sum())
return torch.tensor(out)
def precision(pred, target, num_classes):
r"""Computes the precision:
:math:`\frac{\mathrm{TP}}{\mathrm{TP}+\mathrm{FP}}`.
Args:
pred (Tensor): The predictions.
target (Tensor): The targets.
num_classes (int): The number of classes.
:rtype: :class:`Tensor`
"""
tp = true_positive(pred, target, num_classes).to(torch.float)
fp = false_positive(pred, target, num_classes).to(torch.float)
out = tp / (tp + fp)
out[torch.isnan(out)] = 0
return out
def recall(pred, target, num_classes):
r"""Computes the recall:
:math:`\frac{\mathrm{TP}}{\mathrm{TP}+\mathrm{FN}}`.
Args:
pred (Tensor): The predictions.
target (Tensor): The targets.
num_classes (int): The number of classes.
:rtype: :class:`Tensor`
"""
tp = true_positive(pred, target, num_classes).to(torch.float)
fn = false_negative(pred, target, num_classes).to(torch.float)
out = tp / (tp + fn)
out[torch.isnan(out)] = 0
return out
def f1_score(pred, target, num_classes):
r"""Computes the :math:`F_1` score:
:math:`2 \cdot \frac{\mathrm{precision} \cdot \mathrm{recall}}
{\mathrm{precision}+\mathrm{recall}}`.
Args:
pred (Tensor): The predictions.
target (Tensor): The targets.
num_classes (int): The number of classes.
:rtype: :class:`Tensor`
"""
prec = precision(pred, target, num_classes)
rec = recall(pred, target, num_classes)
score = 2 * (prec * rec) / (prec + rec)
score[torch.isnan(score)] = 0
return score
def init_seed(seed=2020):
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def get_gpu_memory_map():
"""Get the current gpu usage.
Returns
-------
usage: dict
Keys are device ids as integers.
Values are memory usage as integers in MB.
"""
result = subprocess.check_output(
[
'nvidia-smi', '--query-gpu=memory.used',
'--format=csv,nounits,noheader'
], encoding='utf-8')
# Convert lines into a dictionary
gpu_memory = [int(x) for x in result.strip().split('\n')]
gpu_memory_map = dict(zip(range(len(gpu_memory)), gpu_memory))
return gpu_memory_map
def _norm(edge_index, num_nodes, edge_weight, improved=False, dtype=None):
if edge_weight is None:
edge_weight = torch.ones((edge_index.size(1), ),
dtype=dtype,
device=edge_index.device)
edge_weight = edge_weight.view(-1)
assert edge_weight.size(0) == edge_index.size(1)
row, col = edge_index.detach()
deg = scatter_add(edge_weight.clone(), row.clone(), dim=0, dim_size=num_nodes)
deg_inv_sqrt = deg.pow(-1)
deg_inv_sqrt[deg_inv_sqrt == float('inf')] = 0
return deg_inv_sqrt, row, col
def AA(A, edge_index, batch_size=100000):
# The Adamic-Adar heuristic score.
multiplier = 1 / np.log(A.sum(axis=0))
multiplier[np.isinf(multiplier)] = 0
A_ = A.multiply(multiplier).tocsr()
link_loader = DataLoader(range(edge_index.size(1)), batch_size)
scores = []
# for ind in tqdm(link_loader):
for ind in link_loader:
src, dst = edge_index[0, ind], edge_index[1, ind]
cur_scores = np.array(np.sum(A[src].multiply(A_[dst]), 1)).flatten()
scores.append(cur_scores)
scores = np.concatenate(scores, 0)
return torch.FloatTensor(scores), edge_index
def RA(A, edge_index, batch_size=100000, beta=0.5, A2=None, gamma=0.1, num_nodes=0):
# The Adamic-Adar heuristic score.
# multiplier = 1 / np.log(A.sum(axis=0))
multiplier = 1 / (np.power(A.sum(axis=0), beta))
multiplier[np.isinf(multiplier)] = 0
A_ = A.multiply(multiplier).tocsr()
if A2 is not None:
A2_ = A2.multiply(multiplier).tocsr()
link_loader = DataLoader(range(edge_index.size(1)), batch_size)
scores = []
for ind in tqdm(link_loader):
src, dst = edge_index[0, ind], edge_index[1, ind]
cur_scores = np.array(np.sum(A[src].multiply(A_[dst]), 1)).flatten()
# pdb.set_trace()
if A2 is not None and gamma != 0:
cur_scores2 = np.array(np.sum(A[src].multiply(A2_[dst]), 1)).flatten()
cur_scores3 = np.array(np.sum(A[dst].multiply(A2_[src]), 1)).flatten()
cur_scores4 = np.array(np.sum(A2[src].multiply(A2_[dst]), 1)).flatten()
# cur_scores += 0.1 * (cur_scores2)
cur_scores += gamma * (cur_scores2+cur_scores3) + (gamma*gamma) * cur_scores4
scores.append(cur_scores)
scores = np.concatenate(scores, 0)
return torch.FloatTensor(scores), edge_index
def glorot(tensor):
if tensor is not None:
stdv = math.sqrt(6.0 / (tensor.size(-2) + tensor.size(-1)))
tensor.data.uniform_(-stdv, stdv)
def glorot_orthogonal(tensor, scale):
if tensor is not None:
torch.nn.init.orthogonal_(tensor.data)
scale /= ((tensor.size(-2) + tensor.size(-1)) * tensor.var())
tensor.data *= scale.sqrt()
def zeros(tensor):
if tensor is not None:
tensor.data.fill_(0)
def do_edge_split(dataset):
data = dataset[0]
random.seed(234)
torch.manual_seed(234)
data = train_test_split_edges(data)
edge_index, _ = add_self_loops(data.train_pos_edge_index)
data.train_neg_edge_index = negative_sampling(
edge_index, num_nodes=data.num_nodes,
num_neg_samples=data.train_pos_edge_index.size(1))
split_edge = {'train': {}, 'valid': {}, 'test': {}}
split_edge['train']['edge'] = data.train_pos_edge_index.t()
split_edge['train']['edge_neg'] = data.train_neg_edge_index.t()
split_edge['valid']['edge'] = data.val_pos_edge_index.t()
split_edge['valid']['edge_neg'] = data.val_neg_edge_index.t()
split_edge['test']['edge'] = data.test_pos_edge_index.t()
split_edge['test']['edge_neg'] = data.test_neg_edge_index.t()
return split_edge