homa 0.2.1__tar.gz → 0.2.93__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {homa-0.2.1 → homa-0.2.93}/PKG-INFO +1 -1
- {homa-0.2.1 → homa-0.2.93}/pyproject.toml +1 -1
- homa-0.2.93/src/homa/activations/APLU.py +49 -0
- homa-0.2.93/src/homa/activations/ActivationFunction.py +6 -0
- homa-0.2.93/src/homa/activations/AdaptiveActivationFunction.py +15 -0
- homa-0.2.93/src/homa/activations/BaseDLReLU.py +34 -0
- homa-0.2.93/src/homa/activations/CaLU.py +13 -0
- homa-0.2.93/src/homa/activations/DLReLU.py +6 -0
- homa-0.2.93/src/homa/activations/ERF.py +10 -0
- homa-0.2.93/src/homa/activations/Elliot.py +10 -0
- homa-0.2.93/src/homa/activations/ExpExpish.py +9 -0
- homa-0.2.93/src/homa/activations/ExponentialDLReLU.py +6 -0
- homa-0.2.93/src/homa/activations/ExponentialSwish.py +10 -0
- homa-0.2.93/src/homa/activations/GCU.py +9 -0
- homa-0.2.93/src/homa/activations/GaLU.py +11 -0
- homa-0.2.93/src/homa/activations/GaussianReLU.py +50 -0
- homa-0.2.93/src/homa/activations/GeneralizedSwish.py +10 -0
- homa-0.2.93/src/homa/activations/Gish.py +11 -0
- homa-0.2.93/src/homa/activations/LaLU.py +11 -0
- homa-0.2.93/src/homa/activations/LogLogish.py +10 -0
- homa-0.2.93/src/homa/activations/LogSigmoid.py +10 -0
- homa-0.2.93/src/homa/activations/Logish.py +10 -0
- homa-0.2.93/src/homa/activations/MeLU.py +11 -0
- homa-0.2.93/src/homa/activations/MexicanReLU.py +49 -0
- homa-0.2.93/src/homa/activations/MinSin.py +10 -0
- homa-0.2.93/src/homa/activations/NReLU.py +12 -0
- homa-0.2.93/src/homa/activations/NoisyReLU.py +6 -0
- homa-0.2.93/src/homa/activations/PLogish.py +6 -0
- homa-0.2.93/src/homa/activations/ParametricLogish.py +13 -0
- homa-0.2.93/src/homa/activations/Phish.py +11 -0
- homa-0.2.93/src/homa/activations/RReLU.py +16 -0
- homa-0.2.93/src/homa/activations/RandomizedSlopedReLU.py +7 -0
- homa-0.2.93/src/homa/activations/SGELU.py +12 -0
- homa-0.2.93/src/homa/activations/SReLU.py +37 -0
- homa-0.2.93/src/homa/activations/SelfArctan.py +9 -0
- homa-0.2.93/src/homa/activations/ShiftedReLU.py +10 -0
- homa-0.2.93/src/homa/activations/SigmoidDerivative.py +10 -0
- homa-0.2.93/src/homa/activations/SineReLU.py +11 -0
- homa-0.2.93/src/homa/activations/SlopedReLU.py +13 -0
- homa-0.2.93/src/homa/activations/SmallGaLU.py +11 -0
- homa-0.2.93/src/homa/activations/Smish.py +9 -0
- homa-0.2.93/src/homa/activations/SoftsignRReLU.py +17 -0
- homa-0.2.93/src/homa/activations/Suish.py +11 -0
- homa-0.2.93/src/homa/activations/TBSReLU.py +13 -0
- homa-0.2.93/src/homa/activations/TSReLU.py +10 -0
- homa-0.2.93/src/homa/activations/TangentBipolarSigmoidReLU.py +6 -0
- homa-0.2.93/src/homa/activations/TangentSigmoidReLU.py +6 -0
- homa-0.2.93/src/homa/activations/TeLU.py +9 -0
- homa-0.2.93/src/homa/activations/TripleStateSwish.py +15 -0
- homa-0.2.93/src/homa/activations/WideMeLU.py +15 -0
- homa-0.2.93/src/homa/activations/__init__.py +49 -0
- homa-0.2.93/src/homa/activations/learnable/AOAF.py +16 -0
- homa-0.2.93/src/homa/activations/learnable/AReLU.py +19 -0
- homa-0.2.93/src/homa/activations/learnable/DPReLU.py +16 -0
- homa-0.2.93/src/homa/activations/learnable/DualLine.py +18 -0
- homa-0.2.93/src/homa/activations/learnable/FReLU.py +14 -0
- homa-0.2.93/src/homa/activations/learnable/LeLeLU.py +14 -0
- homa-0.2.93/src/homa/activations/learnable/PERU.py +16 -0
- homa-0.2.93/src/homa/activations/learnable/PiLU.py +18 -0
- homa-0.2.93/src/homa/activations/learnable/ShiLU.py +16 -0
- homa-0.2.93/src/homa/activations/learnable/StarReLU.py +16 -0
- homa-0.2.93/src/homa/activations/learnable/__init__.py +10 -0
- homa-0.2.93/src/homa/activations/learnable/concerns/ChannelBased.py +38 -0
- homa-0.2.93/src/homa/activations/learnable/concerns/__init__.py +1 -0
- homa-0.2.93/src/homa/cli/Commands/Command.py +2 -0
- homa-0.2.93/src/homa/cli/Commands/InitCommand.py +34 -0
- homa-0.2.93/src/homa/cli/Commands/__init__.py +2 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/cli/HomaCommand.py +4 -0
- homa-0.2.1/src/homa/vision/ClassificationModel.py → homa-0.2.93/src/homa/vision/Classifier.py +1 -1
- {homa-0.2.1 → homa-0.2.93}/src/homa/vision/Resnet.py +2 -2
- homa-0.2.93/src/homa/vision/StochasticClassifier.py +29 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/vision/StochasticSwin.py +3 -1
- {homa-0.2.1 → homa-0.2.93}/src/homa/vision/Swin.py +4 -3
- homa-0.2.93/src/homa/vision/__init__.py +5 -0
- homa-0.2.93/src/homa/vision/utils.py +12 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa.egg-info/PKG-INFO +1 -1
- homa-0.2.93/src/homa.egg-info/SOURCES.txt +116 -0
- homa-0.2.1/src/homa/activations/__init__.py +0 -2
- homa-0.2.1/src/homa/activations/classes/APLU.py +0 -53
- homa-0.2.1/src/homa/activations/classes/GALU.py +0 -61
- homa-0.2.1/src/homa/activations/classes/MELU.py +0 -63
- homa-0.2.1/src/homa/activations/classes/PDELU.py +0 -44
- homa-0.2.1/src/homa/activations/classes/SReLU.py +0 -50
- homa-0.2.1/src/homa/activations/classes/SmallGALU.py +0 -48
- homa-0.2.1/src/homa/activations/classes/WideMELU.py +0 -68
- homa-0.2.1/src/homa/activations/classes/__init__.py +0 -7
- homa-0.2.1/src/homa/activations/utils.py +0 -94
- homa-0.2.1/src/homa/vision/StochasticClassifier.py +0 -50
- homa-0.2.1/src/homa/vision/StochasticResnet.py +0 -9
- homa-0.2.1/src/homa/vision/__init__.py +0 -4
- homa-0.2.1/src/homa.egg-info/SOURCES.txt +0 -61
- {homa-0.2.1 → homa-0.2.93}/README.md +0 -0
- {homa-0.2.1 → homa-0.2.93}/setup.cfg +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/__init__.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/cli/namespaces/CacheNamespace.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/cli/namespaces/MakeNamespace.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/cli/namespaces/__init__.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/device.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/ensemble/Ensemble.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/ensemble/__init__.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/ensemble/concerns/CalculatesMetricNecessities.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/ensemble/concerns/PredictsProbabilities.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/ensemble/concerns/ReportsClassificationMetrics.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/ensemble/concerns/ReportsEnsembleAccuracy.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/ensemble/concerns/ReportsEnsembleF1.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/ensemble/concerns/ReportsEnsembleKappa.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/ensemble/concerns/ReportsLogits.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/ensemble/concerns/ReportsSize.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/ensemble/concerns/StoresModels.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/ensemble/concerns/__init__.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/loss/LogitNormLoss.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/loss/Loss.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/loss/__init__.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/settings.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/torch/__init__.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/torch/helpers.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/utils.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/vision/Model.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/vision/concerns/HasLabels.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/vision/concerns/HasLogits.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/vision/concerns/HasProbabilities.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/vision/concerns/ReportsAccuracy.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/vision/concerns/ReportsMetrics.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/vision/concerns/Trainable.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/vision/concerns/__init__.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/vision/modules/ResnetModule.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/vision/modules/SwinModule.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa/vision/modules/__init__.py +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa.egg-info/dependency_links.txt +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa.egg-info/entry_points.txt +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa.egg-info/requires.txt +0 -0
- {homa-0.2.1 → homa-0.2.93}/src/homa.egg-info/top_level.txt +0 -0
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "homa"
|
|
7
|
-
version = "0.2.
|
|
7
|
+
version = "0.2.93"
|
|
8
8
|
description = "A curated list of machine learning and deep learning helpers."
|
|
9
9
|
authors = [
|
|
10
10
|
{ name="Taha Shieenavaz", email="tahashieenavaz@gmail.com" },
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class APLU(torch.nn.Module):
|
|
5
|
+
def __init__(
|
|
6
|
+
self, channels: int | None = None, n: int = 2, init_b: str = "linspace"
|
|
7
|
+
):
|
|
8
|
+
super().__init__()
|
|
9
|
+
self.n = n
|
|
10
|
+
self.init_b = init_b
|
|
11
|
+
if channels is None:
|
|
12
|
+
self.register_parameter("a", None)
|
|
13
|
+
self.register_parameter("b", None)
|
|
14
|
+
else:
|
|
15
|
+
self._init_params(channels, device=None, dtype=None)
|
|
16
|
+
|
|
17
|
+
def _init_params(self, channels, device, dtype):
|
|
18
|
+
a = torch.zeros(channels, self.n, device=device, dtype=dtype)
|
|
19
|
+
if self.init_b == "linspace":
|
|
20
|
+
b = (
|
|
21
|
+
torch.linspace(-1.0, 1.0, steps=self.n, device=device, dtype=dtype)
|
|
22
|
+
.expand(channels, -1)
|
|
23
|
+
.contiguous()
|
|
24
|
+
)
|
|
25
|
+
else:
|
|
26
|
+
b = torch.empty(channels, self.n, device=device, dtype=dtype).uniform_(
|
|
27
|
+
-1.0, 1.0
|
|
28
|
+
)
|
|
29
|
+
self.a = torch.nn.Parameter(a)
|
|
30
|
+
self.b = torch.nn.Parameter(b)
|
|
31
|
+
|
|
32
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
33
|
+
if self.a is None or self.b is None:
|
|
34
|
+
self._init_params(x.shape[1], device=x.device, dtype=x.dtype)
|
|
35
|
+
|
|
36
|
+
y = F.relu(x)
|
|
37
|
+
x_exp = x.unsqueeze(-1)
|
|
38
|
+
expand_shape = (
|
|
39
|
+
(
|
|
40
|
+
1,
|
|
41
|
+
x.shape[1],
|
|
42
|
+
)
|
|
43
|
+
+ (1,) * (x.dim() - 2)
|
|
44
|
+
+ (self.n,)
|
|
45
|
+
)
|
|
46
|
+
a = self.a.view(*expand_shape)
|
|
47
|
+
b = self.b.view(*expand_shape)
|
|
48
|
+
hinges = (-x_exp + b).clamp_max(0.0)
|
|
49
|
+
return y + (a * hinges).sum(dim=-1)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from .ActivationFunction import ActivationFunction
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class AdaptiveActivationFunction(ActivationFunction):
|
|
6
|
+
def __init__(self):
|
|
7
|
+
super().__init__()
|
|
8
|
+
|
|
9
|
+
def __repr__(self):
|
|
10
|
+
arguments_text = ""
|
|
11
|
+
|
|
12
|
+
if hasattr(self, "num_channels"):
|
|
13
|
+
arguments_text = f"channels={self.num_channels}"
|
|
14
|
+
|
|
15
|
+
return f"{__class__.__name__}({arguments_text})"
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from .ActivationFunction import ActivationFunction
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class BaseDLReLU(ActivationFunction):
|
|
6
|
+
def __init__(self, a: float = 0.01, init_mse: float = 1.0, mode: str = "linear"):
|
|
7
|
+
super().__init__()
|
|
8
|
+
assert 0.0 < a < 1.0, "a must be in (0,1)"
|
|
9
|
+
assert mode in ("linear", "exp")
|
|
10
|
+
self.a = float(a)
|
|
11
|
+
self.mode = mode
|
|
12
|
+
self.register_buffer("prev_mse", torch.tensor(float(init_mse)))
|
|
13
|
+
|
|
14
|
+
@torch.no_grad()
|
|
15
|
+
def set_prev_mse(self, mse_value):
|
|
16
|
+
if isinstance(mse_value, torch.Tensor):
|
|
17
|
+
mse_value = float(mse_value.detach().cpu().item())
|
|
18
|
+
self.prev_mse.fill_(mse_value)
|
|
19
|
+
|
|
20
|
+
@torch.no_grad()
|
|
21
|
+
def update_from_loss(self, loss_tensor: torch.Tensor):
|
|
22
|
+
self.set_prev_mse(loss_tensor)
|
|
23
|
+
|
|
24
|
+
def forward(self, z: torch.Tensor, mse_prev: torch.Tensor | float | None = None):
|
|
25
|
+
b_t = (
|
|
26
|
+
self.prev_mse
|
|
27
|
+
if mse_prev is None
|
|
28
|
+
else (torch.as_tensor(mse_prev, device=z.device, dtype=z.dtype))
|
|
29
|
+
)
|
|
30
|
+
if self.mode == "linear":
|
|
31
|
+
slope = self.a * b_t
|
|
32
|
+
else:
|
|
33
|
+
slope = self.a * torch.exp(-b_t)
|
|
34
|
+
return torch.where(z >= 0, z, slope * z)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import math
|
|
3
|
+
from .ActivationFunction import ActivationFunction
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class CaLU(ActivationFunction):
|
|
7
|
+
def __init__(self, *args, **kwargs):
|
|
8
|
+
super().__init__(*args, **kwargs)
|
|
9
|
+
|
|
10
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
11
|
+
a = torch.arctan(x) / math.pi
|
|
12
|
+
b = 0.5
|
|
13
|
+
return x * (a + b)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from .ActivationFunction import ActivationFunction
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Elliot(ActivationFunction):
|
|
6
|
+
def __init__(self, *args, **kwargs):
|
|
7
|
+
super().__init__(*args, **kwargs)
|
|
8
|
+
|
|
9
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
10
|
+
return 0.5 + torch.div(0.5 * x, 1 + torch.abs(x))
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from .GaussianReLU import GaussianReLU
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class GaLU(GaussianReLU):
|
|
5
|
+
def __init__(
|
|
6
|
+
self,
|
|
7
|
+
channels: int | None = None,
|
|
8
|
+
max_input: float = 1.0,
|
|
9
|
+
):
|
|
10
|
+
self.hats = [(2.0, 2.0), (1.0, 1.0), (3.0, 1.0)]
|
|
11
|
+
super().__init__(self.hats, channels=channels, max_input=max_input)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from typing import Sequence, Tuple
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class GaussianReLU(torch.nn.Module):
|
|
6
|
+
def __init__(
|
|
7
|
+
self,
|
|
8
|
+
alphas_lambdas: Sequence[Tuple[float, float]],
|
|
9
|
+
channels: int | None = None,
|
|
10
|
+
max_input: float = 1.0,
|
|
11
|
+
):
|
|
12
|
+
super().__init__()
|
|
13
|
+
self.M = float(max_input)
|
|
14
|
+
self.register_buffer(
|
|
15
|
+
"alphas", torch.tensor([a for a, _ in alphas_lambdas], dtype=torch.float32)
|
|
16
|
+
)
|
|
17
|
+
self.register_buffer(
|
|
18
|
+
"lambdas", torch.tensor([l for _, l in alphas_lambdas], dtype=torch.float32)
|
|
19
|
+
)
|
|
20
|
+
self.K = len(alphas_lambdas)
|
|
21
|
+
|
|
22
|
+
if channels is None:
|
|
23
|
+
self.register_parameter("c0", None) # per-channel (PReLU slope)
|
|
24
|
+
self.register_parameter("c", None) # (C, K) coefficients
|
|
25
|
+
else:
|
|
26
|
+
self._init_params(channels, None, None)
|
|
27
|
+
|
|
28
|
+
def _init_params(self, C: int, device, dtype):
|
|
29
|
+
self.c0 = torch.nn.Parameter(torch.zeros(C, device=device, dtype=dtype))
|
|
30
|
+
self.c = torch.nn.Parameter(torch.zeros(C, self.K, device=device, dtype=dtype))
|
|
31
|
+
|
|
32
|
+
def _expand_param(p: torch.Tensor, x: torch.Tensor, add_K: bool = False):
|
|
33
|
+
shape = (
|
|
34
|
+
(1, x.shape[1]) + (1,) * (x.dim() - 2) + ((p.shape[-1],) if add_K else ())
|
|
35
|
+
)
|
|
36
|
+
return p.view(shape)
|
|
37
|
+
|
|
38
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
39
|
+
if self.c0 is None or self.c is None:
|
|
40
|
+
self._init_params(x.shape[1], x.device, x.dtype)
|
|
41
|
+
c0 = self._expand_param(self.c0, x)
|
|
42
|
+
y = torch.nn.functional.relu(x) - c0 * torch.nn.functional.relu(-x)
|
|
43
|
+
a = self.alphas.to(x.device, x.dtype).view(*((1,) * x.dim()), -1)
|
|
44
|
+
l = self.lambdas.to(x.device, x.dtype).view(*((1,) * x.dim()), -1)
|
|
45
|
+
xE = x.unsqueeze(-1)
|
|
46
|
+
term1 = (l * self.M - (xE - a * self.M).abs()).clamp_min(0.0)
|
|
47
|
+
term2 = ((xE - a * self.M - 2 * l * self.M).abs() - l * self.M).clamp_max(0.0)
|
|
48
|
+
hats = term1 + term2
|
|
49
|
+
c = self._expand_param(self.c, x, add_K=True) # (1,C,...,K)
|
|
50
|
+
return y + (c * hats).sum(dim=-1)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from .ActivationFunction import ActivationFunction
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class LaLU(ActivationFunction):
|
|
6
|
+
def __init__(self, *args, **kwargs):
|
|
7
|
+
super().__init__(*args, **kwargs)
|
|
8
|
+
|
|
9
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
10
|
+
phi_laplace = torch.where(x >= 0, 1 - 0.5 * torch.exp(-x), 0.5 * torch.exp(x))
|
|
11
|
+
return x * phi_laplace
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from .MexicanReLU import MexicanReLU
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class MeLU(MexicanReLU):
|
|
5
|
+
def __init__(self, channels: int | None = None, max_input: float = 1.0):
|
|
6
|
+
self.hats = [
|
|
7
|
+
(2.0, 2.0),
|
|
8
|
+
(1.0, 1.0),
|
|
9
|
+
(3.0, 1.0),
|
|
10
|
+
]
|
|
11
|
+
super().__init__(self.hats, channels=channels, max_input=max_input)
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from typing import Sequence, Tuple
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class MexicanReLU(torch.nn.Module):
|
|
6
|
+
def __init__(
|
|
7
|
+
self,
|
|
8
|
+
alphas_lambdas: Sequence[Tuple[float, float]],
|
|
9
|
+
channels: int | None = None,
|
|
10
|
+
max_input: float = 1.0,
|
|
11
|
+
):
|
|
12
|
+
super().__init__()
|
|
13
|
+
self.M = float(max_input)
|
|
14
|
+
self.register_buffer(
|
|
15
|
+
"alphas", torch.tensor([a for a, _ in alphas_lambdas], dtype=torch.float32)
|
|
16
|
+
)
|
|
17
|
+
self.register_buffer(
|
|
18
|
+
"lambdas", torch.tensor([l for _, l in alphas_lambdas], dtype=torch.float32)
|
|
19
|
+
)
|
|
20
|
+
self.K = len(alphas_lambdas)
|
|
21
|
+
|
|
22
|
+
if channels is None:
|
|
23
|
+
self.register_parameter("c0", None) # PReLU negative slope (per-channel)
|
|
24
|
+
self.register_parameter("c", None) # (C, K) coefficients
|
|
25
|
+
else:
|
|
26
|
+
self._init_params(channels, device=None, dtype=None)
|
|
27
|
+
|
|
28
|
+
def _init_params(self, C: int, device, dtype):
|
|
29
|
+
self.c0 = torch.nn.Parameter(torch.zeros(C, device=device, dtype=dtype))
|
|
30
|
+
self.c = torch.nn.Parameter(torch.zeros(C, self.K, device=device, dtype=dtype))
|
|
31
|
+
|
|
32
|
+
def _expand_param(p: torch.Tensor, x: torch.Tensor, n_extra: int = 0):
|
|
33
|
+
shape = (
|
|
34
|
+
(1, x.shape[1]) + (1,) * (x.dim() - 2) + ((p.shape[-1],) if n_extra else ())
|
|
35
|
+
)
|
|
36
|
+
return p.view(shape)
|
|
37
|
+
|
|
38
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
39
|
+
if self.c0 is None or self.c is None:
|
|
40
|
+
self._init_params(x.shape[1], x.device, x.dtype)
|
|
41
|
+
c0 = self._expand_param(self.c0, x)
|
|
42
|
+
y = F.relu(x) - c0 * F.relu(-x)
|
|
43
|
+
xE = x.unsqueeze(-1)
|
|
44
|
+
cE = self._expand_param(self.c, x, n_extra=1)
|
|
45
|
+
aE = self.alphas.to(x.device, x.dtype).view(*((1,) * x.dim()), -1) # (..., K)
|
|
46
|
+
lE = self.lambdas.to(x.device, x.dtype).view(*((1,) * x.dim()), -1) # (..., K)
|
|
47
|
+
hats = (lE * self.M - (xE - aE * self.M).abs()).clamp_min(0.0)
|
|
48
|
+
y = y + (cE * hats).sum(dim=-1)
|
|
49
|
+
return y
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from .ActivationFunction import ActivationFunction
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class NReLU(ActivationFunction):
|
|
6
|
+
def __init__(self):
|
|
7
|
+
super().__init__()
|
|
8
|
+
|
|
9
|
+
def forward(self, x: torch.Tensor):
|
|
10
|
+
sigma = x.std(unbiased=False)
|
|
11
|
+
a = torch.randn_like(x) * sigma
|
|
12
|
+
return torch.where(x >= 0, x + a, 0)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ParametricLogish(torch.nn.Module):
|
|
5
|
+
def __init__(self, alpha: float = 1.0, beta: float = 10.0):
|
|
6
|
+
super().__init__()
|
|
7
|
+
self.alpha = alpha
|
|
8
|
+
self.beta = beta
|
|
9
|
+
|
|
10
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
11
|
+
a = torch.sigmoid(self.beta * x)
|
|
12
|
+
b = torch.log(1 + a)
|
|
13
|
+
return self.alpha * x * b
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from .ActivationFunction import ActivationFunction
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Phish(ActivationFunction):
|
|
6
|
+
def __init__(self):
|
|
7
|
+
super().__init__()
|
|
8
|
+
|
|
9
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
10
|
+
a = torch.nn.functional.gelu(x)
|
|
11
|
+
return x * torch.tanh(a)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from .ActivationFunction import ActivationFunction
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class RReLU(ActivationFunction):
|
|
6
|
+
def __init__(self, lower: int = 3, upper: int = 8):
|
|
7
|
+
super(RReLU, self).__init__()
|
|
8
|
+
self.lower = lower
|
|
9
|
+
self.upper = upper
|
|
10
|
+
|
|
11
|
+
def forward(self, x):
|
|
12
|
+
if self.training:
|
|
13
|
+
a = torch.empty_like(x).uniform_(self.lower, self.upper)
|
|
14
|
+
else:
|
|
15
|
+
a = (self.lower + self.upper) / 2.0
|
|
16
|
+
return torch.where(x >= 0, x, x / a)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import math
|
|
3
|
+
from .ActivationFunction import ActivationFunction
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class SGELU(ActivationFunction):
|
|
7
|
+
def __init__(self, alpha: float = 0.1, *args, **kwargs):
|
|
8
|
+
super().__init__(*args, **kwargs)
|
|
9
|
+
self.alpha = alpha
|
|
10
|
+
|
|
11
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
12
|
+
return self.alpha * x * torch.erf(x / math.sqrt(2))
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class SReLU(torch.nn.Module):
|
|
5
|
+
def __init__(self, channels: int | None = None, max_input: float = 1.0):
|
|
6
|
+
super().__init__()
|
|
7
|
+
self.M = float(max_input)
|
|
8
|
+
if channels is None:
|
|
9
|
+
self.register_parameter("t_l", None)
|
|
10
|
+
self.register_parameter("t_r", None)
|
|
11
|
+
self.register_parameter("a_l", None)
|
|
12
|
+
self.register_parameter("a_r", None)
|
|
13
|
+
else:
|
|
14
|
+
self._init_params(channels, None, None)
|
|
15
|
+
|
|
16
|
+
def _init_params(self, C: int, device, dtype):
|
|
17
|
+
self.t_l = torch.nn.Parameter(torch.zeros(C, device=device, dtype=dtype))
|
|
18
|
+
self.t_r = torch.nn.Parameter(
|
|
19
|
+
torch.full((C,), self.M, device=device, dtype=dtype)
|
|
20
|
+
)
|
|
21
|
+
self.a_l = torch.nn.Parameter(torch.zeros(C, device=device, dtype=dtype))
|
|
22
|
+
self.a_r = torch.nn.Parameter(torch.ones(C, device=device, dtype=dtype))
|
|
23
|
+
|
|
24
|
+
def _expand_param(p: torch.Tensor, x: torch.Tensor):
|
|
25
|
+
return p.view((1, x.shape[1]) + (1,) * (x.dim() - 2))
|
|
26
|
+
|
|
27
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
28
|
+
if self.t_l is None:
|
|
29
|
+
self._init_params(x.shape[1], x.device, x.dtype)
|
|
30
|
+
|
|
31
|
+
t_l = self._expand_param(self.t_l, x)
|
|
32
|
+
t_r = self._expand_param(self.t_r, x)
|
|
33
|
+
a_l = self._expand_param(self.a_l, x)
|
|
34
|
+
a_r = self._expand_param(self.a_r, x)
|
|
35
|
+
y = torch.where(x < t_l, t_l + a_l * (x - t_l), x)
|
|
36
|
+
y = torch.where(x > t_r, t_r + a_r * (x - t_r), y)
|
|
37
|
+
return y
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from .ActivationFunction import ActivationFunction
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class SigmoidDerivative(ActivationFunction):
|
|
6
|
+
def __init__(self):
|
|
7
|
+
super().__init__()
|
|
8
|
+
|
|
9
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
10
|
+
return torch.exp(-x) * torch.sigmoid(x).pow(2)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from .ActivationFunction import ActivationFunction
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class SineReLU(ActivationFunction):
|
|
6
|
+
def __init__(self, epsilon: float = 0.0025):
|
|
7
|
+
super().__init__()
|
|
8
|
+
self.epsilon: float = epsilon
|
|
9
|
+
|
|
10
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
11
|
+
return torch.where(x >= 0, x, self.epsilon * (torch.sin(x) - torch.cos(x)))
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from .ActivationFunction import ActivationFunction
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class SlopedReLU(ActivationFunction):
|
|
6
|
+
def __init__(self, alpha: float):
|
|
7
|
+
super().__init__()
|
|
8
|
+
if not (1 <= alpha < 10):
|
|
9
|
+
raise Exception("Sloped ReLU slope needs to be in [1, 10].")
|
|
10
|
+
self.alpha: float = alpha
|
|
11
|
+
|
|
12
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
13
|
+
return torch.where(x >= 0, self.alpha * x, 0)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from .GaussianReLU import GaussianReLU
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class SmallGaLU(GaussianReLU):
|
|
5
|
+
def __init__(
|
|
6
|
+
self,
|
|
7
|
+
channels: int | None = None,
|
|
8
|
+
max_input: float = 1.0,
|
|
9
|
+
):
|
|
10
|
+
self.hats = [(2.0, 2.0)]
|
|
11
|
+
super().__init__(self.hats, channels=channels, max_input=max_input)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from .ActivationFunction import ActivationFunction
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class SoftsignRReLU(ActivationFunction):
|
|
6
|
+
def __init__(self, lower: float = 1 / 8, upper: float = 1 / 3, *args, **kwargs):
|
|
7
|
+
super().__init__(*args, **kwargs)
|
|
8
|
+
self.lower = lower
|
|
9
|
+
self.upper = upper
|
|
10
|
+
|
|
11
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
12
|
+
a = torch.empty_like(x).uniform_(self.lower, self.upper)
|
|
13
|
+
return torch.where(
|
|
14
|
+
x >= 0,
|
|
15
|
+
torch.div(1, (1 + x).pow(2)) + x,
|
|
16
|
+
torch.div(1, (1 + x).pow(2)) + a * x,
|
|
17
|
+
)
|