-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultilayerPerceptron.py
More file actions
264 lines (232 loc) · 7.99 KB
/
MultilayerPerceptron.py
File metadata and controls
264 lines (232 loc) · 7.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import pandas as pd
import matplotlib
from dataclasses import dataclass
from loguru import logger
from config.config import (
MODEL_PATH,
COLUMNS,
TARGET,
)
from src.preprocessing import (
stratified_train_test_split,
min_max_normalize_features,
one_hot_encode,
standardize_dataset,
)
from src.losses import binary_cross_entropy
from src.metrics import print_metrics
from NeuralNetwork import NeuralNetwork
matplotlib.use("TkAgg")
@dataclass
class MultilayerPerceptronInterface:
def mlp_split(
self,
dataset_path: str,
split: float,
) -> None:
"""
Split the dataset into training and testing sets.
Parameters:
- dataset_path (str): Path to the dataset.
- split (float): Percentage of the dataset to use for testing.
Returns:
- None
"""
pass
def mlp_train(
self,
dataset_path: str,
layer: list[int],
epochs: int,
batch_size: int,
learning_rate: float,
initializer: str,
activation: str,
optimizer: str,
momentum: float,
lambda_reg: float,
patience: int,
random_state: int,
plot_results: bool,
) -> NeuralNetwork:
"""
Train a multilayer perceptron model on the provided dataset.
Parameters:
- dataset_path (str): Path to the dataset.
- layer (list[int]): The number of neurons in each hidden layer.
- epochs (int): Number of epochs for training.
- batch_size (int): Batch size for training.
- learning_rate (float): Learning rate for optimizer.
- initializer (str): Type of weight initializer to use.
- activation (str): Type of activation function to use.
- optimizer (str): Type of optimizer to use.
- momentum (float): Momentum for Nesterov optimizer.
- lambda_reg (float): Regularization parameter for L2 regularization.
- patience (int): Number of epochs to wait before early stopping.
- random_state (int): Random seed for reproducibility.
- plot_results (bool): Flag to plot the results of the model.
"""
pass
def mlp_predict(self, dataset_path: str) -> None:
"""
Load the model and predict on the provided dataset.
Parameters:
- dataset_path (str): Path to the dataset.
Returns:
- None
"""
pass
@dataclass
class MultilayerPerceptron(MultilayerPerceptronInterface):
@staticmethod
def preprocess_data(dataset_path: str) -> pd.DataFrame:
"""
Load and preprocess the dataset.
Parameters:
- dataset_path (str): Path to the dataset.
Returns:
- pd.DataFrame: Preprocessed dataset.
"""
try:
df = pd.read_csv(dataset_path, header=None)
df = df.dropna()
if df.shape[0] == 0:
logger.error("Dataset is empty")
raise ValueError("Dataset is empty")
df.columns = COLUMNS
if df[TARGET].dtype == "object":
df[TARGET] = df[TARGET].map({"M": 1, "B": 0})
return df
except Exception as e:
logger.error(f"Error: preprocessing failed: {e}")
raise ValueError(f"Error: preprocessing failed: {e}")
def mlp_split(
self,
dataset_path: str,
split: float,
) -> None:
"""
Split the dataset into training and testing sets.
Parameters:
- dataset_path (str): Path to the dataset.
- split (float): Percentage of the dataset to use for testing.
Returns:
- None
"""
try:
dataset = self.preprocess_data(dataset_path)
stratified_train_test_split(
dataset,
TARGET,
test_size=split,
random_state=None,
save=True,
)
except Exception as e:
logger.error(f"Error: splitting failed: {e}")
raise ValueError(f"Error: splitting failed: {e}")
def mlp_train(
self,
dataset_path: str,
layer: list[int],
epochs: int,
batch_size: int,
learning_rate: float,
initializer: str,
activation: str,
optimizer: str,
momentum: float,
lambda_reg: float,
patience: int,
random_state: int,
plot_results: bool,
) -> NeuralNetwork:
"""
Train a multilayer perceptron model on the provided dataset.
Parameters:
- dataset_path (str): Path to the dataset.
- layer (list[int]): The number of neurons in each hidden layer.
- epochs (int): Number of epochs for training.
- batch_size (int): Batch size for training.
- learning_rate (float): Learning rate for optimizer.
- initializer (str): Type of weight initializer to use.
- activation (str): Type of activation function to use.
- optimizer (str): Type of optimizer to use.
- momentum (float): Momentum for Nesterov optimizer.
- lambda_reg (float): Regularization parameter for L2 regularization.
- patience (int): Number of epochs to wait before early stopping.
- random_state (int): Random seed for reproducibility.
- plot_results (bool): Flag to plot the results of the model.
"""
try:
df = self.preprocess_data(dataset_path)
(
X_train,
X_test,
y_train,
y_test,
) = stratified_train_test_split(
df,
TARGET,
test_size=0.3,
random_state=random_state,
save=False,
)
X_train = X_train.drop("id", axis=1)
X_test = X_test.drop("id", axis=1)
X_train, X_test, y_train, y_test = standardize_dataset(
X_train, X_test, y_train, y_test
)
X_train = X_train.T.to_numpy()
X_test = X_test.T.to_numpy()
y_train = y_train.T.to_numpy()
y_test = y_test.T.to_numpy()
model = NeuralNetwork(
epochs=epochs,
learning_rate=learning_rate,
hidden_layers=layer,
activation=activation,
initializer=initializer,
lambda_reg=lambda_reg,
optimizer=optimizer,
momentum=momentum,
)
model.fit(
x=X_train,
y=y_train,
validation_data=(X_test, y_test),
batch_size=batch_size,
patience=patience,
save_model=True,
batch_norm=True,
plot_results=plot_results,
)
return model
except Exception as e:
logger.error(f"Error: training failed: {e}")
raise ValueError(f"Error: training failed: {e}")
def mlp_predict(self, dataset_path: str) -> tuple:
"""
Load the model and predict on the provided dataset.
Parameters:
- dataset_path (str): Path to the dataset.
Returns:
- tuple: Accuracy, precision, recall, f1, loss
"""
try:
df = self.preprocess_data(dataset_path)
X = df.drop("id", axis=1)
X = X.drop(TARGET, axis=1)
X = min_max_normalize_features(X).T.to_numpy()
y = df[TARGET]
y = one_hot_encode(y).T.to_numpy()
model = NeuralNetwork()
model.load_model(MODEL_PATH)
y_pred = model.predict(X)
accuracy, precision, recall, f1 = print_metrics(y_pred, y[0])
loss = binary_cross_entropy(y_pred, y[0])
logger.info(f" Loss: {loss:.2f}")
return accuracy, precision, recall, f1, loss
except Exception as e:
logger.error(f"Error: prediction failed: {e}")
raise ValueError(f"Error: prediction failed: {e}")