MEDfl 2.0.5.dev2__py3-none-any.whl → 2.0.5.dev3__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.
- MEDfl/rw/model.py +25 -25
- {medfl-2.0.5.dev2.dist-info → medfl-2.0.5.dev3.dist-info}/METADATA +1 -1
- {medfl-2.0.5.dev2.dist-info → medfl-2.0.5.dev3.dist-info}/RECORD +6 -6
- {medfl-2.0.5.dev2.dist-info → medfl-2.0.5.dev3.dist-info}/WHEEL +0 -0
- {medfl-2.0.5.dev2.dist-info → medfl-2.0.5.dev3.dist-info}/licenses/LICENSE +0 -0
- {medfl-2.0.5.dev2.dist-info → medfl-2.0.5.dev3.dist-info}/top_level.txt +0 -0
MEDfl/rw/model.py
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import math
|
|
2
|
-
from typing import List, Optional, Literal, Callable, Tuple
|
|
3
2
|
import torch
|
|
4
3
|
import torch.nn as nn
|
|
5
4
|
import torch.nn.functional as F
|
|
6
5
|
|
|
7
|
-
Task = Literal["binary", "multiclass", "multilabel", "regression"]
|
|
8
|
-
|
|
9
6
|
_ACTS = {
|
|
10
7
|
"relu": nn.ReLU,
|
|
11
8
|
"gelu": nn.GELU,
|
|
@@ -16,7 +13,7 @@ _ACTS = {
|
|
|
16
13
|
"identity": nn.Identity,
|
|
17
14
|
}
|
|
18
15
|
|
|
19
|
-
def _make_activation(name
|
|
16
|
+
def _make_activation(name):
|
|
20
17
|
if name not in _ACTS:
|
|
21
18
|
raise ValueError(f"Unsupported activation '{name}'. Choose from {list(_ACTS)}")
|
|
22
19
|
return _ACTS[name]()
|
|
@@ -35,16 +32,16 @@ class Net(nn.Module):
|
|
|
35
32
|
|
|
36
33
|
def __init__(
|
|
37
34
|
self,
|
|
38
|
-
input_dim
|
|
39
|
-
hidden_dims
|
|
40
|
-
activation
|
|
41
|
-
batchnorm
|
|
42
|
-
dropout
|
|
43
|
-
task
|
|
44
|
-
num_classes
|
|
45
|
-
output_bias
|
|
46
|
-
return_logits
|
|
47
|
-
weight_init
|
|
35
|
+
input_dim,
|
|
36
|
+
hidden_dims=[64, 32],
|
|
37
|
+
activation="relu",
|
|
38
|
+
batchnorm=True,
|
|
39
|
+
dropout=0.0,
|
|
40
|
+
task="binary",
|
|
41
|
+
num_classes=None,
|
|
42
|
+
output_bias=True,
|
|
43
|
+
return_logits=True,
|
|
44
|
+
weight_init="kaiming",
|
|
48
45
|
):
|
|
49
46
|
super().__init__()
|
|
50
47
|
self.task = task
|
|
@@ -78,10 +75,9 @@ class Net(nn.Module):
|
|
|
78
75
|
else:
|
|
79
76
|
raise ValueError("dropout must be float|list[float]|None")
|
|
80
77
|
|
|
81
|
-
layers = []
|
|
82
|
-
in_dim = input_dim
|
|
83
78
|
self.blocks = nn.ModuleList()
|
|
84
|
-
|
|
79
|
+
in_dim = input_dim
|
|
80
|
+
for h, p in zip(hidden_dims, dropout):
|
|
85
81
|
block = nn.ModuleDict({
|
|
86
82
|
"lin": nn.Linear(in_dim, h, bias=True),
|
|
87
83
|
"bn": nn.BatchNorm1d(h) if batchnorm else nn.Identity(),
|
|
@@ -98,11 +94,15 @@ class Net(nn.Module):
|
|
|
98
94
|
self.apply(lambda m: self._init_weights(m, scheme=weight_init, activation=activation))
|
|
99
95
|
|
|
100
96
|
@staticmethod
|
|
101
|
-
def _init_weights(m
|
|
97
|
+
def _init_weights(m, scheme, activation):
|
|
102
98
|
if isinstance(m, nn.Linear):
|
|
103
99
|
if scheme == "kaiming":
|
|
104
100
|
nonlinearity = "leaky_relu" if activation == "leaky_relu" else "relu"
|
|
105
|
-
nn.init.kaiming_uniform_(
|
|
101
|
+
nn.init.kaiming_uniform_(
|
|
102
|
+
m.weight,
|
|
103
|
+
a=math.sqrt(5) if activation == "leaky_relu" else 0,
|
|
104
|
+
nonlinearity=nonlinearity,
|
|
105
|
+
)
|
|
106
106
|
elif scheme == "xavier":
|
|
107
107
|
nn.init.xavier_uniform_(m.weight)
|
|
108
108
|
if m.bias is not None:
|
|
@@ -110,7 +110,7 @@ class Net(nn.Module):
|
|
|
110
110
|
bound = 1 / math.sqrt(fan_in) if fan_in > 0 else 0
|
|
111
111
|
nn.init.uniform_(m.bias, -bound, bound)
|
|
112
112
|
|
|
113
|
-
def forward(self, x
|
|
113
|
+
def forward(self, x):
|
|
114
114
|
for blk in self.blocks:
|
|
115
115
|
x = blk["lin"](x)
|
|
116
116
|
x = blk["bn"](x)
|
|
@@ -120,7 +120,7 @@ class Net(nn.Module):
|
|
|
120
120
|
return logits if self.return_logits else self._apply_output_activation(logits)
|
|
121
121
|
|
|
122
122
|
# Inference helpers
|
|
123
|
-
def _apply_output_activation(self, logits
|
|
123
|
+
def _apply_output_activation(self, logits):
|
|
124
124
|
if self.task == "binary":
|
|
125
125
|
return torch.sigmoid(logits) # (B, 1)
|
|
126
126
|
elif self.task == "multiclass":
|
|
@@ -128,14 +128,15 @@ class Net(nn.Module):
|
|
|
128
128
|
elif self.task == "multilabel":
|
|
129
129
|
return torch.sigmoid(logits) # (B, L)
|
|
130
130
|
elif self.task == "regression":
|
|
131
|
-
return logits
|
|
131
|
+
return logits # raw regression output
|
|
132
132
|
else:
|
|
133
133
|
raise RuntimeError("Invalid task")
|
|
134
134
|
|
|
135
135
|
@torch.no_grad()
|
|
136
|
-
def predict(self, x
|
|
137
|
-
out = self.forward(x)
|
|
136
|
+
def predict(self, x):
|
|
137
|
+
out = self.forward(x)
|
|
138
138
|
probs = self._apply_output_activation(out) if self.return_logits else out
|
|
139
|
+
|
|
139
140
|
if self.task == "binary":
|
|
140
141
|
return (probs >= 0.5).long()
|
|
141
142
|
elif self.task == "multiclass":
|
|
@@ -144,4 +145,3 @@ class Net(nn.Module):
|
|
|
144
145
|
return (probs >= 0.5).long()
|
|
145
146
|
elif self.task == "regression":
|
|
146
147
|
return probs
|
|
147
|
-
|
|
@@ -21,7 +21,7 @@ MEDfl/NetManager/network.py,sha256=5t705fzWc-BRg-QPAbAcDv5ckDGzsPwj_Q5V0iTgkx0,6
|
|
|
21
21
|
MEDfl/NetManager/node.py,sha256=t90QuYZ8M1X_AG1bwTta0CnlOuodqkmpVda2K7NOgHc,6542
|
|
22
22
|
MEDfl/rw/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
23
23
|
MEDfl/rw/client.py,sha256=Ufa-VCbP83adEs7tV1ltDYakBV2Ge5GCPhjIJ7HsuJ8,17804
|
|
24
|
-
MEDfl/rw/model.py,sha256
|
|
24
|
+
MEDfl/rw/model.py,sha256=IvC5a1ADyn7mKeSBq1Lk_UnkSJOCErPCmpEPSOFxHpk,5343
|
|
25
25
|
MEDfl/rw/server.py,sha256=TmcWUwMJ5BG7owqIVTtTC6w8bR02SESRT9lXh7BqlOg,4986
|
|
26
26
|
MEDfl/rw/strategy.py,sha256=rrRUvuFxL4rdtjN-q-GgB_QQ4ntH9kSyIbTQtiASjVI,13779
|
|
27
27
|
MEDfl/scripts/__init__.py,sha256=Pq1weevsPaU7MRMHfBYeyT0EOFeWLeVM6Y1DVz6jw1A,48
|
|
@@ -29,8 +29,8 @@ MEDfl/scripts/base.py,sha256=QrmG7gkiPYkAy-5tXxJgJmOSLGAKeIVH6i0jq7G9xnA,752
|
|
|
29
29
|
MEDfl/scripts/create_db.py,sha256=MnFtZkTueRZ-3qXPNX4JsXjOKj-4mlkxoRhSFdRcvJw,3817
|
|
30
30
|
alembic/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
31
31
|
alembic/env.py,sha256=-aSZ6SlJeK1ZeqHgM-54hOi9LhJRFP0SZGjut-JnY-4,1588
|
|
32
|
-
medfl-2.0.5.
|
|
33
|
-
medfl-2.0.5.
|
|
34
|
-
medfl-2.0.5.
|
|
35
|
-
medfl-2.0.5.
|
|
36
|
-
medfl-2.0.5.
|
|
32
|
+
medfl-2.0.5.dev3.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
|
33
|
+
medfl-2.0.5.dev3.dist-info/METADATA,sha256=jqEhW5WJalcs9sBxuN27oY5_e1yjwIRUuvY7pzrSpII,6914
|
|
34
|
+
medfl-2.0.5.dev3.dist-info/WHEEL,sha256=lTU6B6eIfYoiQJTZNc-fyaR6BpL6ehTzU3xGYxn2n8k,91
|
|
35
|
+
medfl-2.0.5.dev3.dist-info/top_level.txt,sha256=dIL9X8HOFuaVSzpg40DVveDPrymWRoShHtspH7kkjdI,14
|
|
36
|
+
medfl-2.0.5.dev3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|