homa 0.1.91__tar.gz → 0.1.99__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.
Files changed (74) hide show
  1. {homa-0.1.91 → homa-0.1.99}/PKG-INFO +1 -1
  2. {homa-0.1.91 → homa-0.1.99}/pyproject.toml +1 -1
  3. homa-0.1.99/src/homa/activations/classes/APLU.py +86 -0
  4. homa-0.1.99/src/homa/activations/classes/GALU.py +67 -0
  5. homa-0.1.99/src/homa/activations/classes/MELU.py +70 -0
  6. homa-0.1.99/src/homa/activations/classes/PDELU.py +54 -0
  7. homa-0.1.99/src/homa/activations/classes/SReLU.py +69 -0
  8. homa-0.1.99/src/homa/activations/classes/SmallGALU.py +58 -0
  9. homa-0.1.99/src/homa/activations/classes/WideMELU.py +90 -0
  10. {homa-0.1.91 → homa-0.1.99}/src/homa/ensemble/concerns/CalculatesMetricNecessities.py +2 -2
  11. homa-0.1.99/src/homa/loss/LogitNormLoss.py +12 -0
  12. homa-0.1.99/src/homa/loss/Loss.py +2 -0
  13. homa-0.1.99/src/homa/loss/__init__.py +2 -0
  14. homa-0.1.99/src/homa/vision/StochasticResnet.py +9 -0
  15. homa-0.1.99/src/homa/vision/StochasticSwin.py +9 -0
  16. homa-0.1.99/src/homa/vision/Swin.py +12 -0
  17. {homa-0.1.91 → homa-0.1.99}/src/homa/vision/__init__.py +1 -0
  18. homa-0.1.99/src/homa/vision/modules/SwinModule.py +23 -0
  19. homa-0.1.99/src/homa/vision/modules/__init__.py +2 -0
  20. {homa-0.1.91 → homa-0.1.99}/src/homa.egg-info/PKG-INFO +1 -1
  21. {homa-0.1.91 → homa-0.1.99}/src/homa.egg-info/SOURCES.txt +6 -1
  22. homa-0.1.91/src/homa/activations/classes/APLU.py +0 -48
  23. homa-0.1.91/src/homa/activations/classes/GALU.py +0 -51
  24. homa-0.1.91/src/homa/activations/classes/MELU.py +0 -50
  25. homa-0.1.91/src/homa/activations/classes/PDELU.py +0 -39
  26. homa-0.1.91/src/homa/activations/classes/SReLU.py +0 -49
  27. homa-0.1.91/src/homa/activations/classes/SmallGALU.py +0 -39
  28. homa-0.1.91/src/homa/activations/classes/WideMELU.py +0 -61
  29. homa-0.1.91/src/homa/vision/StochasticResnet.py +0 -8
  30. homa-0.1.91/src/homa/vision/modules/StochasticResnetModule.py +0 -9
  31. homa-0.1.91/src/homa/vision/modules/__init__.py +0 -2
  32. {homa-0.1.91 → homa-0.1.99}/README.md +0 -0
  33. {homa-0.1.91 → homa-0.1.99}/setup.cfg +0 -0
  34. {homa-0.1.91 → homa-0.1.99}/src/homa/__init__.py +0 -0
  35. {homa-0.1.91 → homa-0.1.99}/src/homa/activations/__init__.py +0 -0
  36. {homa-0.1.91 → homa-0.1.99}/src/homa/activations/classes/StochasticActivation.py +0 -0
  37. {homa-0.1.91 → homa-0.1.99}/src/homa/activations/classes/__init__.py +0 -0
  38. {homa-0.1.91 → homa-0.1.99}/src/homa/activations/utils.py +0 -0
  39. {homa-0.1.91 → homa-0.1.99}/src/homa/cli/HomaCommand.py +0 -0
  40. {homa-0.1.91 → homa-0.1.99}/src/homa/cli/namespaces/CacheNamespace.py +0 -0
  41. {homa-0.1.91 → homa-0.1.99}/src/homa/cli/namespaces/MakeNamespace.py +0 -0
  42. {homa-0.1.91 → homa-0.1.99}/src/homa/cli/namespaces/__init__.py +0 -0
  43. {homa-0.1.91 → homa-0.1.99}/src/homa/device.py +0 -0
  44. {homa-0.1.91 → homa-0.1.99}/src/homa/ensemble/Ensemble.py +0 -0
  45. {homa-0.1.91 → homa-0.1.99}/src/homa/ensemble/__init__.py +0 -0
  46. {homa-0.1.91 → homa-0.1.99}/src/homa/ensemble/concerns/PredictsProbabilities.py +0 -0
  47. {homa-0.1.91 → homa-0.1.99}/src/homa/ensemble/concerns/ReportsClassificationMetrics.py +0 -0
  48. {homa-0.1.91 → homa-0.1.99}/src/homa/ensemble/concerns/ReportsEnsembleAccuracy.py +0 -0
  49. {homa-0.1.91 → homa-0.1.99}/src/homa/ensemble/concerns/ReportsEnsembleF1.py +0 -0
  50. {homa-0.1.91 → homa-0.1.99}/src/homa/ensemble/concerns/ReportsEnsembleKappa.py +0 -0
  51. {homa-0.1.91 → homa-0.1.99}/src/homa/ensemble/concerns/ReportsLogits.py +0 -0
  52. {homa-0.1.91 → homa-0.1.99}/src/homa/ensemble/concerns/ReportsSize.py +0 -0
  53. {homa-0.1.91 → homa-0.1.99}/src/homa/ensemble/concerns/StoresModels.py +0 -0
  54. {homa-0.1.91 → homa-0.1.99}/src/homa/ensemble/concerns/__init__.py +0 -0
  55. {homa-0.1.91 → homa-0.1.99}/src/homa/settings.py +0 -0
  56. {homa-0.1.91 → homa-0.1.99}/src/homa/torch/__init__.py +0 -0
  57. {homa-0.1.91 → homa-0.1.99}/src/homa/torch/helpers.py +0 -0
  58. {homa-0.1.91 → homa-0.1.99}/src/homa/utils.py +0 -0
  59. {homa-0.1.91 → homa-0.1.99}/src/homa/vision/ClassificationModel.py +0 -0
  60. {homa-0.1.91 → homa-0.1.99}/src/homa/vision/Model.py +0 -0
  61. {homa-0.1.91 → homa-0.1.99}/src/homa/vision/Resnet.py +0 -0
  62. {homa-0.1.91 → homa-0.1.99}/src/homa/vision/concerns/HasLabels.py +0 -0
  63. {homa-0.1.91 → homa-0.1.99}/src/homa/vision/concerns/HasLogits.py +0 -0
  64. {homa-0.1.91 → homa-0.1.99}/src/homa/vision/concerns/HasProbabilities.py +0 -0
  65. {homa-0.1.91 → homa-0.1.99}/src/homa/vision/concerns/ReportsAccuracy.py +0 -0
  66. {homa-0.1.91 → homa-0.1.99}/src/homa/vision/concerns/ReportsMetrics.py +0 -0
  67. {homa-0.1.91 → homa-0.1.99}/src/homa/vision/concerns/Trainable.py +0 -0
  68. {homa-0.1.91 → homa-0.1.99}/src/homa/vision/concerns/__init__.py +0 -0
  69. {homa-0.1.91 → homa-0.1.99}/src/homa/vision/modules/ResnetModule.py +0 -0
  70. {homa-0.1.91 → homa-0.1.99}/src/homa/vision/utils.py +0 -0
  71. {homa-0.1.91 → homa-0.1.99}/src/homa.egg-info/dependency_links.txt +0 -0
  72. {homa-0.1.91 → homa-0.1.99}/src/homa.egg-info/entry_points.txt +0 -0
  73. {homa-0.1.91 → homa-0.1.99}/src/homa.egg-info/requires.txt +0 -0
  74. {homa-0.1.91 → homa-0.1.99}/src/homa.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: homa
