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 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. """activation"""
  16. import numpy as np
  17. from mindspore.ops import operations as P
  18. from mindspore.ops import functional as F
  19. from mindspore.common.parameter import Parameter
  20. from mindspore.common.initializer import initializer
  21. from mindspore.common.tensor import Tensor
  22. from mindspore._extends import cell_attr_register
  23. from ..cell import Cell
  24. class Softmax(Cell):
  25. r"""
  26. Softmax activation function.
  27. Applies the Softmax function to an n-dimensional input Tensor.
  28. The input is a Tensor of logits transformed with exponential function and then
  29. normalized to lie in range [0, 1] and sum up to 1.
  30. Softmax is defined as:
  31. .. math::
  32. \text{softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_{j=0}^{n-1}\exp(x_j)},
  33. where :math:`x_{i}` is the :math:`i`-th slice along the given dim of the input Tensor.
  34. Args:
  35. axis (Union[int, tuple[int]]): The axis to apply Softmax operation, -1 means the last dimension. Default: -1.
  36. Inputs:
  37. - **x** (Tensor) - The input of Softmax.
  38. Outputs:
  39. Tensor, which has the same type and shape as `x` with values in the range[0,1].
  40. """
  41. def __init__(self, axis=-1):
  42. super(Softmax, self).__init__()
  43. self.softmax = P.Softmax(axis)
  44. def construct(self, x):
  45. return self.softmax(x)
  46. class LogSoftmax(Cell):
  47. r"""
  48. LogSoftmax activation function.
  49. Applies the LogSoftmax function to n-dimensional input tensor.
  50. The input is transformed with Softmax function and then with log function to lie in range[-inf,0).
  51. Logsoftmax is defined as:
  52. :math:`\text{logsoftmax}(x_i) = \log \left(\frac{\exp(x_i)}{\sum_{j=0}^{n-1} \exp(x_j)}\right)`,
  53. where :math:`x_{i}` is the :math:`i`-th slice along the given dim of the input Tensor.
  54. Args:
  55. axis (int): The axis to apply LogSoftmax operation, -1 means the last dimension. Default: -1.
  56. Inputs:
  57. - **x** (Tensor) - The input of LogSoftmax.
  58. Outputs:
  59. Tensor, which has the same type and shape as the input as `x` with values in the range[-inf,0).
  60. """
  61. def __init__(self, axis=-1):
  62. super(LogSoftmax, self).__init__()
  63. self.log_softmax = P.LogSoftmax(axis)
  64. def construct(self, x):
  65. return self.log_softmax(x)
  66. class ELU(Cell):
  67. r"""
  68. Exponential Linear Uint activation function.
  69. Applies the exponential linear unit function element-wise.
  70. The activation function defined as:
  71. .. math::
  72. E_{i} =
  73. \begin{cases}
  74. x, &\text{if } x \geq 0; \cr
  75. \text{alpha} * (\exp(x_i) - 1), &\text{otherwise.}
  76. \end{cases}
  77. Args:
  78. alpha (float): The coefficient of negative factor whose type is float. Default: 1.0.
  79. Inputs:
  80. - **input_data** (Tensor) - The input of ELU.
  81. Outputs:
  82. Tensor, with the same type and shape as the `input_data`.
  83. """
  84. def __init__(self, alpha=1.0):
  85. super(ELU, self).__init__()
  86. self.elu = P.Elu(alpha)
  87. def construct(self, x):
  88. return self.elu(x)
  89. class ReLU(Cell):
  90. r"""
  91. Rectified Linear Unit activation function.
  92. Applies the rectified linear unit function element-wise. It returns
  93. element-wise :math:`\max(0, x)`, specially, the neurons with the negative output
  94. will suppressed and the active neurons will stay the same.
  95. Inputs:
  96. - **input_data** (Tensor) - The input of ReLU.
  97. Outputs:
  98. Tensor, with the same type and shape as the `input_data`.
  99. """
  100. def __init__(self):
  101. super(ReLU, self).__init__()
  102. self.relu = P.ReLU()
  103. def construct(self, x):
  104. return self.relu(x)
  105. class ReLU6(Cell):
  106. r"""
  107. Compute ReLU6 activation function.
  108. ReLU6 is similar to ReLU with a upper limit of 6, which if the inputs are greater than 6, the outputs
  109. will be suppressed to 6.
  110. It computes element-wise as :math:`\min(\max(0, x), 6)`. The input is a Tensor of any valid shape.
  111. Inputs:
  112. - **input_data** (Tensor) - The input of ReLU6.
  113. Outputs:
  114. Tensor, which has the same type with `input_data`.
  115. """
  116. def __init__(self):
  117. super(ReLU6, self).__init__()
  118. self.relu6 = P.ReLU6()
  119. def construct(self, x):
  120. return self.relu6(x)
  121. class LeakyReLU(Cell):
  122. r"""
  123. Leaky ReLU activation function.
  124. LeakyReLU is similar to ReLU, but LeakyReLU has a slope that makes it not equal to 0 at x < 0.
  125. The activation function is defined as:
  126. .. math::
  127. \text{leaky_relu}(x) = \begin{cases}x, &\text{if } x \geq 0; \cr
  128. \text{alpha} * x, &\text{otherwise.}\end{cases}
  129. See https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf
  130. Args:
  131. alpha (float): Slope of the activation function at x < 0. Default: 0.2.
  132. Inputs:
  133. - **input_x** (Tensor) - The input of LeakyReLU.
  134. Outputs:
  135. Tensor, has the same type and shape with the `input_x`.
  136. """
  137. def __init__(self, alpha=0.2):
  138. super(LeakyReLU, self).__init__()
  139. self.greater_equal = P.GreaterEqual()
  140. self.mul = P.Mul()
  141. self.alpha = alpha
  142. def construct(self, x):
  143. alpha = P.Cast()(F.scalar_to_array(self.alpha), P.DType()(x))
  144. if self.alpha <= 1:
  145. out = P.Maximum()(alpha * x, x)
  146. else:
  147. out = P.Minimum()(alpha * x, x)
  148. return out
  149. class Tanh(Cell):
  150. r"""
  151. Tanh activation function.
  152. Applies the Tanh function element-wise, returns a new tensor with the hyperbolic tangent of the elements of input,
  153. The input is a Tensor with any valid shape.
  154. Tanh function is defined as:
  155. .. math::
  156. tanh(x_i) = \frac{\exp(x_i) - \exp(-x_i)}{\exp(x_i) + \exp(-x_i)} = \frac{\exp(2x_i) - 1}{\exp(2x_i) + 1},
  157. where :math:`x_i` is an element of the input Tensor.
  158. Inputs:
  159. - **input_data** (Tensor) - The input of Tanh.
  160. Outputs:
  161. Tensor, with the same type and shape as the `input_data`.
  162. """
  163. def __init__(self):
  164. super(Tanh, self).__init__()
  165. self.tanh = P.Tanh()
  166. def construct(self, x):
  167. return self.tanh(x)
  168. class GELU(Cell):
  169. r"""
  170. Gaussian error linear unit activation function.
  171. Applies GELU function to each element of the input. The input is a Tensor with any valid shape.
  172. GELU is defined as:
  173. :math:`GELU(x_i) = x_i*P(X < x_i)`, where :math:`P` is the cumulative distribution function
  174. of standard Gaussian distribution and :math:`x_i` is the element of the input.
  175. Inputs:
  176. - **input_data** (Tensor) - The input of Tanh.
  177. Outputs:
  178. Tensor, with the same type and shape as the `input_data`.
  179. """
  180. def __init__(self):
  181. super(GELU, self).__init__()
  182. self.gelu = P.Gelu()
  183. def construct(self, x):
  184. return self.gelu(x)
  185. class Sigmoid(Cell):
  186. r"""
  187. Sigmoid activation function.
  188. Applies sigmoid-type activation element-wise.
  189. Sigmoid function is defined as:
  190. :math:`\text{sigmoid}(x_i) = \frac{1}{1 + \exp(-x_i)}`, where :math:`x_i` is the element of the input.
  191. Inputs:
  192. - **input_data** (Tensor) - The input of Tanh.
  193. Outputs:
  194. Tensor, with the same type and shape as the `input_data`.
  195. """
  196. def __init__(self):
  197. super(Sigmoid, self).__init__()
  198. self.sigmoid = P.Sigmoid()
  199. def construct(self, x):
  200. return self.sigmoid(x)
  201. class PReLU(Cell):
  202. r"""
  203. PReLU activation function.
  204. Applies the PReLU function element-wise.
  205. PReLU is defined as: :math:`prelu(x_i)= \max(0, x_i) + w * \min(0, x_i)`, where :math:`x_i`
  206. is an element of an channel of the input.
  207. Here :math:`w` is an learnable parameter with default initial value 0.25.
  208. Parameter :math:`w` has dimensionality of the argument channel. If called without argument
  209. channel, a single parameter :math:`w` will be shared across all channels.
  210. Args:
  211. channel (int): The dimension of input. Default: 1.
  212. w (float): The initial value of w. Default: 0.25.
  213. Inputs:
  214. - **input_data** (Tensor) - The input of Tanh.
  215. Outputs:
  216. Tensor, with the same type and shape as the `input_data`.
  217. """
  218. @cell_attr_register(attrs="")
  219. def __init__(self, channel=1, w=0.25):
  220. super(PReLU, self).__init__()
  221. if isinstance(w, (np.float32, float)):
  222. tmp = np.empty((channel,), dtype=np.float32)
  223. tmp.fill(w)
  224. w = Tensor(tmp)
  225. elif isinstance(w, list):
  226. w = Tensor(w)
  227. if not isinstance(w, Tensor):
  228. raise TypeError("w only support np.float32, float or Tensor type.")
  229. self.w = Parameter(initializer(w, [channel]), name='a')
  230. self.prelu = P.PReLU()
  231. self.relu = P.ReLU()
  232. self.assign = P.Assign()
  233. def construct(self, x):
  234. u = self.relu(self.w)
  235. v = self.prelu(x, u)
  236. if self.training:
  237. self.assign(self.w, u)
  238. return v
  239. class HSwish(Cell):
  240. r"""
  241. rHard swish activation function.
  242. Applies hswish-type activation element-wise. The input is a Tensor with any valid shape.
  243. Hard swish is defined as:
  244. .. math::
  245. \text{hswish}(x_{i}) = x_{i} * \frac{ReLU6(x_{i} + 3)}{6},
  246. where :math:`x_{i}` is the :math:`i`-th slice along the given dim of the input Tensor.
  247. Inputs:
  248. - **input_data** (Tensor) - The input of HSwish.
  249. Outputs:
  250. Tensor, with the same type and shape as the `input_data`.
  251. """
  252. def __init__(self):
  253. super(HSwish, self).__init__()
  254. self.hswish = P.HSwish()
  255. def construct(self, x):
  256. return self.hswish(x)
  257. class HSigmoid(Cell):
  258. r"""
  259. Hard sigmoid activation function.
  260. Applies hard sigmoid activation element-wise. The input is a Tensor with any valid shape.
  261. Hard sigmoid is defined as:
  262. .. math::
  263. \text{hsigmoid}(x_{i}) = max(0, min(1, \frac{2 * x_{i} + 5}{10})),
  264. where :math:`x_{i}` is the :math:`i`-th slice along the given dim of the input Tensor.
  265. Inputs:
  266. - **input_data** (Tensor) - The input of HSigmoid.
  267. Outputs:
  268. Tensor, with the same type and shape as the `input_data`.
  269. """
  270. def __init__(self):
  271. super(HSigmoid, self).__init__()
  272. self.hsigmoid = P.HSigmoid()
  273. def construct(self, x):
  274. return self.hsigmoid(x)
  275. _activation = {
  276. 'softmax': Softmax,
  277. 'logsoftmax': LogSoftmax,
  278. 'relu': ReLU,
  279. 'relu6': ReLU6,
  280. 'tanh': Tanh,
  281. 'gelu': GELU,
  282. 'sigmoid': Sigmoid,
  283. 'prelu': PReLU,
  284. 'leakyrelu': LeakyReLU,
  285. 'hswish': HSwish,
  286. 'hsigmoid': HSigmoid,
  287. }
  288. def get_activation(name):
  289. """
  290. Gets the activation function.
  291. Args:
  292. name (str): The name of the activation function.
  293. Returns:
  294. Function, the activation function.
  295. Examples:
  296. >>> sigmoid = nn.get_activation('sigmoid')
  297. """
  298. if not name:
  299. return None
  300. if name not in _activation:
  301. raise KeyError("Unknown activation type")
  302. return _activation[name]()