forked from Glaciohound/VCML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub_functional.py
274 lines (217 loc) · 7.21 KB
/
sub_functional.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File : sub_functional.py
# Author : Chi Han, Jiayuan Mao
# Email : haanchi@gmail.com, maojiayuan@gmail.com
# Date : 29.07.2019
# Last Modified Date: 23.10.2019
# Last Modified By : Chi Han, Jiayuan Mao
#
# This file is part of the VCML codebase
# Distributed under MIT license
# Several functional classes
import math
import torch
import torch.autograd as autograd
from torch.distributions import Normal
import numpy as np
from utility.common import assert_valid_value
from .utils import \
infinitesimal, infinite, clamp_infinite
class LogitLn_cls(autograd.Function):
"""
Logit function, taking in the ln of a probability as x,
and outputs the logits of it.
This implementation is numerically stable.
ln (p / (1-p)), p = exp(x)
Input:
x: Tensor, x < 0, the ln of the probability
Output:
y: Tensor, the logit of the probability
"""
@staticmethod
def forward(ctx, x, slack=False):
"""
logit = x - ln(1 - exp(x))
When x is close to 0, exp(x) == 1,
the second term will be approximated by ln(-x).
So the final calculation is:
logit = a - b
b = { b1, if exp(x) != 1
| b2, otherwise
Before calculation, x is cast below -infinitesimal
"""
clamped = x > -infinitesimal
x = x.clamp(-infinite, -infinitesimal)
ctx.save_for_backward(x, clamped, torch.BoolTensor([slack]))
a = x
b = stable_softminus(x)
logit = a - b + (x + infinitesimal) * clamped.float()
if not slack:
assert_valid_value(logit)
return logit
@staticmethod
def backward(ctx, grad_output):
"""
d(logit) / d(x) = 1 + 1 / (exp(-x) - 1)
Similarly, if x is close to 1, exp(-x) == 1,
the second term will be approximated by -(1 / x)
grad_x = grad_output * (1 + 1 / a)
a = { exp(-x) - 1, if exp(x) != 1)
| -x, otherwise
"""
x, clamped, slack = ctx.saved_tensors
a1 = (-x).exp() - 1
a2 = -x
switch = x.exp() == 1
a1[switch] = 0
a2[~switch] = 0
a = a1 + a2
grad_x = grad_output * (1 + 1 / a)
# clip out gradient when x is super high
grad_x[clamped] = 1
if not slack:
assert_valid_value(grad_x)
return grad_x, None
class ln_cdf_cls(autograd.Function):
"""
Calculating the log of normal-cdf function
Input:
x: Tensor
Output:
ln: ln of cdf function
"""
@staticmethod
def forward(ctx, x, recursive=True, slack=False):
"""
Method 1 follows that of log_ndtr in scipy:
https://github.com/scipy/scipy/blob/master/scipy/special/cephes/ndtr.c
Method 2 follows from "Abramowitz and Stegun", 7.1.28
Method 3 follows from "global Padé approximations"
"""
method_chosen = 2
if method_chosen == 1:
# Method 1
normal = Normal(0, 1)
th1 = 5
th2 = -5
switch1 = x >= th1
switch2 = (x >= th2) * (x < th1)
switch3 = x < th2
if recursive:
ln1 = -cdf(-x, False)
else:
ln1 = -normal.cdf(-x)
ln2 = normal.cdf(x).log()
ln3_1 = - x.pow(2) / 2 - (-x).log() - math.log(2*math.pi) / 2
ln3_2 = 1
for i in reversed(range(1, 6)):
ln3_2 = 1 - (2 * i - 1) * x.pow(-2) * ln3_2
ln3_2 = ln3_2.log()
ln3 = ln3_1 + ln3_2
ln1[~switch1] = 0
ln2[~switch2] = 0
ln3[~switch3] = 0
ln = ln1 + ln2 + ln3
elif method_chosen == 2:
# Method 2
p = 0.3275911
a = np.array([0,
0.254829592, -0.284496736,
1.421413741, -1.453152027, 1.061405429])
t = 1 / (1 + p * x.abs() / math.sqrt(2))
poly = torch.stack([a[i] * t.pow(i) for i in range(1, 6)]).sum(0)
ln_inside = clamp_infinite(
-x.pow(2)/2 + poly.log() - math.log(2))
ln1 = stable_softminus(ln_inside)
ln2 = ln_inside
ln1[x < 0] = 0
ln2[x >= 0] = 0
ln = ln1 + ln2
elif method_chosen == 3:
# Method 3
a = 0.140012
g = x.pow(2)/2 * ((4/math.pi + a*x.pow(2)/2) / (1 + a*x.pow(2)/2))
ln_eg4 = - g - math.log(4)
# positive normal range
ln1 = ((1 + (1 - (-g).exp()).sqrt()) / 2).log()
# positive infinity
# ln2 = stable_softminus(ln_eg4 + F.softplus(ln_eg4))
ln2 = - (ln_eg4).exp() - (2*ln_eg4).exp()*3/2 \
- (3*ln_eg4).exp()*10/3
# negaitve normal range
ln3 = ((1 - (1 - (-g).exp()).sqrt()) / 2).log()
# negative infinity
ln4 = ln_eg4 - ln2
infinity_flag = (1 - (-g).exp()).sqrt() == 1
switch1 = (x >= 0) * (~infinity_flag)
switch2 = (x >= 0) * infinity_flag
switch3 = (x < 0) * (~infinity_flag)
switch4 = (x < 0) * infinity_flag
ln1[~switch1] = 0
ln2[~switch2] = 0
ln3[~switch3] = 0
ln4[~switch4] = 0
ln = ln1 + ln2 + ln3 + ln4
if not slack:
assert_valid_value(ln)
ctx.save_for_backward(x, ln, torch.BoolTensor([slack]))
return ln
@staticmethod
def backward(ctx, grad_output):
x, ln_cdf_x, slack = ctx.saved_tensors
ln_pdf_x = ln_pdf(x)
grad_x = (ln_pdf_x - ln_cdf_x).exp() * grad_output
if not slack:
assert_valid_value(grad_x, assert_finite=True)
return grad_x, None
class ln_pdf_cls(autograd.Function):
"""
Calculating the log of normal-pdf function
Input:
x: Tensor
Output:
ln: ln of cdf function
"""
@staticmethod
def forward(ctx, x, slack=False):
ln = - x.pow(2) / 2 - math.log(2 * math.pi) / 2
ctx.save_for_backward(x, torch.BoolTensor([slack]))
if not slack:
assert_valid_value(ln)
return ln
@staticmethod
def backward(ctx, grad_output):
x, slack = ctx.saved_tensors
grad_x = -x * grad_output
if not slack:
assert_valid_value(grad_x)
return grad_x, None
ln_pdf = ln_pdf_cls().apply
ln_cdf = ln_cdf_cls().apply
logit_ln = LogitLn_cls().apply
def pdf(x):
output = ln_pdf(x).exp()
return output
def cdf(x, recursive=True):
output = ln_cdf(x, recursive).exp()
return output
def stable_softminus(x):
"""
Calculating log(1 - exp(x)) (x < 0) in a numerically stable way
"""
# negative normal range
y1 = (1 - x.exp()).log()
# negative zero
y2 = (- x - x.pow(2)/2 - x.pow(3)/6).log()
# negative infinity
y3 = - x.exp() - (2*x).exp()/2 - (3*x).exp()/3
switch2 = (1 - x.exp()) == 0
switch3 = (1 - x.exp()) == 1
switch1 = ~switch2 ^ switch3
y1[~switch1] = 0
y2[~switch2] = 0
y3[~switch3] = 0
y = y1 + y2 + y3
return y
__all__ = ['ln_pdf', 'ln_cdf', 'pdf', 'cdf', 'logit_ln']