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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from tensorlayer import logging
  4. import tensorlayer as tl
  5. from tensorlayer.initializers import truncated_normal
  6. from tensorlayer.layers.core import Module
  7. __all__ = [
  8. 'PRelu', 'PRelu6', 'PTRelu6', 'LeakyReLU', 'LeakyReLU6', 'LeakyTwiceRelu6', 'Ramp', 'Swish', 'HardTanh', 'Mish'
  9. ]
  10. class PRelu(Module):
  11. """
  12. The :class:`PRelu` class is Parametric Rectified Linear layer.
  13. It follows f(x) = alpha * x for x < 0, f(x) = x for x >= 0,
  14. where alpha is a learned array with the same shape as x.
  15. Parameters
  16. ----------
  17. channel_shared : boolean
  18. If True, single weight is shared by all channels.
  19. in_channels: int
  20. The number of channels of the previous layer.
  21. If None, it will be automatically detected when the layer is forwarded for the first time.
  22. a_init : initializer
  23. The initializer for initializing the alpha(s).
  24. name : None or str
  25. A unique layer name.
  26. Examples
  27. -----------
  28. >>> inputs = tl.layers.Input([10, 5])
  29. >>> prelulayer = tl.layers.PRelu(channel_shared=True, in_channels=5)(inputs)
  30. References
  31. -----------
  32. - `Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification <http://arxiv.org/abs/1502.01852>`__
  33. - `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__
  34. """
  35. def __init__(
  36. self, channel_shared=False, in_channels=None, a_init=truncated_normal(mean=0.0, stddev=0.05), name=None,
  37. data_format='channels_last', dim=2
  38. ):
  39. super(PRelu, self).__init__(name)
  40. self.channel_shared = channel_shared
  41. self.in_channels = in_channels
  42. self.a_init = a_init
  43. self.data_format = data_format
  44. self.dim = dim
  45. if self.channel_shared:
  46. self.build((None, ))
  47. self._built = True
  48. elif self.in_channels is not None:
  49. self.build((None, self.in_channels))
  50. self._built = True
  51. logging.info("PRelu %s: channel_shared: %s" % (self.name, self.channel_shared))
  52. def __repr__(self):
  53. s = ('{classname}(')
  54. s += 'channel_shared={channel_shared},'
  55. s += 'in_channels={in_channels},'
  56. s += 'name={name}'
  57. s += ')'
  58. return s.format(classname=self.__class__.__name__, **self.__dict__)
  59. def build(self, inputs_shape):
  60. if self.channel_shared:
  61. w_shape = (1, )
  62. elif self.data_format == 'channels_last':
  63. w_shape = (self.in_channels, )
  64. elif self.data_format == 'channels_first':
  65. if self.dim == 2:
  66. w_shape = (1, self.in_channels, 1, 1)
  67. elif self.dim == 1:
  68. w_shape = (1, self.in_channels, 1)
  69. elif self.dim == 3:
  70. w_shape = (1, self.in_channels, 1, 1, 1)
  71. else:
  72. raise Exception("Dim should be equal to 1, 2 or 3")
  73. self.alpha_var = self._get_weights("alpha", shape=w_shape, init=self.a_init)
  74. self.relu = tl.ops.ReLU()
  75. self.sigmoid = tl.ops.Sigmoid()
  76. def forward(self, inputs):
  77. if self._forward_state == False:
  78. if self._built == False:
  79. self.build(tl.get_tensor_shape(inputs))
  80. self._built = True
  81. self._forward_state = True
  82. pos = self.relu(inputs)
  83. self.alpha_var_constrained = self.sigmoid(self.alpha_var)
  84. neg = -self.alpha_var_constrained * self.relu(-inputs)
  85. return pos + neg
  86. class PRelu6(Module):
  87. """
  88. The :class:`PRelu6` class is Parametric Rectified Linear layer integrating ReLU6 behaviour.
  89. This Layer is a modified version of the :class:`PRelu`.
  90. This activation layer use a modified version :func:`tl.act.leaky_relu` introduced by the following paper:
  91. `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
  92. This activation function also use a modified version of the activation function :func:`tf.nn.relu6` introduced by the following paper:
  93. `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__
  94. This activation layer push further the logic by adding `leaky` behaviour both below zero and above six.
  95. The function return the following results:
  96. - When x < 0: ``f(x) = alpha_low * x``.
  97. - When x in [0, 6]: ``f(x) = x``.
  98. - When x > 6: ``f(x) = 6``.
  99. Parameters
  100. ----------
  101. channel_shared : boolean
  102. If True, single weight is shared by all channels.
  103. in_channels: int
  104. The number of channels of the previous layer.
  105. If None, it will be automatically detected when the layer is forwarded for the first time.
  106. a_init : initializer
  107. The initializer for initializing the alpha(s).
  108. name : None or str
  109. A unique layer name.
  110. Examples
  111. -----------
  112. >>> inputs = tl.layers.Input([10, 5])
  113. >>> prelulayer = tl.layers.PRelu6(channel_shared=True, in_channels=5)(inputs)
  114. References
  115. -----------
  116. - `Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification <http://arxiv.org/abs/1502.01852>`__
  117. - `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
  118. - `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__
  119. """
  120. def __init__(
  121. self,
  122. channel_shared=False,
  123. in_channels=None,
  124. a_init=truncated_normal(mean=0.0, stddev=0.05),
  125. name=None, # "prelu6"
  126. data_format='channels_last',
  127. dim=2
  128. ):
  129. super(PRelu6, self).__init__(name)
  130. self.channel_shared = channel_shared
  131. self.in_channels = in_channels
  132. self.a_init = a_init
  133. self.data_format = data_format
  134. self.dim = dim
  135. if self.channel_shared:
  136. self.build((None, ))
  137. self._built = True
  138. elif self.in_channels is not None:
  139. self.build((None, self.in_channels))
  140. self._built = True
  141. logging.info("PRelu6 %s: channel_shared: %s" % (self.name, self.channel_shared))
  142. def __repr__(self):
  143. s = ('{classname}(')
  144. s += 'channel_shared={channel_shared},'
  145. s += 'in_channels={in_channels},'
  146. s += 'name={name}'
  147. s += ')'
  148. return s.format(classname=self.__class__.__name__, **self.__dict__)
  149. def build(self, inputs_shape):
  150. if self.channel_shared:
  151. w_shape = (1, )
  152. elif self.data_format == 'channels_last':
  153. w_shape = (self.in_channels, )
  154. elif self.data_format == 'channels_first':
  155. if self.dim == 2:
  156. w_shape = (1, self.in_channels, 1, 1)
  157. elif self.dim == 1:
  158. w_shape = (1, self.in_channels, 1)
  159. elif self.dim == 3:
  160. w_shape = (1, self.in_channels, 1, 1, 1)
  161. else:
  162. raise Exception("Dim should be equal to 1, 2 or 3")
  163. self.alpha_var = self._get_weights("alpha", shape=w_shape, init=self.a_init)
  164. self.sigmoid = tl.ops.Sigmoid()
  165. self.relu = tl.ops.ReLU()
  166. # @tf.function
  167. def forward(self, inputs):
  168. if self._forward_state == False:
  169. if self._built == False:
  170. self.build(tl.get_tensor_shape(inputs))
  171. self._built = True
  172. self._forward_state = True
  173. alpha_var_constrained = self.sigmoid(self.alpha_var)
  174. pos = self.relu(inputs)
  175. pos_6 = -self.relu(inputs - 6)
  176. neg = -alpha_var_constrained * self.relu(-inputs)
  177. return pos + pos_6 + neg
  178. class PTRelu6(Module):
  179. """
  180. The :class:`PTRelu6` class is Parametric Rectified Linear layer integrating ReLU6 behaviour.
  181. This Layer is a modified version of the :class:`PRelu`.
  182. This activation layer use a modified version :func:`tl.act.leaky_relu` introduced by the following paper:
  183. `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
  184. This activation function also use a modified version of the activation function :func:`tf.nn.relu6` introduced by the following paper:
  185. `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__
  186. This activation layer push further the logic by adding `leaky` behaviour both below zero and above six.
  187. The function return the following results:
  188. - When x < 0: ``f(x) = alpha_low * x``.
  189. - When x in [0, 6]: ``f(x) = x``.
  190. - When x > 6: ``f(x) = 6 + (alpha_high * (x-6))``.
  191. This version goes one step beyond :class:`PRelu6` by introducing leaky behaviour on the positive side when x > 6.
  192. Parameters
  193. ----------
  194. channel_shared : boolean
  195. If True, single weight is shared by all channels.
  196. in_channels: int
  197. The number of channels of the previous layer.
  198. If None, it will be automatically detected when the layer is forwarded for the first time.
  199. a_init : initializer
  200. The initializer for initializing the alpha(s).
  201. name : None or str
  202. A unique layer name.
  203. Examples
  204. -----------
  205. >>> inputs = tl.layers.Input([10, 5])
  206. >>> prelulayer = tl.layers.PTRelu6(channel_shared=True, in_channels=5)(inputs)
  207. References
  208. -----------
  209. - `Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification <http://arxiv.org/abs/1502.01852>`__
  210. - `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__
  211. - `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
  212. """
  213. def __init__(
  214. self,
  215. channel_shared=False,
  216. in_channels=None,
  217. data_format='channels_last',
  218. a_init=truncated_normal(mean=0.0, stddev=0.05),
  219. name=None # "ptrelu6"
  220. ):
  221. super(PTRelu6, self).__init__(name)
  222. self.channel_shared = channel_shared
  223. self.in_channels = in_channels
  224. self.data_format = data_format
  225. self.a_init = a_init
  226. if self.channel_shared:
  227. self.build((None, ))
  228. self._built = True
  229. elif self.in_channels:
  230. self.build((None, self.in_channels))
  231. self._built = True
  232. logging.info("PTRelu6 %s: channel_shared: %s" % (self.name, self.channel_shared))
  233. def __repr__(self):
  234. s = ('{classname}(')
  235. s += 'channel_shared={channel_shared},'
  236. s += 'in_channels={in_channels},'
  237. s += 'name={name}'
  238. s += ')'
  239. return s.format(classname=self.__class__.__name__, **self.__dict__)
  240. def build(self, inputs_shape):
  241. if self.channel_shared:
  242. w_shape = (1, )
  243. elif self.data_format == 'channels_last':
  244. w_shape = (self.in_channels, )
  245. elif self.data_format == 'channels_first':
  246. if self.dim == 2:
  247. w_shape = (1, self.in_channels, 1, 1)
  248. elif self.dim == 1:
  249. w_shape = (1, self.in_channels, 1)
  250. elif self.dim == 3:
  251. w_shape = (1, self.in_channels, 1, 1, 1)
  252. else:
  253. raise Exception("Dim should be equal to 1, 2 or 3")
  254. # Alpha for outputs lower than zeros
  255. self.alpha_low = self._get_weights("alpha_low", shape=w_shape, init=self.a_init)
  256. self.sigmoid = tl.ops.Sigmoid()
  257. self.relu = tl.ops.ReLU()
  258. # Alpha for outputs higher than 6
  259. self.alpha_high = self._get_weights("alpha_high", shape=w_shape, init=self.a_init)
  260. # @tf.function
  261. def forward(self, inputs):
  262. if self._forward_state == False:
  263. if self._built == False:
  264. self.build(tl.get_tensor_shape(inputs))
  265. self._built = True
  266. self._forward_state = True
  267. alpha_low_constrained = self.sigmoid(self.alpha_low)
  268. alpha_high_constrained = self.sigmoid(self.alpha_high)
  269. pos = self.relu(inputs)
  270. pos_6 = -self.relu(inputs - 6) + alpha_high_constrained * self.relu(inputs - 6)
  271. neg = -alpha_low_constrained * self.relu(-inputs)
  272. return pos + pos_6 + neg
  273. class Ramp(Module):
  274. """Ramp activation function.
  275. Reference: [tf.clip_by_value]<https://www.tensorflow.org/api_docs/python/tf/clip_by_value>
  276. Parameters
  277. ----------
  278. x : Tensor
  279. input.
  280. v_min : float
  281. cap input to v_min as a lower bound.
  282. v_max : float
  283. cap input to v_max as a upper bound.
  284. Returns
  285. -------
  286. Tensor
  287. A ``Tensor`` in the same type as ``x``.
  288. Examples
  289. -----------
  290. >>> inputs = tl.layers.Input([10, 5])
  291. >>> prelulayer = tl.layers.Ramp()(inputs)
  292. """
  293. def __init__(self, v_min=0, v_max=1):
  294. super(Ramp, self).__init__()
  295. self._built = True
  296. self.v_min = v_min
  297. self.v_max = v_max
  298. def forward(self, x):
  299. return tl.ops.clip_by_value(x, clip_value_min=self.v_min, clip_value_max=self.v_max)
  300. class LeakyReLU(Module):
  301. """
  302. This function is a modified version of ReLU, introducing a nonzero gradient for negative input. Introduced by the paper:
  303. `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
  304. The function return the following results:
  305. - When x < 0: ``f(x) = alpha_low * x``.
  306. - When x >= 0: ``f(x) = x``.
  307. Parameters
  308. ----------
  309. x : Tensor
  310. Support input type ``float``, ``double``, ``int32``, ``int64``, ``uint8``, ``int16``, or ``int8``.
  311. alpha : float
  312. Slope.
  313. name : str
  314. The function name (optional).
  315. Examples
  316. --------
  317. >>> net = tl.layers.Input([10, 200])
  318. >>> net = tl.layers.LeakyReLU(alpha=0.5)(net)
  319. Returns
  320. -------
  321. Tensor
  322. A ``Tensor`` in the same type as ``x``.
  323. References
  324. ----------
  325. - `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
  326. """
  327. def __init__(self, alpha=0.2):
  328. super(LeakyReLU, self).__init__()
  329. self._built = True
  330. self.alpha = alpha
  331. self._leakyrelu = tl.ops.LeakyReLU(alpha=alpha)
  332. def forward(self, x):
  333. return self._leakyrelu(x)
  334. class LeakyReLU6(Module):
  335. """
  336. This activation function is a modified version :func:`leaky_relu` introduced by the following paper:
  337. `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
  338. This activation function also follows the behaviour of the activation function :func:`tf.ops.relu6` introduced by the following paper:
  339. `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__
  340. The function return the following results:
  341. - When x < 0: ``f(x) = alpha_low * x``.
  342. - When x in [0, 6]: ``f(x) = x``.
  343. - When x > 6: ``f(x) = 6``.
  344. Parameters
  345. ----------
  346. x : Tensor
  347. Support input type ``float``, ``double``, ``int32``, ``int64``, ``uint8``, ``int16``, or ``int8``.
  348. alpha : float
  349. Slope.
  350. name : str
  351. The function name (optional).
  352. Examples
  353. --------
  354. >>> net = tl.layers.Input([10, 200])
  355. >>> net = tl.layers.LeakyReLU6(alpha=0.5)(net)
  356. Returns
  357. -------
  358. Tensor
  359. A ``Tensor`` in the same type as ``x``.
  360. References
  361. ----------
  362. - `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
  363. - `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__
  364. """
  365. def __init__(self, alpha=0.2):
  366. super(LeakyReLU6, self).__init__()
  367. self._built = True
  368. if not (0 < alpha <= 1):
  369. raise ValueError("`alpha` value must be in [0, 1]`")
  370. self.alpha = alpha
  371. self.minimum = tl.ops.Minimum()
  372. self.maximum = tl.ops.Maximum()
  373. def forward(self, x):
  374. return self.minimum(self.maximum(x, self.alpha * x), 6)
  375. class LeakyTwiceRelu6(Module):
  376. """
  377. This activation function is a modified version :func:`leaky_relu` introduced by the following paper:
  378. `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
  379. This activation function also follows the behaviour of the activation function :func:`tf.ops.relu6` introduced by the following paper:
  380. `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__
  381. This function push further the logic by adding `leaky` behaviour both below zero and above six.
  382. The function return the following results:
  383. - When x < 0: ``f(x) = alpha_low * x``.
  384. - When x in [0, 6]: ``f(x) = x``.
  385. - When x > 6: ``f(x) = 6 + (alpha_high * (x-6))``.
  386. Parameters
  387. ----------
  388. x : Tensor
  389. Support input type ``float``, ``double``, ``int32``, ``int64``, ``uint8``, ``int16``, or ``int8``.
  390. alpha_low : float
  391. Slope for x < 0: ``f(x) = alpha_low * x``.
  392. alpha_high : float
  393. Slope for x < 6: ``f(x) = 6 (alpha_high * (x-6))``.
  394. name : str
  395. The function name (optional).
  396. Examples
  397. --------
  398. >>> net = tl.layers.Input([10, 200])
  399. >>> net = tl.layers.LeakyTwiceRelu6(alpha_low=0.5, alpha_high=0.2)(net)
  400. Returns
  401. -------
  402. Tensor
  403. A ``Tensor`` in the same type as ``x``.
  404. References
  405. ----------
  406. - `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
  407. - `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__
  408. """
  409. def __init__(self, alpha_low=0.2, alpha_high=0.2):
  410. super(LeakyTwiceRelu6, self).__init__()
  411. self._built = True
  412. if not (0 < alpha_high <= 1):
  413. raise ValueError("`alpha_high` value must be in [0, 1]`")
  414. if not (0 < alpha_low <= 1):
  415. raise ValueError("`alpha_low` value must be in [0, 1]`")
  416. self.alpha_low = alpha_low
  417. self.alpha_high = alpha_high
  418. self.minimum = tl.ops.Minimum()
  419. self.maximum = tl.ops.Maximum()
  420. def forward(self, x):
  421. x_is_above_0 = self.minimum(x, 6 * (1 - self.alpha_high) + self.alpha_high * x)
  422. x_is_below_0 = self.minimum(self.alpha_low * x, 0)
  423. return self.maximum(x_is_above_0, x_is_below_0)
  424. class Swish(Module):
  425. """Swish function.
  426. See `Swish: a Self-Gated Activation Function <https://arxiv.org/abs/1710.05941>`__.
  427. Parameters
  428. ----------
  429. x : Tensor
  430. input.
  431. name: str
  432. function name (optional).
  433. Examples
  434. --------
  435. >>> net = tl.layers.Input([10, 200])
  436. >>> net = tl.layers.Swish()(net)
  437. Returns
  438. -------
  439. Tensor
  440. A ``Tensor`` in the same type as ``x``.
  441. """
  442. def __init__(self):
  443. super(Swish, self).__init__()
  444. self.sigmoid = tl.ops.Sigmoid()
  445. self._built = True
  446. def forward(self, x):
  447. return self.sigmoid(x) * x
  448. class HardTanh(Module):
  449. """Hard tanh activation function.
  450. Which is a ramp function with low bound of -1 and upper bound of 1, shortcut is `htanh`.
  451. Parameters
  452. ----------
  453. x : Tensor
  454. input.
  455. name : str
  456. The function name (optional).
  457. Examples
  458. --------
  459. >>> net = tl.layers.Input([10, 200])
  460. >>> net = tl.layers.HardTanh()(net)
  461. Returns
  462. -------
  463. Tensor
  464. A ``Tensor`` in the same type as ``x``.
  465. """
  466. def __init__(self):
  467. super(HardTanh, self).__init__()
  468. self._built = True
  469. def forward(self, x):
  470. return tl.ops.clip_by_value(x, -1, 1)
  471. class Mish(Module):
  472. """Mish activation function.
  473. Reference: [Mish: A Self Regularized Non-Monotonic Neural Activation Function .Diganta Misra, 2019]<https://arxiv.org/abs/1908.08681>
  474. Parameters
  475. ----------
  476. x : Tensor
  477. input.
  478. Examples
  479. --------
  480. >>> net = tl.layers.Input([10, 200])
  481. >>> net = tl.layers.Mish()(net)
  482. Returns
  483. -------
  484. Tensor
  485. A ``Tensor`` in the same type as ``x``.
  486. """
  487. def __init__(self):
  488. super(Mish, self).__init__()
  489. self._tanh = tl.ops.Tanh()
  490. self._softplus = tl.ops.Softplus()
  491. self._built = True
  492. def forward(self, x):
  493. return x * self._tanh(self._softplus(x))

TensorLayer3.0 是一款兼容多种深度学习框架为计算后端的深度学习库。计划兼容TensorFlow, Pytorch, MindSpore, Paddle.