-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgate_bijector.py
89 lines (71 loc) · 2.83 KB
/
gate_bijector.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
import tensorflow as tf
import tensorflow_probability as tfp
from tensorflow_probability.python.internal import cache_util
tfb = tfp.bijectors
tfd = tfp.distributions
tfe = tfp.experimental
class GateBijectorForNormal(tfb.Bijector):
def __init__(self, loc, scale, residual_fraction, validate_args=False,
name='gate_bijector_for_normal'):
self.loc = loc
self.scale = scale
self.residual_fraction = residual_fraction
super(GateBijectorForNormal, self).__init__(
validate_args=validate_args,
forward_min_event_ndims=0,
name=name)
def _forward(self, x):
x = self.residual_fraction * (self.loc + self.scale * x) + \
(1 - self.residual_fraction) * x
return x
def _inverse(self, y):
y = (y - self.residual_fraction * self.loc) / (
self.residual_fraction * self.scale + 1 - self.residual_fraction)
return y
def _forward_log_det_jacobian(self, x):
fldj = tf.math.log(
self.residual_fraction * (self.scale-1) + 1)
return fldj
class GateBijector(tfb.Bijector):
_cache = cache_util.BijectorCacheWithGreedyAttrs(
forward_name='_augmented_forward', inverse_name='_augmented_inverse')
def __init__(self, dist_bijector, residual_fraction, validate_args=False, name='gate_bijector'):
self.dist_bijector = dist_bijector
self.residual_fraction = residual_fraction
super(GateBijector, self).__init__(
validate_args=validate_args,
forward_min_event_ndims=0,
name=name)
def _gating_bijector(self, gated_residual_fraction):
return (tfe.bijectors.ScalarFunctionWithInferredInverse(
lambda x, lam: lam * self.dist_bijector(x) + (1 - lam) * x,
additional_scalar_parameters_requiring_gradients=[
gated_residual_fraction]))
def _augmented_forward(self, x):
bij = self._gating_bijector(tf.convert_to_tensor(self.residual_fraction))
fldj = bij.forward_log_det_jacobian(x)
return bij.forward(x), {'ildj': -fldj, 'fldj': fldj}
def _augmented_inverse(self, y):
bij = self._gating_bijector(tf.convert_to_tensor(self.residual_fraction))
ildj = bij.inverse_log_det_jacobian(y)
return bij.inverse(y), {'ildj': ildj, 'fldj': -ildj}
def _forward(self, x):
y, _ = self._augmented_forward(x)
return y
def _inverse(self, y):
x, _ = self._augmented_inverse(y)
return x
def _forward_log_det_jacobian(self, x):
cached = self._cache.forward_attributes(x)
# If LDJ isn't in the cache, call forward once.
if 'fldj' not in cached:
_, attrs = self._augmented_forward(x)
cached.update(attrs)
return cached['fldj']
def _inverse_log_det_jacobian(self, y):
cached = self._cache.inverse_attributes(y)
# If LDJ isn't in the cache, call inverse once.
if 'ildj' not in cached:
_, attrs = self._augmented_inverse(y)
cached.update(attrs)
return cached['ildj']