-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
211 lines (175 loc) · 6.9 KB
/
Main.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
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
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 30 15:31:14 2020
@author: Jannis
"""
import numpy as np
import matplotlib.pyplot as plt
import math
import pandas as pd
import random as r
import csv
import time
import os
from Networks import BDNN
from Networks import ConvBDNN
import Functions as f
import sklearn
import tensorflow
import sys
from sklearn.datasets import fetch_california_housing
from sklearn.datasets import load_digits
from sklearn.datasets import load_iris
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_recall_fscore_support
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import MinMaxScaler
from keras.utils import to_categorical
fracTest=0.5
fracValidation = 0.5
epochs = 30
batchSizeDNN = 64
batchSizeBDNN = 64
randomBatch = True
splittingMethod = "k-means" # k-means or k-median
biasTerms = True
integerWeights = True
maxSplits = 20
regWeight = 0 # Weight for Regularizer
robustRadius = 0.0
numLayers = 1 #number of hidden layers
widthLayers = 100
dataset = "digits"
if dataset=="breast_cancer":
targetColumn = 9
data = np.genfromtxt('Data\\breast-cancer-wisconsin.csv',dtype = float, delimiter=';')
y=data[:,targetColumn]
X=data[:,np.delete(np.arange(0,data.shape[1],1),targetColumn)]
elif dataset=="credit_card":
targetColumn = 23
data = np.genfromtxt('Data\\default_of_credit_card_clients.csv',dtype = float, delimiter=';')
y=data[:,targetColumn]
X=data[:,np.delete(np.arange(0,data.shape[1],1),targetColumn)]
elif dataset=="MNIST":
(X, y), (X_test, y_test) = keras.datasets.mnist.load_data()
X = X.reshape(X.shape[0],-1)
X_test = X_test.reshape(X_test.shape[0],-1)
y_test = to_categorical(y_test)
elif dataset=="california_housing":
X,y = fetch_california_housing(return_X_y=True)
median_y = np.median(y)
y[y<=median_y]=0
y[y>median_y]=1
X = np.array(X)
y=np.array(y)
elif dataset == "digits":
X,y = load_digits(return_X_y=True)
X = np.array(X)
y=np.array(y)
elif dataset == "iris":
X,y = load_iris(return_X_y=True)
X = np.array(X)
y=np.array(y)
elif dataset=="boston_housing":
X,y = load_boston(return_X_y=True)
median_y = np.median(y)
y[y<=median_y]=0
y[y>median_y]=1
X = np.array(X)
y=np.array(y)
y = to_categorical(y)
n=X.shape[1]
print("Number of Instances:", X.shape[0])
print("Number of Attributes:", X.shape[1])
print("Number of Classes:",y.shape[1])
for i in range(y.shape[1]):
print("Fraction of Class ", i+1,":",np.count_nonzero(y,axis=0)[i]/y.shape[0])
sys.exit()
#Create Neural Network
layers = [n]
for i in range(numLayers):
layers.append(widthLayers)
layers.append(y.shape[1])
DNN1 = BDNN(layer_widths=layers, bias_terms= biasTerms)
fileName = dataset + "_bias" + str(int(biasTerms)) + "_intWeights" + str(int(integerWeights)) + "_randBatch" + str(int(randomBatch)) \
+ "_layer" + str(numLayers) + "_width" + str(widthLayers)
fileNameScores = fileName + "_scores.csv"
fileNameHistory = fileName + "_history.csv"
hyperParameters = [fileName,fracTest,fracValidation,epochs,maxSplits,batchSizeDNN,batchSizeBDNN,regWeight,robustRadius]
df = pd.DataFrame(hyperParameters)
df = df.transpose()
df.to_csv("Hyperparameters.csv", sep=';', mode='a', header=False, index=False)
for i in range(10):
if dataset=="MNIST":
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=fracValidation, shuffle=True)
else:
X_train, X_rem, y_train, y_rem = train_test_split(X, y, test_size=fracTest, shuffle=True)
X_val, X_test, y_val, y_test = train_test_split(X_rem, y_rem, test_size=fracValidation, shuffle=True)
print("X_train.shape:",X_train.shape)
print("y_train.shape:",y_train.shape)
print("X_val.shape",X_val.shape)
print("y_val.shape",y_val.shape)
print("X_test.shape",X_test.shape)
print("y_test.shape",y_test.shape)
start = time.time()
history, model = f.trainDNN(DNN1, X_train, y_train, X_val, y_val, batchSizeDNN, epochs, regWeight)
end = time.time()
runtimeDNN = end-start
# print('Confusion Matrix :')
# print(confusion_matrix(y_true,y_pred))
# print(classification_report(y_true,y_pred))
start = time.time()
timeMIP, trainLoss, accuracy, bestBDNN = f.trainBDNN(X_train, y_train, X_val, y_val, DNN1, splittingMethod, maxSplits, regWeight, integerWeights, robustRadius, batchSizeBDNN, randomBatch)
end = time.time()
runtimeBDNN = end-start
print("\n########################Test-Set Evaluation #################")
###################### DNN #######################################
y_pred = np.argmax(model.predict(X_test),axis=1)
y_true = np.argmax(y_test, axis=1)
#Classification Report
df = pd.DataFrame(classification_report(y_true, y_pred, output_dict=True))
df.to_csv("DNN_" + fileNameScores, sep=';', mode='a')
#Time
df = pd.DataFrame(["time_total",runtimeDNN])
df = df.transpose()
df.to_csv("DNN_" + fileNameScores, sep=';', mode='a', header=False, index=False)
#History
history.history['val_accuracy'].insert(0,"val_accuracy")
df = pd.DataFrame(history.history['val_accuracy'])
df = df.transpose()
df.to_csv("DNN_" + fileNameHistory, sep=';', mode='a', header=False, index=False)
history.history['loss'].insert(0,"train_loss")
df = pd.DataFrame(history.history['loss'])
df = df.transpose()
df.to_csv("DNN_" + fileNameHistory, sep=';', mode='a', header=False, index=False)
print("\nDNN:")
print("Test Accuracy DNN:",round(accuracy_score(y_true,y_pred)*100,3))
print("Confusion Matrix:\n",confusion_matrix(y_true,y_pred))
###################### BDNN ################################
y_pred = bestBDNN.predict(X_test)
print("\nBDNN:")
print("Test Accuracy BDNN:",round(accuracy_score(y_true,y_pred)*100,3))
print("Confusion Matrix:\n",confusion_matrix(y_true,y_pred))
#Classification Report
df = pd.DataFrame(classification_report(y_true, y_pred, output_dict=True))
df.to_csv("BDNN_" + fileNameScores, sep=';', mode='a')
#Time
df = pd.DataFrame(["time_total",runtimeBDNN])
df = df.transpose()
df.to_csv("BDNN_" + fileNameScores, sep=';', mode='a', header=False, index=False)
#History
accuracy.insert(0,"val_accuracy")
df = pd.DataFrame(accuracy)
df = df.transpose()
df.to_csv("BDNN_" + fileNameHistory, sep=';', mode='a', header=False, index=False)
trainLoss.insert(0,"train_loss")
df = pd.DataFrame(trainLoss)
df = df.transpose()
df.to_csv("BDNN_" + fileNameHistory, sep=';', mode='a', header=False, index=False)
timeMIP.insert(0,"time")
df = pd.DataFrame(timeMIP)
df = df.transpose()
df.to_csv("BDNN_" + fileNameHistory, sep=';', mode='a', header=False, index=False)