-
Notifications
You must be signed in to change notification settings - Fork 175
/
mha-hybrid-mlp-classification.py
112 lines (94 loc) · 4.3 KB
/
mha-hybrid-mlp-classification.py
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
#!/usr/bin/env python
# Created by "Thieu" at 23:58, 14/12/2021 ----------%
# Email: nguyenthieu2102@gmail.com %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%
# https://machinelearningmastery.com/display-deep-learning-model-training-history-in-keras/
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from sklearn.metrics import accuracy_score
from mealpy import GWO, FloatVar
class HybridMlp:
def __init__(self, dataset, n_hidden_nodes, epoch, pop_size):
self.X_train, self.y_train, self.X_test, self.y_test = dataset[0], dataset[1], dataset[2], dataset[3]
self.n_hidden_nodes = n_hidden_nodes
self.epoch = epoch
self.pop_size = pop_size
self.n_inputs = self.X_train.shape[1]
self.model, self.problem_size, self.n_dims, self.problem = None, None, None, None
self.optimizer, self.solution, self.best_fit = None, None, None
def create_network(self):
# create model
model = Sequential()
model.add(Dense(self.n_hidden_nodes[0], input_dim=self.n_inputs, activation='relu'))
model.add(Dense(self.n_hidden_nodes[1], activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile model
# model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
self.model = model
self.problem_size = self.n_dims = np.sum([np.size(w) for w in self.model.get_weights()])
def create_problem(self):
self.problem = {
"obj_func": self.fitness_function,
"bounds": FloatVar(lb=(-1.,)*self.n_dims, ub=(1.,)*self.n_dims),
"minmax": "max",
"log_to": "console",
}
def decode_solution(self, solution):
# solution: is a vector.
# solution = [w11, w21, w31, w12, w22, w32, b1, b2, wh11, wh21, wh12, wh22, wh13, wh23, bo1, bo2, bo3 ]
# number of weights = n_inputs * n_hidden_nodes + n_hidden_nodes + n_hidden_nodes * n_outputs + n_outputs
# we decode the solution into the neural network weights
# we return the model with the new weight (weight from solution)
weight_sizes = [(w.shape, np.size(w)) for w in self.model.get_weights()]
# ( (3, 5), 15 )
weights = []
cut_point = 0
for ws in weight_sizes:
temp = np.reshape(solution[cut_point: cut_point + ws[1]], ws[0])
# [0: 15], (3, 5),
weights.append(temp)
cut_point += ws[1]
self.model.set_weights(weights)
def prediction(self, solution, x_data):
self.decode_solution(solution)
return self.model.predict(x_data)
def training(self):
self.create_network()
self.create_problem()
self.optimizer = GWO.OriginalGWO(self.epoch, self.pop_size)
# self.optimizer = FPA.OriginalFPA(self.problem, self.epoch, self.pop_size)
self.solution, self.best_fit = self.optimizer.solve(self.problem)
def fitness_function(self, solution): # Used in training process
# Assumption that we have 3 layer , 1 input layer, 1 hidden layer and 1 output layer
# number of nodes are 3, 2, 3
# solution = [w11, w21, w31, w12, w22, w32, b1, b2, wh11, wh21, wh12, wh22, wh13, wh23, bo1, bo2, bo3 ]
self.decode_solution(solution)
yhat = self.model.predict(self.X_train)
yhat = np.argmax(yhat, axis=-1).astype('int')
acc = accuracy_score(self.y_train, yhat)
return acc
# load pima indians dataset
dataset = np.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:, 0:8]
Y = dataset[:, 8]
cut = int(0.6 * len(X)) # 60% for training, 40% for testing
X_train, X_test = X[0: cut], X[cut:]
y_train, y_test = Y[0: cut], Y[cut:]
dataset = [X_train, y_train, X_test, y_test]
n_hidden_nodes = [10, 5]
epoch = 50
pop_size = 50
# define model
model = HybridMlp(dataset, n_hidden_nodes, epoch, pop_size)
# fit model
model.training()
# access to the best model - best set of weights - the final weights of neural network
# model.solution
# evaluate on test set
yhat = model.prediction(solution=model.solution, x_data=X_test)
yhat = np.argmax(yhat, axis=-1).astype('int')
acc = accuracy_score(y_test, yhat)
print('Accuracy: %.3f' % acc)