-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackedautoencoder.py
More file actions
457 lines (391 loc) · 18.4 KB
/
stackedautoencoder.py
File metadata and controls
457 lines (391 loc) · 18.4 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# Backprop on the Vowel Dataset
from random import seed
from random import randrange
from random import random
from csv import reader
from math import exp
import math
from sklearn.metrics import confusion_matrix
from sklearn.metrics import cohen_kappa_score
import numpy as np
import csv
import copy
def loadNew(file):
trainSet = []
lines = csv.reader(open(file, 'r'))
dataset = list(lines)
#print("training set {}".format(dataset))
for i in range(len(dataset[0])-1):
for row in dataset:
try:
row[i] = float(row[i])
except ValueError:
print("Error with row",column,":",row[i])
pass
trainSet = dataset
return trainSet
# Load a CSV file
def loadCsv(filename, file1):
trainSet = []
testSet = []
lines = csv.reader(open(filename, 'r'))
dataset = list(lines)
#print("training set {}".format(dataset))
for i in range(len(dataset[0])-1):
for row in dataset:
try:
row[i] = float(row[i])
except ValueError:
print("Error with row",column,":",row[i])
pass
trainSet = dataset
lines = csv.reader(open(file1, 'r'))
dataset = list(lines)
for i in range(len(dataset[0])-1):
for row in dataset:
try:
row[i] = float(row[i])
except ValueError:
print("Error with row",column,":",row[i])
pass
testSet = dataset
#print("training set {}".format(trainSet))
return trainSet, testSet
# Convert string column to float
def str_column_to_float(dataset, column):
for row in dataset:
try:
row[column] = float(row[column])
except ValueError:
print("Error with row",column,":",row[column])
pass
# Convert string column to integer
def str_column_to_int(dataset, column):
class_values = [row[column] for row in dataset]
unique = set(class_values)
lookup = dict()
for i, value in enumerate(unique):
lookup[value] = i
for row in dataset:
row[column] = lookup[row[column]]
return lookup
# Find the min and max values for each column
def dataset_minmax(dataset):
minmax = list()
stats = [[min(column), max(column)] for column in zip(*dataset)]
return stats
# Rescale dataset columns to the range 0-1
def normalize_dataset(dataset, minmax):
for row in dataset:
for i in range(len(row)-1):
row[i] = (row[i] - minmax[i][0]) / (minmax[i][1] - minmax[i][0])
# Calculate accuracy percentage
def accuracy_metric(actual, predicted):
correct = 0
for i in range(len(actual)):
if actual[i] == predicted[i]:
correct += 1
return correct / float(len(actual)) * 100.0
# Evaluate an algorithm using a cross validation split
def evaluate_algorithm(train_set, test_set, algorithm, *args):
test_set_for_class = copy.deepcopy(test_set)
test_set_again = copy.deepcopy(test_set)
for row in test_set:
del row[-1]
#print(test_set_for_class)
network_one, new_train, new_test = algorithm(train_set, test_set, test_set_for_class, *args)
test_set_for_class = copy.deepcopy(new_test)
for row in new_test:
del row[-1]
network_two, second_train, second_test = backpropagation_two(new_train,new_test, test_set_for_class, *args)
network_three, predicted=back_prop_for_classification(second_train, second_test, *args)
new_network = list()
for layer in network_one:
new_network.append(layer)
for layer in network_two:
new_network.append(layer)
for layer in network_three:
new_network.append(layer)
## for layer in new_network:
## print("\n\n{}\n\n".format(layer))
n_inputs = len(test_set_again[0]) - 1
n_outputs = len(set([row[-1] for row in test_set_again]))
#print("For Classification {}".format(network))
predictions = list()
for row in test_set_again:
prediction = predict(new_network, row)
predictions.append(prediction)
#predicted = back_prop_for_classification(train_set,test_set_for_class, network, *args)
actual = [int(row[-1]) for row in test_set_again]
#print(" {} \n\n {} \n\n".format(actual,predicted))
accuracy = accuracy_metric(actual, predictions)
cm = confusion_matrix(actual, predictions)
print('\n'.join([''.join(['{:4}'.format(item) for item in row]) for row in cm]))
#confusionmatrix = np.matrix(cm)
FP = cm.sum(axis=0) - np.diag(cm)
FN = cm.sum(axis=1) - np.diag(cm)
TP = np.diag(cm)
TN = cm.sum() - (FP + FN + TP)
print('False Positives\n {}'.format(FP))
print('False Negetives\n {}'.format(FN))
print('True Positives\n {}'.format(TP))
print('True Negetives\n {}'.format(TN))
TPR = TP/(TP+FN)
print('Sensitivity \n {}'.format(TPR))
TNR = TN/(TN+FP)
print('Specificity \n {}'.format(TNR))
Precision = TP/(TP+FP)
print('Precision \n {}'.format(Precision))
Recall = TP/(TP+FN)
print('Recall \n {}'.format(Recall))
Acc = (TP+TN)/(TP+TN+FP+FN)
print('Áccuracy \n{}'.format(Acc))
Fscore = 2*(Precision*Recall)/(Precision+Recall)
print('FScore \n{}'.format(Fscore))
re_train_network(new_network, train_set, l_rate, n_epoch, n_outputs)
#print("For Classification {}".format(network))
predictions = list()
for row in test_set_again:
prediction = predict(new_network, row)
predictions.append(prediction)
actual = [int(row[-1]) for row in test_set_again]
#print(" {} \n\n {} \n\n".format(actual,predicted))
accuracy = accuracy_metric(actual, predictions)
cm = confusion_matrix(actual, predictions)
print('\n'.join([''.join(['{:4}'.format(item) for item in row]) for row in cm]))
#confusionmatrix = np.matrix(cm)
FP = cm.sum(axis=0) - np.diag(cm)
FN = cm.sum(axis=1) - np.diag(cm)
TP = np.diag(cm)
TN = cm.sum() - (FP + FN + TP)
print('False Positives\n {}'.format(FP))
print('False Negetives\n {}'.format(FN))
print('True Positives\n {}'.format(TP))
print('True Negetives\n {}'.format(TN))
TPR = TP/(TP+FN)
print('Sensitivity \n {}'.format(TPR))
TNR = TN/(TN+FP)
print('Specificity \n {}'.format(TNR))
Precision = TP/(TP+FP)
print('Precision \n {}'.format(Precision))
Recall = TP/(TP+FN)
print('Recall \n {}'.format(Recall))
Acc = (TP+TN)/(TP+TN+FP+FN)
print('Áccuracy \n{}'.format(Acc))
Fscore = 2*(Precision*Recall)/(Precision+Recall)
print('FScore \n{}'.format(Fscore))
#return scores
# Calculate neuron activation for an input
def activate(weights, inputs):
activation = weights[-1]
for i in range(len(weights)-1):
activation += weights[i] * inputs[i]
return activation
# Transfer neuron activation
def transfer(activation):
return 1.0 / (1.0 + exp(-activation))
# Forward propagate input to a network output
def forward_propagate(network, row):
inputs = row
for layer in network:
new_inputs = []
for neuron in layer:
activation = activate(neuron['weights'], inputs)
neuron['output'] = transfer(activation)
new_inputs.append(neuron['output'])
inputs = new_inputs
return inputs
# Calculate the derivative of an neuron output
def transfer_derivative(output):
return output * (1.0 - output)
# Backpropagate error and store in neurons
def backward_propagate_error(network, expected):
for i in reversed(range(len(network))):
layer = network[i]
errors = list()
if i != len(network)-1:
for j in range(len(layer)):
error = 0.0
for neuron in network[i + 1]:
error += (neuron['weights'][j] * neuron['delta'])
errors.append(error)
else:
for j in range(len(layer)):
neuron = layer[j]
errors.append(expected[j] - neuron['output'])
for j in range(len(layer)):
neuron = layer[j]
neuron['delta'] = errors[j] * transfer_derivative(neuron['output'])
# Update network weights with error
def update_weights(network, row, l_rate):
for i in range(len(network)):
inputs = row[:-1]
if i != 0:
inputs = [neuron['output'] for neuron in network[i - 1]]
for neuron in network[i]:
for j in range(len(inputs)):
temp = l_rate * neuron['delta'] * inputs[j] + mu * neuron['prev'][j]
neuron['weights'][j] += temp
#print("neuron weight{} \n".format(neuron['weights'][j]))
neuron['prev'][j] = temp
temp = l_rate * neuron['delta'] + mu * neuron['prev'][-1]
neuron['weights'][-1] += temp
neuron['prev'][-1] = temp
# Train a network for a fixed number of epochs
def train_network(network, train, l_rate, n_epoch, n_outputs):
for epoch in range(n_epoch):
for row in train:
outputs = forward_propagate(network, row)
expected=row
backward_propagate_error(network, expected)
update_weights(network, row, l_rate)
def re_train_network(network_two, train_set, l_rate, n_epoch, n_outputs):
for epoch in range(n_epoch):
for row in train_set:
outputs = forward_propagate(network_two, row)
#print(outputs)
expected = [0 for i in range(n_outputs)]
#print(row)
expected[int(row[-1])-1] = 1
#print("expected row{}\n".format(expected))
backward_propagate_error(network_two, expected)
update_weights(network_two, row, l_rate)
def prepare_dataset(network, test_with_class):
new_data_train = list()
for row in trainingSet:
outputs = forward_propagate(network, row)
outputs.append(int(row[-1]))
#print(" {} \n".format(row))
new_data_train.append(outputs)
new_data_test = list()
for row in test_with_class:
outputs = forward_propagate(network, row)
outputs.append(int(row[-1]))
#print(" {} \n".format(row))
new_data_test.append(outputs)
return new_data_train, new_data_test
def prepare_dataset_two(network, train, test, test_with_class):
second_data_train = list()
for row in train:
outputs = forward_propagate(network, row)
outputs.append(row[-1])
#print(" {} \n".format(row))
second_data_train.append(outputs)
## csvfile = "newdata_train_two.csv"
## with open(csvfile, "w") as output:
## writer = csv.writer(output, lineterminator='\n')
## writer.writerows(second_data_train)
second_data_test = list()
for row in test_with_class:
outputs = forward_propagate(network, row)
outputs.append(row[-1])
#print(" {} \n".format(row))
second_data_test.append(outputs)
## csvfile = "newdata_test_two.csv"
## with open(csvfile, "w") as output:
## writer = csv.writer(output, lineterminator='\n')
## writer.writerows(second_data_test)
## return second_data_train, second_data_test
# Initialize a network
def initialize_network(n_inputs, n_hidden, n_outputs):
network = list()
hidden_layer = [{'weights':[np.random.uniform(0.0, 0.2) for i in range(n_inputs + 1)], 'prev':[0 for i in range(n_inputs+1)]} for i in range(n_hidden)]
network.append(hidden_layer)
## hidden_layer = [{'weights':[random() for i in range(n_hidden + 1)], 'prev':[0 for i in range(n_hidden+1)]} for i in range(n_hidden)]
## network.append(hidden_layer)
output_layer = [{'weights':[np.random.uniform(0, 0.2) for i in range(n_hidden + 1)],'prev':[0 for i in range(n_hidden+1)]} for i in range(n_outputs)]
network.append(output_layer)
#print("FIRST {} \n\n".format(network))
return network
def reinitialize_to_classify(n_inputs, n_outputs):
network_two = list()
output_layer = [{'weights':[np.random.uniform(0, 0.2) for i in range(n_inputs + 1)],'prev':[0 for i in range(n_inputs + 1)]} for i in range(n_outputs)]
network_two.append(output_layer)
#print(network)
return network_two
# Make a prediction with a network
def predict(network, row):
outputs = forward_propagate(network, row)
return outputs.index(max(outputs)) + 1
# Backpropagation Algorithm With Stochastic Gradient Descent
def back_propagation(train, test, test_with_class, l_rate, n_epoch, n_hidden):
n_inputs = len(train[0]) - 1
n_outputs = n_inputs
network = initialize_network(n_inputs, n_hidden, n_outputs)
train_network(network, train, l_rate, n_epoch, n_outputs)
#print("network {}\n".format(network))
avg_msq=0
for row in test:
output = forward_propagate(network, row)
#print(" row {} ---- output {}".format(row,output))
r = np.array(row)
o = np.array(output)
err = r - o
mssq = np.sum(err**2)/len(err)
#print("MSE = {}".format(mssq))
avg_msq=avg_msq+ mssq
avg_msq=avg_msq/len(test)
print(" MSE First {} ".format(avg_msq))
#print("FIRST {} \n\n".format(network))
#network = transform_auto(network,train, test, l_rate, n_epoch, n_hidden, n_outputs)
network = network[:-1]
new_train, new_test = prepare_dataset(network,test_with_class)
#print(network)
return network, new_train, new_test
def backpropagation_two(train ,test, test_with_class, *args):
n_inputs = len(train[0]) - 1
n_outputs = n_inputs
n_hidden_two = 9
network_two = initialize_network(n_inputs, n_hidden_two, n_outputs)
train_network(network_two, train, l_rate, n_epoch, n_outputs)
#print("network {}\n".format(network))
avg_msq=0
for row in test:
output = forward_propagate(network_two, row)
#print(" row {} ---- output {}".format(row,output))
r = np.array(row)
o = np.array(output)
err = r - o
mssq = np.sum(err**2)/len(err)
#print("MSE = {}".format(mssq))
avg_msq=avg_msq+ mssq
avg_msq=avg_msq/len(test)
print(" MSE Second {} ".format(avg_msq))
network_two = network_two[:-1]
second_train, second_test = prepare_dataset_two(network_two,train, test, test_with_class)
#print(network)
return network_two, second_train, second_test
def back_prop_for_classification(train_set,test_set_for_class, *args):
n_inputs = len(train_set[0]) - 1
n_outputs = len(set([row[-1] for row in train_set]))
#print(n_outputs)
network_three = reinitialize_to_classify(n_inputs, n_outputs)
re_train_network(network_three, train_set, l_rate, n_epoch, n_outputs)
#print("For Classification {}".format(network))
predictions = list()
for row in test_set_for_class:
prediction = predict(network_three, row)
predictions.append(prediction)
#print(predictions)
return network_three, predictions
# Test Backprop on Seeds dataset
seed(1)
# load and prepare data
filename = 'sat_train_new.csv'
file1 = 'sat_test_new.csv'
#sRatio = 0.80
trainingSet, testSet = loadCsv(filename, file1)
# normalize input variables
minmax = dataset_minmax(trainingSet)
normalize_dataset(trainingSet, minmax)
# normalize input variables
minmax = dataset_minmax(testSet)
normalize_dataset(testSet, minmax)
# evaluate algorithm
l_rate = 0.3
mu=0.1
n_epoch = 300
n_hidden = 19
scores = evaluate_algorithm(trainingSet,testSet, back_propagation, l_rate, n_epoch, n_hidden)
#print('Scores: %s' % scores)
#print('Mean Accuracy: %.3f%%' % (sum(scores)/float(len(scores))))