-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
swish and mish activation functions (#1235)
* swish and mish activation functions Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com> * update examples Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com> * add new classes to __init__ Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com> * networks.rst Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright 2020 MONAI Consortium | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import torch | ||
from torch import nn | ||
|
||
|
||
class Swish(nn.Module): | ||
r"""Applies the element-wise function: | ||
.. math:: | ||
\text{Swish}(x) = x * \text{Sigmoid}(\alpha * x) for constant value alpha. | ||
Citation: Searching for Activation Functions, Ramachandran et al., 2017, https://arxiv.org/abs/1710.05941. | ||
Shape: | ||
- Input: :math:`(N, *)` where `*` means, any number of additional | ||
dimensions | ||
- Output: :math:`(N, *)`, same shape as the input | ||
Examples:: | ||
>>> m = Act['swish']() | ||
>>> input = torch.randn(2) | ||
>>> output = m(input) | ||
""" | ||
|
||
def __init__(self, alpha=1.0): | ||
super().__init__() | ||
self.alpha = alpha | ||
|
||
def forward(self, input: torch.Tensor) -> torch.Tensor: | ||
return input * torch.sigmoid(self.alpha * input) | ||
|
||
|
||
class Mish(nn.Module): | ||
r"""Applies the element-wise function: | ||
.. math:: | ||
\text{Mwish}(x) = x * tanh(\text{softplus}(x)). | ||
Citation: Mish: A Self Regularized Non-Monotonic Activation Function, Diganta Misra, 2019, https://arxiv.org/abs/1908.08681. | ||
Shape: | ||
- Input: :math:`(N, *)` where `*` means, any number of additional | ||
dimensions | ||
- Output: :math:`(N, *)`, same shape as the input | ||
Examples:: | ||
>>> m = Act['mish']() | ||
>>> input = torch.randn(2) | ||
>>> output = m(input) | ||
""" | ||
|
||
def forward(self, input: torch.Tensor) -> torch.Tensor: | ||
return input * torch.tanh(torch.nn.functional.softplus(input)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters