Skip to content

Commit

Permalink
Update Mish to default PyTorch 1.9 version (#2733)
Browse files Browse the repository at this point in the history
* Update Mish to default PyTorch 1.9 version
  • Loading branch information
digantamisra98 authored Aug 11, 2021
1 parent ce4960d commit 33184fa
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions monai/networks/blocks/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@
import torch
from torch import nn

from monai.utils import optional_import

if optional_import("torch.nn.functional", name="mish")[1]:

def monai_mish(x):
return torch.nn.functional.mish(x, inplace=True)


else:

def monai_mish(x):
return x * torch.tanh(torch.nn.functional.softplus(x))


class Swish(nn.Module):
r"""Applies the element-wise function:
Expand All @@ -30,6 +43,8 @@ class Swish(nn.Module):
Examples::
>>> import torch
>>> from monai.networks.layers.factories import Act
>>> m = Act['swish']()
>>> input = torch.randn(2)
>>> output = m(input)
Expand Down Expand Up @@ -85,6 +100,8 @@ class MemoryEfficientSwish(nn.Module):
Examples::
>>> import torch
>>> from monai.networks.layers.factories import Act
>>> m = Act['memswish']()
>>> input = torch.randn(2)
>>> output = m(input)
Expand All @@ -111,10 +128,12 @@ class Mish(nn.Module):
Examples::
>>> import torch
>>> from monai.networks.layers.factories import Act
>>> 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))
def forward(self, input: torch.Tensor):
return monai_mish(input)

0 comments on commit 33184fa

Please sign in to comment.