-
Notifications
You must be signed in to change notification settings - Fork 1
/
ANN.py
155 lines (122 loc) · 5.74 KB
/
ANN.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
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
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 28 12:13:53 2020
@author: Maria
"""
import numpy as np
# For the ANN building
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
# For making predictions
from sklearn.metrics import confusion_matrix
# For evaluating
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from sklearn.metrics import multilabel_confusion_matrix
# For tuning
from sklearn.model_selection import GridSearchCV
from keras.constraints import maxnorm
class ANN:
def __init__(self, X_train, y_train, batch_size, epochs, output_units):
classifier = Sequential()
classifier.add(Dense(units = 30, kernel_initializer = 'uniform', activation = 'relu', input_dim = X_train.shape[1]))
classifier.add(Dropout(rate=0.3))
classifier.add(Dense(units = 30, kernel_initializer = 'uniform', activation = 'relu')) #relu
classifier.add(Dropout(rate=0.3))
classifier.add(Dense(units = output_units, kernel_initializer = 'uniform', activation = 'sigmoid'))
classifier.compile(optimizer = 'rmsprop', loss = 'binary_crossentropy', metrics = ['accuracy'])
classifier.fit(X_train, y_train, batch_size = batch_size, epochs = epochs, verbose=0)
self.model = classifier
def evaluate_model(X_train, y_train):
classifier = KerasClassifier(build_fn = build_classifier, batch_size = 10, epochs = 500)
accuracies = cross_val_score(estimator = classifier, X = X_train, y = y_train, cv = 10, n_jobs = 1)
mean = accuracies.mean()
variance = accuracies.std()
return accuracies, mean, variance
def cross_validate(self, X_train, y_train, X_test, y_test):
from sklearn.model_selection import StratifiedKFold
seed = 7 # fix random seed for reproducibility
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed)
cvscores = []
for train, test in kfold.split(X_train, y_train):
# create model
model = Sequential()
model.add(Dense(30, input_dim=X_train.shape[1], activation='relu'))
model.add(Dense(30, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='RMSprop', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=100, batch_size=10, verbose=0)
# evaluate the model
scores = model.evaluate(X_test, y_test, verbose=3)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
cvscores.append(scores[1] * 100)
print("%.2f%% (+/- %.2f%%)" % (np.mean(cvscores), np.std(cvscores)))
return cvscores
def gridSearch(inputs_train, output_train):
model = KerasClassifier(build_fn=create_model, verbose=10)
# defining grid search parameters
param_grid = {'optimizer': ['RMSprop'],
'batch_size': [10],
'epochs': [100],
# 'learn_rate': [0.001, 0.01, 0.1, 0.2, 0.3],
# 'momentum': [0.0, 0.2, 0.4, 0.6, 0.8, 0.9],
'init_mode': ['lecun_uniform'],
'activation': ['softmax'],
'weight_constraint': [1],
'dropout_rate': [0.0,0.5,0.9],
'neurons': [10, 30]
}
grid = GridSearchCV(estimator=model, param_grid=param_grid, cv=3, verbose=10)
grid_result = grid.fit(inputs_train, output_train)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
return grid.best_params_, grid.best_score_
def predict_all(self, X_pred, y_true):
y_pred = self.model.predict(X_pred)
y_bool = []
accuracy = 0
if np.size(y_pred,1) == 1: # binary prediction (1s or 0s) predicting1 category
# print("Predicting 1 category")
for n in y_pred:
if n > 0.75:
n = 1
else:
n = 0
y_bool.append(n)
# Making the Confusion Matrix
accuracy = confusion_matrix(y_true, y_bool)
# print(accuracy)
else: # Predicting 4 categories
# print("Predicting 4 categories")
accuracy = multilabel_confusion_matrix(y_true, y_pred.round())
# print(accuracy)
return y_pred, y_bool, accuracy
def build_classifier():
classifier = Sequential()
classifier.add(Dense(units = 30, kernel_initializer = 'uniform', activation = 'relu', input_dim = 55))
classifier.add(Dense(units = 30, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
return classifier
# Grid Search
def create_model(optimizer='adam',
#learn_rate=0.01,
#momentum=0,
init_mode='uniform',
activation='relu',
dropout_rate=0.0,
weight_constraint=0,
neurons=1
):
model = Sequential()
model.add(Dense(neurons,
input_dim=38,
kernel_initializer=init_mode,
activation=activation,
kernel_constraint=maxnorm(weight_constraint)))
model.add(Dropout(dropout_rate))
model.add(Dense(1))
#opimizer = SGD(lr=learn_rate, momentum=momentum)
model.compile(loss='mse', optimizer=optimizer, metrics=['mse', 'acc'])
return model