activation.py 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  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. # TODO: define activation functions of neural network
  15. from paddle.framework import get_default_dtype
  16. from .. import functional as F
  17. from ..initializer import Constant
  18. from .layers import Layer
  19. __all__ = []
  20. class CELU(Layer):
  21. r"""
  22. CELU Activation.
  23. .. math::
  24. CELU(x) = max(0, x) + min(0, \alpha * (e^{x/\alpha}-1))
  25. Parameters:
  26. alpha (float, optional): The 'alpha' value of the CELU formulation. Default is 1.0.
  27. name (str, optional): Name for the operation (optional, default is None).
  28. For more information, please refer to :ref:`api_guide_Name`.
  29. Shape:
  30. - input: Tensor with any shape.
  31. - output: Tensor with the same shape as input.
  32. Examples:
  33. .. code-block:: python
  34. >>> import paddle
  35. >>> x = paddle.to_tensor([[-1. ,6.], [1., 15.6]])
  36. >>> m = paddle.nn.CELU(0.2)
  37. >>> out = m(x)
  38. >>> print(out)
  39. Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
  40. [[-0.19865242, 6. ],
  41. [ 1. , 15.60000038]])
  42. """
  43. def __init__(self, alpha=1.0, name=None):
  44. super().__init__()
  45. self._alpha = alpha
  46. self._name = name
  47. def forward(self, x):
  48. return F.celu(x, self._alpha, self._name)
  49. def extra_repr(self):
  50. name_str = f', name={self._name}' if self._name else ''
  51. return f'alpha={self._alpha}{name_str}'
  52. class ELU(Layer):
  53. r"""
  54. ELU Activation.
  55. .. math::
  56. ELU(x)=
  57. \left\{
  58. \begin{array}{lcl}
  59. x,& &\text{if } \ x > 0 \\
  60. alpha * (e^{x} - 1),& &\text{if } \ x <= 0
  61. \end{array}
  62. \right.
  63. Parameters:
  64. alpha (float, optional): The 'alpha' value of the ELU formulation. Default is 1.0.
  65. name (str, optional): Name for the operation (optional, default is None).
  66. For more information, please refer to :ref:`api_guide_Name`.
  67. Shape:
  68. - input: Tensor with any shape.
  69. - output: Tensor with the same shape as input.
  70. Examples:
  71. .. code-block:: python
  72. >>> import paddle
  73. >>> x = paddle.to_tensor([[-1. ,6.], [1., 15.6]])
  74. >>> m = paddle.nn.ELU(0.2)
  75. >>> out = m(x)
  76. >>> print(out)
  77. Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
  78. [[-0.12642412, 6. ],
  79. [ 1. , 15.60000038]])
  80. """
  81. def __init__(self, alpha=1.0, name=None):
  82. super().__init__()
  83. self._alpha = alpha
  84. self._name = name
  85. def forward(self, x):
  86. return F.elu(x, self._alpha, self._name)
  87. def extra_repr(self):
  88. name_str = f', name={self._name}' if self._name else ''
  89. return f'alpha={self._alpha}{name_str}'
  90. class GLU(Layer):
  91. r"""
  92. GLU Activation.
  93. .. math::
  94. GLU(a, b) = a \otimes \sigma(b) where :math:`a` is the first half of the input matrices and :math:`b` is the second half.
  95. Parameters:
  96. axis (int, optional): The axis along which split the input tensor. It
  97. should be in range [-D, D), where D is the dimensions of ``x`` .
  98. If ``axis`` < 0, it works the same way as :math:`axis + D` .
  99. Default is -1.
  100. name (str, optional): Name for the operation (optional, default is None).
  101. For more information, please refer to :ref:`api_guide_Name`.
  102. Shape:
  103. - input: Tensor which the size of the given axis is even.
  104. - output: Tensor which the size of the given axis is halved.
  105. Examples:
  106. .. code-block:: python
  107. >>> import paddle
  108. >>> x = paddle.to_tensor(
  109. ... [[-0.22014759, -1.76358426, 0.80566144, 0.04241343],
  110. ... [-1.94900405, -1.89956081, 0.17134808, -1.11280477]]
  111. ... )
  112. >>> m = paddle.nn.GLU()
  113. >>> out = m(x)
  114. >>> print(out)
  115. Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
  116. [[-0.15216254, -0.90048921],
  117. [-1.05778778, -0.46985325]])
  118. """
  119. def __init__(self, axis=-1, name=None):
  120. super().__init__()
  121. self._axis = axis
  122. self._name = name
  123. def forward(self, x):
  124. return F.glu(x, self._axis, self._name)
  125. def extra_repr(self):
  126. name_str = f', name={self._name}' if self._name else ''
  127. return f'axis={self._axis}{name_str}'
  128. class GELU(Layer):
  129. r"""
  130. GELU Activation.
  131. If approximate is True
  132. .. math::
  133. GELU(x) = 0.5 * x * (1 + tanh(\sqrt{\frac{2}{\pi}} * (x + 0.044715x^{3})))
  134. else
  135. .. math::
  136. GELU(x) = 0.5 * x * (1 + erf(\frac{x}{\sqrt{2}}))
  137. Parameters:
  138. approximate (bool, optional): Wether to enable approximation. Default is False.
  139. name (str, optional): Name for the operation (optional, default is None).
  140. For more information, please refer to :ref:`api_guide_Name`.
  141. Shape:
  142. - input: Tensor with any shape.
  143. - output: Tensor with the same shape as input.
  144. Examples:
  145. .. code-block:: python
  146. >>> import paddle
  147. >>> x = paddle.to_tensor([[-1, 0.5],[1, 1.5]])
  148. >>> m = paddle.nn.GELU()
  149. >>> out = m(x)
  150. >>> print(out)
  151. Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
  152. [[-0.15865529, 0.34573123],
  153. [ 0.84134471, 1.39978933]])
  154. >>> m = paddle.nn.GELU(True)
  155. >>> out = m(x)
  156. >>> print(out)
  157. Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
  158. [[-0.15880796, 0.34571400],
  159. [ 0.84119201, 1.39957154]])
  160. """
  161. def __init__(self, approximate=False, name=None):
  162. super().__init__()
  163. self._approximate = approximate
  164. self._name = name
  165. def forward(self, x):
  166. return F.gelu(x, self._approximate, self._name)
  167. def extra_repr(self):
  168. name_str = f', name={self._name}' if self._name else ''
  169. return f'approximate={self._approximate}{name_str}'
  170. class Hardshrink(Layer):
  171. r"""
  172. Hardshrink Activation
  173. .. math::
  174. hardshrink(x)=
  175. \left\{
  176. \begin{array}{rcl}
  177. x, & & if \ x > threshold \\
  178. x, & & if \ x < -threshold \\
  179. 0, & & if \ others
  180. \end{array}
  181. \right.
  182. Parameters:
  183. threshold (float, optional): The value of threshold for hardthrink. Default is 0.5
  184. name (str, optional): Name for the operation (optional, default is None).
  185. For more information, please refer to :ref:`api_guide_Name`.
  186. Shape:
  187. - input: Tensor with any shape.
  188. - output: Tensor with the same shape as input.
  189. Examples:
  190. .. code-block:: python
  191. >>> import paddle
  192. >>> x = paddle.to_tensor([-1, 0.3, 2.5])
  193. >>> m = paddle.nn.Hardshrink()
  194. >>> out = m(x)
  195. >>> print(out)
  196. Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
  197. [-1. , 0. , 2.50000000])
  198. """
  199. def __init__(self, threshold=0.5, name=None):
  200. super().__init__()
  201. self._threshold = threshold
  202. self._name = name
  203. def forward(self, x):
  204. return F.hardshrink(x, self._threshold, self._name)
  205. def extra_repr(self):
  206. name_str = f', name={self._name}' if self._name else ''
  207. return f'threshold={self._threshold}{name_str}'
  208. class Hardswish(Layer):
  209. r"""
  210. Hardswish activation. Create a callable object of `Hardswish`. Hardswish
  211. is proposed in MobileNetV3, and performs better in computational stability
  212. and efficiency compared to swish function. For more details please refer
  213. to: https://arxiv.org/pdf/1905.02244.pdf
  214. .. math::
  215. Hardswish(x)=
  216. \left\{
  217. \begin{array}{cll}
  218. 0 &, & \text{if } x \leq -3 \\
  219. x &, & \text{if } x \geq 3 \\
  220. \frac{x(x+3)}{6} &, & \text{otherwise}
  221. \end{array}
  222. \right.
  223. Parameters:
  224. name (str, optional): Name for the operation (optional, default is None).
  225. For more information, please refer to :ref:`api_guide_Name`.
  226. Shape:
  227. - input: Tensor with any shape.
  228. - output: Tensor with the same shape as input.
  229. Examples:
  230. .. code-block:: python
  231. >>> import paddle
  232. >>> x = paddle.to_tensor([-4., 5., 1.])
  233. >>> m = paddle.nn.Hardswish()
  234. >>> out = m(x)
  235. >>> print(out)
  236. Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
  237. [-0. , 5. , 0.66666669])
  238. """
  239. def __init__(self, name=None):
  240. super().__init__()
  241. self._name = name
  242. def forward(self, x):
  243. return F.hardswish(x, self._name)
  244. def extra_repr(self):
  245. name_str = f'name={self._name}' if self._name else ''
  246. return name_str
  247. class Tanh(Layer):
  248. r"""
  249. Tanh Activation.
  250. .. math::
  251. Tanh(x) = \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}
  252. Parameters:
  253. name (str, optional): Name for the operation (optional, default is None).
  254. For more information, please refer to :ref:`api_guide_Name`.
  255. Shape:
  256. - input: Tensor with any shape.
  257. - output: Tensor with the same shape as input.
  258. Examples:
  259. .. code-block:: python
  260. >>> import paddle
  261. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  262. >>> m = paddle.nn.Tanh()
  263. >>> out = m(x)
  264. >>> print(out)
  265. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  266. [-0.37994900, -0.19737528, 0.09966799, 0.29131261])
  267. """
  268. def __init__(self, name=None):
  269. super().__init__()
  270. self._name = name
  271. def forward(self, x):
  272. return F.tanh(x, self._name)
  273. def extra_repr(self):
  274. name_str = f'name={self._name}' if self._name else ''
  275. return name_str
  276. class Hardtanh(Layer):
  277. r"""
  278. Hardtanh Activation. Create a callable object of `Hardtanh`.
  279. .. math::
  280. Hardtanh(x)=
  281. \left\{
  282. \begin{array}{cll}
  283. max,& & \text{if } x > max \\
  284. min,& & \text{if } x < min \\
  285. x,& & \text{otherwise}
  286. \end{array}
  287. \right.
  288. Parameters:
  289. min (float, optional): The value of min for Hardtanh. Default is -1.
  290. max (float, optional): The value of max for Hardtanh. Default is 1.
  291. name (str, optional): Name for the operation (optional, default is None).
  292. For more information, please refer to :ref:`api_guide_Name`.
  293. Shape:
  294. - input: Tensor with any shape.
  295. - output: Tensor with the same shape as input.
  296. Examples:
  297. .. code-block:: python
  298. >>> import paddle
  299. >>> x = paddle.to_tensor([-1.5, 0.3, 2.5])
  300. >>> m = paddle.nn.Hardtanh()
  301. >>> out = m(x)
  302. >>> print(out)
  303. Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
  304. [-1. , 0.30000001, 1. ])
  305. """
  306. def __init__(self, min=-1.0, max=1.0, name=None):
  307. super().__init__()
  308. self._min = min
  309. self._max = max
  310. self._name = name
  311. def forward(self, x):
  312. return F.hardtanh(x, self._min, self._max, self._name)
  313. def extra_repr(self):
  314. name_str = f', name={self._name}' if self._name else ''
  315. return f'min={self._min}, max={self._max}{name_str}'
  316. class PReLU(Layer):
  317. """
  318. PReLU Activation. The calculation formula is follows:
  319. If approximate calculation is used:
  320. .. math::
  321. PReLU(x) = max(0, x) + weight * min(0, x)
  322. x is input Tensor.
  323. Parameters:
  324. num_parameters (int, optional): Number of `weight` to learn. The supported values are:
  325. 1 - a single parameter `alpha` is used for all input channels;
  326. Number of channels - a separate `alpha` is used for each input channel.
  327. Default is 1.
  328. init (float, optional): Init value of learnable `weight`. Default is 0.25.
  329. weight_attr(ParamAttr, optional): The parameter attribute for the learnable `weight`.
  330. Default is None. For more information, please refer to :ref:`api_paddle_ParamAttr`.
  331. name (str, optional): Name for the operation (optional, default is None).
  332. For more information, please refer to :ref:`api_guide_Name`.
  333. data_format(str, optional): Data format that specifies the layout of input.
  334. It may be "NC", "NCL", "NCHW", "NCDHW", "NLC", "NHWC" or "NDHWC". Default: "NCHW".
  335. Shape:
  336. - input: Tensor with any shape. Default dtype is float32.
  337. - output: Tensor with the same shape as input.
  338. Examples:
  339. .. code-block:: python
  340. >>> import paddle
  341. >>> data = paddle.to_tensor([[[[-2.0, 3.0, -4.0, 5.0],
  342. ... [ 3.0, -4.0, 5.0, -6.0],
  343. ... [-7.0, -8.0, 8.0, 9.0]],
  344. ... [[ 1.0, -2.0, -3.0, 4.0],
  345. ... [-5.0, 6.0, 7.0, -8.0],
  346. ... [ 6.0, 7.0, 8.0, 9.0]]]])
  347. ...
  348. >>> m = paddle.nn.PReLU(1, 0.25)
  349. >>> out = m(data)
  350. >>> print(out)
  351. Tensor(shape=[1, 2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=False,
  352. [[[[-0.50000000, 3. , -1. , 5. ],
  353. [ 3. , -1. , 5. , -1.50000000],
  354. [-1.75000000, -2. , 8. , 9. ]],
  355. [[ 1. , -0.50000000, -0.75000000, 4. ],
  356. [-1.25000000, 6. , 7. , -2. ],
  357. [ 6. , 7. , 8. , 9. ]]]])
  358. """
  359. def __init__(
  360. self,
  361. num_parameters=1,
  362. init=0.25,
  363. weight_attr=None,
  364. data_format="NCHW",
  365. name=None,
  366. ):
  367. super().__init__()
  368. self._num_parameters = num_parameters
  369. self._init = init
  370. self._weight_attr = weight_attr
  371. self._name = name
  372. self._data_format = data_format
  373. self._weight = self.create_parameter(
  374. attr=self._weight_attr,
  375. shape=[self._num_parameters],
  376. dtype=get_default_dtype(),
  377. is_bias=False,
  378. default_initializer=Constant(self._init),
  379. )
  380. def forward(self, x):
  381. return F.prelu(x, self._weight, data_format=self._data_format)
  382. def extra_repr(self):
  383. name_str = f', name={self._name}' if self._name else ''
  384. return f'num_parameters={self._num_parameters}, data_format={self._data_format}, init={self._init}, dtype={self._dtype}{name_str}'
  385. class RReLU(Layer):
  386. r"""
  387. RReLU activation layer.
  388. Applies the randomized leaky rectified liner unit function to improve generalization performance,
  389. as described in the paper:
  390. `Empirical Evaluation of Rectified Activations in Convolutional Network <https://arxiv.org/abs/1505.00853>`_
  391. During training, randomly samples the negative slope for activation values as described below:
  392. .. math::
  393. RReLU(x)=
  394. \left\{
  395. \begin{array}{rcl}
  396. x, & & if \ x >= 0 \\
  397. a * x, & & otherwise \\
  398. \end{array}
  399. \right.
  400. where :math:`x` is the input tensor,
  401. :math:`a` is randomly sampled from uniform distribution in range (:math:`lower`, :math:`upper`),
  402. In the test phase, the negative slope will take the average value of :math:`lower` and :math:`upper`:
  403. .. math::
  404. RReLU(x)=
  405. \left\{
  406. \begin{array}{rcl}
  407. x, & & if \ x >= 0 \\
  408. (lower + upper) * 0.5 * x, & & otherwise \\
  409. \end{array}
  410. \right.
  411. where :math:`x` is the input tensor,
  412. :math:`lower` and :math:`upper` are the bounds of uniform distribution.
  413. Parameters:
  414. lower (float, optional): The lower bound of uniform distribution. Default: 1.0/8.0.
  415. upper (float, optional): The upper bound of uniform distribution. Default: 1.0/3.0.
  416. name (str, optional): Name for the operation (optional, default is None).
  417. For more information, please refer to :ref:`api_guide_Name`.
  418. Shape:
  419. - input: Tensor with any shape. Default dtype is float32.
  420. - output: Tensor with the same shape as input.
  421. Examples:
  422. .. code-block:: python
  423. >>> import paddle
  424. >>> paddle.seed(2023)
  425. >>> input_tensor = paddle.to_tensor([[[[-2.0, 3.0, -4.0, 5.0],
  426. ... [ 3.0, -4.0, 5.0, -6.0],
  427. ... [-7.0, -8.0, 8.0, 9.0]],
  428. ... [[ 1.0, -2.0, -3.0, 4.0],
  429. ... [-5.0, 6.0, 7.0, -8.0],
  430. ... [ 6.0, 7.0, 8.0, 9.0]]]], dtype='float32')
  431. ...
  432. >>> rrelu_layer = paddle.nn.RReLU(0.1, 0.3)
  433. >>> out = rrelu_layer(input_tensor)
  434. >>> print(out)
  435. Tensor(shape=[1, 2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  436. [[[[-0.54633451, 3. , -0.81611776, 5. ],
  437. [ 3. , -0.60768753, 5. , -1.68630385],
  438. [-1.29360127, -1.45026064, 8. , 9. ]],
  439. [[ 1. , -0.58808362, -0.74662417, 4. ],
  440. [-1.01785135, 6. , 7. , -1.97268605],
  441. [ 6. , 7. , 8. , 9. ]]]])
  442. """
  443. def __init__(self, lower=1.0 / 8.0, upper=1.0 / 3.0, name=None):
  444. super().__init__()
  445. self._lower = lower
  446. self._upper = upper
  447. self._name = name
  448. def forward(self, x):
  449. return F.rrelu(
  450. x, lower=self._lower, upper=self._upper, training=self.training
  451. )
  452. def extra_repr(self):
  453. name_str = f', name={self._name}' if self._name else ''
  454. return f'lower={self._lower}, upper={self._upper}, training={self.training}, dtype={self._dtype}{name_str}'
  455. class ReLU(Layer):
  456. """
  457. ReLU Activation. The calculation formula is follows:
  458. .. math::
  459. ReLU(x) = max(x, 0)
  460. x is input Tensor.
  461. Parameters:
  462. name (str, optional): Name for the operation (optional, default is None).
  463. For more information, please refer to :ref:`api_guide_Name`.
  464. Shape:
  465. - input: Tensor with any shape.
  466. - output: Tensor with the same shape as input.
  467. Examples:
  468. .. code-block:: python
  469. >>> import paddle
  470. >>> x = paddle.to_tensor([-2., 0., 1.])
  471. >>> m = paddle.nn.ReLU()
  472. >>> out = m(x)
  473. >>> print(out)
  474. Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
  475. [0., 0., 1.])
  476. """
  477. def __init__(self, name=None):
  478. super().__init__()
  479. self._name = name
  480. def forward(self, x):
  481. return F.relu(x, self._name)
  482. def extra_repr(self):
  483. name_str = f'name={self._name}' if self._name else ''
  484. return name_str
  485. class ReLU6(Layer):
  486. """
  487. ReLU6 Activation
  488. .. math::
  489. ReLU6(x) = min(max(0,x), 6)
  490. x is input Tensor.
  491. Parameters:
  492. name (str, optional): Name for the operation (optional, default is None).
  493. For more information, please refer to :ref:`api_guide_Name`.
  494. Shape:
  495. - input: Tensor with any shape.
  496. - output: Tensor with the same shape as input.
  497. Examples:
  498. .. code-block:: python
  499. >>> import paddle
  500. >>> x = paddle.to_tensor([-1., 0.3, 6.5])
  501. >>> m = paddle.nn.ReLU6()
  502. >>> out = m(x)
  503. >>> print(out)
  504. Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
  505. [0. , 0.30000000, 6. ])
  506. """
  507. def __init__(self, name=None):
  508. super().__init__()
  509. self._name = name
  510. def forward(self, x):
  511. return F.relu6(x, self._name)
  512. def extra_repr(self):
  513. name_str = f'name={self._name}' if self._name else ''
  514. return name_str
  515. class SELU(Layer):
  516. r"""
  517. SELU Activation
  518. .. math::
  519. SELU(x)= scale *
  520. \left\{
  521. \begin{array}{lcl}
  522. x,& &\text{if } \ x > 0 \\
  523. alpha * e^{x} - alpha,& &\text{if } \ x <= 0
  524. \end{array}
  525. \right.
  526. Parameters:
  527. scale (float, optional): The value of scale(must be greater than 1.0) for SELU. Default is 1.0507009873554804934193349852946.
  528. alpha (float, optional): The value of alpha(must be no less than zero) for SELU. Default is 1.6732632423543772848170429916717.
  529. name (str, optional): Name for the operation (optional, default is None).
  530. For more information, please refer to :ref:`api_guide_Name`.
  531. Shape:
  532. - input: Tensor with any shape.
  533. - output: Tensor with the same shape as input.
  534. Examples:
  535. .. code-block:: python
  536. >>> import paddle
  537. >>> x = paddle.to_tensor([[0.0, 1.0],[2.0, 3.0]])
  538. >>> m = paddle.nn.SELU()
  539. >>> out = m(x)
  540. >>> print(out)
  541. Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
  542. [[0. , 1.05070102],
  543. [2.10140204, 3.15210295]])
  544. """
  545. def __init__(
  546. self,
  547. scale=1.0507009873554804934193349852946,
  548. alpha=1.6732632423543772848170429916717,
  549. name=None,
  550. ):
  551. super().__init__()
  552. self._scale = scale
  553. self._alpha = alpha
  554. self._name = name
  555. def forward(self, x):
  556. return F.selu(x, self._scale, self._alpha, self._name)
  557. def extra_repr(self):
  558. name_str = f', name={self._name}' if self._name else ''
  559. return f'scale={self._scale:.16f}, alpha={self._alpha:.16f}{name_str}'
  560. class LeakyReLU(Layer):
  561. r"""
  562. Leaky ReLU Activation. Create a callable object of `LeakyReLU` to calculate
  563. the `LeakyReLU` of input `x`.
  564. .. math::
  565. LeakyReLU(x)=
  566. \left\{
  567. \begin{array}{rcl}
  568. x, & & if \ x >= 0 \\
  569. negative\_slope * x, & & otherwise \\
  570. \end{array}
  571. \right.
  572. Parameters:
  573. negative_slope (float, optional): Slope of the activation function at
  574. :math:`x < 0` . Default is 0.01.
  575. name (str, optional): Name for the operation (optional, default is None).
  576. For more information, please refer to :ref:`api_guide_Name`.
  577. Shape:
  578. - input: Tensor with any shape.
  579. - output: Tensor with the same shape as input.
  580. Examples:
  581. .. code-block:: python
  582. >>> import paddle
  583. >>> m = paddle.nn.LeakyReLU()
  584. >>> x = paddle.to_tensor([-2.0, 0, 1])
  585. >>> out = m(x)
  586. >>> print(out)
  587. Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
  588. [-0.02000000, 0. , 1. ])
  589. """
  590. def __init__(self, negative_slope=0.01, name=None):
  591. super().__init__()
  592. self._negative_slope = negative_slope
  593. self._name = name
  594. def forward(self, x):
  595. return F.leaky_relu(x, self._negative_slope, self._name)
  596. def extra_repr(self):
  597. name_str = f', name={self._name}' if self._name else ''
  598. return f'negative_slope={self._negative_slope}{name_str}'
  599. class Sigmoid(Layer):
  600. r"""
  601. this interface is used to construct a callable object of the ``Sigmoid`` class. This layer calculate the `sigmoid` of input x.
  602. .. math::
  603. sigmoid(x) = \frac{1}{1 + e^{-x}}
  604. Parameters:
  605. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
  606. Shape:
  607. x: N-D tensor, available dtype is float16, float32, float64.
  608. Returns:
  609. A callable object of Sigmoid.
  610. Examples:
  611. .. code-block:: python
  612. >>> import paddle
  613. >>> m = paddle.nn.Sigmoid()
  614. >>> x = paddle.to_tensor([1.0, 2.0, 3.0, 4.0])
  615. >>> out = m(x)
  616. >>> print(out)
  617. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  618. [0.73105860, 0.88079703, 0.95257413, 0.98201376])
  619. """
  620. def __init__(self, name=None):
  621. super().__init__()
  622. self.name = name
  623. def forward(self, x):
  624. return F.sigmoid(x, self.name)
  625. def extra_repr(self):
  626. name_str = f'name={self.name}' if self.name else ''
  627. return name_str
  628. class Hardsigmoid(Layer):
  629. r"""
  630. ``Hardsigmoid`` Activation Layers, Construct a callable object of
  631. the ``Hardsigmoid`` class. This layer calculate the `hardsigmoid` of input x.
  632. A 3-part piecewise linear approximation of sigmoid(https://arxiv.org/abs/1603.00391),
  633. which is much faster than sigmoid.
  634. .. math::
  635. Hardsigmoid(x)=
  636. \left\{
  637. \begin{array}{rcl}
  638. 0, & & \text{if } \ x \leq -3 \\
  639. 1, & & \text{if } \ x \geq 3 \\
  640. x/6 + 1/2, & & \text{otherwise}
  641. \end{array}
  642. \right.
  643. Parameters:
  644. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  645. Shape:
  646. x: N-D tensor, available dtype is float32, float64.
  647. Returns:
  648. A callable object of Hardsigmoid.
  649. Examples:
  650. .. code-block:: python
  651. >>> import paddle
  652. >>> m = paddle.nn.Hardsigmoid()
  653. >>> x = paddle.to_tensor([-4., 5., 1.])
  654. >>> out = m(x)
  655. >>> print(out)
  656. Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
  657. [0. , 1. , 0.66666669])
  658. """
  659. def __init__(self, name=None):
  660. super().__init__()
  661. self.name = name
  662. def forward(self, x):
  663. return F.hardsigmoid(x, name=self.name)
  664. def extra_repr(self):
  665. name_str = f'name={self.name}' if self.name else ''
  666. return name_str
  667. class Softplus(Layer):
  668. r"""
  669. Softplus Activation
  670. .. math::
  671. softplus(x)=\begin{cases}
  672. \frac{1}{\beta} * \log(1 + e^{\beta * x}),&x\leqslant\frac{\varepsilon}{\beta};\\
  673. x,&x>\frac{\varepsilon}{\beta}.
  674. \end{cases}
  675. Parameters:
  676. beta (float, optional): The value of :math:`\beta` for Softplus. Default is 1
  677. threshold (float, optional): The value of :math:`\varepsilon` for Softplus. Default is 20
  678. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
  679. Shape:
  680. - input: Tensor with any shape.
  681. - output: Tensor with the same shape as input.
  682. Examples:
  683. .. code-block:: python
  684. >>> import paddle
  685. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3], dtype='float32')
  686. >>> m = paddle.nn.Softplus()
  687. >>> out = m(x)
  688. >>> print(out)
  689. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  690. [0.51301527, 0.59813893, 0.74439669, 0.85435522])
  691. """
  692. def __init__(self, beta=1, threshold=20, name=None):
  693. super().__init__()
  694. self._beta = beta
  695. self._threshold = threshold
  696. self._name = name
  697. def forward(self, x):
  698. return F.softplus(x, self._beta, self._threshold, self._name)
  699. def extra_repr(self):
  700. name_str = f', name={self._name}' if self._name else ''
  701. return f'beta={self._beta}, threshold={self._threshold}{name_str}'
  702. class Softshrink(Layer):
  703. r"""
  704. Softshrink Activation
  705. .. math::
  706. Softshrink(x)=
  707. \left\{
  708. \begin{array}{rcl}
  709. x - threshold,& & \text{if } x > threshold \\
  710. x + threshold,& & \text{if } x < -threshold \\
  711. 0,& & \text{otherwise}
  712. \end{array}
  713. \right.
  714. Parameters:
  715. threshold (float, optional): The value of threshold(must be no less than zero) for softplus. Default is 0.5
  716. name (str, optional): Name for the operation (optional, default is None).
  717. For more information, please refer to :ref:`api_guide_Name`.
  718. Shape:
  719. - input: Tensor with any shape.
  720. - output: Tensor with the same shape as input.
  721. Examples:
  722. .. code-block:: python
  723. >>> import paddle
  724. >>> x = paddle.to_tensor([-0.9, -0.2, 0.1, 0.8])
  725. >>> m = paddle.nn.Softshrink()
  726. >>> out = m(x)
  727. >>> print(out)
  728. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  729. [-0.39999998, 0. , 0. , 0.30000001])
  730. """
  731. def __init__(self, threshold=0.5, name=None):
  732. super().__init__()
  733. self._threshold = threshold
  734. self._name = name
  735. def forward(self, x):
  736. return F.softshrink(x, self._threshold, self._name)
  737. def extra_repr(self):
  738. name_str = f', name={self._name}' if self._name else ''
  739. return f'threshold={self._threshold}{name_str}'
  740. class Softsign(Layer):
  741. r"""
  742. Softsign Activation
  743. .. math::
  744. Softsign(x) = \frac{x}{1 + |x|}
  745. Parameters:
  746. name (str, optional): Name for the operation (optional, default is None).
  747. For more information, please refer to :ref:`api_guide_Name`.
  748. Shape:
  749. - input: Tensor with any shape.
  750. - output: Tensor with the same shape as input.
  751. Examples:
  752. .. code-block:: python
  753. >>> import paddle
  754. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  755. >>> m = paddle.nn.Softsign()
  756. >>> out = m(x)
  757. >>> print(out)
  758. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  759. [-0.28571430, -0.16666666, 0.09090909, 0.23076925])
  760. """
  761. def __init__(self, name=None):
  762. super().__init__()
  763. self._name = name
  764. def forward(self, x):
  765. return F.softsign(x, self._name)
  766. def extra_repr(self):
  767. name_str = f'name={self._name}' if self._name else ''
  768. return name_str
  769. class Swish(Layer):
  770. r"""
  771. Swish Activation.
  772. .. math::
  773. Swish(x) = \frac{x}{1 + e^{-x}}
  774. Parameters:
  775. name (str, optional): Name for the operation (optional, default is None).
  776. For more information, please refer to :ref:`api_guide_Name`.
  777. Shape:
  778. - input: Tensor with any shape.
  779. - output: Tensor with the same shape as input.
  780. Examples:
  781. .. code-block:: python
  782. >>> import paddle
  783. >>> x = paddle.to_tensor([-2., 0., 1.])
  784. >>> m = paddle.nn.Swish()
  785. >>> out = m(x)
  786. >>> print(out)
  787. Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
  788. [-0.23840584, 0. , 0.73105860])
  789. """
  790. def __init__(self, name=None):
  791. super().__init__()
  792. self._name = name
  793. def forward(self, x):
  794. return F.swish(x, self._name)
  795. def extra_repr(self):
  796. name_str = f'name={self._name}' if self._name else ''
  797. return name_str
  798. class Mish(Layer):
  799. r"""
  800. Mish Activation.
  801. .. math::
  802. softplus(x) = \begin{cases}
  803. x, \text{if } x > \text{threshold} \\
  804. \ln(1 + e^{x}), \text{otherwise}
  805. \end{cases}
  806. Mish(x) = x * \tanh(softplus(x))
  807. Parameters:
  808. name (str, optional): Name for the operation (optional, default is None).
  809. For more information, please refer to :ref:`api_guide_Name`.
  810. Shape:
  811. - input: Tensor with any shape.
  812. - output: Tensor with the same shape as input.
  813. Examples:
  814. .. code-block:: python
  815. >>> import paddle
  816. >>> x = paddle.to_tensor([-5., 0., 5.])
  817. >>> m = paddle.nn.Mish()
  818. >>> out = m(x)
  819. >>> print(out)
  820. Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
  821. [-0.03357624, 0. , 4.99955177])
  822. """
  823. def __init__(self, name=None):
  824. super().__init__()
  825. self._name = name
  826. def forward(self, x):
  827. return F.mish(x, self._name)
  828. def extra_repr(self):
  829. name_str = f'name={self._name}' if self._name else ''
  830. return name_str
  831. class Tanhshrink(Layer):
  832. """
  833. Tanhshrink Activation
  834. .. math::
  835. Tanhshrink(x) = x - tanh(x)
  836. Parameters:
  837. name (str, optional): Name for the operation (optional, default is None).
  838. For more information, please refer to :ref:`api_guide_Name`.
  839. Shape:
  840. - input: Tensor with any shape.
  841. - output: Tensor with the same shape as input.
  842. Examples:
  843. .. code-block:: python
  844. >>> import paddle
  845. >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
  846. >>> m = paddle.nn.Tanhshrink()
  847. >>> out = m(x)
  848. >>> print(out)
  849. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  850. [-0.02005100, -0.00262472, 0.00033201, 0.00868741])
  851. """
  852. def __init__(self, name=None):
  853. super().__init__()
  854. self._name = name
  855. def forward(self, x):
  856. return F.tanhshrink(x, self._name)
  857. def extra_repr(self):
  858. name_str = f'name={self._name}' if self._name else ''
  859. return name_str
  860. class ThresholdedReLU(Layer):
  861. r"""
  862. Thresholded ReLU Activation
  863. .. math::
  864. ThresholdedReLU(x) =
  865. \left\{
  866. \begin{array}{rl}
  867. x,& \text{if } \ x > threshold \\
  868. 0,& \text{otherwise}
  869. \end{array}
  870. \right.
  871. Parameters:
  872. threshold (float, optional): The value of threshold for ThresholdedReLU. Default is 1.0
  873. name (str, optional): Name for the operation (optional, default is None).
  874. For more information, please refer to :ref:`api_guide_Name`.
  875. Shape:
  876. - input: Tensor with any shape.
  877. - output: Tensor with the same shape as input.
  878. Examples:
  879. .. code-block:: python
  880. >>> import paddle
  881. >>> x = paddle.to_tensor([2., 0., 1.])
  882. >>> m = paddle.nn.ThresholdedReLU()
  883. >>> out = m(x)
  884. >>> print(out)
  885. Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
  886. [2., 0., 0.])
  887. """
  888. def __init__(self, threshold=1.0, name=None):
  889. super().__init__()
  890. self._threshold = threshold
  891. self._name = name
  892. def forward(self, x):
  893. return F.thresholded_relu(x, self._threshold, self._name)
  894. def extra_repr(self):
  895. name_str = f', name={self._name}' if self._name else ''
  896. return f'threshold={self._threshold}{name_str}'
  897. class Silu(Layer):
  898. r"""
  899. Silu Activation
  900. .. math::
  901. silu(x) = \frac{x}{1 + \mathrm{e}^{-x}}
  902. Where :math:`x` is the input Tensor.
  903. Parameters:
  904. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
  905. Shape:
  906. - input: Tensor with any shape.
  907. - output: Tensor with the same shape as input.
  908. Examples:
  909. .. code-block:: python
  910. >>> import paddle
  911. >>> x = paddle.to_tensor([1.0, 2.0, 3.0, 4.0])
  912. >>> m = paddle.nn.Silu()
  913. >>> out = m(x)
  914. >>> print(out)
  915. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  916. [0.73105860, 1.76159406, 2.85772228, 3.92805505])
  917. """
  918. def __init__(self, name=None):
  919. super().__init__()
  920. self._name = name
  921. def forward(self, x):
  922. return F.silu(x, self._name)
  923. def extra_repr(self):
  924. name_str = f'name={self._name}' if self._name else ''
  925. return name_str
  926. class LogSigmoid(Layer):
  927. r"""
  928. LogSigmoid Activation.
  929. .. math::
  930. LogSigmoid(x) = log \frac{1}{1 + e^{-x}}
  931. Parameters:
  932. x (Tensor): The input Tensor with data type float32, or float64.
  933. name (str, optional): Name for the operation (optional, default is None).
  934. For more information, please refer to :ref:`api_guide_Name`.
  935. Shape:
  936. - input: Tensor with any shape.
  937. - output: Tensor with the same shape as input.
  938. Examples:
  939. .. code-block:: python
  940. >>> import paddle
  941. >>> x = paddle.to_tensor([1.0, 2.0, 3.0, 4.0])
  942. >>> m = paddle.nn.LogSigmoid()
  943. >>> out = m(x)
  944. >>> print(out)
  945. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  946. [-0.31326166, -0.12692805, -0.04858733, -0.01814996])
  947. """
  948. def __init__(self, name=None):
  949. super().__init__()
  950. self._name = name
  951. def forward(self, x):
  952. return F.log_sigmoid(x, self._name)
  953. def extra_repr(self):
  954. name_str = f'name={self._name}' if self._name else ''
  955. return name_str
  956. class Softmax(Layer):
  957. r"""
  958. Softmax Activation.
  959. This operator implements the softmax layer. The calculation process is as follows:
  960. 1. The dimension :attr:`axis` of ``x`` will be permuted to the last.
  961. 2. Then ``x`` will be logically flattened to a 2-D matrix. The matrix's second
  962. dimension(row length) is the same as the dimension :attr:`axis` of ``x``,
  963. and the first dimension(column length) is the product of all other dimensions
  964. of ``x``. For each row of the matrix, the softmax operator squashes the
  965. K-dimensional(K is the width of the matrix, which is also the size of ``x``'s
  966. dimension :attr:`axis`) vector of arbitrary real values to a K-dimensional
  967. vector of real values in the range [0, 1] that add up to 1.
  968. 3. After the softmax operation is completed, the inverse operations of steps 1 and 2
  969. are performed to restore the two-dimensional matrix to the same dimension as the ``x`` .
  970. It computes the exponential of the given dimension and the sum of exponential
  971. values of all the other dimensions in the K-dimensional vector input.
  972. Then the ratio of the exponential of the given dimension and the sum of
  973. exponential values of all the other dimensions is the output of the softmax
  974. operator.
  975. For each row :math:`i` and each column :math:`j` in the matrix, we have:
  976. .. math::
  977. Softmax[i, j] = \frac{\exp(x[i, j])}{\sum_j(exp(x[i, j])}
  978. Example:
  979. .. code-block:: text
  980. Case 1:
  981. Input:
  982. x.shape = [2, 3, 4]
  983. x.data = [[[2.0, 3.0, 4.0, 5.0],
  984. [3.0, 4.0, 5.0, 6.0],
  985. [7.0, 8.0, 8.0, 9.0]],
  986. [[1.0, 2.0, 3.0, 4.0],
  987. [5.0, 6.0, 7.0, 8.0],
  988. [6.0, 7.0, 8.0, 9.0]]]
  989. Attrs:
  990. axis = -1
  991. Output:
  992. out.shape = [2, 3, 4]
  993. out.data = [[[0.0320586 , 0.08714432, 0.23688282, 0.64391426],
  994. [0.0320586 , 0.08714432, 0.23688282, 0.64391426],
  995. [0.07232949, 0.19661193, 0.19661193, 0.53444665]],
  996. [[0.0320586 , 0.08714432, 0.23688282, 0.64391426],
  997. [0.0320586 , 0.08714432, 0.23688282, 0.64391426],
  998. [0.0320586 , 0.08714432, 0.23688282, 0.64391426]]]
  999. Case 2:
  1000. Input:
  1001. x.shape = [2, 3, 4]
  1002. x.data = [[[2.0, 3.0, 4.0, 5.0],
  1003. [3.0, 4.0, 5.0, 6.0],
  1004. [7.0, 8.0, 8.0, 9.0]],
  1005. [[1.0, 2.0, 3.0, 4.0],
  1006. [5.0, 6.0, 7.0, 8.0],
  1007. [6.0, 7.0, 8.0, 9.0]]]
  1008. Attrs:
  1009. axis = 1
  1010. Output:
  1011. out.shape = [2, 3, 4]
  1012. out.data = [[[0.00657326, 0.00657326, 0.01714783, 0.01714783],
  1013. [0.01786798, 0.01786798, 0.04661262, 0.04661262],
  1014. [0.97555875, 0.97555875, 0.93623955, 0.93623955]],
  1015. [[0.00490169, 0.00490169, 0.00490169, 0.00490169],
  1016. [0.26762315, 0.26762315, 0.26762315, 0.26762315],
  1017. [0.72747516, 0.72747516, 0.72747516, 0.72747516]]]
  1018. Parameters:
  1019. axis (int, optional): The axis along which to perform log_softmax
  1020. calculations. It should be in range [-D, D), where D is the
  1021. dimensions of ``x`` . If ``axis`` < 0, it works the same way as
  1022. :math:`axis + D` . Default is -1.
  1023. name (str, optional): Name for the operation (optional, default is None).
  1024. For more information, please refer to :ref:`api_guide_Name`.
  1025. Shape:
  1026. - input: Tensor with any shape.
  1027. - output: Tensor with the same shape as input.
  1028. Examples:
  1029. .. code-block:: python
  1030. >>> import paddle
  1031. >>> x = paddle.to_tensor([[[2.0, 3.0, 4.0, 5.0],
  1032. ... [3.0, 4.0, 5.0, 6.0],
  1033. ... [7.0, 8.0, 8.0, 9.0]],
  1034. ... [[1.0, 2.0, 3.0, 4.0],
  1035. ... [5.0, 6.0, 7.0, 8.0],
  1036. ... [6.0, 7.0, 8.0, 9.0]]], dtype='float32')
  1037. >>> m = paddle.nn.Softmax()
  1038. >>> out = m(x)
  1039. >>> print(out)
  1040. Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  1041. [[[0.03205860, 0.08714432, 0.23688284, 0.64391428],
  1042. [0.03205860, 0.08714432, 0.23688284, 0.64391428],
  1043. [0.07232949, 0.19661194, 0.19661194, 0.53444666]],
  1044. [[0.03205860, 0.08714432, 0.23688284, 0.64391428],
  1045. [0.03205860, 0.08714432, 0.23688284, 0.64391428],
  1046. [0.03205860, 0.08714432, 0.23688284, 0.64391428]]])
  1047. """
  1048. def __init__(self, axis=-1, name=None):
  1049. super().__init__()
  1050. self._axis = axis
  1051. self._dtype = None
  1052. self._name = name
  1053. def forward(self, x):
  1054. return F.softmax(x, self._axis, name=self._name)
  1055. def extra_repr(self):
  1056. name_str = f', name={self._name}' if self._name else ''
  1057. return f'axis={self._axis}{name_str}'
  1058. class LogSoftmax(Layer):
  1059. r"""
  1060. This operator implements the log_softmax layer. The calculation process is as follows:
  1061. .. math::
  1062. \begin{array} {rcl}
  1063. Out[i, j] &= &log(softmax(x)) \\
  1064. &= &log(\frac{\exp(X[i, j])}{\sum_j(\exp(X[i, j])})
  1065. \end{array}
  1066. Parameters:
  1067. axis (int, optional): The axis along which to perform log_softmax
  1068. calculations. It should be in range [-D, D), where D is the
  1069. dimensions of the input Tensor . If ``axis`` < 0, it works the
  1070. same way as :math:`axis + D` . Default is -1.
  1071. name (str, optional): Name for the operation (optional, default is None).
  1072. For more information, please refer to :ref:`api_guide_Name`.
  1073. Shape:
  1074. - input: Tensor with any shape.
  1075. - output: Tensor with the same shape as input.
  1076. Examples:
  1077. .. code-block:: python
  1078. >>> import paddle
  1079. >>> x = [[[-2.0, 3.0, -4.0, 5.0],
  1080. ... [ 3.0, -4.0, 5.0, -6.0],
  1081. ... [-7.0, -8.0, 8.0, 9.0]],
  1082. ... [[ 1.0, -2.0, -3.0, 4.0],
  1083. ... [-5.0, 6.0, 7.0, -8.0],
  1084. ... [ 6.0, 7.0, 8.0, 9.0]]]
  1085. >>> m = paddle.nn.LogSoftmax()
  1086. >>> x = paddle.to_tensor(x)
  1087. >>> out = m(x)
  1088. >>> print(out)
  1089. Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  1090. [[[-7.12783957 , -2.12783957 , -9.12783909 , -0.12783945 ],
  1091. [-2.12705135 , -9.12705135 , -0.12705141 , -11.12705135],
  1092. [-16.31326103, -17.31326103, -1.31326187 , -0.31326184 ]],
  1093. [[-3.05181193 , -6.05181217 , -7.05181217 , -0.05181199 ],
  1094. [-12.31326675, -1.31326652 , -0.31326646 , -15.31326675],
  1095. [-3.44018984 , -2.44018984 , -1.44018972 , -0.44018975 ]]])
  1096. """
  1097. def __init__(self, axis=-1, name=None):
  1098. super().__init__()
  1099. self._axis = axis
  1100. self._name = name
  1101. def forward(self, x):
  1102. return F.log_softmax(x, self._axis)
  1103. def extra_repr(self):
  1104. name_str = f', name={self._name}' if self._name else ''
  1105. return f'axis={self._axis}{name_str}'
  1106. class Maxout(Layer):
  1107. r"""
  1108. Maxout Activation. Create a callable object of `Maxout`.
  1109. Assumed the input shape is (N, Ci, H, W).
  1110. The output shape is (N, Co, H, W).
  1111. Then Co = Ci/groups and the operator formula is as follows:
  1112. .. math::
  1113. \begin{array}{l}
  1114. &out_{si+j} = \max_{k} x_{gsi + sk + j} \\
  1115. &g = groups \\
  1116. &s = \frac{input.size}{num\_channels} \\
  1117. &0 \le i < \frac{num\_channels}{groups} \\
  1118. &0 \le j < s \\
  1119. &0 \le k < groups
  1120. \end{array}
  1121. Parameters:
  1122. groups (int): The groups number of maxout. `groups` specifies the
  1123. index of channel dimension where maxout will be performed. This must be
  1124. a factor of number of features. Default is 1.
  1125. axis (int, optional): The axis along which to perform maxout calculations.
  1126. It should be 1 when data format is NCHW, be -1 or 3 when data format
  1127. is NHWC. If ``axis`` < 0, it works the same way as :math:`axis + D` ,
  1128. where D is the dimensions of ``x`` . Default is 1.
  1129. name (str, optional): Name for the operation (optional, default is None).
  1130. For more information, please refer to :ref:`api_guide_Name`.
  1131. Shape:
  1132. - input: :math:`(N, C_{in}, H_{in}, W_{in})`
  1133. - output: :math:`(N, C_{out}, H_{out}, W_{out})`
  1134. Examples:
  1135. .. code-block:: python
  1136. >>> import paddle
  1137. >>> paddle.seed(100)
  1138. >>> x = paddle.rand([1, 2, 3, 4])
  1139. >>> m = paddle.nn.Maxout(groups=2)
  1140. >>> out = m(x)
  1141. >>> print(out)
  1142. Tensor(shape=[1, 1, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  1143. [[[[0.85139430, 0.95717543, 0.43864486, 0.51577556],
  1144. [0.84765935, 0.45680618, 0.39412445, 0.72039396],
  1145. [0.59444654, 0.78120756, 0.78364515, 0.90572405]]]])
  1146. """
  1147. def __init__(self, groups, axis=1, name=None):
  1148. super().__init__()
  1149. self._groups = groups
  1150. self._axis = axis
  1151. self._name = name
  1152. def forward(self, x):
  1153. return F.maxout(x, self._groups, self._axis, self._name)
  1154. def extra_repr(self):
  1155. name_str = f', name={self._name}' if self._name else ''
  1156. return f'groups={self._groups}, axis={self._axis}{name_str}'
  1157. class Softmax2D(Layer):
  1158. r"""
  1159. Softmax2D Activation.
  1160. Given a Tensor with shape (B, C, H, W) or (C, H, W), it will apply Softmax to each location (C, h_i, w_j).
  1161. The sum of result in each location (C, H_i, W_j) will be one.
  1162. Shape:
  1163. - Input: :math:`(B, C, H, W)` or :math:`(C, H, W)`
  1164. - Output: :math:`(B, C, H, W)` or :math:`(C, H, W)` (same as input)
  1165. Returns:
  1166. A Tensor of the same shape and dtype as input with value in range [0, 1].
  1167. Examples:
  1168. .. code-block:: python
  1169. >>> import paddle
  1170. >>> paddle.seed(100)
  1171. >>> x = paddle.rand([1, 2, 3, 4])
  1172. >>> m = paddle.nn.Softmax2D()
  1173. >>> out = m(x)
  1174. >>> print(out)
  1175. Tensor(shape=[1, 2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  1176. [[[[0.42608523, 0.32081410, 0.39483935, 0.55642301],
  1177. [0.38131708, 0.45118359, 0.44891062, 0.46053308],
  1178. [0.35746980, 0.60766530, 0.38638926, 0.70425135]],
  1179. [[0.57391477, 0.67918587, 0.60516071, 0.44357699],
  1180. [0.61868292, 0.54881644, 0.55108935, 0.53946698],
  1181. [0.64253020, 0.39233473, 0.61361068, 0.29574865]]]])
  1182. """
  1183. def __init__(self, name=None):
  1184. super().__init__()
  1185. self._dtype = None
  1186. self._name = name
  1187. def forward(self, x):
  1188. assert (
  1189. x.ndim == 3 or x.ndim == 4
  1190. ), f"Softmax2D requires a 3D or 4D tensor as input. Received: {x.ndim}D."
  1191. return F.softmax(x, axis=-3, dtype=self._dtype, name=self._name)
  1192. def extra_repr(self):
  1193. name_str = f'name={self._name}' if self._name else ''
  1194. return name_str