forked from yilei-wu/WMH-DualTasker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
63 lines (58 loc) · 2.12 KB
/
utils.py
File metadata and controls
63 lines (58 loc) · 2.12 KB
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
import numpy as np
from scipy.stats import norm
import torch
def num2vect(x, sigma):
'''
x: list of scalar (0-35)
sigma = 0 -> hard label (one-hot encoding)
sigma > 0 -> soft label (softmax)
'''
start = 0
end = 30
if len(x) == 1:
x = int(x)
v = np.zeros((end - start))
centers = (np.arange(end) + 0.5)
if np.isscalar(x):
if sigma == 0:
v[x] = 1
return torch.from_numpy(v)
elif sigma > 0:
for i in range(end):
x1 = centers[i] - 0.5
x2 = centers[i] + 0.5
cdfs = norm.cdf([x1, x2], loc=x, scale=sigma)
v[i] = cdfs[1] - cdfs[0]
return torch.from_numpy(v)
else:
raise("x needs to be a scaler !")
else:
v = np.zeros((len(x), (end - start)))
for idx, q in enumerate(x):
q = int(q)
centers = (np.arange(end) + 0.5)
if np.isscalar(q):
if sigma == 0:
v[idx, q] = 1
elif sigma > 0:
for i in range(end):
x1 = centers[i] - 0.5
x2 = centers[i] + 0.5
cdfs = norm.cdf([x1, x2], loc=q, scale=sigma)
v[idx, i] = cdfs[1] - cdfs[0]
else:
raise("x needs to be a scaler !")
return torch.from_numpy(v)
if __name__ == "__main__":
print(num2vect(4, sigma=1))
'''
[1.31822679e-03 2.14002339e-02 1.35905122e-01 3.41344746e-01
3.41344746e-01 1.35905122e-01 2.14002339e-02 1.31822679e-03
3.13845903e-05 2.85664984e-07 9.85307835e-10 1.27919897e-12
6.66133815e-16 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00]
'''