3
- Version: 0.1.91
3
+ Version: 0.1.99
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
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "homa"
7
- version = "0.1.91"
7
+ version = "0.1.99"
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,86 @@
1
+ import torch
2
+ from torch import nn
3
+ from torch.nn.parameter import Parameter, UninitializedParameter
4
+ import torch.nn.functional as F
5
+
6
+
7
+ class APLU(nn.Module):
8
+ def __init__(self, max_input: float = 1.0):
9
+ super().__init__()
10
+ self.max_input = float(max_input)
11
+ self.alpha = UninitializedParameter()
12
+ self.beta = UninitializedParameter()
13
+ self.gamma = UninitializedParameter()
14
+ self.xi = UninitializedParameter()
15
+ self.psi = UninitializedParameter()
16
+ self.mu = UninitializedParameter()
17
+ self._num_channels = None
18
+
19
+ def _initialize_parameters(self, x: torch.Tensor):
20
+ if x.ndim < 2:
21
+ raise ValueError(
22
+ f"Input tensor must have at least 2 dimensions (N, C), but got shape {tuple(x.shape)}"
23
+ )
24
+
25
+ channels = int(x.shape[1])
26
+ self._num_channels = channels
27
+ param_shape = [1] * x.ndim
28
+ param_shape[1] = channels
29
+
30
+ with torch.no_grad():
31
+ self.alpha = Parameter(
32
+ torch.zeros(param_shape, dtype=x.dtype, device=x.device)
33
+ )
34
+ self.beta = Parameter(
35
+ torch.zeros(param_shape, dtype=x.dtype, device=x.device)
36
+ )
37
+ self.gamma = Parameter(
38
+ torch.zeros(param_shape, dtype=x.dtype, device=x.device)
39
+ )
40
+ self.xi = Parameter(
41
+ torch.empty(param_shape, dtype=x.dtype, device=x.device).uniform_(
42
+ 0.0, self.max_input
43
+ )
44
+ )
45
+ self.psi = Parameter(
46
+ torch.empty(param_shape, dtype=x.dtype, device=x.device).uniform_(
47
+ 0.0, self.max_input
48
+ )
49
+ )
50
+ self.mu = Parameter(
51
+ torch.empty(param_shape, dtype=x.dtype, device=x.device).uniform_(
52
+ 0.0, self.max_input
53
+ )
54
+ )
55
+
56
+ def reset_parameters(self):
57
+ if isinstance(self.alpha, UninitializedParameter):
58
+ return
59
+
60
+ with torch.no_grad():
61
+ self.alpha.zero_()
62
+ self.beta.zero_()
63
+ self.gamma.zero_()
64
+ self.xi.uniform_(0.0, self.max_input)
65
+ self.psi.uniform_(0.0, self.max_input)
66
+ self.mu.uniform_(0.0, self.max_input)
67
+
68
+ def forward(self, x: torch.Tensor):
69
+ if isinstance(self.alpha, UninitializedParameter):
70
+ self._initialize_parameters(x)
71
+
72
+ if x.ndim < 2:
73
+ raise ValueError(
74
+ f"Input tensor must have at least 2 dimensions (N, C), but got shape {tuple(x.shape)}"
75
+ )
76
+ if self._num_channels is not None and x.shape[1] != self._num_channels:
77
+ raise RuntimeError(
78
+ f"APLU was initialized with C={self._num_channels} but got C={x.shape[1]}. "
79
+ "Create a new APLU for a different channel size."
80
+ )
81
+
82
+ a = F.relu(x)
83
+ b = self.alpha * F.relu(-x + self.xi)
84
+ c = self.beta * F.relu(-x + self.psi)
85
+ d = self.gamma * F.relu(-x + self.mu)
86
+ return a + b + c + d
@@ -0,0 +1,67 @@
1
+ import torch
2
+ from torch import nn
3
+ from torch.nn.parameter import Parameter, UninitializedParameter
4
+ import torch.nn.functional as F
5
+
6
+
7
+ class GALU(nn.Module):
8
+ def __init__(self, max_input: float = 1.0):
9
+ super().__init__()
10
+ if max_input <= 0:
11
+ raise ValueError("max_input must be positive.")
12
+ self.max_input = float(max_input)
13
+ self.alpha: torch.Tensor = UninitializedParameter()
14
+ self.beta: torch.Tensor = UninitializedParameter()
15
+ self.gamma: torch.Tensor = UninitializedParameter()
16
+ self.delta: torch.Tensor = UninitializedParameter()
17
+
18
+ def _initialize_parameters(self, x: torch.Tensor):
19
+ if x.ndim < 2:
20
+ raise ValueError(
21
+ f"Input tensor must have at least 2 dimensions (N, C), but got shape {tuple(x.shape)}"
22
+ )
23
+ param_shape = [1] * x.ndim
24
+ param_shape[1] = int(x.shape[1])
25
+ zeros = torch.zeros(param_shape, dtype=x.dtype, device=x.device)
26
+ with torch.no_grad():
27
+ for name in ("alpha", "beta", "gamma", "delta"):
28
+ setattr(self, name, Parameter(zeros.clone()))
29
+
30
+ def reset_parameters(self):
31
+ for name in ("alpha", "beta", "gamma", "delta"):
32
+ p = getattr(self, name)
33
+ if not isinstance(p, UninitializedParameter):
34
+ with torch.no_grad():
35
+ p.zero_()
36
+
37
+ def forward(self, x: torch.Tensor):
38
+ if isinstance(self.alpha, UninitializedParameter):
39
+ self._initialize_parameters(x)
40
+
41
+ if x.ndim < 2:
42
+ raise ValueError(
43
+ f"Input tensor must have at least 2 dimensions (N, C), but got shape {tuple(x.shape)}"
44
+ )
45
+ if not isinstance(self.alpha, UninitializedParameter) and x.shape[1] != self.alpha.shape[1]:
46
+ raise RuntimeError(
47
+ f"GALU was initialized with C={self.alpha.shape[1]} but got C={x.shape[1]}. "
48
+ "Create a new GALU for a different channel size."
49
+ )
50
+
51
+ x_norm = x / self.max_input
52
+ zero = x.new_zeros(1)
53
+ part_prelu = F.relu(x_norm) + self.alpha * torch.minimum(x_norm, zero)
54
+ part_beta = self.beta * (
55
+ F.relu(1.0 - torch.abs(x_norm - 1.0))
56
+ + torch.minimum(torch.abs(x_norm - 3.0) - 1.0, zero)
57
+ )
58
+ part_gamma = self.gamma * (
59
+ F.relu(0.5 - torch.abs(x_norm - 0.5))
60
+ + torch.minimum(torch.abs(x_norm - 1.5) - 0.5, zero)
61
+ )
62
+ part_delta = self.delta * (
63
+ F.relu(0.5 - torch.abs(x_norm - 2.5))
64
+ + torch.minimum(torch.abs(x_norm - 3.5) - 0.5, zero)
65
+ )
66
+ z = part_prelu + part_beta + part_gamma + part_delta
67
+ return z * self.max_input
@@ -0,0 +1,70 @@
1
+ import torch
2
+ from torch import nn
3
+ import torch.nn.functional as F
4
+
5
+
6
+ class MELU(nn.Module):
7
+ def __init__(self, maxInput: float = 1.0):
8
+ super().__init__()
9
+ self.maxInput = float(maxInput)
10
+ self._num_channels = None
11
+ self.register_parameter("alpha", None)
12
+ self.register_parameter("beta", None)
13
+ self.register_parameter("gamma", None)
14
+ self.register_parameter("delta", None)
15
+ self.register_parameter("xi", None)
16
+ self.register_parameter("psi", None)
17
+
18
+ def _ensure_parameters(self, x: torch.Tensor):
19
+ if x.dim() != 4:
20
+ raise ValueError(
21
+ f"Expected 4D input (N, C, H, W), got {x.dim()}D with shape {tuple(x.shape)}"
22
+ )
23
+ c = int(x.shape[1])
24
+ if self._num_channels is None:
25
+ self._num_channels = c
26
+ elif c != self._num_channels:
27
+ raise RuntimeError(
28
+ f"MELU was initialized with C={self._num_channels} but got C={c}. "
29
+ "Create a new MELU for a different channel size."
30
+ )
31
+
32
+ if self.alpha is None:
33
+ shape = (1, c, 1, 1)
34
+ device, dtype = x.device, x.dtype
35
+ for name in ("alpha", "beta", "gamma", "delta", "xi", "psi"):
36
+ setattr(
37
+ self,
38
+ name,
39
+ nn.Parameter(torch.zeros(shape, dtype=dtype, device=device)),
40
+ )
41
+
42
+ def reset_parameters(self):
43
+ for p in (self.alpha, self.beta, self.gamma, self.delta, self.xi, self.psi):
44
+ if p is not None:
45
+ with torch.no_grad():
46
+ p.zero_()
47
+
48
+ def forward(self, X: torch.Tensor) -> torch.Tensor:
49
+ self._ensure_parameters(X)
50
+
51
+ X_norm = X / self.maxInput
52
+ Y = torch.roll(X_norm, shifts=-1, dims=1)
53
+
54
+ term1 = F.relu(X_norm)
55
+ term2 = self.alpha * torch.clamp(X_norm, max=0)
56
+
57
+ dist_sq_beta = (X_norm - 2) ** 2 + (Y - 2) ** 2
58
+ dist_sq_gamma = (X_norm - 1) ** 2 + (Y - 1) ** 2
59
+ dist_sq_delta = (X_norm - 1) ** 2 + (Y - 3) ** 2
60
+ dist_sq_xi = (X_norm - 3) ** 2 + (Y - 1) ** 2
61
+ dist_sq_psi = (X_norm - 3) ** 2 + (Y - 3) ** 2
62
+
63
+ term3 = self.beta * torch.sqrt(F.relu(2 - dist_sq_beta))
64
+ term4 = self.gamma * torch.sqrt(F.relu(1 - dist_sq_gamma))
65
+ term5 = self.delta * torch.sqrt(F.relu(1 - dist_sq_delta))
66
+ term6 = self.xi * torch.sqrt(F.relu(1 - dist_sq_xi))
67
+ term7 = self.psi * torch.sqrt(F.relu(1 - dist_sq_psi))
68
+
69
+ Z_norm = term1 + term2 + term3 + term4 + term5 + term6 + term7
70
+ return Z_norm * self.maxInput
@@ -0,0 +1,54 @@
1
+ import torch
2
+ from torch import nn
3
+ import torch.nn.functional as F
4
+
5
+
6
+ class PDELU(nn.Module):
7
+ def __init__(self, theta: float = 0.5):
8
+ super().__init__()
9
+ if theta == 1.0:
10
+ raise ValueError(
11
+ "theta cannot be 1.0, as it would cause a division by zero."
12
+ )
13
+ self.theta = float(theta)
14
+ self._power_val = 1.0 / (1.0 - self.theta)
15
+ self.register_parameter("alpha", None)
16
+ self._num_channels = None
17
+
18
+ def _ensure_parameters(self, x: torch.Tensor):
19
+ if x.ndim < 2:
20
+ raise ValueError(
21
+ f"Input tensor must have at least 2 dimensions (N, C), but got shape {tuple(x.shape)}"
22
+ )
23
+
24
+ c = int(x.shape[1])
25
+ if self._num_channels is None:
26
+ self._num_channels = c
27
+ elif c != self._num_channels:
28
+ raise RuntimeError(
29
+ f"PDELU was initialized with C={self._num_channels} but got C={c}. "
30
+ "Create a new PDELU for a different channel size."
31
+ )
32
+
33
+ if self.alpha is None:
34
+ param_shape = [1] * x.ndim
35
+ param_shape[1] = c
36
+ self.alpha = nn.Parameter(
37
+ torch.full(param_shape, 0.1, dtype=x.dtype, device=x.device)
38
+ )
39
+
40
+ def reset_parameters(self):
41
+ if self.alpha is not None:
42
+ with torch.no_grad():
43
+ self.alpha.fill_(0.1)
44
+
45
+ def forward(self, x: torch.Tensor):
46
+ self._ensure_parameters(x)
47
+
48
+ positive_part = F.relu(x)
49
+ inner_term = F.relu(1.0 + (1.0 - self.theta) * x)
50
+ powered_term = torch.pow(inner_term, self._power_val)
51
+ subtracted_term = powered_term - 1.0
52
+ zero = torch.zeros(1, dtype=x.dtype, device=x.device)
53
+ negative_part = self.alpha * torch.minimum(subtracted_term, zero)
54
+ return positive_part + negative_part
@@ -0,0 +1,69 @@
1
+ import torch
2
+ from torch import nn
3
+
4
+
5
+ class SReLU(nn.Module):
6
+ def __init__(
7
+ self,
8
+ alpha_init: float = 0.0,
9
+ beta_init: float = 0.0,
10
+ gamma_init: float = 1.0,
11
+ delta_init: float = 1.0,
12
+ ):
13
+ super().__init__()
14
+ self.alpha_init_val = float(alpha_init)
15
+ self.beta_init_val = float(beta_init)
16
+ self.gamma_init_val = float(gamma_init)
17
+ self.delta_init_val = float(delta_init)
18
+ self._num_channels = None
19
+ self.register_parameter("alpha", None)
20
+ self.register_parameter("beta", None)
21
+ self.register_parameter("gamma", None)
22
+ self.register_parameter("delta", None)
23
+
24
+ def _ensure_parameters(self, x: torch.Tensor):
25
+ if x.dim() != 4:
26
+ raise ValueError(
27
+ f"Expected 4D input (N, C, H, W), got {x.dim()}D with shape {tuple(x.shape)}"
28
+ )
29
+ c = int(x.shape[1])
30
+ if self._num_channels is None:
31
+ self._num_channels = c
32
+ elif c != self._num_channels:
33
+ raise RuntimeError(
34
+ f"SReLU was initialized with C={self._num_channels} but got C={c}. "
35
+ "Create a new SReLU for different channel sizes."
36
+ )
37
+
38
+ if self.alpha is None:
39
+ shape = (1, c, 1, 1)
40
+ device, dtype = x.device, x.dtype
41
+ self.alpha = nn.Parameter(
42
+ torch.full(shape, self.alpha_init_val, dtype=dtype, device=device)
43
+ )
44
+ self.beta = nn.Parameter(
45
+ torch.full(shape, self.beta_init_val, dtype=dtype, device=device)
46
+ )
47
+ self.gamma = nn.Parameter(
48
+ torch.full(shape, self.gamma_init_val, dtype=dtype, device=device)
49
+ )
50
+ self.delta = nn.Parameter(
51
+ torch.full(shape, self.delta_init_val, dtype=dtype, device=device)
52
+ )
53
+
54
+ def reset_parameters(self):
55
+ if self.alpha is not None:
56
+ with torch.no_grad():
57
+ self.alpha.fill_(self.alpha_init_val)
58
+ self.beta.fill_(self.beta_init_val)
59
+ self.gamma.fill_(self.gamma_init_val)
60
+ self.delta.fill_(self.delta_init_val)
61
+
62
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
63
+ self._ensure_parameters(x)
64
+
65
+ start = self.beta + self.alpha * (x - self.beta)
66
+ finish = self.delta + self.gamma * (x - self.delta)
67
+ out = torch.where(x < self.beta, start, x)
68
+ out = torch.where(x > self.delta, finish, out)
69
+ return out
@@ -0,0 +1,58 @@
1
+ import torch
2
+ from torch import nn
3
+ from torch.nn.parameter import Parameter
4
+ import torch.nn.functional as F
5
+
6
+
7
+ class SmallGALU(nn.Module):
8
+ def __init__(self, max_input: float = 1.0):
9
+ super().__init__()
10
+ if max_input <= 0:
11
+ raise ValueError("max_input must be positive.")
12
+ self.max_input = float(max_input)
13
+ self.register_parameter("alpha", None)
14
+ self.register_parameter("beta", None)
15
+ self._num_channels = None
16
+
17
+ def _initialize_parameters(self, x: torch.Tensor):
18
+ if x.ndim < 2:
19
+ raise ValueError(
20
+ f"Input tensor must have at least 2 dimensions (N, C), but got shape {tuple(x.shape)}"
21
+ )
22
+ self._num_channels = int(x.shape[1])
23
+ param_shape = [1] * x.ndim
24
+ param_shape[1] = self._num_channels
25
+ device = x.device
26
+ dtype = x.dtype
27
+ self.alpha = Parameter(torch.zeros(param_shape, dtype=dtype, device=device))
28
+ self.beta = Parameter(torch.zeros(param_shape, dtype=dtype, device=device))
29
+
30
+ def reset_parameters(self):
31
+ if self.alpha is not None:
32
+ with torch.no_grad():
33
+ self.alpha.zero_()
34
+ self.beta.zero_()
35
+
36
+ def forward(self, x: torch.Tensor):
37
+ if self.alpha is None:
38
+ self._initialize_parameters(x)
39
+ else:
40
+ if x.ndim < 2:
41
+ raise ValueError(
42
+ f"Input tensor must have at least 2 dimensions (N, C), but got shape {tuple(x.shape)}"
43
+ )
44
+ if x.shape[1] != self._num_channels:
45
+ raise RuntimeError(
46
+ f"SmallGALU was initialized with C={self._num_channels} but got C={x.shape[1]}. "
47
+ "Create a new SmallGALU for a different channel size."
48
+ )
49
+
50
+ x_norm = x / self.max_input
51
+ zero = torch.zeros(1, dtype=x.dtype, device=x.device)
52
+ part_prelu = F.relu(x_norm) + self.alpha * torch.minimum(x_norm, zero)
53
+ part_beta = self.beta * (
54
+ F.relu(1.0 - torch.abs(x_norm - 1.0))
55
+ + torch.minimum(torch.abs(x_norm - 3.0) - 1.0, zero)
56
+ )
57
+ z = part_prelu + part_beta
58
+ return z * self.max_input
@@ -0,0 +1,90 @@
1
+ import torch
2
+ from torch import nn
3
+ import torch.nn.functional as F
4
+
5
+
6
+ class WideMELU(nn.Module):
7
+ def __init__(self, maxInput: float = 1.0):
8
+ super().__init__()
9
+ self.maxInput = float(maxInput)
10
+ self._num_channels = None
11
+ self.register_parameter("alpha", None)
12
+ self.register_parameter("beta", None)
13
+ self.register_parameter("gamma", None)
14
+ self.register_parameter("delta", None)
15
+ self.register_parameter("xi", None)
16
+ self.register_parameter("psi", None)
17
+ self.register_parameter("theta", None)
18
+ self.register_parameter("lam", None)
19
+
20
+ def _ensure_parameters(self, x: torch.Tensor):
21
+ if x.dim() != 4:
22
+ raise ValueError(
23
+ f"Expected 4D input (N, C, H, W), got {x.dim()}D with shape {tuple(x.shape)}"
24
+ )
25
+
26
+ c = int(x.shape[1])
27
+ if self._num_channels is None:
28
+ self._num_channels = c
29
+ elif c != self._num_channels:
30
+ raise RuntimeError(
31
+ f"WideMELU was initialized with C={self._num_channels} but got C={c}. "
32
+ "Create a new WideMELU for different channel sizes."
33
+ )
34
+
35
+ if self.alpha is None:
36
+ shape = (1, c, 1, 1)
37
+ device, dtype = x.device, x.dtype
38
+ for name in (
39
+ "alpha",
40
+ "beta",
41
+ "gamma",
42
+ "delta",
43
+ "xi",
44
+ "psi",
45
+ "theta",
46
+ "lam",
47
+ ):
48
+ param = nn.Parameter(torch.zeros(shape, dtype=dtype, device=device))
49
+ setattr(self, name, param)
50
+
51
+ def reset_parameters(self):
52
+ params = (
53
+ self.alpha,
54
+ self.beta,
55
+ self.gamma,
56
+ self.delta,
57
+ self.xi,
58
+ self.psi,
59
+ self.theta,
60
+ self.lam,
61
+ )
62
+ for p in params:
63
+ if p is not None:
64
+ with torch.no_grad():
65
+ p.zero_()
66
+
67
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
68
+ self._ensure_parameters(x)
69
+
70
+ X_norm = x / self.maxInput
71
+ Y = torch.roll(X_norm, shifts=-1, dims=1)
72
+
73
+ term1 = F.relu(X_norm)
74
+ term2 = self.alpha * torch.clamp(X_norm, max=0)
75
+ dist_sq_beta = (X_norm - 2) ** 2 + (Y - 2) ** 2
76
+ dist_sq_gamma = (X_norm - 1) ** 2 + (Y - 1) ** 2
77
+ dist_sq_delta = (X_norm - 1) ** 2 + (Y - 3) ** 2
78
+ dist_sq_xi = (X_norm - 3) ** 2 + (Y - 1) ** 2
79
+ dist_sq_psi = (X_norm - 3) ** 2 + (Y - 3) ** 2
80
+ dist_sq_theta = (X_norm - 1) ** 2 + (Y - 2) ** 2
81
+ dist_sq_lambda = (X_norm - 3) ** 2 + (Y - 2) ** 2
82
+ term3 = self.beta * torch.sqrt(F.relu(2 - dist_sq_beta))
83
+ term4 = self.gamma * torch.sqrt(F.relu(1 - dist_sq_gamma))
84
+ term5 = self.delta * torch.sqrt(F.relu(1 - dist_sq_delta))
85
+ term6 = self.xi * torch.sqrt(F.relu(1 - dist_sq_xi))
86
+ term7 = self.psi * torch.sqrt(F.relu(1 - dist_sq_psi))
87
+ term8 = self.theta * torch.sqrt(F.relu(1 - dist_sq_theta))
88
+ term9 = self.lam * torch.sqrt(F.relu(1 - dist_sq_lambda))
89
+ Z_norm = term1 + term2 + term3 + term4 + term5 + term6 + term7 + term8 + term9
90
+ return Z_norm * self.maxInput
@@ -18,7 +18,7 @@ class CalculatesMetricNecessities:
18
18
  model.eval()
