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.

activation.py 7.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import numpy as np
  10. from ..functional import gelu, leaky_relu, prelu, relu, sigmoid, silu, softmax
  11. from ..tensor import Parameter
  12. from .module import Module
  13. class Softmax(Module):
  14. r"""Applies a softmax function. Softmax is defined as:
  15. .. math::
  16. \text{Softmax}(x_{i}) = \frac{exp(x_i)}{\sum_j exp(x_j)}
  17. It is applied to all elements along axis, and rescales elements so that
  18. they stay in the range `[0, 1]` and sum to 1.
  19. Args:
  20. axis: Along which axis softmax will be applied. By default,
  21. softmax will apply along the highest ranked axis.
  22. Examples:
  23. .. testcode::
  24. import numpy as np
  25. import megengine as mge
  26. import megengine.module as M
  27. data = mge.tensor(np.array([-2,-1,0,1,2]).astype(np.float32))
  28. softmax = M.Softmax()
  29. output = softmax(data)
  30. with np.printoptions(precision=6):
  31. print(output.numpy())
  32. Outputs:
  33. .. testoutput::
  34. [0.011656 0.031685 0.086129 0.234122 0.636409]
  35. """
  36. def __init__(self, axis=None, **kwargs):
  37. super().__init__(**kwargs)
  38. self.axis = axis
  39. def forward(self, inputs):
  40. return softmax(inputs, self.axis)
  41. def _module_info_string(self) -> str:
  42. return "axis={axis}".format(axis=self.axis)
  43. class Sigmoid(Module):
  44. r"""Applies the element-wise function:
  45. .. math::
  46. \text{Sigmoid}(x) = \frac{1}{1 + \exp(-x)}
  47. Examples:
  48. .. testcode::
  49. import numpy as np
  50. import megengine as mge
  51. import megengine.module as M
  52. data = mge.tensor(np.array([-2,-1,0,1,2,]).astype(np.float32))
  53. sigmoid = M.Sigmoid()
  54. output = sigmoid(data)
  55. with np.printoptions(precision=6):
  56. print(output.numpy())
  57. Outputs:
  58. .. testoutput::
  59. [0.119203 0.268941 0.5 0.731059 0.880797]
  60. """
  61. def forward(self, inputs):
  62. return sigmoid(inputs)
  63. class SiLU(Module):
  64. r"""Applies the element-wise function:
  65. .. math::
  66. \text{SiLU}(x) = \frac{x}{1 + \exp(-x)}
  67. Examples:
  68. .. testcode::
  69. import numpy as np
  70. import megengine as mge
  71. import megengine.module as M
  72. data = mge.tensor(np.array([-2,-1,0,1,2,]).astype(np.float32))
  73. silu = M.SiLU()
  74. output = silu(data)
  75. with np.printoptions(precision=6):
  76. print(output.numpy())
  77. Outputs:
  78. .. testoutput::
  79. [-0.238406 -0.268941 0. 0.731059 1.761594]
  80. """
  81. def forward(self, inputs):
  82. return silu(inputs)
  83. class GELU(Module):
  84. r"""Applies the element-wise function:
  85. .. math::
  86. \text{GELU}(x) = x\Phi(x)
  87. where :math:`\Phi(x)` is the Cumulative Distribution Function for Gaussian Distribution.
  88. Examples:
  89. .. testcode::
  90. import numpy as np
  91. import megengine as mge
  92. import megengine.module as M
  93. data = mge.tensor(np.array([-2,-1,0,1,2,]).astype(np.float32))
  94. gelu = M.GELU()
  95. output = gelu(data)
  96. with np.printoptions(precision=4):
  97. print(output.numpy())
  98. Outputs:
  99. .. testoutput::
  100. [-0.0455 -0.1587 0. 0.8413 1.9545]
  101. """
  102. def forward(self, inputs):
  103. return gelu(inputs)
  104. class ReLU(Module):
  105. r"""Applies the element-wise function:
  106. .. math::
  107. \text{ReLU}(x) = \max(x, 0)
  108. Examples:
  109. .. testcode::
  110. import numpy as np
  111. import megengine as mge
  112. import megengine.module as M
  113. data = mge.tensor(np.array([-2,-1,0,1,2,]).astype(np.float32))
  114. relu = M.ReLU()
  115. output = relu(data)
  116. with np.printoptions(precision=6):
  117. print(output.numpy())
  118. Outputs:
  119. .. testoutput::
  120. [0. 0. 0. 1. 2.]
  121. """
  122. def forward(self, x):
  123. return relu(x)
  124. class PReLU(Module):
  125. r"""Applies the element-wise function:
  126. .. math::
  127. \text{PReLU}(x) = \max(0,x) + a * \min(0,x)
  128. or
  129. .. math::
  130. \text{PReLU}(x) =
  131. \begin{cases}
  132. x, & \text{ if } x \geq 0 \\
  133. ax, & \text{ otherwise }
  134. \end{cases}
  135. Here :math:`a` is a learnable parameter. When called without arguments, `PReLU()` uses
  136. a single paramter :math:`a` across all input channel. If called with `PReLU(num_of_channels)`, each input channle will has it's own :math:`a`.
  137. Args:
  138. num_parameters: number of :math:`a` to learn, there is only two
  139. values are legitimate: 1, or the number of channels at input. Default: 1
  140. init: the initial value of :math:`a`. Default: 0.25
  141. Examples:
  142. .. testcode::
  143. import numpy as np
  144. import megengine as mge
  145. import megengine.module as M
  146. data = mge.tensor(np.array([-1.2, -3.7, 2.7]).astype(np.float32))
  147. prelu = M.PReLU()
  148. output = prelu(data)
  149. print(output.numpy())
  150. Outputs:
  151. .. testoutput::
  152. [-0.3 -0.925 2.7 ]
  153. """
  154. def __init__(self, num_parameters: int = 1, init: float = 0.25, **kwargs):
  155. super().__init__(**kwargs)
  156. self.num_parameters = num_parameters
  157. if num_parameters > 1:
  158. # Assume format is NCHW
  159. self.weight = Parameter(
  160. data=np.full((1, num_parameters, 1, 1), init, dtype=np.float32)
  161. )
  162. else:
  163. self.weight = Parameter(data=[init])
  164. def forward(self, inputs):
  165. return prelu(inputs, self.weight)
  166. class LeakyReLU(Module):
  167. r"""Applies the element-wise function:
  168. .. math::
  169. \text{LeakyReLU}(x) = \max(0,x) + negative\_slope \times \min(0,x)
  170. or
  171. .. math::
  172. \text{LeakyReLU}(x) =
  173. \begin{cases}
  174. x, & \text{ if } x \geq 0 \\
  175. negative\_slope \times x, & \text{ otherwise }
  176. \end{cases}
  177. Examples:
  178. .. testcode::
  179. import numpy as np
  180. import megengine as mge
  181. import megengine.module as M
  182. data = mge.tensor(np.array([-8, -12, 6, 10]).astype(np.float32))
  183. leakyrelu = M.LeakyReLU(0.01)
  184. output = leakyrelu(data)
  185. print(output.numpy())
  186. Outputs:
  187. .. testoutput::
  188. [-0.08 -0.12 6. 10. ]
  189. """
  190. def __init__(self, negative_slope: float = 0.01, **kwargs):
  191. super().__init__(**kwargs)
  192. self.negative_slope = negative_slope
  193. def forward(self, inputs):
  194. return leaky_relu(inputs, self.negative_slope)