MEDfl 0.2.1__py3-none-any.whl → 2.0.0__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/LearningManager/__init__.py +13 -13
- MEDfl/LearningManager/client.py +150 -181
- MEDfl/LearningManager/dynamicModal.py +287 -287
- MEDfl/LearningManager/federated_dataset.py +60 -60
- MEDfl/LearningManager/flpipeline.py +192 -192
- MEDfl/LearningManager/model.py +223 -223
- MEDfl/LearningManager/params.yaml +14 -14
- MEDfl/LearningManager/params_optimiser.py +442 -442
- MEDfl/LearningManager/plot.py +229 -229
- MEDfl/LearningManager/server.py +181 -189
- MEDfl/LearningManager/strategy.py +82 -138
- MEDfl/LearningManager/utils.py +331 -331
- MEDfl/NetManager/__init__.py +10 -10
- MEDfl/NetManager/database_connector.py +43 -43
- MEDfl/NetManager/dataset.py +92 -92
- MEDfl/NetManager/flsetup.py +320 -320
- MEDfl/NetManager/net_helper.py +254 -254
- MEDfl/NetManager/net_manager_queries.py +142 -142
- MEDfl/NetManager/network.py +194 -194
- MEDfl/NetManager/node.py +184 -184
- MEDfl/__init__.py +2 -2
- MEDfl/scripts/__init__.py +1 -1
- MEDfl/scripts/base.py +29 -29
- MEDfl/scripts/create_db.py +126 -126
- Medfl/LearningManager/__init__.py +13 -0
- Medfl/LearningManager/client.py +150 -0
- Medfl/LearningManager/dynamicModal.py +287 -0
- Medfl/LearningManager/federated_dataset.py +60 -0
- Medfl/LearningManager/flpipeline.py +192 -0
- Medfl/LearningManager/model.py +223 -0
- Medfl/LearningManager/params.yaml +14 -0
- Medfl/LearningManager/params_optimiser.py +442 -0
- Medfl/LearningManager/plot.py +229 -0
- Medfl/LearningManager/server.py +181 -0
- Medfl/LearningManager/strategy.py +82 -0
- Medfl/LearningManager/utils.py +331 -0
- Medfl/NetManager/__init__.py +10 -0
- Medfl/NetManager/database_connector.py +43 -0
- Medfl/NetManager/dataset.py +92 -0
- Medfl/NetManager/flsetup.py +320 -0
- Medfl/NetManager/net_helper.py +254 -0
- Medfl/NetManager/net_manager_queries.py +142 -0
- Medfl/NetManager/network.py +194 -0
- Medfl/NetManager/node.py +184 -0
- Medfl/__init__.py +3 -0
- Medfl/scripts/__init__.py +2 -0
- Medfl/scripts/base.py +30 -0
- Medfl/scripts/create_db.py +126 -0
- alembic/env.py +61 -61
- {MEDfl-0.2.1.dist-info → medfl-2.0.0.dist-info}/METADATA +120 -108
- medfl-2.0.0.dist-info/RECORD +55 -0
- {MEDfl-0.2.1.dist-info → medfl-2.0.0.dist-info}/WHEEL +1 -1
- {MEDfl-0.2.1.dist-info → medfl-2.0.0.dist-info/licenses}/LICENSE +674 -674
- MEDfl-0.2.1.dist-info/RECORD +0 -31
- {MEDfl-0.2.1.dist-info → medfl-2.0.0.dist-info}/top_level.txt +0 -0
@@ -1,287 +1,287 @@
|
|
1
|
-
import torch
|
2
|
-
import torch.nn as nn
|
3
|
-
from sklearn.svm import SVC
|
4
|
-
|
5
|
-
class DynamicModel:
|
6
|
-
"""DynamicModel class for creating various types of neural network models."""
|
7
|
-
|
8
|
-
# Create a binary classifier model
|
9
|
-
@staticmethod
|
10
|
-
def create_binary_classifier(input_dim, hidden_dims, output_dim, activation='relu', dropout_rate=0.0,
|
11
|
-
batch_norm=False, use_gpu=False):
|
12
|
-
"""
|
13
|
-
Creates a binary classifier neural network model with customizable architecture.
|
14
|
-
|
15
|
-
Args:
|
16
|
-
input_dim (int): Dimension of the input data.
|
17
|
-
hidden_dims (List[int]): List of dimensions for hidden layers.
|
18
|
-
output_dim (int): Dimension of the output (number of classes).
|
19
|
-
activation (str, optional): Activation function for hidden layers. Default is 'relu'.
|
20
|
-
dropout_rate (float, optional): Dropout rate for regularization. Default is 0.0 (no dropout).
|
21
|
-
batch_norm (bool, optional): Whether to apply batch normalization. Default is False.
|
22
|
-
use_gpu (bool, optional): Whether to use GPU acceleration. Default is False.
|
23
|
-
|
24
|
-
Returns:
|
25
|
-
torch.nn.Module: Created PyTorch model.
|
26
|
-
"""
|
27
|
-
|
28
|
-
layers = []
|
29
|
-
|
30
|
-
for i in range(len(hidden_dims)):
|
31
|
-
if i == 0:
|
32
|
-
layers.append(nn.Linear(input_dim, hidden_dims[0]))
|
33
|
-
else:
|
34
|
-
layers.append(nn.Linear(hidden_dims[i - 1], hidden_dims[i]))
|
35
|
-
|
36
|
-
if batch_norm:
|
37
|
-
layers.append(nn.BatchNorm1d(hidden_dims[i]))
|
38
|
-
|
39
|
-
activation_layer = nn.ReLU() if activation == 'relu' else nn.Sigmoid()
|
40
|
-
layers.append(activation_layer)
|
41
|
-
|
42
|
-
if dropout_rate > 0.0:
|
43
|
-
layers.append(nn.Dropout(dropout_rate))
|
44
|
-
|
45
|
-
layers.append(nn.Linear(hidden_dims[-1], output_dim))
|
46
|
-
layers.append(nn.Sigmoid())
|
47
|
-
|
48
|
-
model = nn.Sequential(*layers)
|
49
|
-
|
50
|
-
if use_gpu:
|
51
|
-
model = model.cuda()
|
52
|
-
|
53
|
-
return model
|
54
|
-
|
55
|
-
# Create a multi-class classifier model
|
56
|
-
@staticmethod
|
57
|
-
def create_multiclass_classifier(input_dim, hidden_dims, output_dim, activation='relu', dropout_rate=0.0,
|
58
|
-
batch_norm=False, use_gpu=False):
|
59
|
-
"""
|
60
|
-
Creates a multiclass classifier neural network model with customizable architecture.
|
61
|
-
|
62
|
-
Args:
|
63
|
-
input_dim (int): Dimension of the input data.
|
64
|
-
hidden_dims (List[int]): List of dimensions for hidden layers.
|
65
|
-
output_dim (int): Dimension of the output (number of classes).
|
66
|
-
activation (str, optional): Activation function for hidden layers. Default is 'relu'.
|
67
|
-
dropout_rate (float, optional): Dropout rate for regularization. Default is 0.0 (no dropout).
|
68
|
-
batch_norm (bool, optional): Whether to apply batch normalization. Default is False.
|
69
|
-
use_gpu (bool, optional): Whether to use GPU acceleration. Default is False.
|
70
|
-
|
71
|
-
Returns:
|
72
|
-
torch.nn.Module: Created PyTorch model.
|
73
|
-
"""
|
74
|
-
layers = []
|
75
|
-
|
76
|
-
for i in range(len(hidden_dims)):
|
77
|
-
if i == 0:
|
78
|
-
layers.append(nn.Linear(input_dim, hidden_dims[0]))
|
79
|
-
else:
|
80
|
-
layers.append(nn.Linear(hidden_dims[i - 1], hidden_dims[i]))
|
81
|
-
|
82
|
-
if batch_norm:
|
83
|
-
layers.append(nn.BatchNorm1d(hidden_dims[i]))
|
84
|
-
|
85
|
-
activation_layer = nn.ReLU() if activation == 'relu' else nn.Sigmoid()
|
86
|
-
layers.append(activation_layer)
|
87
|
-
|
88
|
-
if dropout_rate > 0.0:
|
89
|
-
layers.append(nn.Dropout(dropout_rate))
|
90
|
-
|
91
|
-
layers.append(nn.Linear(hidden_dims[-1], output_dim))
|
92
|
-
layers.append(nn.LogSoftmax(dim=1))
|
93
|
-
|
94
|
-
model = nn.Sequential(*layers)
|
95
|
-
|
96
|
-
if use_gpu:
|
97
|
-
model = model.cuda()
|
98
|
-
|
99
|
-
return model
|
100
|
-
|
101
|
-
# Create a linear regressor model
|
102
|
-
@staticmethod
|
103
|
-
def create_linear_regressor(input_dim, output_dim, use_gpu=False):
|
104
|
-
"""
|
105
|
-
Creates a linear regressor neural network model.
|
106
|
-
|
107
|
-
Args:
|
108
|
-
input_dim (int): Dimension of the input data.
|
109
|
-
output_dim (int): Dimension of the output.
|
110
|
-
|
111
|
-
Returns:
|
112
|
-
torch.nn.Module: Created PyTorch model.
|
113
|
-
"""
|
114
|
-
class LinearRegressionModel(nn.Module):
|
115
|
-
def __init__(self):
|
116
|
-
super(LinearRegressionModel, self).__init__()
|
117
|
-
self.linear = nn.Linear(input_dim, output_dim)
|
118
|
-
|
119
|
-
def forward(self, x):
|
120
|
-
return self.linear(x)
|
121
|
-
|
122
|
-
model = LinearRegressionModel()
|
123
|
-
|
124
|
-
if use_gpu:
|
125
|
-
model = model.cuda()
|
126
|
-
|
127
|
-
return model
|
128
|
-
|
129
|
-
# Create a logistic regressor model
|
130
|
-
@staticmethod
|
131
|
-
def create_logistic_regressor(input_dim, use_gpu=False):
|
132
|
-
"""
|
133
|
-
Creates a logistic regressor neural network model.
|
134
|
-
|
135
|
-
Args:
|
136
|
-
input_dim (int): Dimension of the input data.
|
137
|
-
|
138
|
-
Returns:
|
139
|
-
torch.nn.Module: Created PyTorch model.
|
140
|
-
"""
|
141
|
-
class LogisticRegressionModel(nn.Module):
|
142
|
-
def __init__(self):
|
143
|
-
super(LogisticRegressionModel, self).__init__()
|
144
|
-
self.linear = nn.Linear(input_dim, 1)
|
145
|
-
|
146
|
-
def forward(self, x):
|
147
|
-
return torch.sigmoid(self.linear(x))
|
148
|
-
|
149
|
-
model = LogisticRegressionModel()
|
150
|
-
|
151
|
-
if use_gpu:
|
152
|
-
model = model.cuda()
|
153
|
-
|
154
|
-
return model
|
155
|
-
|
156
|
-
@staticmethod
|
157
|
-
def create_convolutional_neural_network(input_channels, output_channels, kernel_size, use_gpu=False):
|
158
|
-
"""
|
159
|
-
Creates a convolutional neural network (CNN) model.
|
160
|
-
|
161
|
-
Args:
|
162
|
-
input_channels (int): Number of input channels.
|
163
|
-
output_channels (int): Number of output channels.
|
164
|
-
kernel_size (int): Size of the convolutional kernel.
|
165
|
-
|
166
|
-
Returns:
|
167
|
-
torch.nn.Module: Created PyTorch model.
|
168
|
-
"""
|
169
|
-
|
170
|
-
model = nn.Sequential(
|
171
|
-
nn.Conv2d(input_channels, output_channels, kernel_size),
|
172
|
-
nn.ReLU(),
|
173
|
-
nn.MaxPool2d(2)
|
174
|
-
)
|
175
|
-
|
176
|
-
if use_gpu:
|
177
|
-
model = model.cuda()
|
178
|
-
|
179
|
-
return model
|
180
|
-
|
181
|
-
@staticmethod
|
182
|
-
def create_recurrent_neural_network(input_size, hidden_size, use_gpu=False):
|
183
|
-
"""
|
184
|
-
Creates a recurrent neural network (RNN) model.
|
185
|
-
|
186
|
-
Args:
|
187
|
-
input_size (int): Size of the input.
|
188
|
-
hidden_size (int): Size of the hidden layer.
|
189
|
-
|
190
|
-
Returns:
|
191
|
-
torch.nn.Module: Created PyTorch model.
|
192
|
-
"""
|
193
|
-
|
194
|
-
model = nn.RNN(input_size, hidden_size, batch_first=True)
|
195
|
-
|
196
|
-
if use_gpu:
|
197
|
-
model = model.cuda()
|
198
|
-
|
199
|
-
return model
|
200
|
-
|
201
|
-
@staticmethod
|
202
|
-
def create_lstm_network(input_size, hidden_size, use_gpu=False):
|
203
|
-
"""
|
204
|
-
Creates a Long Short-Term Memory (LSTM) network model.
|
205
|
-
|
206
|
-
Args:
|
207
|
-
input_size (int): Size of the input layer.
|
208
|
-
hidden_size (int): Size of the hidden layer.
|
209
|
-
|
210
|
-
Returns:
|
211
|
-
torch.nn.Module: Created PyTorch model.
|
212
|
-
"""
|
213
|
-
|
214
|
-
model = nn.LSTM(input_size, hidden_size, batch_first=True)
|
215
|
-
|
216
|
-
if use_gpu:
|
217
|
-
model = model.cuda()
|
218
|
-
|
219
|
-
return model
|
220
|
-
|
221
|
-
# Create the dynamic model
|
222
|
-
def create_model(self, model_type: str, params_dict={}) -> torch.nn.Module:
|
223
|
-
"""
|
224
|
-
Create a specific type of model dynamically based on the given parameters.
|
225
|
-
|
226
|
-
Args:
|
227
|
-
model_type (str): Type of the model to create ('Binary Classifier', 'Multiclass Classifier', 'Linear Regressor', 'Logistic Regressor', 'SVM', 'Neural Network Classifier', 'Convolutional Neural Network', 'Recurrent Neural Network', 'LSTM Network', 'Autoencoder').
|
228
|
-
params_dict (dict): Dictionary containing parameters for model creation.
|
229
|
-
|
230
|
-
Returns:
|
231
|
-
torch.nn.Module: Created PyTorch model.
|
232
|
-
"""
|
233
|
-
if model_type == 'Binary Classifier':
|
234
|
-
return self.create_binary_classifier(
|
235
|
-
params_dict['input_dim'], params_dict['hidden_dims'],
|
236
|
-
params_dict['output_dim'], params_dict.get('activation', 'relu'),
|
237
|
-
params_dict.get('dropout_rate', 0.0), params_dict.get('batch_norm', False),
|
238
|
-
params_dict.get('use_gpu', False)
|
239
|
-
)
|
240
|
-
elif model_type == 'Multiclass Classifier':
|
241
|
-
return self.create_multiclass_classifier(
|
242
|
-
params_dict['input_dim'], params_dict['hidden_dims'],
|
243
|
-
params_dict['output_dim'], params_dict.get('activation', 'relu'),
|
244
|
-
params_dict.get('dropout_rate', 0.0), params_dict.get('batch_norm', False),
|
245
|
-
params_dict.get('use_gpu', False)
|
246
|
-
)
|
247
|
-
elif model_type == 'Linear Regressor':
|
248
|
-
return self.create_linear_regressor(
|
249
|
-
params_dict['input_dim'], params_dict['output_dim'],
|
250
|
-
params_dict.get('use_gpu', False)
|
251
|
-
)
|
252
|
-
elif model_type == 'Logistic Regressor':
|
253
|
-
return self.create_logistic_regressor(
|
254
|
-
params_dict['input_dim'], params_dict.get('use_gpu', False)
|
255
|
-
)
|
256
|
-
elif model_type == 'Neural Network Classifier':
|
257
|
-
return self.create_neural_network_classifier(
|
258
|
-
params_dict['input_dim'], params_dict['output_dim'],
|
259
|
-
params_dict['hidden_dims'], params_dict.get('activation', 'relu'),
|
260
|
-
params_dict.get('dropout_rate', 0.0), params_dict.get('batch_norm', False),
|
261
|
-
params_dict.get('num_layers', 2), params_dict.get('use_gpu', False)
|
262
|
-
)
|
263
|
-
elif model_type == 'Convolutional Neural Network':
|
264
|
-
return self.create_convolutional_neural_network(
|
265
|
-
params_dict['input_channels'], params_dict['output_channels'],
|
266
|
-
params_dict['kernel_size'], params_dict.get('use_gpu', False)
|
267
|
-
)
|
268
|
-
elif model_type == 'Recurrent Neural Network':
|
269
|
-
return self.create_recurrent_neural_network(
|
270
|
-
params_dict['input_size'], params_dict['hidden_size'],
|
271
|
-
params_dict.get('use_gpu', False)
|
272
|
-
)
|
273
|
-
elif model_type == 'LSTM Network':
|
274
|
-
return self.create_lstm_network(
|
275
|
-
params_dict['input_size'], params_dict['hidden_size'],
|
276
|
-
params_dict.get('use_gpu', False)
|
277
|
-
)
|
278
|
-
elif model_type == 'Autoencoder':
|
279
|
-
return self.create_autoencoder(
|
280
|
-
params_dict['input_size'], params_dict['encoder_hidden_size'],
|
281
|
-
params_dict.get('use_gpu', False)
|
282
|
-
)
|
283
|
-
else:
|
284
|
-
raise ValueError("Invalid model type provided")
|
285
|
-
|
286
|
-
|
287
|
-
|
1
|
+
import torch
|
2
|
+
import torch.nn as nn
|
3
|
+
from sklearn.svm import SVC
|
4
|
+
|
5
|
+
class DynamicModel:
|
6
|
+
"""DynamicModel class for creating various types of neural network models."""
|
7
|
+
|
8
|
+
# Create a binary classifier model
|
9
|
+
@staticmethod
|
10
|
+
def create_binary_classifier(input_dim, hidden_dims, output_dim, activation='relu', dropout_rate=0.0,
|
11
|
+
batch_norm=False, use_gpu=False):
|
12
|
+
"""
|
13
|
+
Creates a binary classifier neural network model with customizable architecture.
|
14
|
+
|
15
|
+
Args:
|
16
|
+
input_dim (int): Dimension of the input data.
|
17
|
+
hidden_dims (List[int]): List of dimensions for hidden layers.
|
18
|
+
output_dim (int): Dimension of the output (number of classes).
|
19
|
+
activation (str, optional): Activation function for hidden layers. Default is 'relu'.
|
20
|
+
dropout_rate (float, optional): Dropout rate for regularization. Default is 0.0 (no dropout).
|
21
|
+
batch_norm (bool, optional): Whether to apply batch normalization. Default is False.
|
22
|
+
use_gpu (bool, optional): Whether to use GPU acceleration. Default is False.
|
23
|
+
|
24
|
+
Returns:
|
25
|
+
torch.nn.Module: Created PyTorch model.
|
26
|
+
"""
|
27
|
+
|
28
|
+
layers = []
|
29
|
+
|
30
|
+
for i in range(len(hidden_dims)):
|
31
|
+
if i == 0:
|
32
|
+
layers.append(nn.Linear(input_dim, hidden_dims[0]))
|
33
|
+
else:
|
34
|
+
layers.append(nn.Linear(hidden_dims[i - 1], hidden_dims[i]))
|
35
|
+
|
36
|
+
if batch_norm:
|
37
|
+
layers.append(nn.BatchNorm1d(hidden_dims[i]))
|
38
|
+
|
39
|
+
activation_layer = nn.ReLU() if activation == 'relu' else nn.Sigmoid()
|
40
|
+
layers.append(activation_layer)
|
41
|
+
|
42
|
+
if dropout_rate > 0.0:
|
43
|
+
layers.append(nn.Dropout(dropout_rate))
|
44
|
+
|
45
|
+
layers.append(nn.Linear(hidden_dims[-1], output_dim))
|
46
|
+
layers.append(nn.Sigmoid())
|
47
|
+
|
48
|
+
model = nn.Sequential(*layers)
|
49
|
+
|
50
|
+
if use_gpu:
|
51
|
+
model = model.cuda()
|
52
|
+
|
53
|
+
return model
|
54
|
+
|
55
|
+
# Create a multi-class classifier model
|
56
|
+
@staticmethod
|
57
|
+
def create_multiclass_classifier(input_dim, hidden_dims, output_dim, activation='relu', dropout_rate=0.0,
|
58
|
+
batch_norm=False, use_gpu=False):
|
59
|
+
"""
|
60
|
+
Creates a multiclass classifier neural network model with customizable architecture.
|
61
|
+
|
62
|
+
Args:
|
63
|
+
input_dim (int): Dimension of the input data.
|
64
|
+
hidden_dims (List[int]): List of dimensions for hidden layers.
|
65
|
+
output_dim (int): Dimension of the output (number of classes).
|
66
|
+
activation (str, optional): Activation function for hidden layers. Default is 'relu'.
|
67
|
+
dropout_rate (float, optional): Dropout rate for regularization. Default is 0.0 (no dropout).
|
68
|
+
batch_norm (bool, optional): Whether to apply batch normalization. Default is False.
|
69
|
+
use_gpu (bool, optional): Whether to use GPU acceleration. Default is False.
|
70
|
+
|
71
|
+
Returns:
|
72
|
+
torch.nn.Module: Created PyTorch model.
|
73
|
+
"""
|
74
|
+
layers = []
|
75
|
+
|
76
|
+
for i in range(len(hidden_dims)):
|
77
|
+
if i == 0:
|
78
|
+
layers.append(nn.Linear(input_dim, hidden_dims[0]))
|
79
|
+
else:
|
80
|
+
layers.append(nn.Linear(hidden_dims[i - 1], hidden_dims[i]))
|
81
|
+
|
82
|
+
if batch_norm:
|
83
|
+
layers.append(nn.BatchNorm1d(hidden_dims[i]))
|
84
|
+
|
85
|
+
activation_layer = nn.ReLU() if activation == 'relu' else nn.Sigmoid()
|
86
|
+
layers.append(activation_layer)
|
87
|
+
|
88
|
+
if dropout_rate > 0.0:
|
89
|
+
layers.append(nn.Dropout(dropout_rate))
|
90
|
+
|
91
|
+
layers.append(nn.Linear(hidden_dims[-1], output_dim))
|
92
|
+
layers.append(nn.LogSoftmax(dim=1))
|
93
|
+
|
94
|
+
model = nn.Sequential(*layers)
|
95
|
+
|
96
|
+
if use_gpu:
|
97
|
+
model = model.cuda()
|
98
|
+
|
99
|
+
return model
|
100
|
+
|
101
|
+
# Create a linear regressor model
|
102
|
+
@staticmethod
|
103
|
+
def create_linear_regressor(input_dim, output_dim, use_gpu=False):
|
104
|
+
"""
|
105
|
+
Creates a linear regressor neural network model.
|
106
|
+
|
107
|
+
Args:
|
108
|
+
input_dim (int): Dimension of the input data.
|
109
|
+
output_dim (int): Dimension of the output.
|
110
|
+
|
111
|
+
Returns:
|
112
|
+
torch.nn.Module: Created PyTorch model.
|
113
|
+
"""
|
114
|
+
class LinearRegressionModel(nn.Module):
|
115
|
+
def __init__(self):
|
116
|
+
super(LinearRegressionModel, self).__init__()
|
117
|
+
self.linear = nn.Linear(input_dim, output_dim)
|
118
|
+
|
119
|
+
def forward(self, x):
|
120
|
+
return self.linear(x)
|
121
|
+
|
122
|
+
model = LinearRegressionModel()
|
123
|
+
|
124
|
+
if use_gpu:
|
125
|
+
model = model.cuda()
|
126
|
+
|
127
|
+
return model
|
128
|
+
|
129
|
+
# Create a logistic regressor model
|
130
|
+
@staticmethod
|
131
|
+
def create_logistic_regressor(input_dim, use_gpu=False):
|
132
|
+
"""
|
133
|
+
Creates a logistic regressor neural network model.
|
134
|
+
|
135
|
+
Args:
|
136
|
+
input_dim (int): Dimension of the input data.
|
137
|
+
|
138
|
+
Returns:
|
139
|
+
torch.nn.Module: Created PyTorch model.
|
140
|
+
"""
|
141
|
+
class LogisticRegressionModel(nn.Module):
|
142
|
+
def __init__(self):
|
143
|
+
super(LogisticRegressionModel, self).__init__()
|
144
|
+
self.linear = nn.Linear(input_dim, 1)
|
145
|
+
|
146
|
+
def forward(self, x):
|
147
|
+
return torch.sigmoid(self.linear(x))
|
148
|
+
|
149
|
+
model = LogisticRegressionModel()
|
150
|
+
|
151
|
+
if use_gpu:
|
152
|
+
model = model.cuda()
|
153
|
+
|
154
|
+
return model
|
155
|
+
|
156
|
+
@staticmethod
|
157
|
+
def create_convolutional_neural_network(input_channels, output_channels, kernel_size, use_gpu=False):
|
158
|
+
"""
|
159
|
+
Creates a convolutional neural network (CNN) model.
|
160
|
+
|
161
|
+
Args:
|
162
|
+
input_channels (int): Number of input channels.
|
163
|
+
output_channels (int): Number of output channels.
|
164
|
+
kernel_size (int): Size of the convolutional kernel.
|
165
|
+
|
166
|
+
Returns:
|
167
|
+
torch.nn.Module: Created PyTorch model.
|
168
|
+
"""
|
169
|
+
|
170
|
+
model = nn.Sequential(
|
171
|
+
nn.Conv2d(input_channels, output_channels, kernel_size),
|
172
|
+
nn.ReLU(),
|
173
|
+
nn.MaxPool2d(2)
|
174
|
+
)
|
175
|
+
|
176
|
+
if use_gpu:
|
177
|
+
model = model.cuda()
|
178
|
+
|
179
|
+
return model
|
180
|
+
|
181
|
+
@staticmethod
|
182
|
+
def create_recurrent_neural_network(input_size, hidden_size, use_gpu=False):
|
183
|
+
"""
|
184
|
+
Creates a recurrent neural network (RNN) model.
|
185
|
+
|
186
|
+
Args:
|
187
|
+
input_size (int): Size of the input.
|
188
|
+
hidden_size (int): Size of the hidden layer.
|
189
|
+
|
190
|
+
Returns:
|
191
|
+
torch.nn.Module: Created PyTorch model.
|
192
|
+
"""
|
193
|
+
|
194
|
+
model = nn.RNN(input_size, hidden_size, batch_first=True)
|
195
|
+
|
196
|
+
if use_gpu:
|
197
|
+
model = model.cuda()
|
198
|
+
|
199
|
+
return model
|
200
|
+
|
201
|
+
@staticmethod
|
202
|
+
def create_lstm_network(input_size, hidden_size, use_gpu=False):
|
203
|
+
"""
|
204
|
+
Creates a Long Short-Term Memory (LSTM) network model.
|
205
|
+
|
206
|
+
Args:
|
207
|
+
input_size (int): Size of the input layer.
|
208
|
+
hidden_size (int): Size of the hidden layer.
|
209
|
+
|
210
|
+
Returns:
|
211
|
+
torch.nn.Module: Created PyTorch model.
|
212
|
+
"""
|
213
|
+
|
214
|
+
model = nn.LSTM(input_size, hidden_size, batch_first=True)
|
215
|
+
|
216
|
+
if use_gpu:
|
217
|
+
model = model.cuda()
|
218
|
+
|
219
|
+
return model
|
220
|
+
|
221
|
+
# Create the dynamic model
|
222
|
+
def create_model(self, model_type: str, params_dict={}) -> torch.nn.Module:
|
223
|
+
"""
|
224
|
+
Create a specific type of model dynamically based on the given parameters.
|
225
|
+
|
226
|
+
Args:
|
227
|
+
model_type (str): Type of the model to create ('Binary Classifier', 'Multiclass Classifier', 'Linear Regressor', 'Logistic Regressor', 'SVM', 'Neural Network Classifier', 'Convolutional Neural Network', 'Recurrent Neural Network', 'LSTM Network', 'Autoencoder').
|
228
|
+
params_dict (dict): Dictionary containing parameters for model creation.
|
229
|
+
|
230
|
+
Returns:
|
231
|
+
torch.nn.Module: Created PyTorch model.
|
232
|
+
"""
|
233
|
+
if model_type == 'Binary Classifier':
|
234
|
+
return self.create_binary_classifier(
|
235
|
+
params_dict['input_dim'], params_dict['hidden_dims'],
|
236
|
+
params_dict['output_dim'], params_dict.get('activation', 'relu'),
|
237
|
+
params_dict.get('dropout_rate', 0.0), params_dict.get('batch_norm', False),
|
238
|
+
params_dict.get('use_gpu', False)
|
239
|
+
)
|
240
|
+
elif model_type == 'Multiclass Classifier':
|
241
|
+
return self.create_multiclass_classifier(
|
242
|
+
params_dict['input_dim'], params_dict['hidden_dims'],
|
243
|
+
params_dict['output_dim'], params_dict.get('activation', 'relu'),
|
244
|
+
params_dict.get('dropout_rate', 0.0), params_dict.get('batch_norm', False),
|
245
|
+
params_dict.get('use_gpu', False)
|
246
|
+
)
|
247
|
+
elif model_type == 'Linear Regressor':
|
248
|
+
return self.create_linear_regressor(
|
249
|
+
params_dict['input_dim'], params_dict['output_dim'],
|
250
|
+
params_dict.get('use_gpu', False)
|
251
|
+
)
|
252
|
+
elif model_type == 'Logistic Regressor':
|
253
|
+
return self.create_logistic_regressor(
|
254
|
+
params_dict['input_dim'], params_dict.get('use_gpu', False)
|
255
|
+
)
|
256
|
+
elif model_type == 'Neural Network Classifier':
|
257
|
+
return self.create_neural_network_classifier(
|
258
|
+
params_dict['input_dim'], params_dict['output_dim'],
|
259
|
+
params_dict['hidden_dims'], params_dict.get('activation', 'relu'),
|
260
|
+
params_dict.get('dropout_rate', 0.0), params_dict.get('batch_norm', False),
|
261
|
+
params_dict.get('num_layers', 2), params_dict.get('use_gpu', False)
|
262
|
+
)
|
263
|
+
elif model_type == 'Convolutional Neural Network':
|
264
|
+
return self.create_convolutional_neural_network(
|
265
|
+
params_dict['input_channels'], params_dict['output_channels'],
|
266
|
+
params_dict['kernel_size'], params_dict.get('use_gpu', False)
|
267
|
+
)
|
268
|
+
elif model_type == 'Recurrent Neural Network':
|
269
|
+
return self.create_recurrent_neural_network(
|
270
|
+
params_dict['input_size'], params_dict['hidden_size'],
|
271
|
+
params_dict.get('use_gpu', False)
|
272
|
+
)
|
273
|
+
elif model_type == 'LSTM Network':
|
274
|
+
return self.create_lstm_network(
|
275
|
+
params_dict['input_size'], params_dict['hidden_size'],
|
276
|
+
params_dict.get('use_gpu', False)
|
277
|
+
)
|
278
|
+
elif model_type == 'Autoencoder':
|
279
|
+
return self.create_autoencoder(
|
280
|
+
params_dict['input_size'], params_dict['encoder_hidden_size'],
|
281
|
+
params_dict.get('use_gpu', False)
|
282
|
+
)
|
283
|
+
else:
|
284
|
+
raise ValueError("Invalid model type provided")
|
285
|
+
|
286
|
+
|
287
|
+
|