19
19
  logits = model(x)
20
20
  sum_logits = logits if sum_logits is None else sum_logits + logits
21
- predictions = sum_logits.argmax(dim=1)
22
- predictions.extend(predictions.cpu().numpy())
21
+ batch_predictions = sum_logits.argmax(dim=1)
22
+ predictions.extend(batch_predictions.cpu().numpy())
23
23
  labels.extend(y.cpu().numpy())
24
24
  return predictions, labels
@@ -0,0 +1,12 @@
1
+ import torch
2
+ from .Loss import Loss
3
+
4
+
5
+ class LogitNormLoss(Loss):
6
+ def __init__(self, *args, **kwargs):
7
+ super().__init__(*args, **kwargs)
8
+
9
+ def forward(self, logits, target):
10
+ norms = torch.norm(logits, p=2, dim=-1, keepdim=True) + 1e-7
11
+ normalized_logits = torch.div(logits, norms)
12
+ return torch.nn.functional.cross_entropy(normalized_logits, target)
@@ -0,0 +1,2 @@
1
+ class Loss:
2
+ pass
@@ -0,0 +1,2 @@
1
+ from .LogitNormLoss import LogitNormLoss
2
+ from .Loss import Loss
@@ -0,0 +1,9 @@
1
+ from .Resnet import Resnet
2
+ from .utils import replace_relu
3
+ from ..activations import StochasticActivation
4
+
5
+
6
+ class StochasticResnet(Resnet):
7
+ def __init__(self, *args, **kwargs):
8
+ super().__init__(*args, **kwargs)
9
+ replace_relu(self.network, StochasticActivation)
@@ -0,0 +1,9 @@
1
+ from .Swin import Swin
2
+ from .utils import replace_gelu
3
+ from ..activations import StochasticActivation
4
+
5
+
6
+ class StochasticSwin(Swin):
7
+ def __init__(self, *args, **kwargs):
8
+ super().__init__(*args, **kwargs)
9
+ replace_gelu(self.network.encoder, StochasticActivation)
@@ -0,0 +1,12 @@
1
+ import torch
2
+ from .ClassificationModel import ClassificationModel
3
+ from .concerns import Trainable, ReportsMetrics
4
+ from .modules import SwinModule
5
+
6
+
7
+ class Swin(ClassificationModel, Trainable, ReportsMetrics):
8
+ def __init__(self, num_classes: int, lr: float = 0.0001):
9
+ super().__init__()
10
+ self.network = SwinModule(num_classes=num_classes)
11
+ self.optimizer = torch.optim.AdamW(self.network.parameters(), lr=lr)
12
+ self.criterion = torch.nn.CrossEntropyLoss()
@@ -1,3 +1,4 @@
1
1
  from .Model import Model
