-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNN_data_split_experiments.py
178 lines (116 loc) · 4.32 KB
/
CNN_data_split_experiments.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
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 28 18:57:33 2018
@author: keert
"""
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
import itertools
import scipy.io
import time
import pandas as pd
## Keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten
from keras import optimizers
from keras.losses import categorical_crossentropy
def plot_model_history(model_history, l):
plt.plot(range(1,len(model_history.history['loss'])+1), model_history.history['loss'])
plt.title('Learning Curve for ' + l + " PEs in first hidden layer")
plt.xlabel('Iter #')
plt.ylabel('Mean Square Error (MSE)')
plt.grid(True)
figname = "topo_"+ l +".png"
plt.savefig(figname)
plt.show()
data = scipy.io.loadmat('fashion_mnist.mat')
x_train = data['train_x']
y_train = data['train_y']
x_test = data['test_x']
y_test = data['test_y']
x_full = np.concatenate((x_train,x_test),axis=0)
y_full = np.concatenate((y_train,y_test),axis=0)
X=np.concatenate((x_full, y_full), axis=1)
np.random.shuffle(X)
num_epochs = 10
split = [2/3, 3/4, 4/5, 5/6] #Number of proceesing elements
epochs = 10
act = 'relu'
mse_list = []
acc_list = []
time_list = []
len_vec = split #this variable is to save time - no need to change variable names
#in results part....
loss_plots = np.zeros((epochs, (len(len_vec))))
print("Starting Topology experiments")
input_sha = (28, 28, 1)
num_classes = 10
# create model
print("Starting Topology experiments")
for i, l in enumerate(split):
print("running for "+ str(l) + " split")
t1 = time.time()
t1 = time.time()
sep=round(70000*l)
x_full=X[:,0:784]
y_full=X[:,784:]
x_train = x_full[0:sep]
y_train = y_full[0:sep]
x_test = x_full[sep:]
y_test = y_full[sep:]
x_train_reshaped = x_train.reshape(len(x_train),28,28,1)
x_test_reshaped = x_test.reshape(len(x_test),28,28,1)
model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5), strides=(1, 1),
activation='relu',
input_shape= input_sha))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss= categorical_crossentropy,
optimizer= optimizers.SGD(lr=0.1),
metrics=['mse','accuracy']
)
model_info = model.fit(x_train_reshaped, y_train, validation_split= 0.1, epochs=10, verbose=2)
plt.figure()
plot_model_history(model_info, str(l))
loss_plots[:,i] = model_info.history['loss']
training_scores = model.evaluate(x_test_reshaped, y_test)
print("For " + str(l) + " split")
print("\n%s: %.2f%%" % (model.metrics_names[1], training_scores[1]*100))
print("\n%s: %.2f%%" % (model.metrics_names[2], training_scores[2]*100))
t2 = time.time()
mse_list.append(training_scores[1]*100)
acc_list.append(training_scores[2]*100)
time_dur = t2 - t1
time_list.append(time_dur)
#Saving results to csv files to plot them
#Hidden layer units
sp_arr = np.array(split).reshape(len(len_vec),1)
#Learning rate
lr = [0.1] * len(len_vec)
lr_arr = np.array(lr).reshape(len(len_vec),1)
#Activation function
act_func = [act] * len(len_vec)
act_arr = np.array(act_func).reshape(len(len_vec),1)
#Optimizer
opt = ['SGD'] * len(len_vec)
opt_arr = np.array(opt).reshape(len(len_vec),1)
#Number of epcohs
epoc = [10] * len(len_vec)
epoch_arr = np.array(epoc).reshape(len(len_vec),1)
#MSE, Accuracy and Time for each experiment (in this file, for different # of PEs)
mse_arr = np.array(mse_list).reshape(len(len_vec),1)
acc_arr = np.array(acc_list).reshape(len(len_vec),1)
time_arr = np.array(time_list).reshape(len(len_vec),1)
results = np.concatenate((sp_arr, act_arr, opt_arr, lr_arr, epoch_arr, mse_arr, acc_arr, time_arr), axis=1)
result = pd.DataFrame(results)
result.to_csv("split_exp.csv")
#Saving values for learning curves
error_plots = pd.DataFrame(loss_plots)
error_plots.to_csv("split_error_plots.csv")