-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultilayer-perceptron.py
More file actions
233 lines (190 loc) · 6.52 KB
/
multilayer-perceptron.py
File metadata and controls
233 lines (190 loc) · 6.52 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
import sys, os
import matplotlib.pyplot as plt
import numpy as np
from AI_learn.neural_network import MLPClassifier
from AI_learn.dataset import load_dataset
from AI_learn.preprocessing import StandartScaler, one_hot, MinMaxScaler
features_name=[
'Index',
'Diagnosis',
'Radius',
'Texture',
'Perimeter',
'Area',
'Smoothness',
'Compactness',
'Concavity',
'Concave_Points',
'Symmetry',
'Fractal_Dimension',
'RadiusSE',
'TextureSE',
'PerimeterSE',
'AreaSE',
'SmoothnessSE',
'CompactnessSE',
'ConcavitySE',
'Concave_PointSE',
'SymmetrySE',
'Fractal_DimensionSE',
'RadiusW',
'TextureW',
'PerimeterW',
'AreaW',
'SmoothnessW',
'CompactnessW',
'ConcavityW',
'Concave_PointsW',
'SymmetryW',
'Fractal_DimensionW'
]
def verif_arg() -> bool:
number_of_arg = len(sys.argv)
save = False
if number_of_arg < 3 or number_of_arg >= 5:
print('Wrong number of arguments')
exit(1)
if number_of_arg == 4:
if sys.argv[3] == '--save' or sys.argv[3] == '-s':
save = True
else:
print(f'\'{sys.argv[3]}\' is not a valid option!')
exit(1)
if sys.argv[1] != 'fit' and sys.argv[1] != 'predict':
print(f'\'{sys.argv[1]}\' is not a valid option!')
exit(1)
if sys.argv[2].split('.')[-1] != 'csv':
print(f'\'{sys.argv[2]}\' is not a csv file!')
exit(1)
return save
def print_metrics(ax, fig, model, name_of_model, save):
ax[0].plot(range(0, len(model.loss_), 1), model.loss_, label=f'loss_{name_of_model}')
ax[1].plot(range(0, len(model.val_loss_), 1), model.val_loss_, label=f'val_loss_{name_of_model}')
ax[2].plot(range(0, len(model.acc_), 1), model.acc_, label=f'acc_{name_of_model}')
ax[0].set_xlabel('N iterations')
ax[1].set_xlabel('N iterations')
ax[2].set_xlabel('N iterations')
ax[0].set_ylabel('Loss')
ax[1].set_ylabel('Validation loss')
ax[2].set_ylabel('Accuracy')
ax[0].legend()
ax[1].legend()
ax[2].legend()
file = None
if save == True:
try:
os.mkdir(f'{os.path.abspath(".")}/metrics')
except FileExistsError:
pass
try:
file = open(f'metrics/{name_of_model}.metrics', 'x')
except FileExistsError:
file = open(f'metrics/{name_of_model}.metrics', 'w')
plt.savefig('metrics/figures.png')
print(f'Metrics of {name_of_model}\'s model:')
print('-' * 65)
for epoch, loss in enumerate(model.loss_):
line = f'epoch {(epoch + 1)}/{len(model.loss_)} - loss: {loss:.5f} - val_loss: {model.val_loss_[epoch]:.5f} - acc: {model.acc_[epoch]:.5f}'
if save == True:
file.write(f'{line}\n')
print(line)
print('-' * 65)
def save_model(best_model):
with open('model.npy', 'wb') as file:
np.save(file, best_model.hidden_layers_)
np.save(file, len(best_model.parameters_) // 2)
np.save(file, best_model.normalize_)
np.save(file, best_model.activation_)
np.save(file, best_model.out_activation_)
if best_model.normalize_ == True:
np.save(file, best_model.normalize_mean_)
np.save(file, best_model.normalize_std_)
for c in range(1, len(best_model.parameters_) // 2 + 1):
np.save(file, best_model.parameters_[f'W{c}'])
np.save(file, best_model.parameters_[f'b{c}'])
def load_model():
hidden_layer = None
normalize = None
mean = None
std = None
parameters = {}
activation = ''
out_activation = ''
try:
with open('model.npy', 'rb') as file:
hidden_layer = np.load(file)
C = np.load(file)
normalize = np.load(file)
activation = np.load(file)
out_activation = np.load(file)
if normalize == True:
mean = np.load(file)
std = np.load(file)
for c in range(1, C + 1):
parameters[f'W{c}'] = np.load(file)
parameters[f'b{c}'] = np.load(file)
except FileNotFoundError:
print('model.npy doesn\'t exist. Please try to launch a fit program!')
exit(1)
return (hidden_layer, mean, std, parameters, normalize, activation, out_activation)
if __name__ == '__main__':
save = verif_arg()
dataset = load_dataset(sys.argv[2], y_name='Diagnosis', target_order={'M': 0, 'B': 1}, indesirable_feature=['Index'], features_name=features_name)
X = dataset.data.T
y = dataset.target
y_onehot = one_hot(y).T
if sys.argv[1] == 'fit':
adam = MLPClassifier(
hidden_layers=(10, 10),
n_iter=100,
learning_rate_init=0.001,
normalize=True,
multiclass=True,
shuffle=True,
activation='tanh',
out_activation='softmax',
solver='adam',
batch_size=32,
epsilon=1e-08,
)
adam.fit(X, y_onehot, random_state=0, test_size=0.2)
sgd = MLPClassifier(
hidden_layers=(10, 10),
n_iter=100,
learning_rate_init=0.001,
normalize=True,
multiclass=True,
shuffle=True,
activation='tanh',
out_activation='softmax',
solver='sgd',
batch_size=8,
epsilon=1e-08,
)
sgd.fit(X, y_onehot, random_state=0, test_size=0.2)
fig, ax = plt.subplots(1, 3, figsize=(14, 7))
best_model = adam
best_model_name = 'adam'
if adam.val_loss_[-1] > sgd.val_loss_[-1]:
best_model = sgd
best_model_name = 'sgd'
save_model(best_model)
print_metrics(ax, fig, adam, 'adam', save)
print_metrics(ax, fig, sgd, 'sgd', save)
print(f'The best model are {best_model_name}.')
plt.show()
elif sys.argv[1] == 'predict':
hidden_layers, mean, std, parameters, normalize, activation, out_activation = load_model()
model = MLPClassifier(
hidden_layers,
multiclass=True,
activation=np.array_str(activation),
out_activation=np.array_str(out_activation)
)
X = StandartScaler(X, mean, std)
model.parameters_ = parameters
A = model.predict_proba(X)
score = model.score(X, y_onehot)
loss = model.log_loss(y_onehot, A)
print(f'The cost function value is {loss:.5f}')
print(f'The precision score is {score * 100:.2f}%')