2
2
  from .Resnet import Resnet
3
3
  from .StochasticResnet import StochasticResnet
4
+ from .Swin import Swin
@@ -0,0 +1,23 @@
1
+ import torch
2
+ from torchvision.models import swin_v2_b
3
+ from torch.nn.init import kaiming_uniform_ as kaiming
4
+
5
+
6
+ class SwinModule(torch.nn.Module):
7
+ def __init__(self, num_classes: int):
8
+ super().__init__()
9
+ self.num_classes = num_classes
10
+ self._create_encoder()
11
+ self._create_fc()
12
+
13
+ def _create_encoder(self):
14
+ self.encoder = swin_v2_b(weights="DEFAULT")
15
+ self.encoder.head = torch.nn.Identity()
16
+
17
+ def _create_fc(self):
18
+ self.fc = torch.nn.Linear(1024, self.num_classes)
19
+ kaiming(self.fc.weight, mode="fan_in", nonlinearity="relu")
20
+
21
+ def forward(self, images: torch.Tensor):
22
+ features = self.encoder(images)
23
+ return self.fc(features)
@@ -0,0 +1,2 @@
1
+ from .ResnetModule import ResnetModule
2
+ from .SwinModule import SwinModule
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: homa
3
- Version: 0.1.91
3
+ Version: 0.1.99
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
@@ -37,12 +37,17 @@ src/homa/ensemble/concerns/ReportsLogits.py
37
37
  src/homa/ensemble/concerns/ReportsSize.py
