update activations.py

pull/1/head
Glenn Jocher 5 years ago
parent eb99dff9ef
commit f346da9f2b

@ -3,8 +3,21 @@ import torch.nn as nn
import torch.nn.functional as F import torch.nn.functional as F
# Swish ------------------------------------------------------------------------ # Swish https://arxiv.org/pdf/1905.02244.pdf ---------------------------------------------------------------------------
class SwishImplementation(torch.autograd.Function): class Swish(nn.Module): #
@staticmethod
def forward(x):
return x * torch.sigmoid(x)
class HardSwish(nn.Module):
@staticmethod
def forward(x):
return x * F.hardtanh(x + 3, 0., 6., True) / 6.
class MemoryEfficientSwish(nn.Module):
class F(torch.autograd.Function):
@staticmethod @staticmethod
def forward(ctx, x): def forward(ctx, x):
ctx.save_for_backward(x) ctx.save_for_backward(x)
@ -16,27 +29,19 @@ class SwishImplementation(torch.autograd.Function):
sx = torch.sigmoid(x) sx = torch.sigmoid(x)
return grad_output * (sx * (1 + x * (1 - sx))) return grad_output * (sx * (1 + x * (1 - sx)))
def forward(self, x):
class MemoryEfficientSwish(nn.Module): return self.F.apply(x)
@staticmethod
def forward(x):
return SwishImplementation.apply(x)
class HardSwish(nn.Module): # https://arxiv.org/pdf/1905.02244.pdf
@staticmethod
def forward(x):
return x * F.hardtanh(x + 3, 0., 6., True) / 6.
class Swish(nn.Module): # Mish https://github.com/digantamisra98/Mish --------------------------------------------------------------------------
class Mish(nn.Module):
@staticmethod @staticmethod
def forward(x): def forward(x):
return x * torch.sigmoid(x) return x * F.softplus(x).tanh()
# Mish ------------------------------------------------------------------------ class MemoryEfficientMish(nn.Module):
class MishImplementation(torch.autograd.Function): class F(torch.autograd.Function):
@staticmethod @staticmethod
def forward(ctx, x): def forward(ctx, x):
ctx.save_for_backward(x) ctx.save_for_backward(x)
@ -49,23 +54,14 @@ class MishImplementation(torch.autograd.Function):
fx = F.softplus(x).tanh() fx = F.softplus(x).tanh()
return grad_output * (fx + x * sx * (1 - fx * fx)) return grad_output * (fx + x * sx * (1 - fx * fx))
def forward(self, x):
class MemoryEfficientMish(nn.Module): return self.F.apply(x)
@staticmethod
def forward(x):
return MishImplementation.apply(x)
class Mish(nn.Module): # https://github.com/digantamisra98/Mish
@staticmethod
def forward(x):
return x * F.softplus(x).tanh()
# FReLU https://arxiv.org/abs/2007.11824 -------------------------------------- # FReLU https://arxiv.org/abs/2007.11824 -------------------------------------------------------------------------------
class FReLU(nn.Module): class FReLU(nn.Module):
def __init__(self, c1, k=3): # ch_in, kernel def __init__(self, c1, k=3): # ch_in, kernel
super(FReLU, self).__init__() super().__init__()
self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1) self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1)
self.bn = nn.BatchNorm2d(c1) self.bn = nn.BatchNorm2d(c1)

Loading…
Cancel
Save