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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. # Copyright 2020-2021 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._checkparam import Validator as validator
  18. from mindspore._extends import cell_attr_register
  19. from mindspore.common import dtype as mstype
  20. from mindspore.common.parameter import Parameter
  21. from mindspore.common.tensor import Tensor
  22. from mindspore.ops import functional as F
  23. from mindspore.ops import operations as P
  24. from ..cell import Cell
  25. __all__ = ['Softmax',
  26. 'LogSoftmax',
  27. 'ReLU',
  28. 'ReLU6',
  29. 'Tanh',
  30. 'GELU',
  31. 'FastGelu',
  32. 'Sigmoid',
  33. 'PReLU',
  34. 'get_activation',
  35. 'LeakyReLU',
  36. 'HSigmoid',
  37. 'HSwish',
  38. 'ELU',
  39. 'LogSigmoid',
  40. 'SoftShrink',
  41. 'HShrink',
  42. 'CELU',
  43. ]
  44. class CELU(Cell):
  45. r"""
  46. Continuously differentiable exponential linear units activation function.
  47. Applies the continuously differentiable exponential linear units function element-wise.
  48. .. math::
  49. \text{CELU}(x) = \max(0,x) + \min(0, \alpha * (\exp(x/\alpha) - 1))
  50. It returns element-wise :math:`\max(0,x) + \min(0, \alpha * (\exp(x/\alpha) - 1))`.
  51. The picture about CELU looks like this `CELU <https://arxiv.org/abs/1704.07483>`_.
  52. Args:
  53. alpha (float): The :math:`\alpha` value for the Celu formulation. Default: 1.0
  54. Inputs:
  55. - **x** (Tensor) - The input of CELU. The required dtype is float16 or float32.
  56. The shape is :math:`(N,*)` where :math:`*` means, any number of additional dimensions.
  57. Outputs:
  58. Tensor, with the same type and shape as the `x`.
  59. Raises:
  60. TypeError: If `alpha` is not a float.
  61. ValueError: If `alpha` has the value of 0.
  62. TypeError: If `x` is not a Tensor.
  63. TypeError: If the dtype of 'input_x' is neither float16 nor float32.
  64. Supported Platforms:
  65. ``Ascend``
  66. Examples:
  67. >>> x = Tensor(np.array([-2.0, -1.0, 1.0, 2.0]), mindspore.float32)
  68. >>> celu = nn.CELU()
  69. >>> output = celu(x)
  70. >>> print(output)
  71. [-0.86466473 -0.63212055 1. 2. ]
  72. """
  73. def __init__(self, alpha=1.0):
  74. """Initialize CELU."""
  75. super(CELU, self).__init__()
  76. self.celu = P.CeLU(alpha=alpha)
  77. def construct(self, x):
  78. return self.celu(x)
  79. class Softmax(Cell):
  80. r"""
  81. Softmax activation function.
  82. Applies the Softmax function to an n-dimensional input Tensor.
  83. The input is a Tensor of logits transformed with exponential function and then
  84. normalized to lie in range [0, 1] and sum up to 1.
  85. Softmax is defined as:
  86. .. math::
  87. \text{softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_{j=0}^{n-1}\exp(x_j)},
  88. where :math:`x_{i}` is the :math:`i`-th slice in the given dimension of the input Tensor.
  89. Args:
  90. axis (Union[int, tuple[int]]): The axis to apply Softmax operation, -1 means the last dimension. Default: -1.
  91. Inputs:
  92. - **x** (Tensor) - The input of Softmax with data type of float16 or float32.
  93. Outputs:
  94. Tensor, which has the same type and shape as `x` with values in the range[0,1].
  95. Raises:
  96. TypeError: If `axis` is neither an int nor a tuple.
  97. TypeError: If dtype of `x` is neither float16 nor float32.
  98. ValueError: If `axis` is a tuple whose length is less than 1.
  99. ValueError: If `axis` is a tuple whose elements are not all in range [-len(x), len(x)).
  100. Supported Platforms:
  101. ``Ascend`` ``GPU`` ``CPU``
  102. Examples:
  103. >>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
  104. >>> softmax = nn.Softmax()
  105. >>> output = softmax(x)
  106. >>> print(output)
  107. [0.03168 0.01166 0.0861 0.636 0.2341 ]
  108. """
  109. def __init__(self, axis=-1):
  110. """Initialize Softmax."""
  111. super(Softmax, self).__init__()
  112. self.softmax = P.Softmax(axis)
  113. def construct(self, x):
  114. return self.softmax(x)
  115. class LogSoftmax(Cell):
  116. r"""
  117. LogSoftmax activation function.
  118. Applies the LogSoftmax function to n-dimensional input tensor.
  119. The input is transformed by the Softmax function and then by the log function to lie in range[-inf,0).
  120. Logsoftmax is defined as:
  121. .. math::
  122. \text{logsoftmax}(x_i) = \log \left(\frac{\exp(x_i)}{\sum_{j=0}^{n-1} \exp(x_j)}\right),
  123. where :math:`x_{i}` is the :math:`i`-th slice in the given dimension of the input Tensor.
  124. Args:
  125. axis (int): The axis to apply LogSoftmax operation, -1 means the last dimension. Default: -1.
  126. Inputs:
  127. - **x** (Tensor) - The input of LogSoftmax, with float16 or float32 data type.
  128. Outputs:
  129. Tensor, which has the same type and shape as the input as `x` with values in the range[-inf,0).
  130. Raises:
  131. TypeError: If `axis` is not an int.
  132. TypeError: If dtype of `x` is neither float16 nor float32.
  133. ValueError: If `axis` is not in range [-len(x), len(x)).
  134. Supported Platforms:
  135. ``Ascend`` ``GPU`` ``CPU``
  136. Examples:
  137. >>> x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
  138. >>> log_softmax = nn.LogSoftmax()
  139. >>> output = log_softmax(x)
  140. >>> print(output)
  141. [[-5.00672150e+00 -6.72150636e-03 -1.20067215e+01]
  142. [-7.00091219e+00 -1.40009127e+01 -9.12250078e-04]]
  143. """
  144. def __init__(self, axis=-1):
  145. """Initialize LogSoftmax."""
  146. super(LogSoftmax, self).__init__()
  147. self.log_softmax = P.LogSoftmax(axis)
  148. def construct(self, x):
  149. return self.log_softmax(x)
  150. class ELU(Cell):
  151. r"""
  152. Exponential Linear Uint activation function.
  153. Applies the exponential linear unit function element-wise.
  154. The activation function is defined as:
  155. .. math::
  156. E_{i} =
  157. \begin{cases}
  158. x, &\text{if } x \geq 0; \cr
  159. \text{alpha} * (\exp(x_i) - 1), &\text{otherwise.}
  160. \end{cases}
  161. The picture about ELU looks like this `ELU <https://en.wikipedia.org/wiki/
  162. Activation_function#/media/File:Activation_elu.svg>`_.
  163. Args:
  164. alpha (float): The coefficient of negative factor whose type is float. Default: 1.0.
  165. Inputs:
  166. - **x** (Tensor) - The input of ELU with data type of float16 or float32.
  167. The shape is :math:`(N,*)` where :math:`*` means,any number of additional dimensions.
  168. Outputs:
  169. Tensor, with the same type and shape as the `x`.
  170. Raises:
  171. TypeError: If `alpha` is not a float.
  172. TypeError: If dtype of `x` is neither float16 nor float32.
  173. ValueError: If `alpha` is not equal to 1.0.
  174. Supported Platforms:
  175. ``Ascend`` ``GPU`` ``CPU``
  176. Examples:
  177. >>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float32)
  178. >>> elu = nn.ELU()
  179. >>> result = elu(x)
  180. >>> print(result)
  181. [-0.63212055 -0.86466473 0. 2. 1.]
  182. """
  183. def __init__(self, alpha=1.0):
  184. """Initialize ELU."""
  185. super(ELU, self).__init__()
  186. self.elu = P.Elu(alpha)
  187. def construct(self, x):
  188. return self.elu(x)
  189. class ReLU(Cell):
  190. r"""
  191. Rectified Linear Unit activation function.
  192. Applies the rectified linear unit function element-wise.
  193. .. math::
  194. \text{ReLU}(x) = (x)^+ = \max(0, x),
  195. It returns element-wise :math:`\max(0, x)`, specially, the neurons with the negative output
  196. will be suppressed and the active neurons will stay the same.
  197. The picture about ReLU looks like this `ReLU <https://en.wikipedia.org/wiki/
  198. Activation_function#/media/File:Activation_rectified_linear.svg>`_.
  199. Inputs:
  200. - **x** (Tensor) - The input of ReLU. The data type is Number.
  201. The shape is :math:`(N,*)` where :math:`*` means, any number of additional dimensions.
  202. Outputs:
  203. Tensor, with the same type and shape as the `x`.
  204. Raises:
  205. TypeError: If dtype of `x` is not a number.
  206. Supported Platforms:
  207. ``Ascend`` ``GPU`` ``CPU``
  208. Examples:
  209. >>> x = Tensor(np.array([-1, 2, -3, 2, -1]), mindspore.float16)
  210. >>> relu = nn.ReLU()
  211. >>> output = relu(x)
  212. >>> print(output)
  213. [0. 2. 0. 2. 0.]
  214. """
  215. def __init__(self):
  216. """Initialize ReLU."""
  217. super(ReLU, self).__init__()
  218. self.relu = P.ReLU()
  219. def construct(self, x):
  220. return self.relu(x)
  221. class ReLU6(Cell):
  222. r"""
  223. Compute ReLU6 activation function.
  224. ReLU6 is similar to ReLU with a upper limit of 6, which if the inputs are greater than 6, the outputs
  225. will be suppressed to 6.
  226. It computes element-wise as
  227. .. math::
  228. \min(\max(0, x), 6).
  229. The input is a Tensor of any valid shape.
  230. Inputs:
  231. - **x** (Tensor) - The input of ReLU6 with data type of float16 or float32.
  232. The shape is :math:`(N,*)` where :math:`*` means, any number of additional dimensions.
  233. Outputs:
  234. Tensor, which has the same type as `x`.
  235. Raises:
  236. TypeError: If dtype of `x` is neither float16 nor float32.
  237. Supported Platforms:
  238. ``Ascend`` ``GPU`` ``CPU``
  239. Examples:
  240. >>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
  241. >>> relu6 = nn.ReLU6()
  242. >>> output = relu6(x)
  243. >>> print(output)
  244. [0. 0. 0. 2. 1.]
  245. """
  246. def __init__(self):
  247. """Initialize ReLU6."""
  248. super(ReLU6, self).__init__()
  249. self.relu6 = P.ReLU6()
  250. def construct(self, x):
  251. return self.relu6(x)
  252. class LeakyReLU(Cell):
  253. r"""
  254. Leaky ReLU activation function.
  255. LeakyReLU is similar to ReLU, but LeakyReLU has a slope that makes it not equal to 0 at x < 0.
  256. The activation function is defined as:
  257. .. math::
  258. \text{leaky_relu}(x) = \begin{cases}x, &\text{if } x \geq 0; \cr
  259. \text{alpha} * x, &\text{otherwise.}\end{cases}
  260. See https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf
  261. Args:
  262. alpha (Union[int, float]): Slope of the activation function at x < 0. Default: 0.2.
  263. Inputs:
  264. - **x** (Tensor) - The input of LeakyReLU.
  265. The shape is :math:`(N,*)` where :math:`*` means, any number of additional dimensions.
  266. Outputs:
  267. Tensor, has the same type and shape as the `x`.
  268. Raises:
  269. TypeError: If `alpha` is not a float or an int.
  270. Supported Platforms:
  271. ``Ascend`` ``GPU`` ``CPU``
  272. Examples:
  273. >>> x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
  274. >>> leaky_relu = nn.LeakyReLU()
  275. >>> output = leaky_relu(x)
  276. >>> print(output)
  277. [[-0.2 4. -1.6]
  278. [ 2. -1. 9. ]]
  279. """
  280. def __init__(self, alpha=0.2):
  281. """Initialize LeakyReLU."""
  282. super(LeakyReLU, self).__init__()
  283. validator.check_value_type('alpha', alpha, [float, int], self.cls_name)
  284. self.greater_equal = P.GreaterEqual()
  285. self.mul = P.Mul()
  286. self.alpha = alpha
  287. self.select_op = P.Maximum()
  288. if self.alpha > 1:
  289. self.select_op = P.Minimum()
  290. def construct(self, x):
  291. alpha_array = P.Cast()(F.scalar_to_array(self.alpha), P.DType()(x))
  292. out = self.select_op(alpha_array * x, x)
  293. return out
  294. class Tanh(Cell):
  295. r"""
  296. Tanh activation function.
  297. Applies the Tanh function element-wise, returns a new tensor with the hyperbolic tangent of the elements of input,
  298. The input is a Tensor with any valid shape.
  299. Tanh function is defined as:
  300. .. math::
  301. 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},
  302. where :math:`x_i` is an element of the input Tensor.
  303. Inputs:
  304. - **x** (Tensor) - The input of Tanh with data type of float16 or float32.
  305. The shape is :math:`(N,*)` where :math:`*` means, any number of additional dimensions.
  306. Outputs:
  307. Tensor, with the same type and shape as the `x`.
  308. Raises:
  309. TypeError: If dtype of `x` is neither float16 nor float32.
  310. Supported Platforms:
  311. ``Ascend`` ``GPU`` ``CPU``
  312. Examples:
  313. >>> x = Tensor(np.array([1, 2, 3, 2, 1]), mindspore.float16)
  314. >>> tanh = nn.Tanh()
  315. >>> output = tanh(x)
  316. >>> print(output)
  317. [0.7617 0.964 0.995 0.964 0.7617]
  318. """
  319. def __init__(self):
  320. """Initialize Tanh."""
  321. super(Tanh, self).__init__()
  322. self.tanh = P.Tanh()
  323. def construct(self, x):
  324. return self.tanh(x)
  325. class GELU(Cell):
  326. r"""
  327. Gaussian error linear unit activation function.
  328. Applies GELU function to each element of the input. The input is a Tensor with any valid shape.
  329. GELU is defined as:
  330. .. math::
  331. GELU(x_i) = x_i*P(X < x_i),
  332. where :math:`P` is the cumulative distribution function
  333. of standard Gaussian distribution and :math:`x_i` is the element of the input.
  334. The picture about GELU looks like this `GELU <https://en.wikipedia.org/wiki/
  335. Activation_function#/media/File:Activation_gelu.png>`_.
  336. Args:
  337. approximate (bool): Whether to enable approximation. Default: True.
  338. If approximate is True, The gaussian error linear activation is:
  339. :math:`0.5 * x * (1 + tanh(sqrt(2 / pi) * (x + 0.044715 * x^3)))`
  340. else, it is:
  341. :math:`x * P(X <= x) = 0.5 * x * (1 + erf(x / sqrt(2)))`, where P(X) ~ N(0, 1).
  342. Inputs:
  343. - **x** (Tensor) - The input of GELU with data type of float16 or float32.
  344. The shape is :math:`(N,*)` where :math:`*` means, any number of additional dimensions.
  345. Outputs:
  346. Tensor, with the same type and shape as the `x`.
  347. Raises:
  348. TypeError: If dtype of `x` is neither float16 nor float32.
  349. Supported Platforms:
  350. ``Ascend`` ``GPU`` ``CPU``
  351. Examples:
  352. >>> x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
  353. >>> gelu = nn.GELU()
  354. >>> output = gelu(x)
  355. >>> print(output)
  356. [[-1.5880802e-01 3.9999299e+00 -3.1077917e-21]
  357. [ 1.9545976e+00 -2.2918017e-07 9.0000000e+00]]
  358. >>> gelu = nn.GELU(approximate=False)
  359. >>> output = gelu(x)
  360. >>> print(output)
  361. [[-1.5865526e-01 3.9998732e+00 -0.0000000e+00]
  362. [ 1.9544997e+00 -1.4901161e-06 9.0000000e+00]]
  363. """
  364. def __init__(self, approximate=True):
  365. """Initialize GELU."""
  366. super(GELU, self).__init__()
  367. validator.check_bool(approximate, 'approximate', self.cls_name)
  368. self.approximate = approximate
  369. if self.approximate:
  370. self.gelu = P.GeLU()
  371. else:
  372. self.erf = P.Erf()
  373. self.sqrt = P.Sqrt()
  374. self.const0 = Tensor(0.5, mstype.float32)
  375. self.const1 = Tensor(1.0, mstype.float32)
  376. self.const2 = Tensor(2.0, mstype.float32)
  377. def construct(self, x):
  378. if self.approximate:
  379. return self.gelu(x)
  380. return x * F.cast(self.const0, x.dtype) * (F.cast(self.const1, x.dtype) + \
  381. self.erf(x / self.sqrt(F.cast(self.const2, x.dtype))))
  382. class FastGelu(Cell):
  383. r"""
  384. Fast Gaussian error linear unit activation function.
  385. Applies FastGelu function to each element of the input. The input is a Tensor with any valid shape.
  386. FastGelu is defined as:
  387. .. math::
  388. FastGelu(x_i) = \frac {x_i} {1 + \exp(-1.702 * \left| x_i \right|)} *
  389. \exp(0.851 * (x_i - \left| x_i \right|))
  390. where :math:`x_i` is the element of the input.
  391. Inputs:
  392. - **x** (Tensor) - The input of FastGelu with data type of float16 or float32.
  393. The shape is :math:`(N,*)` where :math:`*` means, any number of additional dimensions.
  394. Outputs:
  395. Tensor, with the same type and shape as the `x`.
  396. Raises:
  397. TypeError: If dtype of `x` is neither float16 nor float32.
  398. Supported Platforms:
  399. ``Ascend``
  400. Examples:
  401. >>> x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
  402. >>> fast_gelu = nn.FastGelu()
  403. >>> output = fast_gelu(x)
  404. >>> print(output)
  405. [[-1.5418735e-01 3.9921875e+00 -9.7473649e-06]
  406. [ 1.9375000e+00 -1.0052517e-03 8.9824219e+00]]
  407. """
  408. def __init__(self):
  409. """Initialize FastGelu."""
  410. super(FastGelu, self).__init__()
  411. self.fast_gelu = P.FastGeLU()
  412. def construct(self, x):
  413. return self.fast_gelu(x)
  414. class Sigmoid(Cell):
  415. r"""
  416. Sigmoid activation function.
  417. Applies sigmoid-type activation element-wise.
  418. Sigmoid function is defined as:
  419. .. math::
  420. \text{sigmoid}(x_i) = \frac{1}{1 + \exp(-x_i)},
  421. where :math:`x_i` is the element of the input.
  422. The picture about Sigmoid looks like this `Sigmoid <https://en.wikipedia.org/wiki/
  423. Sigmoid_function#/media/File:Logistic-curve.svg>`_.
  424. Inputs:
  425. - **x** (Tensor) - The input of Sigmoid with data type of float16 or float32.
  426. The shape is :math:`(N,*)` where :math:`*` means, any number of additional dimensions.
  427. Outputs:
  428. Tensor, with the same type and shape as the `x`.
  429. Raises:
  430. TypeError: If dtype of `x` is neither float16 nor float32.
  431. Supported Platforms:
  432. ``Ascend`` ``GPU`` ``CPU``
  433. Examples:
  434. >>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
  435. >>> sigmoid = nn.Sigmoid()
  436. >>> output = sigmoid(x)
  437. >>> print(output)
  438. [0.2688 0.11914 0.5 0.881 0.7305 ]
  439. """
  440. def __init__(self):
  441. """Initialize Sigmoid."""
  442. super(Sigmoid, self).__init__()
  443. self.sigmoid = P.Sigmoid()
  444. def construct(self, x):
  445. return self.sigmoid(x)
  446. class PReLU(Cell):
  447. r"""
  448. PReLU activation function.
  449. Applies the PReLU function element-wise.
  450. PReLU is defined as:
  451. .. math::
  452. prelu(x_i)= \max(0, x_i) + w * \min(0, x_i),
  453. where :math:`x_i` is an element of an channel of the input.
  454. Here :math:`w` is a learnable parameter with a default initial value 0.25.
  455. Parameter :math:`w` has dimensionality of the argument channel. If called without argument
  456. channel, a single parameter :math:`w` will be shared across all channels.
  457. The picture about PReLU looks like this `PReLU <https://en.wikipedia.org/wiki/
  458. Activation_function#/media/File:Activation_prelu.svg>`_.
  459. Args:
  460. channel (int): The elements number of parameter.
  461. It could be an int, and the value is 1 or the channels number of input tensor `x`. Default: 1.
  462. w (Union[float, list, Tensor]): The initial value of parameter. It could be a float, a float list or
  463. a tensor has the same dtype as the input tensor `x`. Default: 0.25.
  464. Inputs:
  465. - **x** (Tensor) - The input of PReLU with data type of float16 or float32.
  466. The shape is :math:`(N,*)` where :math:`*` means, any number of additional dimensions.
  467. Outputs:
  468. Tensor, with the same dtype and shape as the `x`.
  469. Raises:
  470. TypeError: If `channel` is not an int.
  471. TypeError: If `w` is not one of a float, a float list, a float Tensor.
  472. TypeError: If dtype of `x` is neither float16 nor float32.
  473. ValueError: If the `x` is a 0-D or 1-D Tensor on Ascend.
  474. ValueError: If `channel` is less than 1.
  475. Supported Platforms:
  476. ``Ascend`` ``GPU``
  477. Examples:
  478. >>> x = Tensor(np.array([[[[0.1, 0.6], [0.9, 0.9]]]]), mindspore.float32)
  479. >>> prelu = nn.PReLU()
  480. >>> output = prelu(x)
  481. >>> print(output)
  482. [[[[0.1 0.6]
  483. [0.9 0.9]]]]
  484. """
  485. @cell_attr_register(attrs="")
  486. def __init__(self, channel=1, w=0.25):
  487. """Initialize PReLU."""
  488. super(PReLU, self).__init__()
  489. validator.check_positive_int(channel, 'channel', self.cls_name)
  490. if isinstance(w, (float, np.float32)):
  491. tmp = np.empty((channel,), dtype=np.float32)
  492. tmp.fill(w)
  493. w = Tensor(tmp, dtype=mstype.float32)
  494. elif isinstance(w, list):
  495. if len(w) != channel:
  496. raise ValueError(f"For '{self.cls_name}', the length of 'w' should be equal to the 'channel' when "
  497. f"the 'w' is a list, but got the length of 'w': {len(w)}, the 'channel': {channel}.")
  498. for i in w:
  499. if not isinstance(i, (float, np.float32)):
  500. raise ValueError(f"For '{self.cls_name}', all elements in 'w' should be "
  501. f"float when the 'w' is a list, but got {i}.")
  502. w = Tensor(w, dtype=mstype.float32)
  503. elif isinstance(w, Tensor):
  504. if w.dtype not in (mstype.float16, mstype.float32):
  505. raise ValueError(f"For '{self.cls_name}', the dtype of 'w' should be float16 or "
  506. f"float32 when the 'w' is a tensor, but got {w.dtype}.")
  507. if len(w.shape) != 1 or w.shape[0] != channel:
  508. raise ValueError(f"For '{self.cls_name}', the dimension of 'w' should be 1, and the elements number "
  509. f"should be equal to the 'channel' when the 'w' is a tensor, "
  510. f"but got 'w' shape {w.shape}, the 'channel' {channel}.")
  511. else:
  512. raise TypeError(f"For '{self.cls_name}', the 'w' only supported float, list and tensor, "
  513. f"but got {type(w).__name__}.")
  514. self.w = Parameter(w, name='a')
  515. self.prelu = P.PReLU()
  516. self.relu = P.ReLU()
  517. self.assign = P.Assign()
  518. def construct(self, x):
  519. u = self.relu(self.w)
  520. v = self.prelu(x, F.cast(u, x.dtype))
  521. if self.training:
  522. self.assign(self.w, u)
  523. return v
  524. class HSwish(Cell):
  525. r"""
  526. Hard swish activation function.
  527. Applies hswish-type activation element-wise. The input is a Tensor with any valid shape.
  528. Hard swish is defined as:
  529. .. math::
  530. \text{hswish}(x_{i}) = x_{i} * \frac{ReLU6(x_{i} + 3)}{6},
  531. where :math:`x_{i}` is the :math:`i`-th slice in the given dimension of the input Tensor.
  532. Inputs:
  533. - **x** (Tensor) - The input of HSwish, data type must be float16 or float32.
  534. The shape is :math:`(N,*)` where :math:`*` means, any number of additional dimensions.
  535. Outputs:
  536. Tensor, with the same type and shape as the `x`.
  537. Raises:
  538. TypeError: If dtype of `x` is neither float16 nor float32.
  539. Supported Platforms:
  540. ``GPU`` ``CPU``
  541. Examples:
  542. >>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
  543. >>> hswish = nn.HSwish()
  544. >>> result = hswish(x)
  545. >>> print(result)
  546. [-0.3333 -0.3333 0 1.666 0.6665]
  547. """
  548. def __init__(self):
  549. """Initialize HSwish."""
  550. super(HSwish, self).__init__()
  551. self.hswish = P.HSwish()
  552. def construct(self, x):
  553. return self.hswish(x)
  554. class HSigmoid(Cell):
  555. r"""
  556. Hard sigmoid activation function.
  557. Applies hard sigmoid activation element-wise. The input is a Tensor with any valid shape.
  558. Hard sigmoid is defined as:
  559. .. math::
  560. \text{hsigmoid}(x_{i}) = max(0, min(1, \frac{x_{i} + 3}{6})),
  561. where :math:`x_{i}` is the :math:`i`-th slice in the given dimension of the input Tensor.
  562. Inputs:
  563. - **input_x** (Tensor) - The input of HSigmoid. The shape is :math:`(N,*)` where :math:`*` means, any number of
  564. additional dimensions.
  565. Outputs:
  566. Tensor, with the same type and shape as the `input_x`.
  567. Raises:
  568. TypeError: If `input_x` is not a Tensor.
  569. Supported Platforms:
  570. ``Ascend`` ``GPU`` ``CPU``
  571. Examples:
  572. >>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
  573. >>> hsigmoid = nn.HSigmoid()
  574. >>> result = hsigmoid(x)
  575. >>> print(result)
  576. [0.3333 0.1666 0.5 0.8335 0.6665]
  577. """
  578. def __init__(self):
  579. """Initialize HSigmoid."""
  580. super(HSigmoid, self).__init__()
  581. self.hsigmoid = P.HSigmoid()
  582. def construct(self, input_x):
  583. return self.hsigmoid(input_x)
  584. class LogSigmoid(Cell):
  585. r"""
  586. Logsigmoid activation function.
  587. Applies logsigmoid activation element-wise. The input is a Tensor with any valid shape.
  588. Logsigmoid is defined as:
  589. .. math::
  590. \text{logsigmoid}(x_{i}) = log(\frac{1}{1 + \exp(-x_i)}),
  591. where :math:`x_{i}` is the element of the input.
  592. Inputs:
  593. - **x** (Tensor) - The input of LogSigmoid with data type of float16 or float32.
  594. The shape is :math:`(N,*)` where :math:`*` means, any number of additional dimensions.
  595. Outputs:
  596. Tensor, with the same type and shape as the `x`.
  597. Raises:
  598. TypeError: If dtype of `x` is neither float16 nor float32.
  599. Supported Platforms:
  600. ``Ascend`` ``GPU`` ``CPU``
  601. Examples:
  602. >>> net = nn.LogSigmoid()
  603. >>> x = Tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32)
  604. >>> output = net(x)
  605. >>> print(output)
  606. [-0.31326166 -0.12692806 -0.04858734]
  607. """
  608. def __init__(self):
  609. """Initialize LogSigmoid."""
  610. super(LogSigmoid, self).__init__()
  611. self.mul = P.Mul()
  612. self.exp = P.Exp()
  613. self.add = P.Add()
  614. self.rec = P.Reciprocal()
  615. self.log = P.Log()
  616. def construct(self, input_x):
  617. neg_input = self.mul(input_x, -1)
  618. exp_neg_input = self.exp(neg_input)
  619. exp_neg_input_1 = self.add(exp_neg_input, 1)
  620. rec_exp_neg_input_1 = self.rec(exp_neg_input_1)
  621. ret = self.log(rec_exp_neg_input_1)
  622. return ret
  623. class SoftShrink(Cell):
  624. r"""
  625. Applies the soft shrinkage function elementwise.
  626. .. math::
  627. \text{SoftShrink}(x) =
  628. \begin{cases}
  629. x - \lambda, & \text{ if } x > \lambda \\
  630. x + \lambda, & \text{ if } x < -\lambda \\
  631. 0, & \text{ otherwise }
  632. \end{cases}
  633. Args:
  634. lambd: the :math:`\lambda` must be no less than zero value for the Softshrink formulation. Default: 0.5.
  635. Inputs:
  636. - **input_x** (Tensor) - The input of SoftShrink with data type of float16 or float32.
  637. Any number of additional dimensions.
  638. Outputs:
  639. Tensor, has the same shape and data type as `input_x`.
  640. Raises:
  641. TypeError: If lambd is not a float.
  642. TypeError: If input_x is not a Tensor.
  643. TypeError: If dtype of input_x is neither float16 nor float32.
  644. ValueError: If lambd is less than 0.
  645. Supported Platforms:
  646. ``Ascend``
  647. Examples:
  648. >>> input_x = Tensor(np.array([[ 0.5297, 0.7871, 1.1754], [ 0.7836, 0.6218, -1.1542]]), mstype.float16)
  649. >>> softshrink = nn.SoftShrink()
  650. >>> output = softshrink(input_x)
  651. >>> print(output)
  652. [[ 0.02979 0.287 0.676 ]
  653. [ 0.2837 0.1216 -0.6543 ]]
  654. """
  655. def __init__(self, lambd=0.5):
  656. super(SoftShrink, self).__init__()
  657. self.softshrink = P.SoftShrink(lambd)
  658. def construct(self, input_x):
  659. output = self.softshrink(input_x)
  660. return output
  661. class HShrink(Cell):
  662. r"""
  663. Applies the hard shrinkage function element-wise, each element complies the follow function:
  664. .. math::
  665. \text{HardShrink}(x) =
  666. \begin{cases}
  667. x, & \text{ if } x > \lambda \\
  668. x, & \text{ if } x < -\lambda \\
  669. 0, & \text{ otherwise }
  670. \end{cases}
  671. Args:
  672. lambd (float): The value for the HardShrink formulation. Default: 0.5
  673. Inputs:
  674. - **input_x** (Tensor) - The input of HardShrink with data type of float16 or float32.
  675. Outputs:
  676. Tensor, the same shape and data type as the input.
  677. Supported Platforms:
  678. ``Ascend``
  679. Raises:
  680. TypeError: If `lambd` is not a float.
  681. TypeError: If dtype of `input_x` is neither float16 nor float32.
  682. Examples:
  683. >>> input_x = Tensor(np.array([[ 0.5, 1, 2.0],[0.0533,0.0776,-2.1233]]),mstype.float32)
  684. >>> hshrink = nn.HShrink()
  685. >>> output = hshrink(input_x)
  686. >>> print(output)
  687. [[ 0. 1. 2. ]
  688. [ 0. 0. -2.1233]]
  689. """
  690. def __init__(self, lambd=0.5):
  691. super(HShrink, self).__init__()
  692. self.hshrink = P.HShrink(lambd)
  693. def construct(self, input_x):
  694. return self.hshrink(input_x)
  695. _activation = {
  696. 'softmax': Softmax,
  697. 'logsoftmax': LogSoftmax,
  698. 'relu': ReLU,
  699. 'relu6': ReLU6,
  700. 'tanh': Tanh,
  701. 'gelu': GELU,
  702. 'fast_gelu': FastGelu,
  703. 'elu': ELU,
  704. 'sigmoid': Sigmoid,
  705. 'prelu': PReLU,
  706. 'leakyrelu': LeakyReLU,
  707. 'hswish': HSwish,
  708. 'hsigmoid': HSigmoid,
  709. 'logsigmoid': LogSigmoid,
  710. 'softshrink': SoftShrink,
  711. 'hshrink': HShrink,
  712. }
  713. def get_activation(name, prim_name=None):
  714. """
  715. Gets the activation function.
  716. Args:
  717. name (str): The name of the activation function.
  718. prim_name (Union[str, None]): The name of primitive. Default: None.
  719. Returns:
  720. Function, the activation function.
  721. Supported Platforms:
  722. ``Ascend`` ``GPU`` ``CPU``
  723. Examples:
  724. >>> sigmoid = nn.get_activation('sigmoid')
  725. >>> print(sigmoid)
  726. Sigmoid<>
  727. """
  728. msg_prefix = f"For '{prim_name}', the" if prim_name else "The"
  729. if name is None:
  730. return None
  731. if name not in _activation:
  732. raise KeyError(f"{msg_prefix} 'name' should be in {list(_activation.keys())}, but got {name}.")
  733. return _activation[name]()