38
38
  src/homa/ensemble/concerns/StoresModels.py
39
39
  src/homa/ensemble/concerns/__init__.py
40
+ src/homa/loss/LogitNormLoss.py
41
+ src/homa/loss/Loss.py
42
+ src/homa/loss/__init__.py
40
43
  src/homa/torch/__init__.py
41
44
  src/homa/torch/helpers.py
42
45
  src/homa/vision/ClassificationModel.py
43
46
  src/homa/vision/Model.py
44
47
  src/homa/vision/Resnet.py
45
48
  src/homa/vision/StochasticResnet.py
49
+ src/homa/vision/StochasticSwin.py
50
+ src/homa/vision/Swin.py
46
51
  src/homa/vision/__init__.py
47
52
  src/homa/vision/utils.py
48
53
  src/homa/vision/concerns/HasLabels.py
@@ -53,5 +58,5 @@ src/homa/vision/concerns/ReportsMetrics.py
53
58
  src/homa/vision/concerns/Trainable.py
54
59
  src/homa/vision/concerns/__init__.py
55
60
  src/homa/vision/modules/ResnetModule.py
56
- src/homa/vision/modules/StochasticResnetModule.py
61
+ src/homa/vision/modules/SwinModule.py
57
62
  src/homa/vision/modules/__init__.py
