-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathloss_reweighting.py
104 lines (74 loc) · 2.4 KB
/
loss_reweighting.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
# coding:utf-8
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
def sd(x):
return np.std(x, axis=0, ddof=1)
def sd_gpu(x):
return torch.std(x, dim=0)
def normalize_gpu(x):
x = F.normalize(x, p=1, dim=1)
return x
def normalize(x):
mean = np.mean(x, axis=0)
std = sd(x)
std[std == 0] = 1
x = (x - mean) / std
return x
def random_fourier_features_gpu(x, w=None, b=None, num_f=None, sum=True, sigma=None, seed=None):
if num_f is None:
num_f = 1
n = x.size(0)
r = x.size(1)
x = x.view(n, r, 1)
c = x.size(2)
if sigma is None or sigma == 0:
sigma = 1
if w is None:
w = 1 / sigma * (torch.randn(size=(num_f, c)))
b = 2 * np.pi * torch.rand(size=(r, num_f))
b = b.repeat((n, 1, 1))
Z = torch.sqrt(torch.tensor(2.0 / num_f).cuda())
mid = torch.matmul(x.cuda(), w.t().cuda())
mid = mid + b.cuda()
mid -= mid.min(dim=1, keepdim=True)[0]
mid /= mid.max(dim=1, keepdim=True)[0].cuda()
mid *= np.pi / 2.0
if sum:
Z = Z * (torch.cos(mid).cuda() + torch.sin(mid).cuda())
else:
Z = Z * torch.cat((torch.cos(mid).cuda(), torch.sin(mid).cuda()), dim=-1)
return Z
def lossc(inputs, target, weight):
loss = nn.NLLLoss(reduce=False)
return loss(inputs, target).view(1, -1).mm(weight).view(1)
def cov(x, w=None):
if w is None:
n = x.shape[0]
cov = torch.matmul(x.t(), x) / n
e = torch.mean(x, dim=0).view(-1, 1)
res = cov - torch.matmul(e, e.t())
else:
w = w.view(-1, 1)
cov = torch.matmul((w * x).t(), x)
e = torch.sum(w * x, dim=0).view(-1, 1)
res = cov - torch.matmul(e, e.t())
return res
def lossb_expect(cfeaturec, weight, num_f, sum=True):
cfeaturecs = random_fourier_features_gpu(cfeaturec, num_f=num_f, sum=sum).cuda()
loss = Variable(torch.FloatTensor([0]).cuda())
weight = weight.cuda()
for i in range(cfeaturecs.size()[-1]):
cfeaturec = cfeaturecs[:, :, i]
cov1 = cov(cfeaturec, weight)
cov_matrix = cov1 * cov1
loss += torch.sum(cov_matrix) - torch.trace(cov_matrix)
return loss
def lossq(cfeatures, cfs):
return - cfeatures.pow(2).sum(1).mean(0).view(1) / cfs
def lossn(cfeatures):
return cfeatures.mean(0).pow(2).mean(0).view(1)
if __name__ == '__main__':
pass