You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

softplus.py 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Softplus Bijector"""
  16. import numpy as np
  17. from mindspore.ops import operations as P
  18. from mindspore.nn.layer.activation import LogSigmoid
  19. from mindspore._checkparam import Validator as validator
  20. from ..distribution._utils.utils import cast_to_tensor
  21. from ..distribution._utils.custom_ops import exp_generic, expm1_generic, log_generic
  22. from .bijector import Bijector
  23. class Softplus(Bijector):
  24. r"""
  25. Softplus Bijector.
  26. This Bijector performs the operation:
  27. .. math::
  28. Y = \frac{\log(1 + e ^ {kX})}{k}
  29. where k is the sharpness factor.
  30. Args:
  31. sharpness (float): The scale factor. Default: 1.0.
  32. name (str): The name of the Bijector. Default: 'Softplus'.
  33. Examples:
  34. >>> # To initialize a Softplus bijector of sharpness 2.
  35. >>> softplus = nn.probability.bijector.Softfplus(2)
  36. >>>
  37. >>> # To use ScalarAffine bijector in a network.
  38. >>> class net(Cell):
  39. >>> def __init__(self):
  40. >>> super(net, self).__init__():
  41. >>> self.sp1 = nn.probability.bijector.Softflus(2)
  42. >>>
  43. >>> def construct(self, value):
  44. >>> # Similar calls can be made to other functions
  45. >>> # by replacing 'forward' by the name of the function.
  46. >>> ans1 = self.sp1.forward(value)
  47. >>> ans2 = self.sp1.inverse(value)
  48. >>> ans3 = self.sp1.forward_log_jacobian(value)
  49. >>> ans4 = self.sp1.inverse_log_jacobian(value)
  50. """
  51. def __init__(self,
  52. sharpness=1.0,
  53. name='Softplus'):
  54. """
  55. Constructor of Softplus Bijector.
  56. """
  57. param = dict(locals())
  58. validator.check_value_type('sharpness', sharpness,
  59. [int, float], type(self).__name__)
  60. super(Softplus, self).__init__(name=name, param=param)
  61. self._sharpness = cast_to_tensor(sharpness)
  62. self.exp = exp_generic
  63. self.log = log_generic
  64. self.expm1 = expm1_generic
  65. self.abs = P.Abs()
  66. self.dtypeop = P.DType()
  67. self.cast = P.Cast()
  68. self.fill = P.Fill()
  69. self.greater = P.Greater()
  70. self.less = P.Less()
  71. self.log_sigmoid = LogSigmoid()
  72. self.logicalor = P.LogicalOr()
  73. self.select = P.Select()
  74. self.shape = P.Shape()
  75. self.sigmoid = P.Sigmoid()
  76. self.softplus = self._softplus
  77. self.inverse_softplus = self._inverse_softplus
  78. self.threshold = np.log(np.finfo(np.float32).eps) + 1
  79. self.tiny = np.exp(self.threshold)
  80. def _softplus(self, x):
  81. too_small = self.less(x, self.threshold)
  82. too_large = self.greater(x, -self.threshold)
  83. too_small_value = self.exp(x)
  84. too_large_value = x
  85. ones = self.fill(self.dtypeop(x), self.shape(x), 1.0)
  86. too_small_or_too_large = self.logicalor(too_small, too_large)
  87. x = self.select(too_small_or_too_large, ones, x)
  88. y = self.log(self.exp(x) + 1.0)
  89. return self.select(too_small, too_small_value, self.select(too_large, too_large_value, y))
  90. def _inverse_softplus(self, x):
  91. r"""
  92. .. math::
  93. f(x) = \frac{\log(1 + e^{x}))}
  94. f^{-1}(y) = \frac{\log(e^{y} - 1)}
  95. """
  96. too_small = self.less(x, self.tiny)
  97. too_large = self.greater(x, -self.threshold)
  98. too_small_value = self.log(x)
  99. too_large_value = x
  100. ones = self.fill(self.dtypeop(x), self.shape(x), 1.0)
  101. too_small_or_too_large = self.logicalor(too_small, too_large)
  102. x = self.select(too_small_or_too_large, ones, x)
  103. y = x + self.log(self.abs(self.expm1(-x)))
  104. return self.select(too_small, too_small_value, self.select(too_large, too_large_value, y))
  105. @property
  106. def sharpness(self):
  107. return self._sharpness
  108. def extend_repr(self):
  109. str_info = f'sharpness = {self.sharpness}'
  110. return str_info
  111. def shape_mapping(self, shape):
  112. return shape
  113. def _forward(self, x):
  114. x = self._check_value(x, 'value')
  115. sharpness_local = self.cast_param_by_value(x, self.sharpness)
  116. scaled_value = sharpness_local * x
  117. forward_v = self.softplus(scaled_value) / sharpness_local
  118. return forward_v
  119. def _inverse(self, y):
  120. r"""
  121. .. math::
  122. f(x) = \frac{\log(1 + e^{kx}))}{k}
  123. f^{-1}(y) = \frac{\log(e^{ky} - 1)}{k}
  124. """
  125. y = self._check_value(y, 'value')
  126. sharpness_local = self.cast_param_by_value(y, self.sharpness)
  127. scaled_value = sharpness_local * y
  128. inverse_v = self.inverse_softplus(scaled_value) / sharpness_local
  129. return inverse_v
  130. def _forward_log_jacobian(self, x):
  131. r"""
  132. .. math:
  133. f(x) = \log(1 + e^{kx}) / k
  134. f'(x) = \frac{e^{kx}}{ 1 + e^{kx}}
  135. \log(f'(x)) = kx - \log(1 + e^{kx}) = kx - f(kx)
  136. """
  137. x = self._check_value(x, 'value')
  138. sharpness_local = self.cast_param_by_value(x, self.sharpness)
  139. scaled_value = sharpness_local * x
  140. forward_log_j = self.log_sigmoid(scaled_value)
  141. return forward_log_j
  142. def _inverse_log_jacobian(self, y):
  143. r"""
  144. .. math:
  145. f(y) = \frac{\log(e^{ky} - 1)}{k}
  146. f'(y) = \frac{e^{ky}}{e^{ky} - 1}
  147. \log(f'(y)) = ky - \log(e^{ky} - 1) = ky - f(ky)
  148. """
  149. y = self._check_value(y, 'value')
  150. sharpness_local = self.cast_param_by_value(y, self.sharpness)
  151. scaled_value = sharpness_local * y
  152. inverse_log_j = scaled_value - self.inverse_softplus(scaled_value)
  153. return inverse_log_j