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

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