homa 0.2.94__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.
Potentially problematic release.
This version of homa might be problematic. Click here for more details.
- homa/__init__.py +2 -0
- homa/activations/APLU.py +49 -0
- homa/activations/ActivationFunction.py +6 -0
- homa/activations/AdaptiveActivationFunction.py +15 -0
- homa/activations/BaseDLReLU.py +34 -0
- homa/activations/CaLU.py +13 -0
- homa/activations/DLReLU.py +6 -0
- homa/activations/ERF.py +10 -0
- homa/activations/Elliot.py +10 -0
- homa/activations/ExpExpish.py +9 -0
- homa/activations/ExponentialDLReLU.py +6 -0
- homa/activations/ExponentialSwish.py +10 -0
- homa/activations/GCU.py +9 -0
- homa/activations/GaLU.py +11 -0
- homa/activations/GaussianReLU.py +50 -0
- homa/activations/GeneralizedSwish.py +10 -0
- homa/activations/Gish.py +11 -0
- homa/activations/LaLU.py +11 -0
- homa/activations/LogLogish.py +10 -0
- homa/activations/LogSigmoid.py +10 -0
- homa/activations/Logish.py +10 -0
- homa/activations/MeLU.py +11 -0
- homa/activations/MexicanReLU.py +49 -0
- homa/activations/MinSin.py +10 -0
- homa/activations/NReLU.py +12 -0
- homa/activations/NoisyReLU.py +6 -0
- homa/activations/PLogish.py +6 -0
- homa/activations/ParametricLogish.py +13 -0
- homa/activations/Phish.py +11 -0
- homa/activations/RReLU.py +16 -0
- homa/activations/RandomizedSlopedReLU.py +7 -0
- homa/activations/SGELU.py +12 -0
- homa/activations/SReLU.py +37 -0
- homa/activations/SelfArctan.py +9 -0
- homa/activations/ShiftedReLU.py +10 -0
- homa/activations/SigmoidDerivative.py +10 -0
- homa/activations/SineReLU.py +11 -0
- homa/activations/SlopedReLU.py +13 -0
- homa/activations/SmallGaLU.py +11 -0
- homa/activations/Smish.py +9 -0
- homa/activations/SoftsignRReLU.py +17 -0
- homa/activations/Suish.py +11 -0
- homa/activations/TBSReLU.py +13 -0
- homa/activations/TSReLU.py +10 -0
- homa/activations/TangentBipolarSigmoidReLU.py +6 -0
- homa/activations/TangentSigmoidReLU.py +6 -0
- homa/activations/TeLU.py +9 -0
- homa/activations/TripleStateSwish.py +15 -0
- homa/activations/WideMeLU.py +15 -0
- homa/activations/__init__.py +49 -0
- homa/activations/learnable/AOAF.py +16 -0
- homa/activations/learnable/AReLU.py +19 -0
- homa/activations/learnable/DPReLU.py +16 -0
- homa/activations/learnable/DualLine.py +18 -0
- homa/activations/learnable/FReLU.py +14 -0
- homa/activations/learnable/LeLeLU.py +14 -0
- homa/activations/learnable/PERU.py +16 -0
- homa/activations/learnable/PiLU.py +18 -0
- homa/activations/learnable/ShiLU.py +16 -0
- homa/activations/learnable/StarReLU.py +16 -0
- homa/activations/learnable/__init__.py +10 -0
- homa/activations/learnable/concerns/ChannelBased.py +38 -0
- homa/activations/learnable/concerns/__init__.py +1 -0
- homa/cli/Commands/Command.py +2 -0
- homa/cli/Commands/InitCommand.py +34 -0
- homa/cli/Commands/__init__.py +2 -0
- homa/cli/HomaCommand.py +16 -0
- homa/cli/namespaces/CacheNamespace.py +29 -0
- homa/cli/namespaces/MakeNamespace.py +18 -0
- homa/cli/namespaces/__init__.py +2 -0
- homa/device.py +25 -0
- homa/ensemble/Ensemble.py +16 -0
- homa/ensemble/__init__.py +1 -0
- homa/ensemble/concerns/CalculatesMetricNecessities.py +24 -0
- homa/ensemble/concerns/PredictsProbabilities.py +15 -0
- homa/ensemble/concerns/ReportsClassificationMetrics.py +13 -0
- homa/ensemble/concerns/ReportsEnsembleAccuracy.py +11 -0
- homa/ensemble/concerns/ReportsEnsembleF1.py +10 -0
- homa/ensemble/concerns/ReportsEnsembleKappa.py +10 -0
- homa/ensemble/concerns/ReportsLogits.py +17 -0
- homa/ensemble/concerns/ReportsSize.py +11 -0
- homa/ensemble/concerns/StoresModels.py +36 -0
- homa/ensemble/concerns/__init__.py +9 -0
- homa/loss/LogitNormLoss.py +12 -0
- homa/loss/Loss.py +2 -0
- homa/loss/__init__.py +2 -0
- homa/settings.py +12 -0
- homa/torch/__init__.py +1 -0
- homa/torch/helpers.py +6 -0
- homa/utils.py +2 -0
- homa/vision/Classifier.py +5 -0
- homa/vision/Model.py +2 -0
- homa/vision/Resnet.py +13 -0
- homa/vision/StochasticClassifier.py +29 -0
- homa/vision/StochasticSwin.py +11 -0
- homa/vision/Swin.py +13 -0
- homa/vision/__init__.py +5 -0
- homa/vision/concerns/HasLabels.py +13 -0
- homa/vision/concerns/HasLogits.py +12 -0
- homa/vision/concerns/HasProbabilities.py +9 -0
- homa/vision/concerns/ReportsAccuracy.py +27 -0
- homa/vision/concerns/ReportsMetrics.py +6 -0
- homa/vision/concerns/Trainable.py +29 -0
- homa/vision/concerns/__init__.py +6 -0
- homa/vision/modules/ResnetModule.py +23 -0
- homa/vision/modules/SwinModule.py +23 -0
- homa/vision/modules/__init__.py +2 -0
- homa/vision/utils.py +12 -0
- homa-0.2.94.dist-info/METADATA +75 -0
- homa-0.2.94.dist-info/RECORD +113 -0
- homa-0.2.94.dist-info/WHEEL +5 -0
- homa-0.2.94.dist-info/entry_points.txt +2 -0
- homa-0.2.94.dist-info/top_level.txt +1 -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)
|
homa/activations/TeLU.py
ADDED
|
@@ -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)
|
|
@@ -0,0 +1,49 @@
|
|
|
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,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}")
|
homa/cli/HomaCommand.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import fire
|
|
2
|
+
from .namespaces import MakeNamespace, CacheNamespace
|
|
3
|
+
from .Commands import InitCommand
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class HomaCommand:
|
|
7
|
+
def __init__(self):
|
|
8
|
+
self.make = MakeNamespace()
|
|
9
|
+
self.cache = CacheNamespace()
|
|
10
|
+
|
|
11
|
+
def init(self):
|
|
12
|
+
InitCommand.run()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def main():
|
|
16
|
+
fire.Fire(HomaCommand)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import shutil
|
|
2
|
+
import sys
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class CacheNamespace:
|
|
7
|
+
def clear(self):
|
|
8
|
+
root = Path.cwd()
|
|
9
|
+
removed = 0
|
|
10
|
+
errors: list[str] = []
|
|
11
|
+
|
|
12
|
+
for candidate in root.rglob("__pycache__"):
|
|
13
|
+
if not candidate.is_dir():
|
|
14
|
+
continue
|
|
15
|
+
try:
|
|
16
|
+
shutil.rmtree(candidate)
|
|
17
|
+
removed += 1
|
|
18
|
+
except OSError as exc:
|
|
19
|
+
errors.append(f"{candidate}: {exc}")
|
|
20
|
+
|
|
21
|
+
if errors:
|
|
22
|
+
print("Failed to remove the following paths:", file=sys.stderr)
|
|
23
|
+
for error in errors:
|
|
24
|
+
print(f" - {error}", file=sys.stderr)
|
|
25
|
+
return 1
|
|
26
|
+
|
|
27
|
+
return (
|
|
28
|
+
f"Removed {removed} __pycache__ director{'ies' if removed != 1 else 'y'}."
|
|
29
|
+
)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class MakeNamespace:
|
|
5
|
+
def trait(self, name: str):
|
|
6
|
+
class_name = name.split(".")[-1]
|
|
7
|
+
file = name.replace(".", "/") + ".py"
|
|
8
|
+
path = Path(file)
|
|
9
|
+
parent = path.parent
|
|
10
|
+
parent.mkdir(parents=True, exist_ok=True)
|
|
11
|
+
path.touch()
|
|
12
|
+
|
|
13
|
+
# copy the tempalte path
|
|
14
|
+
current_path = Path(__file__).parent.parent.resolve()
|
|
15
|
+
template_path = current_path / "templates" / "trait.txt"
|
|
16
|
+
content = template_path.read_text()
|
|
17
|
+
content = content.replace("{{CLASS}}", class_name)
|
|
18
|
+
path.write_text(content)
|
homa/device.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def get_device():
|
|
5
|
+
if torch.backends.mps.is_available():
|
|
6
|
+
return mps()
|
|
7
|
+
if torch.cuda.is_available():
|
|
8
|
+
return cuda()
|
|
9
|
+
return cpu()
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def cpu():
|
|
13
|
+
return torch.device("cpu")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def cuda():
|
|
17
|
+
return torch.device("cuda")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def mps():
|
|
21
|
+
return torch.device("mps")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def device():
|
|
25
|
+
return get_device()
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from .concerns import (
|
|
2
|
+
ReportsSize,
|
|
3
|
+
StoresModels,
|
|
4
|
+
ReportsClassificationMetrics,
|
|
5
|
+
PredictsProbabilities,
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Ensemble(
|
|
10
|
+
ReportsSize,
|
|
11
|
+
ReportsClassificationMetrics,
|
|
12
|
+
PredictsProbabilities,
|
|
13
|
+
StoresModels,
|
|
14
|
+
):
|
|
15
|
+
def __init__(self):
|
|
16
|
+
super().__init__()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .Ensemble import Ensemble
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from ...device import get_device
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class CalculatesMetricNecessities:
|
|
6
|
+
def __init__(self, *args, **kwargs):
|
|
7
|
+
super().__init__(*args, **kwargs)
|
|
8
|
+
|
|
9
|
+
@torch.no_grad()
|
|
10
|
+
def metric_necessities(self, dataloader):
|
|
11
|
+
predictions, labels = [], []
|
|
12
|
+
device = get_device()
|
|
13
|
+
for x, y in dataloader:
|
|
14
|
+
x, y = x.to(device), y.to(device)
|
|
15
|
+
sum_logits = None
|
|
16
|
+
for model in self.models:
|
|
17
|
+
model.to(device)
|
|
18
|
+
model.eval()
|
|
19
|
+
logits = model(x)
|
|
20
|
+
sum_logits = logits if sum_logits is None else sum_logits + logits
|
|
21
|
+
batch_predictions = sum_logits.argmax(dim=1)
|
|
22
|
+
predictions.extend(batch_predictions.cpu().numpy())
|
|
23
|
+
labels.extend(y.cpu().numpy())
|
|
24
|
+
return predictions, labels
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from .ReportsLogits import ReportsLogits
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class PredictsProbabilities(ReportsLogits):
|
|
6
|
+
def __init__(self, *args, **kwargs):
|
|
7
|
+
super().__init__(*args, **kwargs)
|
|
8
|
+
|
|
9
|
+
def predict(self, x: torch.Tensor) -> torch.Tensor:
|
|
10
|
+
logits = self.logits(x)
|
|
11
|
+
return torch.nn.functional.softmax(logits, dim=1)
|
|
12
|
+
|
|
13
|
+
@torch.no_grad()
|
|
14
|
+
def predict_(self, x: torch.Tensor) -> torch.Tensor:
|
|
15
|
+
return self.predict(x)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from .ReportsEnsembleF1 import ReportsEnsembleF1
|
|
2
|
+
from .ReportsEnsembleAccuracy import ReportsEnsembleAccuracy
|
|
3
|
+
from .ReportsEnsembleKappa import ReportsEnsembleKappa
|
|
4
|
+
from .CalculatesMetricNecessities import CalculatesMetricNecessities
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class ReportsClassificationMetrics(
|
|
8
|
+
CalculatesMetricNecessities,
|
|
9
|
+
ReportsEnsembleAccuracy,
|
|
10
|
+
ReportsEnsembleF1,
|
|
11
|
+
ReportsEnsembleKappa,
|
|
12
|
+
):
|
|
13
|
+
pass
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from sklearn.metrics import accuracy_score as accuracy
|
|
2
|
+
from torch.utils.data import DataLoader
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class ReportsEnsembleAccuracy:
|
|
6
|
+
def __init__(self, *args, **kwargs):
|
|
7
|
+
super().__init__(*args, **kwargs)
|
|
8
|
+
|
|
9
|
+
def accuracy(self, dataloader: DataLoader) -> float:
|
|
10
|
+
predictions, labels = self.metric_necessities(dataloader)
|
|
11
|
+
return accuracy(labels, predictions)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from sklearn.metrics import f1_score as f1
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ReportsEnsembleF1:
|
|
5
|
+
def __init__(self, *args, **kwargs):
|
|
6
|
+
super().__init__(*args, **kwargs)
|
|
7
|
+
|
|
8
|
+
def f1(self) -> float:
|
|
9
|
+
predictions, labels = self.metric_necessities()
|
|
10
|
+
return f1(labels, predictions, average="weighted")
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from sklearn.metrics import cohen_kappa_score as kappa
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ReportsEnsembleKappa:
|
|
5
|
+
def __init__(self, *args, **kwargs):
|
|
6
|
+
super().__init__(*args, **kwargs)
|
|
7
|
+
|
|
8
|
+
def accuracy(self) -> float:
|
|
9
|
+
predictions, labels = self.metric_necessities()
|
|
10
|
+
return kappa(labels, predictions)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ReportsLogits:
|
|
5
|
+
def __init__(self, *args, **kwargs):
|
|
6
|
+
super().__init__(*args, **kwargs)
|
|
7
|
+
|
|
8
|
+
def logits(self, x: torch.Tensor) -> torch.Tensor:
|
|
9
|
+
batch_size = x.shape[0]
|
|
10
|
+
logits = torch.zeros((batch_size, self.num_classes))
|
|
11
|
+
for model in self.models:
|
|
12
|
+
logits += model(x)
|
|
13
|
+
return logits
|
|
14
|
+
|
|
15
|
+
@torch.no_grad()
|
|
16
|
+
def logits_(self, *args, **kwargs):
|
|
17
|
+
return self.logits(*args, **kwargs)
|