-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
scale.py
106 lines (85 loc) · 2.93 KB
/
scale.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
# Copyright 2019 The TensorFlow Probability Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Scale bijector."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow.compat.v2 as tf
from tensorflow_probability.python.bijectors import bijector
from tensorflow_probability.python.internal import assert_util
from tensorflow_probability.python.internal import dtype_util
from tensorflow_probability.python.internal import tensor_util
__all__ = [
'Scale',
]
class Scale(bijector.Bijector):
"""Compute `Y = g(X; scale) = scale * X`.
Examples:
```python
# Y = 2 * X
b = Scale(scale=2.)
```
"""
def __init__(self,
scale,
validate_args=False,
name='scale'):
"""Instantiates the `Scale` bijector.
This `Bijector`'s forward operation is:
```none
Y = g(X) = scale * X
```
Args:
scale: Floating-point `Tensor`.
validate_args: Python `bool` indicating whether arguments should be
checked for correctness.
name: Python `str` name given to ops managed by this object.
"""
parameters = dict(locals())
with tf.name_scope(name) as name:
dtype = dtype_util.common_dtype([scale], dtype_hint=tf.float32)
self._scale = tensor_util.convert_nonref_to_tensor(
scale, dtype=dtype, name='scale')
super(Scale, self).__init__(
forward_min_event_ndims=0,
is_constant_jacobian=True,
validate_args=validate_args,
dtype=dtype,
parameters=parameters,
name=name)
@property
def scale(self):
"""The `scale` term in `Y = scale * X`."""
return self._scale
def _is_increasing(self):
return self.scale > 0
def _forward(self, x):
return x * self.scale
def _inverse(self, y):
return y / self.scale
def _forward_log_det_jacobian(self, x):
return tf.math.log(tf.abs(self.scale))
def _parameter_control_dependencies(self, is_init):
if not self.validate_args:
return []
assertions = []
if (self.scale is not None and
is_init != tensor_util.is_ref(self.scale)):
assertions.append(
assert_util.assert_none_equal(
self.scale,
tf.zeros([], dtype=self._scale.dtype),
message='Argument `scale` must be non-zero.'))
return assertions