You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
4.4 KiB
160 lines
4.4 KiB
5 years ago
|
""" Activations
|
||
|
|
||
|
A collection of activations fn and modules with a common interface so that they can
|
||
|
easily be swapped. All have an `inplace` arg even if not used.
|
||
|
|
||
4 years ago
|
Hacked together by / Copyright 2020 Ross Wightman
|
||
5 years ago
|
"""
|
||
|
|
||
5 years ago
|
import torch
|
||
|
from torch import nn as nn
|
||
|
from torch.nn import functional as F
|
||
|
|
||
|
|
||
5 years ago
|
def swish(x, inplace: bool = False):
|
||
|
"""Swish - Described in: https://arxiv.org/abs/1710.05941
|
||
|
"""
|
||
|
return x.mul_(x.sigmoid()) if inplace else x.mul(x.sigmoid())
|
||
5 years ago
|
|
||
|
|
||
|
class Swish(nn.Module):
|
||
5 years ago
|
def __init__(self, inplace: bool = False):
|
||
5 years ago
|
super(Swish, self).__init__()
|
||
|
self.inplace = inplace
|
||
|
|
||
|
def forward(self, x):
|
||
|
return swish(x, self.inplace)
|
||
|
|
||
|
|
||
5 years ago
|
def mish(x, inplace: bool = False):
|
||
|
"""Mish: A Self Regularized Non-Monotonic Neural Activation Function - https://arxiv.org/abs/1908.08681
|
||
|
NOTE: I don't have a working inplace variant
|
||
|
"""
|
||
|
return x.mul(F.softplus(x).tanh())
|
||
|
|
||
|
|
||
5 years ago
|
class Mish(nn.Module):
|
||
5 years ago
|
"""Mish: A Self Regularized Non-Monotonic Neural Activation Function - https://arxiv.org/abs/1908.08681
|
||
|
"""
|
||
5 years ago
|
def __init__(self, inplace: bool = False):
|
||
5 years ago
|
super(Mish, self).__init__()
|
||
|
|
||
|
def forward(self, x):
|
||
5 years ago
|
return mish(x)
|
||
5 years ago
|
|
||
|
|
||
5 years ago
|
def sigmoid(x, inplace: bool = False):
|
||
5 years ago
|
return x.sigmoid_() if inplace else x.sigmoid()
|
||
|
|
||
|
|
||
|
# PyTorch has this, but not with a consistent inplace argmument interface
|
||
|
class Sigmoid(nn.Module):
|
||
5 years ago
|
def __init__(self, inplace: bool = False):
|
||
5 years ago
|
super(Sigmoid, self).__init__()
|
||
|
self.inplace = inplace
|
||
|
|
||
|
def forward(self, x):
|
||
|
return x.sigmoid_() if self.inplace else x.sigmoid()
|
||
|
|
||
|
|
||
5 years ago
|
def tanh(x, inplace: bool = False):
|
||
5 years ago
|
return x.tanh_() if inplace else x.tanh()
|
||
|
|
||
|
|
||
|
# PyTorch has this, but not with a consistent inplace argmument interface
|
||
|
class Tanh(nn.Module):
|
||
5 years ago
|
def __init__(self, inplace: bool = False):
|
||
5 years ago
|
super(Tanh, self).__init__()
|
||
|
self.inplace = inplace
|
||
|
|
||
|
def forward(self, x):
|
||
|
return x.tanh_() if self.inplace else x.tanh()
|
||
|
|
||
|
|
||
5 years ago
|
def hard_swish(x, inplace: bool = False):
|
||
5 years ago
|
inner = F.relu6(x + 3.).div_(6.)
|
||
|
return x.mul_(inner) if inplace else x.mul(inner)
|
||
|
|
||
|
|
||
|
class HardSwish(nn.Module):
|
||
5 years ago
|
def __init__(self, inplace: bool = False):
|
||
5 years ago
|
super(HardSwish, self).__init__()
|
||
|
self.inplace = inplace
|
||
|
|
||
|
def forward(self, x):
|
||
5 years ago
|
return hard_swish(x, self.inplace)
|
||
5 years ago
|
|
||
|
|
||
5 years ago
|
def hard_sigmoid(x, inplace: bool = False):
|
||
5 years ago
|
if inplace:
|
||
|
return x.add_(3.).clamp_(0., 6.).div_(6.)
|
||
|
else:
|
||
|
return F.relu6(x + 3.) / 6.
|
||
|
|
||
|
|
||
|
class HardSigmoid(nn.Module):
|
||
5 years ago
|
def __init__(self, inplace: bool = False):
|
||
5 years ago
|
super(HardSigmoid, self).__init__()
|
||
|
self.inplace = inplace
|
||
|
|
||
|
def forward(self, x):
|
||
|
return hard_sigmoid(x, self.inplace)
|
||
|
|
||
5 years ago
|
|
||
|
def hard_mish(x, inplace: bool = False):
|
||
|
""" Hard Mish
|
||
|
Experimental, based on notes by Mish author Diganta Misra at
|
||
|
https://github.com/digantamisra98/H-Mish/blob/0da20d4bc58e696b6803f2523c58d3c8a82782d0/README.md
|
||
|
"""
|
||
|
if inplace:
|
||
|
return x.mul_(0.5 * (x + 2).clamp(min=0, max=2))
|
||
|
else:
|
||
|
return 0.5 * x * (x + 2).clamp(min=0, max=2)
|
||
|
|
||
|
|
||
|
class HardMish(nn.Module):
|
||
|
def __init__(self, inplace: bool = False):
|
||
|
super(HardMish, self).__init__()
|
||
|
self.inplace = inplace
|
||
|
|
||
|
def forward(self, x):
|
||
|
return hard_mish(x, self.inplace)
|
||
4 years ago
|
|
||
|
|
||
|
class PReLU(nn.PReLU):
|
||
|
"""Applies PReLU (w/ dummy inplace arg)
|
||
|
"""
|
||
|
def __init__(self, num_parameters: int = 1, init: float = 0.25, inplace: bool = False) -> None:
|
||
|
super(PReLU, self).__init__(num_parameters=num_parameters, init=init)
|
||
|
|
||
|
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
||
|
return F.prelu(input, self.weight)
|
||
|
|
||
|
|
||
|
def gelu(x: torch.Tensor, inplace: bool = False) -> torch.Tensor:
|
||
|
return F.gelu(x)
|
||
|
|
||
|
|
||
|
class GELU(nn.Module):
|
||
|
"""Applies the Gaussian Error Linear Units function (w/ dummy inplace arg)
|
||
|
"""
|
||
|
def __init__(self, inplace: bool = False):
|
||
|
super(GELU, self).__init__()
|
||
|
|
||
|
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
||
|
return F.gelu(input)
|
||
2 years ago
|
|
||
|
|
||
|
def gelu_tanh(x: torch.Tensor, inplace: bool = False) -> torch.Tensor:
|
||
|
return F.gelu(x, approximate='tanh')
|
||
|
|
||
|
|
||
|
class GELUTanh(nn.Module):
|
||
|
"""Applies the Gaussian Error Linear Units function (w/ dummy inplace arg)
|
||
|
"""
|
||
|
def __init__(self, inplace: bool = False):
|
||
|
super(GELUTanh, self).__init__()
|
||
|
|
||
|
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
||
|
return F.gelu(input, approximate='tanh')
|