-
Notifications
You must be signed in to change notification settings - Fork 0
/
impl.py
167 lines (140 loc) · 4.01 KB
/
impl.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
import math
import logsumexp
import torch as th
def proj_simplex_simul(
x: th.Tensor, # 2-D array of weights,
# projection is performed along the 0-th axis
s: float = 1., # axis interesection
):
K = x.shape[1]
k = th.linspace(1, K, K, device=x.device)
x_s = th.sort(x, dim=1, descending=True)[0]
t = (th.cumsum(x_s, dim=1) - s) / k[None]
mu = th.max(t, dim=1, keepdim=True)[0]
return th.clamp(x - mu, 0, s)
def weight_init(
vmin: float,
vmax: float,
n_w: int,
scale: float,
mode: str,
) -> th.Tensor:
x = th.linspace(vmin, vmax, n_w, dtype=th.float32)
match mode:
case "constant":
w = th.ones_like(x) * scale
case "linear":
w = x * scale
case "quadratic":
w = x**2 * scale
case "abs":
w = th.abs(x) * scale
w -= w.max()
w = w.abs()
case "student-t":
alpha = 100
w = scale * math.sqrt(alpha) * x / (1 + 0.5 * alpha * x**2)
case "Student-T":
a_ = 0.1 * 78
b_ = 0.1 * 78**2
denom = 1 + (a_ * x)**2
w = b_ / (2 * a_**2) * th.log(denom)
return w
def f_for(x, ws, mus, sigma):
accumulated = th.zeros_like(x)
max_exponent = th.tensor([-1e20]).to(x)
for mu in mus:
d = x - mu
exponent = -d**2 / (2 * sigma**2)
max_exponent = th.maximum(max_exponent, th.max(exponent))
for w, mu in zip(ws.T, mus):
d = x - mu
exponent = -d**2 / (2 * sigma**2)
accumulated += w[:, None] * th.exp(exponent - max_exponent)
return -(th.log(accumulated) + max_exponent)
def f(x, ws, mus, sigma):
d = x[:, :, None] - mus[None, None, :]
exponent = -d**2 / (2 * sigma**2)
max_exponent = th.max(exponent)
return -(
th.log((ws[:, None] * th.exp(exponent - max_exponent)).sum(2)) +
max_exponent
)
def f_prime(x, ws, mus, sigma):
d = x[:, :, None] - mus[None, None, :]
exponent = -d**2 / (2 * sigma**2)
max_exponent = th.max(exponent)
return (ws[:, None] * d * th.exp(exponent - max_exponent)).sum(2) / (
ws[:, None] * th.exp(exponent - max_exponent) * sigma**2
).sum(2)
class TorchForAutogradNet(th.nn.Module):
def __init__(
self,
ws: th.Tensor,
mus: th.Tensor,
sigma: float,
):
super().__init__()
self.ws = ws
self.mus = mus
self.sigma = sigma
def grad_(self, x):
f_ = f_for(x, self.ws, self.mus, self.sigma)
return f_, th.autograd.grad(f_.sum(), x)
class TorchAutogradNet(th.nn.Module):
def __init__(
self,
ws: th.Tensor,
mus: th.Tensor,
sigma: float,
):
super().__init__()
self.ws = ws
self.mus = mus
self.sigma = sigma
def grad_(self, x):
f_ = f(x, self.ws, self.mus, self.sigma)
return f_, th.autograd.grad(f_.sum(), x)
class TorchExplicitNet(th.nn.Module):
def __init__(
self,
ws: th.Tensor,
mus: th.Tensor,
sigma: float,
):
super().__init__()
self.ws = ws
self.mus = mus
self.sigma = sigma
def grad_(self, x):
return (
f(x, self.ws, self.mus, self.sigma),
f_prime(x, self.ws, self.mus, self.sigma),
)
class CppAutogradNet(th.nn.Module):
def __init__(
self,
ws: th.Tensor,
mus: th.Tensor,
sigma: float,
):
super().__init__()
self.ws = ws
self.mus = mus
self.sigma = sigma
def grad_(self, x):
f_ = logsumexp.pot_act(x, self.ws, self.mus, self.sigma)[0]
return f_, th.autograd.grad(f_.sum(), x)
class CppExplicitNet(th.nn.Module):
def __init__(
self,
ws: th.Tensor,
mus: th.Tensor,
sigma: float,
):
super().__init__()
self.ws = ws
self.mus = mus
self.sigma = sigma
def grad_(self, x):
return logsumexp.pot_act(x, self.ws, self.mus, self.sigma)