@@ -1,48 +0,0 @@
1
- import torch
2
-
3
-
4
- class APLU(torch.nn.Module):
5
- def __init__(self, max_input: float = 1.0):
6
- super(APLU, self).__init__()
7
- self.max_input = max_input
8
- self.alpha = None
9
- self.beta = None
10
- self.gamma = None
11
- self.xi = None
12
- self.psi = None
13
- self.mu = None
14
- self._num_channels = None
15
-
16
- def _initialize_parameters(self, x):
17
- if x.ndim < 2:
18
- raise ValueError(
19
- f"Input tensor must have at least 2 dimensions (N, C), but got shape {x.shape}"
20
- )
21
-
22
- num_channels = x.shape[1]
23
- self._num_channels = num_channels
24
-
25
- param_shape = [1] * x.ndim
26
- param_shape[1] = num_channels
27
-
28
- self.alpha = torch.nn.Parameter(torch.zeros(param_shape))
29
- self.beta = torch.nn.Parameter(torch.zeros(param_shape))
30
- self.gamma = torch.nn.Parameter(torch.zeros(param_shape))
31
-
32
- self.xi = torch.nn.Parameter(self.max_input * torch.rand(param_shape))
33
- self.psi = torch.nn.Parameter(self.max_input * torch.rand(param_shape))
34
- self.mu = torch.nn.Parameter(self.max_input * torch.rand(param_shape))
35
-
36
- def forward(self, x):
37
- if self.alpha is None:
38
- self._initialize_parameters(x)
39
-
40
- a = torch.relu(x)
41
-
42
- # following are called hinges
43
- b = self.alpha * torch.relu(-x + self.xi)
44
- c = self.beta * torch.relu(-x + self.psi)
45
- d = self.gamma * torch.relu(-x + self.mu)
46
- z = a + b + c + d
47
-
48
- return z
@@ -1,51 +0,0 @@
1
- import torch
2
-
3
-
4
- class GALU(torch.nn.Module):
5
- def __init__(self, max_input: float = 1.0):
6
- super(GALU, self).__init__()
7
- if max_input <= 0:
8
- raise ValueError("max_input must be positive.")
9
- self.max_input = max_input
10
- self.alpha = None
11
- self.beta = None
12
- self.gamma = None
13
- self.delta = None
14
- self._num_channels = None
15
-
16
- def _initialize_parameters(self, x):
17
- if x.ndim < 2:
18
- raise ValueError(
19
- f"Input tensor must have at least 2 dimensions (N, C), but got shape {x.shape}"
20
- )
21
-
22
- num_channels = x.shape[1]
23
- self._num_channels = num_channels
24
- param_shape = [1] * x.ndim
25
- param_shape[1] = num_channels
26
- self.alpha = torch.nn.Parameter(torch.zeros(param_shape))
27
- self.beta = torch.nn.Parameter(torch.zeros(param_shape))
28
- self.gamma = torch.nn.Parameter(torch.zeros(param_shape))
29
- self.delta = torch.nn.Parameter(torch.zeros(param_shape))
30
-
31
- def forward(self, x):
32
- if self.alpha is None:
33
- self._initialize_parameters(x)
34
-
35
- zero = torch.tensor(0.0, device=x.device, dtype=x.dtype)
36
- x_norm = x / self.max_input
37
- part_prelu = torch.relu(x_norm) + self.alpha * torch.min(x_norm, zero)
38
- part_beta = self.beta * (
39
- torch.relu(1.0 - torch.abs(x_norm - 1.0))
40
- + torch.min(torch.abs(x_norm - 3.0) - 1.0, zero)
41
- )
42
- part_gamma = self.gamma * (
43
- torch.relu(0.5 - torch.abs(x_norm - 0.5))
44
- + torch.min(torch.abs(x_norm - 1.5) - 0.5, zero)
45
- )
46
- part_delta = self.delta * (
47
- torch.relu(0.5 - torch.abs(x_norm - 2.5))
48
- + torch.min(torch.abs(x_norm - 3.5) - 0.5, zero)
49
- )
50
- z = part_prelu + part_beta + part_gamma + part_delta
51
- return z * self.max_input
@@ -1,50 +0,0 @@
1
- import torch
2
-
3
-
4
- class MELU(torch.nn.Module):
5
- def __init__(self, maxInput: float = 1.0):
6
- super().__init__()
7
- self.maxInput = float(maxInput)
8
- self.alpha = None
9
- self.beta = None
10
- self.gamma = None
11
- self.delta = None
12
- self.xi = None
13
- self.psi = None
14
- self._initialized = False
15
-
16
- def _initialize_parameters(self, X: torch.Tensor):
17
- if X.dim() != 4:
18
- raise ValueError(
19
- f"Expected 4D input (B, C, H, W), but got {X.dim()}D input."
20
- )
21
- num_channels = X.shape[1]
22
- shape = (1, num_channels, 1, 1)
23
- self.alpha = torch.nn.Parameter(torch.zeros(shape))
24
- self.beta = torch.nn.Parameter(torch.zeros(shape))
25
- self.gamma = torch.nn.Parameter(torch.zeros(shape))
26
- self.delta = torch.nn.Parameter(torch.zeros(shape))
27
- self.xi = torch.nn.Parameter(torch.zeros(shape))
28
- self.psi = torch.nn.Parameter(torch.zeros(shape))
29
- self._initialized = True
30
-
31
- def forward(self, X: torch.Tensor) -> torch.Tensor:
32
- if not self._initialized:
33
- self._initialize_parameters(X)
34
- X_norm = X / self.maxInput
35
- Y = torch.roll(X_norm, shifts=-1, dims=1)
36
- term1 = torch.relu(X_norm)
37
- term2 = self.alpha * torch.clamp(X_norm, max=0)
38
- dist_sq_beta = (X_norm - 2) ** 2 + (Y - 2) ** 2
39
- dist_sq_gamma = (X_norm - 1) ** 2 + (Y - 1) ** 2
40
- dist_sq_delta = (X_norm - 1) ** 2 + (Y - 3) ** 2
41
- dist_sq_xi = (X_norm - 3) ** 2 + (Y - 1) ** 2
42
- dist_sq_psi = (X_norm - 3) ** 2 + (Y - 3) ** 2
43
- term3 = self.beta * torch.sqrt(torch.relu(2 - dist_sq_beta))
44
- term4 = self.gamma * torch.sqrt(torch.relu(1 - dist_sq_gamma))
45
- term5 = self.delta * torch.sqrt(torch.relu(1 - dist_sq_delta))
46
- term6 = self.xi * torch.sqrt(torch.relu(1 - dist_sq_xi))
47
- term7 = self.psi * torch.sqrt(torch.relu(1 - dist_sq_psi))
48
- Z_norm = term1 + term2 + term3 + term4 + term5 + term6 + term7
49
- Z = Z_norm * self.maxInput
50
- return Z
@@ -1,39 +0,0 @@
1
- import torch
2
-
3
-
4
- class PDELU(torch.nn.Module):
5
- def __init__(self, theta: float = 0.5):
6
- super(PDELU, self).__init__()
7
- if theta == 1.0:
8
- raise ValueError(
9
- "theta cannot be 1.0, as it would cause a division by zero."
10
- )
11
- self.theta = theta
12
- self._power_val = 1.0 / (1.0 - self.theta)
13
- self.alpha = torch.nn.UninitializedParameter()
14
- self._num_channels = None
15
-
16
- def _initialize_parameters(self, x: torch.Tensor):
17
- if x.ndim < 2:
18
- raise ValueError(
19
- f"Input tensor must have at least 2 dimensions (N, C), but got shape {x.shape}"
20
- )
21
-
22
- num_channels = x.shape[1]
23
- self._num_channels = num_channels
24
- param_shape = [1] * x.ndim
25
- param_shape[1] = num_channels
26
- init_tensor = torch.zeros(param_shape) + 0.1
27
- self.alpha = torch.nn.Parameter(init_tensor)
28
-
29
- def forward(self, x: torch.Tensor):
30
- if self.alpha is None:
31
- self._initialize_parameters(x)
32
-
33
- zero = torch.tensor(0.0, device=x.device, dtype=x.dtype)
34
- positive_part = torch.relu(x)
35
- inner_term = torch.relu(1.0 + (1.0 - self.theta) * x)
36
- powered_term = torch.pow(inner_term, self._power_val)
37
- subtracted_term = powered_term - 1.0
38
- negative_part = self.alpha * torch.min(subtracted_term, zero)
39
- return positive_part + negative_part
@@ -1,49 +0,0 @@
1
- import torch
2
-
3
-
4
- class SReLU(torch.nn.Module):
5
- def __init__(
6
- self,
7
- alpha_init: float = 0.0,
8
- beta_init: float = 0.0,
9
- gamma_init: float = 1.0,
10
- delta_init: float = 1.0,
11
- ):
12
- super().__init__()
13
- self.alpha_init_val = alpha_init
14
- self.beta_init_val = beta_init
15
- self.gamma_init_val = gamma_init
16
- self.delta_init_val = delta_init
17
- self.alpha = torch.nn.UninitializedParameter()
18
- self.beta = torch.nn.UninitializedParameter()
19
- self.gamma = torch.nn.UninitializedParameter()
20
- self.delta = torch.nn.UninitializedParameter()
21
-
22
- def _initialize_parameters(self, x: torch.Tensor):
23
- if isinstance(self.alpha, torch.nn.UninitializedParameter):
24
- if x.dim() < 2:
25
- raise ValueError(
26
- f"Input tensor must have at least 2 dimensions (N, C), but got {x.dim()}"
27
- )
28
-
29
- num_channels = x.shape[1]
30
- param_shape = [1] * x.dim()
31
- param_shape[1] = num_channels
32
- self.alpha = torch.nn.Parameter(
33
- torch.full(param_shape, self.alpha_init_val)
34
- )
35
- self.beta = torch.nn.Parameter(torch.full(param_shape, self.beta_init_val))
36
- self.gamma = torch.nn.Parameter(
37
- torch.full(param_shape, self.gamma_init_val)
38
- )
39
- self.delta = torch.nn.Parameter(
40
- torch.full(param_shape, self.delta_init_val)
41
- )
42
-
43
- def forward(self, x: torch.Tensor) -> torch.Tensor:
44
- self._initialize_parameters(x)
45
- start = self.beta + self.alpha * (x - self.beta)
46
- finish = self.delta + self.gamma * (x - self.delta)
47
- out = torch.where(x < self.beta, start, x)
48
- out = torch.where(x > self.delta, finish, out)
49
- return out
@@ -1,39 +0,0 @@
1
- import torch
2
-
3
-
4
- class SmallGALU(torch.nn.Module):
5
- def __init__(self, max_input: float = 1.0):
6
- super(SmallGALU, self).__init__()
7
- if max_input <= 0:
8
- raise ValueError("max_input must be positive.")
9
- self.max_input = max_input
10
- self.alpha = None
11
- self.beta = None
12
- self._num_channels = None
13
-
14
- def _initialize_parameters(self, x):
15
- if x.ndim < 2:
16
- raise ValueError(
17
- f"Input tensor must have at least 2 dimensions (N, C), but got shape {x.shape}"
18
- )
19
-
20
- num_channels = x.shape[1]
21
- self._num_channels = num_channels
22
- param_shape = [1] * x.ndim
23
- param_shape[1] = num_channels
24
- self.alpha = torch.nn.Parameter(torch.zeros(param_shape))
25
- self.beta = torch.nn.Parameter(torch.zeros(param_shape))
26
-
27
- def forward(self, x):
28
- if self.alpha is None:
29
- self._initialize_parameters(x)
30
-
31
- zero = torch.tensor(0.0, device=x.device, dtype=x.dtype)
32
- x_norm = x / self.max_input
33
- part_prelu = torch.relu(x_norm) + self.alpha * torch.min(x_norm, zero)
34
- part_beta = self.beta * (
35
- torch.relu(1.0 - torch.abs(x_norm - 1.0))
36
- + torch.min(torch.abs(x_norm - 3.0) - 1.0, zero)
37
- )
38
- z = part_prelu + part_beta
39
- return z * self.max_input
@@ -1,61 +0,0 @@
1
- import torch
2
-
3
-
4
- class WideMELU(torch.nn.Module):
5
- def __init__(self, maxInput: float = 1.0):
6
- super().__init__()
7
- self.maxInput = float(maxInput)
8
- self.alpha = None
9
- self.beta = None
10
- self.gamma = None
11
- self.delta = None
12
- self.xi = None
13
- self.psi = None
14
- self.theta = None
15
- self.lam = None
16
- self._initialized = False
17
-
18
- def _initialize_parameters(self, X: torch.Tensor):
19
- if X.dim() != 4:
20
- raise ValueError(
21
- f"Expected 4D input (B, C, H, W), but got {X.dim()}D input."
22
- )
23
-
24
- num_channels = X.shape[1]
25
- shape = (1, num_channels, 1, 1)
26
-
27
- self.alpha = torch.nn.Parameter(torch.zeros(shape))
28
- self.beta = torch.nn.Parameter(torch.zeros(shape))
29
- self.gamma = torch.nn.Parameter(torch.zeros(shape))
30
- self.delta = torch.nn.Parameter(torch.zeros(shape))
31
- self.xi = torch.nn.Parameter(torch.zeros(shape))
32
- self.psi = torch.nn.Parameter(torch.zeros(shape))
33
- self.theta = torch.nn.Parameter(torch.zeros(shape))
34
- self.lam = torch.nn.Parameter(torch.zeros(shape))
35
- self._initialized = True
36
-
37
- def forward(self, X: torch.Tensor) -> torch.Tensor:
38
- if not self._initialized:
39
- self._initialize_parameters(X)
40
- X_norm = X / self.maxInput
41
- Y = torch.roll(X_norm, shifts=-1, dims=1)
42
- term1 = torch.relu(X_norm)
43
- term2 = self.alpha * torch.clamp(X_norm, max=0)
44
- dist_sq_beta = (X_norm - 2) ** 2 + (Y - 2) ** 2
45
- dist_sq_gamma = (X_norm - 1) ** 2 + (Y - 1) ** 2
46
- dist_sq_delta = (X_norm - 1) ** 2 + (Y - 3) ** 2
47
- dist_sq_xi = (X_norm - 3) ** 2 + (Y - 1) ** 2
48
- dist_sq_psi = (X_norm - 3) ** 2 + (Y - 3) ** 2
49
- dist_sq_theta = (X_norm - 1) ** 2 + (Y - 2) ** 2
50
- dist_sq_lambda = (X_norm - 3) ** 2 + (Y - 2) ** 2
51
-
52
- term3 = self.beta * torch.sqrt(torch.relu(2 - dist_sq_beta))
53
- term4 = self.gamma * torch.sqrt(torch.relu(1 - dist_sq_gamma))
54
- term5 = self.delta * torch.sqrt(torch.relu(1 - dist_sq_delta))
55
- term6 = self.xi * torch.sqrt(torch.relu(1 - dist_sq_xi))
56
- term7 = self.psi * torch.sqrt(torch.relu(1 - dist_sq_psi))
57
- term8 = self.theta * torch.sqrt(torch.relu(1 - dist_sq_theta))
58
- term9 = self.lam * torch.sqrt(torch.relu(1 - dist_sq_lambda))
59
- Z_norm = term1 + term2 + term3 + term4 + term5 + term6 + term7 + term8 + term9
60
- Z = Z_norm * self.maxInput
61
- return Z
@@ -1,8 +0,0 @@
1
- from .modules import StochasticResnetModule
2
- from .Resnet import Resnet
3
-
4
-
5
- class StochasticResnet(Resnet):
6
- def __init__(self, **kwargs):
7
- super().__init__(**kwargs)
8
- self.model = StochasticResnetModule(kwargs["num_classes"])
@@ -1,9 +0,0 @@
1
- from .ResnetModule import ResnetModule
2
- from ..utils import replace_relu
3
- from ...activations import StochasticActivation
4
-
5
-
6
- class StochasticResnetModule(ResnetModule):
7
- def __init__(self, *args, **kwargs):
8
- super().__init__(*args, **kwargs)
9
- replace_relu(self, StochasticActivation)
@@ -1,2 +0,0 @@
1
- from .ResnetModule import ResnetModule
2
- from .StochasticResnetModule import StochasticResnetModule
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes