homa 0.2.0__py3-none-any.whl → 0.2.95__py3-none-any.whl

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.
Files changed (90) hide show
  1. homa/activations/APLU.py +49 -0
  2. homa/activations/ActivationFunction.py +6 -0
  3. homa/activations/AdaptiveActivationFunction.py +15 -0
  4. homa/activations/BaseDLReLU.py +34 -0
  5. homa/activations/CaLU.py +13 -0
  6. homa/activations/DLReLU.py +6 -0
  7. homa/activations/ERF.py +10 -0
  8. homa/activations/Elliot.py +10 -0
  9. homa/activations/ExpExpish.py +9 -0
  10. homa/activations/ExponentialDLReLU.py +6 -0
  11. homa/activations/ExponentialSwish.py +10 -0
  12. homa/activations/GCU.py +9 -0
  13. homa/activations/GaLU.py +11 -0
  14. homa/activations/GaussianReLU.py +50 -0
  15. homa/activations/GeneralizedSwish.py +10 -0
  16. homa/activations/Gish.py +11 -0
  17. homa/activations/LaLU.py +11 -0
  18. homa/activations/LogLogish.py +10 -0
  19. homa/activations/LogSigmoid.py +10 -0
  20. homa/activations/Logish.py +10 -0
  21. homa/activations/MeLU.py +11 -0
  22. homa/activations/MexicanReLU.py +49 -0
  23. homa/activations/MinSin.py +10 -0
  24. homa/activations/NReLU.py +12 -0
  25. homa/activations/NoisyReLU.py +6 -0
  26. homa/activations/PLogish.py +6 -0
  27. homa/activations/ParametricLogish.py +13 -0
  28. homa/activations/Phish.py +11 -0
  29. homa/activations/RReLU.py +16 -0
  30. homa/activations/RandomizedSlopedReLU.py +7 -0
  31. homa/activations/SGELU.py +12 -0
  32. homa/activations/SReLU.py +37 -0
  33. homa/activations/SelfArctan.py +9 -0
  34. homa/activations/ShiftedReLU.py +10 -0
  35. homa/activations/SigmoidDerivative.py +10 -0
  36. homa/activations/SineReLU.py +11 -0
  37. homa/activations/SlopedReLU.py +13 -0
  38. homa/activations/SmallGaLU.py +11 -0
  39. homa/activations/Smish.py +9 -0
  40. homa/activations/SoftsignRReLU.py +17 -0
  41. homa/activations/Suish.py +11 -0
  42. homa/activations/TBSReLU.py +13 -0
  43. homa/activations/TSReLU.py +10 -0
  44. homa/activations/TangentBipolarSigmoidReLU.py +6 -0
  45. homa/activations/TangentSigmoidReLU.py +6 -0
  46. homa/activations/TeLU.py +9 -0
  47. homa/activations/TripleStateSwish.py +15 -0
  48. homa/activations/WideMeLU.py +15 -0
  49. homa/activations/__init__.py +49 -2
  50. homa/activations/learnable/AOAF.py +16 -0
  51. homa/activations/learnable/AReLU.py +19 -0
  52. homa/activations/learnable/DPReLU.py +16 -0
  53. homa/activations/learnable/DualLine.py +18 -0
  54. homa/activations/learnable/FReLU.py +14 -0
  55. homa/activations/learnable/LeLeLU.py +14 -0
  56. homa/activations/learnable/PERU.py +16 -0
  57. homa/activations/learnable/PiLU.py +18 -0
  58. homa/activations/learnable/ShiLU.py +16 -0
  59. homa/activations/learnable/StarReLU.py +16 -0
  60. homa/activations/learnable/__init__.py +10 -0
  61. homa/activations/learnable/concerns/ChannelBased.py +38 -0
  62. homa/activations/learnable/concerns/__init__.py +1 -0
  63. homa/cli/Commands/Command.py +2 -0
  64. homa/cli/Commands/InitCommand.py +34 -0
  65. homa/cli/Commands/__init__.py +2 -0
  66. homa/cli/HomaCommand.py +4 -0
  67. homa/ensemble/concerns/StoresModels.py +9 -3
  68. homa/vision/{ClassificationModel.py → Classifier.py} +1 -1
  69. homa/vision/Resnet.py +2 -2
  70. homa/vision/StochasticClassifier.py +23 -22
  71. homa/vision/StochasticSwin.py +3 -1
  72. homa/vision/Swin.py +4 -3
  73. homa/vision/__init__.py +2 -1
  74. homa/vision/utils.py +12 -0
  75. {homa-0.2.0.dist-info → homa-0.2.95.dist-info}/METADATA +1 -1
  76. homa-0.2.95.dist-info/RECORD +113 -0
  77. homa/activations/classes/APLU.py +0 -86
  78. homa/activations/classes/GALU.py +0 -67
  79. homa/activations/classes/MELU.py +0 -70
  80. homa/activations/classes/PDELU.py +0 -54
  81. homa/activations/classes/SReLU.py +0 -69
  82. homa/activations/classes/SmallGALU.py +0 -58
  83. homa/activations/classes/WideMELU.py +0 -90
  84. homa/activations/classes/__init__.py +0 -7
  85. homa/activations/utils.py +0 -27
  86. homa/vision/StochasticResnet.py +0 -9
  87. homa-0.2.0.dist-info/RECORD +0 -58
  88. {homa-0.2.0.dist-info → homa-0.2.95.dist-info}/WHEEL +0 -0
  89. {homa-0.2.0.dist-info → homa-0.2.95.dist-info}/entry_points.txt +0 -0
  90. {homa-0.2.0.dist-info → homa-0.2.95.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,13 @@
1
+ import torch
2
+ from .ActivationFunction import ActivationFunction
3
+
4
+
5
+ class TBSReLU(ActivationFunction):
6
+ def __init__(self):
7
+ super().__init__()
8
+
9
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
10
+ a = 1 - torch.exp(-x)
11
+ b = 1 + torch.exp(-x)
12
+ c = a / b
13
+ return x * torch.tanh(c)
@@ -0,0 +1,10 @@
1
+ import torch
2
+ from .ActivationFunction import ActivationFunction
3
+
4
+
5
+ class TSReLU(ActivationFunction):
6
+ def __init__(self):
7
+ super().__init__()
8
+
9
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
10
+ return x * torch.tanh(torch.sigmoid(x))
@@ -0,0 +1,6 @@
1
+ from .TBSReLU import TBSReLU
2
+
3
+
4
+ class TangentBipolarSigmoidReLU(TBSReLU):
5
+ def __init__(self, *args, **kwargs):
6
+ super().__init__(*args, **kwargs)
@@ -0,0 +1,6 @@
1
+ from .TSReLU import TSReLU
2
+
3
+
4
+ class TangentSigmoidReLU(TSReLU):
5
+ def __init__(self, *args, **kwargs):
6
+ super().__init__(*args, **kwargs)
@@ -0,0 +1,9 @@
1
+ import torch
2
+
3
+
4
+ class TeLU(torch.nn.Module):
5
+ def __init__(self):
6
+ super().__init__()
7
+
8
+ def forward(self, x: torch.Tensor):
9
+ return x * torch.tanh(torch.exp(x))
@@ -0,0 +1,15 @@
1
+ import torch
2
+ from .ActivationFunction import ActivationFunction
3
+
4
+
5
+ class TripleStateSwish(ActivationFunction):
6
+ def __init__(self, alpha: float = 20, beta: float = 40, *args, **kwargs):
7
+ super().__init__(*args, **kwargs)
8
+ self.alpha = alpha
9
+ self.beta = beta
10
+
11
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
12
+ a = 1 / (1 + torch.exp(-x))
13
+ b = 1 / (1 + torch.exp(-x + self.alpha))
14
+ c = 1 / (1 + torch.exp(-x + self.beta))
15
+ return x * a * (a + b + c)
@@ -0,0 +1,15 @@
1
+ from .MexicanReLU import MexicanReLU
2
+
3
+
4
+ class WideMeLU(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
+ (0.5, 0.5),
11
+ (1.5, 0.5),
12
+ (2.5, 0.5),
13
+ (3.5, 0.5),
14
+ ]
15
+ super().__init__(self.hats, channels=channels, max_input=max_input)
@@ -1,2 +1,49 @@
1
- from .classes import *
2
- from .utils import *
1
+ from .ShiftedReLU import ShiftedReLU
2
+ from .PLogish import PLogish
3
+ from .ParametricLogish import ParametricLogish
4
+ from .ExpExpish import ExpExpish
5
+ from .GeneralizedSwish import GeneralizedSwish
6
+ from .TBSReLU import TBSReLU
7
+ from .NoisyReLU import NoisyReLU
8
+ from .ExponentialDLReLU import ExponentialDLReLU
9
+ from .SReLU import SReLU
10
+ from .TangentSigmoidReLU import TangentSigmoidReLU
11
+ from .Phish import Phish
12
+ from .WideMeLU import WideMeLU
13
+ from .SelfArctan import SelfArctan
14
+ from .LogSigmoid import LogSigmoid
15
+ from .SlopedReLU import SlopedReLU
16
+ from .SmallGaLU import SmallGaLU
17
+ from .MinSin import MinSin
18
+ from .LaLU import LaLU
19
+ from .MexicanReLU import MexicanReLU
20
+ from .APLU import APLU
21
+ from .ERF import ERF
22
+ from .TangentBipolarSigmoidReLU import TangentBipolarSigmoidReLU
23
+ from .BaseDLReLU import BaseDLReLU
24
+ from .Logish import Logish
25
+ from .TripleStateSwish import TripleStateSwish
26
+ from .ExponentialSwish import ExponentialSwish
27
+ from .TeLU import TeLU
28
+ from .Elliot import Elliot
29
+ from .MeLU import MeLU
30
+ from .GaussianReLU import GaussianReLU
31
+ from .ActivationFunction import ActivationFunction
32
+ from .RReLU import RReLU
33
+ from .Suish import Suish
34
+ from .SoftsignRReLU import SoftsignRReLU
35
+ from .Gish import Gish
36
+ from .NReLU import NReLU
37
+ from .LogLogish import LogLogish
38
+ from .SGELU import SGELU
39
+ from .GaLU import GaLU
40
+ from .TSReLU import TSReLU
41
+ from .SineReLU import SineReLU
42
+ from .DLReLU import DLReLU
43
+ from .CaLU import CaLU
44
+ from .RandomizedSlopedReLU import RandomizedSlopedReLU
45
+ from .GCU import GCU
46
+ from .SigmoidDerivative import SigmoidDerivative
47
+ from .Smish import Smish
48
+ from .AdaptiveActivationFunction import AdaptiveActivationFunction
49
+ from .learnable import *
@@ -0,0 +1,16 @@
1
+ import torch
2
+ from .concerns import ChannelBased
3
+ from ..AdaptiveActivationFunction import AdaptiveActivationFunction
4
+
5
+
6
+ class AOAF(AdaptiveActivationFunction, ChannelBased):
7
+ def __init__(self, b: float = 0.17, c: float = 0.17):
8
+ super().__init__()
9
+ self.a = None
10
+ self.b = b
11
+ self.c = c
12
+
13
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
14
+ self.initialize(x, "a")
15
+ a = self.a.view(self.parameter_shape(x))
16
+ return torch.relu(x - self.b * a) + self.c * a
@@ -0,0 +1,19 @@
1
+ import torch
2
+ from ..AdaptiveActivationFunction import AdaptiveActivationFunction
3
+ from ...device import get_device
4
+
5
+
6
+ class AReLU(AdaptiveActivationFunction):
7
+ def __init__(self):
8
+ super(AReLU, self).__init__()
9
+ self.a = torch.nn.Parameter(torch.tensor(0.9, requires_grad=True))
10
+ self.b = torch.nn.Parameter(torch.tensor(2.0, requires_grad=True))
11
+ self.a.to(get_device())
12
+ self.b.to(get_device())
13
+
14
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
15
+ negative_slope = torch.clamp(self.a, 0.01, 0.99)
16
+ positive_slope = 1 + torch.sigmoid(self.b)
17
+ positive = positive_slope * torch.relu(x)
18
+ negative = negative_slope * (-torch.relu(-x))
19
+ return positive + negative
@@ -0,0 +1,16 @@
1
+ import torch
2
+ from ..AdaptiveActivationFunction import AdaptiveActivationFunction
3
+ from .concerns import ChannelBased
4
+
5
+
6
+ class DPReLU(AdaptiveActivationFunction, ChannelBased):
7
+ def __init__(self):
8
+ super().__init__()
9
+ self.a = None
10
+ self.b = None
11
+
12
+ def forward(self, x: torch.Tensor):
13
+ self.initialize(x, ["a", "b"], [1, 0.01])
14
+ a = self.a.view(self.parameter_shape(x))
15
+ b = self.b.view(self.parameter_shape(x))
16
+ return torch.where(x >= 0, a * x, b * x)
@@ -0,0 +1,18 @@
1
+ import torch
2
+ from ..AdaptiveActivationFunction import AdaptiveActivationFunction
3
+ from .concerns import ChannelBased
4
+
5
+
6
+ class DualLine(AdaptiveActivationFunction, ChannelBased):
7
+ def __init__(self):
8
+ super().__init__()
9
+ self.a = None
10
+ self.b = None
11
+ self.m = None
12
+
13
+ def forward(self, x: torch.Tensor):
14
+ self.initialize(x, ["a", "b", "m"], [1, 0.01, -0.22])
15
+ a = self.a.view(self.parameter_shape(x))
16
+ b = self.b.view(self.parameter_shape(x))
17
+ m = self.m.view(self.parameter_shape(x))
18
+ return torch.where(x >= 0, a * x + m, b * x + m)
@@ -0,0 +1,14 @@
1
+ import torch
2
+ from ..AdaptiveActivationFunction import AdaptiveActivationFunction
3
+ from .concerns import ChannelBased
4
+
5
+
6
+ class FReLU(AdaptiveActivationFunction, ChannelBased):
7
+ def __init__(self):
8
+ super().__init__()
9
+ self.b = None
10
+
11
+ def forward(self, x: torch.Tensor):
12
+ self.initialize(x, "b")
13
+ b = self.b.view(self.parameter_shape(x))
14
+ return torch.where(x >= 0, x + b, b)
@@ -0,0 +1,14 @@
1
+ import torch
2
+ from ..AdaptiveActivationFunction import AdaptiveActivationFunction
3
+ from .concerns import ChannelBased
4
+
5
+
6
+ class LeLeLU(AdaptiveActivationFunction, ChannelBased):
7
+ def __init__(self):
8
+ super().__init__()
9
+ self.a = None
10
+
11
+ def forward(self, x: torch.Tensor):
12
+ self.initialize(x, "a")
13
+ a = self.a.view(self.parameter_shape(x))
14
+ return torch.where(x >= 0, a * x, 0.01 * a * x)
@@ -0,0 +1,16 @@
1
+ import torch
2
+ from .concerns import ChannelBased
3
+ from ..AdaptiveActivationFunction import AdaptiveActivationFunction
4
+
5
+
6
+ class PERU(AdaptiveActivationFunction, ChannelBased):
7
+ def __init__(self):
8
+ super().__init__()
9
+ self.a = None
10
+ self.b = None
11
+
12
+ def forward(self, x: torch.Tensor):
13
+ self.initialize(x, ["a", "b"])
14
+ a = self.a.view(self.parameter_shape(x))
15
+ b = self.b.view(self.parameter_shape(x))
16
+ return torch.where(x >= 0, a * x, a * x * torch.exp(b * x))
@@ -0,0 +1,18 @@
1
+ import torch
2
+ from ..AdaptiveActivationFunction import AdaptiveActivationFunction
3
+ from .concerns import ChannelBased
4
+
5
+
6
+ class PiLU(AdaptiveActivationFunction, ChannelBased):
7
+ def __init__(self):
8
+ super().__init__()
9
+ self.a = None
10
+ self.b = None
11
+ self.c = None
12
+
13
+ def forward(self, x: torch.Tensor):
14
+ self.initialize(x, ["a", "b", "c"], [1, 0.01, 1])
15
+ a = self.a.view(self.parameter_shape(x))
16
+ b = self.b.view(self.parameter_shape(x))
17
+ c = self.c.view(self.parameter_shape(x))
18
+ return torch.where(x >= c, a * x + c * (1 - a), b * x + c * (1 - b))
@@ -0,0 +1,16 @@
1
+ import torch
2
+ from ..ActivationFunction import ActivationFunction
3
+ from .concerns import ChannelBased
4
+
5
+
6
+ class ShiLU(ActivationFunction, ChannelBased):
7
+ def __init__(self):
8
+ super().__init__()
9
+ self.a = None
10
+ self.b = None
11
+
12
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
13
+ self.initialize(x, ["a", "b"])
14
+ a = self.a.view(self.parameter_shape(x))
15
+ b = self.b.view(self.parameter_shape(x))
16
+ return torch.relu(x) * a + b
@@ -0,0 +1,16 @@
1
+ import torch
2
+ from ..ActivationFunction import ActivationFunction
3
+ from .concerns import ChannelBased
4
+
5
+
6
+ class StarReLU(ActivationFunction, ChannelBased):
7
+ def __init__(self):
8
+ super().__init__()
9
+ self.a = None
10
+ self.b = None
11
+
12
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
13
+ self.initialize(x, ["a", "b"])
14
+ a = self.a.view(self.parameter_shape(x))
15
+ b = self.b.view(self.parameter_shape(x))
16
+ return a * torch.relu(x).pow(2) + b
@@ -0,0 +1,10 @@
1
+ from .DualLine import DualLine
2
+ from .LeLeLU import LeLeLU
3
+ from .AReLU import AReLU
4
+ from .PERU import PERU
5
+ from .ShiLU import ShiLU
6
+ from .StarReLU import StarReLU
7
+ from .DPReLU import DPReLU
8
+ from .PiLU import PiLU
9
+ from .FReLU import FReLU
10
+ from .AOAF import AOAF
@@ -0,0 +1,38 @@
1
+ import torch
2
+ from typing import List
3
+
4
+
5
+ class ChannelBased:
6
+ def __init__(self, *args, **kwargs):
7
+ super().__init__(*args, **kwargs)
8
+ self._initialized = False
9
+ self.num_channels = None
10
+
11
+ def initialize(
12
+ self, x: torch.Tensor, attrs: List[str] | str, values: List[float] | float = []
13
+ ):
14
+ if getattr(self, "_initialized", False):
15
+ return
16
+
17
+ if not isinstance(values, list):
18
+ values = [values]
19
+
20
+ if not isinstance(attrs, list):
21
+ attrs = [attrs]
22
+
23
+ self.num_channels = x.shape[1]
24
+ device = x.device
25
+ for index, attr in enumerate(attrs):
26
+ if index < len(values) and values[index] is not None:
27
+ default_value = float(values[index])
28
+ else:
29
+ default_value = 1.0
30
+ param = torch.nn.Parameter(torch.full((self.num_channels,), default_value))
31
+ param = param.to(device)
32
+ setattr(self, attr, param)
33
+ self._initialized = True
34
+
35
+ def parameter_shape(self, x: torch.Tensor) -> tuple | None:
36
+ if hasattr(self, "num_channels"):
37
+ return (1, self.num_channels) + (1,) * (x.ndim - 2)
38
+ return None
@@ -0,0 +1 @@
1
+ from .ChannelBased import ChannelBased
@@ -0,0 +1,2 @@
1
+ class Command:
2
+ pass
@@ -0,0 +1,34 @@
1
+ import ast
2
+ from pathlib import Path
3
+ from .Command import Command
4
+
5
+
6
+ class InitCommand(Command):
7
+ @staticmethod
8
+ def run():
9
+ path = Path(".")
10
+ init_file = path / "__init__.py"
11
+ init_file.write_text("")
12
+ for file in path.iterdir():
13
+ if file.name == "__init__.py" or file.suffix != ".py":
14
+ continue
15
+ tree = ast.parse(file.read_text())
16
+ classes = [
17
+ node.name
18
+ for node in tree.body
19
+ if isinstance(node, ast.ClassDef) and not node.name.startswith("_")
20
+ ]
21
+ functions = [
22
+ node.name
23
+ for node in tree.body
24
+ if isinstance(node, ast.FunctionDef) and not node.name.startswith("_")
25
+ ]
26
+ if not (classes or functions):
27
+ continue
28
+ module = file.stem
29
+ lines = [
30
+ f"from .{module} import {name}\n" for name in (*classes, *functions)
31
+ ]
32
+ with init_file.open("a") as f:
33
+ f.writelines(lines)
34
+ print(f"Processed {file}: classes={classes}, functions={functions}")
@@ -0,0 +1,2 @@
1
+ from .Command import Command
2
+ from .InitCommand import InitCommand
homa/cli/HomaCommand.py CHANGED
@@ -1,5 +1,6 @@
1
1
  import fire
2
2
  from .namespaces import MakeNamespace, CacheNamespace
3
+ from .Commands import InitCommand
3
4
 
4
5
 
5
6
  class HomaCommand:
@@ -7,6 +8,9 @@ class HomaCommand:
7
8
  self.make = MakeNamespace()
8
9
  self.cache = CacheNamespace()
9
10
 
11
+ def init(self):
12
+ InitCommand.run()
13
+
10
14
 
11
15
  def main():
12
16
  fire.Fire(HomaCommand)
@@ -1,5 +1,5 @@
1
1
  import torch
2
- from copy import deepcopy
2
+ import io
3
3
  from typing import List
4
4
  from ...vision import Model
5
5
 
@@ -12,11 +12,17 @@ class StoresModels:
12
12
  def record(self, model: Model | torch.nn.Module):
13
13
  model_: torch.nn.Module | None = None
14
14
  if isinstance(model, Model):
15
- model_ = deepcopy(model.network)
15
+ model_ = model.network
16
16
  elif isinstance(model, torch.nn.Module):
17
- model_ = deepcopy(model)
17
+ model_ = model
18
18
  else:
19
19
  raise TypeError("Wrong input to ensemble record")
20
+
21
+ device = model_.device
22
+ buffer = io.BytesIO()
23
+ torch.save(model_.to("cpu"), buffer)
24
+ buffer.seek(0)
25
+ model_ = torch.load(buffer, map_location=device)
20
26
  self.models.append(model_)
21
27
 
22
28
  def push(self, *args, **kwargs):
@@ -1,5 +1,5 @@
1
1
  from .Model import Model
2
2
 
3
3
 
4
- class ClassificationModel(Model):
4
+ class Classifier(Model):
5
5
  pass
homa/vision/Resnet.py CHANGED
@@ -1,11 +1,11 @@
1
1
  import torch
2
2
  from .modules import ResnetModule
3
- from .ClassificationModel import ClassificationModel
3
+ from .Classifier import Classifier
4
4
  from .concerns import Trainable, ReportsMetrics
5
5
  from ..device import get_device
6
6
 
7
7
 
8
- class Resnet(ClassificationModel, Trainable, ReportsMetrics):
8
+ class Resnet(Classifier, Trainable, ReportsMetrics):
9
9
  def __init__(self, num_classes: int, lr: float = 0.001):
10
10
  super().__init__()
11
11
  self.network = ResnetModule(num_classes).to(get_device())
@@ -1,28 +1,29 @@
1
- import torch
2
- import random
3
- from ..activations import APLU, GALU, SmallGALU, MELU, WideMELU, PDELU, SReLU
1
+ from ..activations import (
2
+ AOAF,
3
+ AReLU,
4
+ DPReLU,
5
+ DualLine,
6
+ FReLU,
7
+ LeLeLU,
8
+ PERU,
9
+ PiLU,
10
+ ShiLU,
11
+ StarReLU,
12
+ )
4
13
 
5
14
 
6
15
  class StochasticClassifier:
7
16
  def __init__(self, *args, **kwargs):
8
17
  super().__init__(*args, **kwargs)
9
- self.pool = [
10
- APLU,
11
- GALU,
12
- SmallGALU,
13
- MELU,
14
- WideMELU,
15
- PDELU,
16
- SReLU,
17
- torch.nn.ReLU,
18
- torch.nn.PReLU,
19
- torch.nn.LeakyReLU,
20
- torch.nn.ELU,
18
+ self._activation_pool = [
19
+ AOAF,
20
+ AReLU,
21
+ DPReLU,
22
+ DualLine,
23
+ FReLU,
24
+ LeLeLU,
25
+ PERU,
26
+ PiLU,
27
+ ShiLU,
28
+ StarReLU,
21
29
  ]
22
-
23
- def replace_activations(self, needle: torch.Tensor) -> None:
24
- replacement = random.choice(self.pool)
25
- for parent in self.network.modules():
26
- for name, child in list(parent.named_children()):
27
- if isinstance(child, needle):
28
- setattr(parent, name, replacement())
@@ -1,9 +1,11 @@
1
1
  import torch
2
2
  from .Swin import Swin
3
3
  from .StochasticClassifier import StochasticClassifier
4
+ from .utils import replace_activations
4
5
 
5
6
 
6
7
  class StochasticSwin(Swin, StochasticClassifier):
7
8
  def __init__(self, *args, **kwargs):
8
9
  super().__init__(*args, **kwargs)
9
- self.replace_activations(torch.nn.GELU)
10
+ replace_activations(self.network, torch.nn.GELU, self._activation_pool)
11
+ replace_activations(self.network, torch.nn.ReLU, self._activation_pool)
homa/vision/Swin.py CHANGED
@@ -1,12 +1,13 @@
1
1
  import torch
2
- from .ClassificationModel import ClassificationModel
2
+ from .Classifier import Classifier
3
3
  from .concerns import Trainable, ReportsMetrics
4
4
  from .modules import SwinModule
5
+ from ..device import get_device
5
6
 
6
7
 
7
- class Swin(ClassificationModel, Trainable, ReportsMetrics):
8
+ class Swin(Classifier, Trainable, ReportsMetrics):
8
9
  def __init__(self, num_classes: int, lr: float = 0.0001):
9
10
  super().__init__()
10
- self.network = SwinModule(num_classes=num_classes)
11
+ self.network = SwinModule(num_classes=num_classes).to(get_device())
11
12
  self.optimizer = torch.optim.AdamW(self.network.parameters(), lr=lr)
12
13
  self.criterion = torch.nn.CrossEntropyLoss()
homa/vision/__init__.py CHANGED
@@ -1,4 +1,5 @@
1
+ from .Classifier import Classifier
1
2
  from .Model import Model
2
3
  from .Resnet import Resnet
3
- from .StochasticResnet import StochasticResnet
4
4
  from .Swin import Swin
5
+ from .StochasticSwin import StochasticSwin
homa/vision/utils.py ADDED
@@ -0,0 +1,12 @@
1
+ import torch
2
+ import random
3
+
4
+
5
+ def replace_activations(module, needle: torch.nn.Module, candidates: list):
6
+ for name, child in module.named_children():
7
+ if isinstance(child, needle):
8
+ new_activation = random.choice(candidates)
9
+ setattr(module, name, new_activation())
10
+ else:
11
+ replace_activations(child, needle, candidates)
12
+ return module
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: homa
3
- Version: 0.2.0
3
+ Version: 0.2.95
4
4
  Summary: A curated list of machine learning and deep learning helpers.
5
5
  Author-email: Taha Shieenavaz <tahashieenavaz@gmail.com>
6
6
  Requires-Python: >=3.7