-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
66 lines (46 loc) · 1.39 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
import torch
from torch import nn
from torch.nn import functional as F
def masked_loss(out, label, mask):
loss = F.cross_entropy(out, label, reduction='none')
#all phage
#w = torch.Tensor([3.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 3.0, 2.0, 3.0]).cuda()
#loss = F.cross_entropy(out, label, w, reduction='none')
mask = mask.float()
mask = mask / mask.mean()
loss *= mask
loss = loss.mean()
return loss
def masked_acc(out, label, mask):
# [node, f]
pred = out.argmax(dim=1)
correct = torch.eq(pred, label).float()
mask = mask.float()
mask = mask / mask.mean()
correct *= mask
acc = correct.mean()
return acc
def sparse_dropout(x, rate, noise_shape):
"""
:param x:
:param rate:
:param noise_shape: int scalar
:return:
"""
random_tensor = 1 - rate
random_tensor += torch.rand(noise_shape).to(x.device)
dropout_mask = torch.floor(random_tensor).byte().bool()
i = x._indices() # [2, 49216]
v = x._values() # [49216]
# [2, 4926] => [49216, 2] => [remained node, 2] => [2, remained node]
i = i[:, dropout_mask]
v = v[dropout_mask]
out = torch.sparse.FloatTensor(i, v, x.shape).to(x.device)
out = out * (1./ (1-rate))
return out
def dot(x, y, sparse=False):
if sparse:
res = torch.sparse.mm(x, y)
else:
res = torch.mm(x, y)
return res