-
Notifications
You must be signed in to change notification settings - Fork 178
/
utils.py
301 lines (235 loc) · 9.72 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
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, add_self_loops
import torch.nn.functional as F
from torch.distributions.uniform import Uniform
import time
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=None, 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 sample_adj(edge_index, edge_weight, thr=0.5, sampling_type='random', binary=False):
# # tmp = (edge_weight - torch.mean(edge_weight)) / torch.std(edge_weight)
# if sampling_type == 'gumbel':
# sampled = pyro.distributions.RelaxedBernoulliStraightThrough(temperature=1,
# probs=edge_weight).rsample(thr=thr)
# elif sampling_type == 'random':
# sampled = pyro.distributions.Bernoulli(1-thr).sample(edge_weight.shape).cuda()
# elif sampling_type == 'topk':
# indices = torch.topk(edge_weight, k=int(edge_weight.shape[0]*0.8))[1]
# sampled = torch.zeros_like(edge_weight)
# sampled[indices] = 1
# # print(sampled.sum()/edge_weight.shape[0])
# edge_index = edge_index[:,sampled==1]
# edge_weight = edge_weight*sampled
# edge_weight = edge_weight[edge_weight!=0]
# if binary:
# return edge_index, sampled[sampled!=0]
# else:
# return edge_index, edge_weight
def to_heterogeneous(edge_index, num_nodes, n_id, edge_type, num_edge, device='cuda', args=None):
# edge_index = adj[0]
# num_nodes = adj[2][0]
edge_type_indices = []
# pdb.set_trace()
for k in range(edge_index.shape[1]):
edge_tmp = edge_index[:,k]
e_type = edge_type[n_id[edge_tmp[0]].item()][n_id[edge_tmp[1]].item()]
edge_type_indices.append(e_type)
edge_type_indices = np.array(edge_type_indices)
A = []
for e_type in range(num_edge):
edge_tmp = edge_index[:,edge_type_indices==e_type]
#################################### j -> i ########################################
edge_tmp = torch.flip(edge_tmp, [0])
#################################### j -> i ########################################
value_tmp = torch.ones(edge_tmp.shape[1]).type(torch.FloatTensor)
if args.model == 'FastGTN':
edge_tmp, value_tmp = add_self_loops(edge_tmp, edge_weight=value_tmp, fill_value=1e-20, num_nodes=num_nodes)
deg_inv_sqrt, deg_row, deg_col = _norm(edge_tmp.detach(), num_nodes, value_tmp.detach())
value_tmp = deg_inv_sqrt[deg_row] * value_tmp
A.append((edge_tmp.to(device), value_tmp.to(device)))
edge_tmp = torch.stack((torch.arange(0,n_id.shape[0]),torch.arange(0,n_id.shape[0]))).type(torch.LongTensor)
value_tmp = torch.ones(num_nodes).type(torch.FloatTensor)
A.append([edge_tmp.to(device),value_tmp.to(device)])
return A
# def to_heterogeneous(adj, n_id, edge_type, num_edge, device='cuda'):
# edge_index = adj[0]
# num_nodes = adj[2][0]
# edge_type_indices = []
# for k in range(edge_index.shape[1]):
# edge_tmp = edge_index[:,k]
# e_type = edge_type[n_id[edge_tmp[0]].item()][n_id[edge_tmp[1]].item()]
# edge_type_indices.append(e_type)
# edge_type_indices = np.array(edge_type_indices)
# A = []
# for e_type in range(num_edge):
# edge_tmp = edge_index[:,edge_type_indices==e_type]
# value_tmp = torch.ones(edge_tmp.shape[1]).type(torch.FloatTensor)
# A.append((edge_tmp.to(device), value_tmp.to(device)))
# edge_tmp = torch.stack((torch.arange(0,n_id.shape[0]),torch.arange(0,n_id.shape[0]))).type(torch.LongTensor)
# value_tmp = torch.ones(num_nodes).type(torch.FloatTensor)
# A.append([edge_tmp.to(device),value_tmp.to(device)])
# return A
def generate_non_local_graph(args, feat_trans, H, A, num_edge, num_nodes):
K = args.K
# if not args.knn:
# pdb.set_trace()
x = F.relu(feat_trans(H))
# D_ = torch.sigmoid([email protected]())
D_ = [email protected]()
_, D_topk_indices = D_.t().sort(dim=1, descending=True)
D_topk_indices = D_topk_indices[:,:K]
D_topk_value = D_.t()[torch.arange(D_.shape[0]).unsqueeze(-1).expand(D_.shape[0], K), D_topk_indices]
edge_j = D_topk_indices.reshape(-1)
edge_i = torch.arange(D_.shape[0]).unsqueeze(-1).expand(D_.shape[0], K).reshape(-1).to(H.device)
edge_index = torch.stack([edge_i, edge_j])
edge_value = (D_topk_value).reshape(-1)
edge_value = D_topk_value.reshape(-1)
return [edge_index, edge_value]
# if len(A) < num_edge:
# deg_inv_sqrt, deg_row, deg_col = _norm(edge_index, num_nodes, edge_value)
# edge_value = deg_inv_sqrt[deg_col] * edge_value
# g = (edge_index, edge_value)
# A.append(g)
# else:
# deg_inv_sqrt, deg_row, deg_col = _norm(edge_index, num_nodes, edge_value)
# edge_value = deg_inv_sqrt[deg_col] * edge_value
# g = (edge_index, edge_value)
# A[-1] = g