activations.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. """ Activations
  2. A collection of activations fn and modules with a common interface so that they can
  3. easily be swapped. All have an `inplace` arg even if not used.
  4. Hacked together by / Copyright 2020 Ross Wightman
  5. """
  6. import torch
  7. from torch import nn as nn
  8. from torch.nn import functional as F
  9. def swish(x, inplace: bool = False):
  10. """Swish - Described in: https://arxiv.org/abs/1710.05941
  11. """
  12. return x.mul_(x.sigmoid()) if inplace else x.mul(x.sigmoid())
  13. class Swish(nn.Module):
  14. def __init__(self, inplace: bool = False):
  15. super().__init__()
  16. self.inplace = inplace
  17. def forward(self, x):
  18. return swish(x, self.inplace)
  19. def mish(x, inplace: bool = False):
  20. """Mish: A Self Regularized Non-Monotonic Neural Activation Function - https://arxiv.org/abs/1908.08681
  21. NOTE: I don't have a working inplace variant
  22. """
  23. return x.mul(F.softplus(x).tanh())
  24. class Mish(nn.Module):
  25. """Mish: A Self Regularized Non-Monotonic Neural Activation Function - https://arxiv.org/abs/1908.08681
  26. """
  27. def __init__(self, inplace: bool = False):
  28. super().__init__()
  29. def forward(self, x):
  30. return mish(x)
  31. def sigmoid(x, inplace: bool = False):
  32. return x.sigmoid_() if inplace else x.sigmoid()
  33. # PyTorch has this, but not with a consistent inplace argument interface
  34. class Sigmoid(nn.Module):
  35. def __init__(self, inplace: bool = False):
  36. super().__init__()
  37. self.inplace = inplace
  38. def forward(self, x):
  39. return x.sigmoid_() if self.inplace else x.sigmoid()
  40. def tanh(x, inplace: bool = False):
  41. return x.tanh_() if inplace else x.tanh()
  42. # PyTorch has this, but not with a consistent inplace argument interface
  43. class Tanh(nn.Module):
  44. def __init__(self, inplace: bool = False):
  45. super().__init__()
  46. self.inplace = inplace
  47. def forward(self, x):
  48. return x.tanh_() if self.inplace else x.tanh()
  49. def hard_swish(x, inplace: bool = False):
  50. inner = F.relu6(x + 3.).div_(6.)
  51. return x.mul_(inner) if inplace else x.mul(inner)
  52. class HardSwish(nn.Module):
  53. def __init__(self, inplace: bool = False):
  54. super().__init__()
  55. self.inplace = inplace
  56. def forward(self, x):
  57. return hard_swish(x, self.inplace)
  58. def hard_sigmoid(x, inplace: bool = False):
  59. if inplace:
  60. return x.add_(3.).clamp_(0., 6.).div_(6.)
  61. else:
  62. return F.relu6(x + 3.) / 6.
  63. class HardSigmoid(nn.Module):
  64. def __init__(self, inplace: bool = False):
  65. super().__init__()
  66. self.inplace = inplace
  67. def forward(self, x):
  68. return hard_sigmoid(x, self.inplace)
  69. def hard_mish(x, inplace: bool = False):
  70. """ Hard Mish
  71. Experimental, based on notes by Mish author Diganta Misra at
  72. https://github.com/digantamisra98/H-Mish/blob/0da20d4bc58e696b6803f2523c58d3c8a82782d0/README.md
  73. """
  74. if inplace:
  75. return x.mul_(0.5 * (x + 2).clamp(min=0, max=2))
  76. else:
  77. return 0.5 * x * (x + 2).clamp(min=0, max=2)
  78. class HardMish(nn.Module):
  79. def __init__(self, inplace: bool = False):
  80. super().__init__()
  81. self.inplace = inplace
  82. def forward(self, x):
  83. return hard_mish(x, self.inplace)
  84. class PReLU(nn.PReLU):
  85. """Applies PReLU (w/ dummy inplace arg)
  86. """
  87. def __init__(self, num_parameters: int = 1, init: float = 0.25, inplace: bool = False) -> None:
  88. super().__init__(num_parameters=num_parameters, init=init)
  89. def forward(self, input: torch.Tensor) -> torch.Tensor:
  90. return F.prelu(input, self.weight)
  91. def gelu(x: torch.Tensor, inplace: bool = False) -> torch.Tensor:
  92. return F.gelu(x)
  93. class GELU(nn.Module):
  94. """Applies the Gaussian Error Linear Units function (w/ dummy inplace arg)
  95. """
  96. def __init__(self, inplace: bool = False):
  97. super().__init__()
  98. def forward(self, input: torch.Tensor) -> torch.Tensor:
  99. return F.gelu(input)
  100. def gelu_tanh(x: torch.Tensor, inplace: bool = False) -> torch.Tensor:
  101. return F.gelu(x, approximate='tanh')
  102. class GELUTanh(nn.Module):
  103. """Applies the Gaussian Error Linear Units function (w/ dummy inplace arg)
  104. """
  105. def __init__(self, inplace: bool = False):
  106. super().__init__()
  107. def forward(self, input: torch.Tensor) -> torch.Tensor:
  108. return F.gelu(input, approximate='tanh')
  109. def quick_gelu(x: torch.Tensor, inplace: bool = False) -> torch.Tensor:
  110. return x * torch.sigmoid(1.702 * x)
  111. class QuickGELU(nn.Module):
  112. """Applies the Gaussian Error Linear Units function (w/ dummy inplace arg)
  113. """
  114. def __init__(self, inplace: bool = False):
  115. super().__init__()
  116. def forward(self, input: torch.Tensor) -> torch.Tensor:
  117. return quick_gelu(input)