activation.py 923 B

123456789101112131415161718192021
  1. # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. """Activation modules."""
  3. import torch
  4. import torch.nn as nn
  5. class AGLU(nn.Module):
  6. """Unified activation function module from https://github.com/kostas1515/AGLU."""
  7. def __init__(self, device=None, dtype=None) -> None:
  8. """Initialize the Unified activation function."""
  9. super().__init__()
  10. self.act = nn.Softplus(beta=-1.0)
  11. self.lambd = nn.Parameter(nn.init.uniform_(torch.empty(1, device=device, dtype=dtype))) # lambda parameter
  12. self.kappa = nn.Parameter(nn.init.uniform_(torch.empty(1, device=device, dtype=dtype))) # kappa parameter
  13. def forward(self, x: torch.Tensor) -> torch.Tensor:
  14. """Compute the forward pass of the Unified activation function."""
  15. lam = torch.clamp(self.lambd, min=0.0001)
  16. return torch.exp((1 / lam) * self.act((self.kappa * x) - torch.log(lam)))