-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
234 lines (208 loc) · 9.89 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
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
import collections
import os
import random
import sys
import pandas as pd
import numpy as np
from numpy import nan
from IPython.display import display
import matplotlib.pyplot as plt
from itertools import chain
from toolkit import Toolkit
from LinearNetwork import LinearNetwork
from FowardFeedNetwork import FowardFeedNetwork
from Autoencoder import Autoencoder
# load files
myToolkit = Toolkit()
myToolkit.fileNames = ['abalone.data', 'machine.data', 'breast-cancer-wisconsin.data', 'forestfires.data','house-votes-84.data', 'car.data']
myToolkit.load_data()
learningRates = [0.7, 0.5, 0.2, 0.1, 0.05, 0.01, 0.005, 0.0001]
steps = 1000
stepsList = [step for step in range(steps+1) if step > 0 and step % 100 == 0]
'''
Plot single line plot
'''
def plot(fileName, x, y, title):
plt.plot(x,y)
plt.xlabel('Steps')
plt.ylabel('Accuracy')
plt.title(fileName + ' ' + title)
plt.show()
'''
Plot multiple lines on 1 plot
Input:
mapping - dictionary of file and accuracy lists
title - title of plot
typee - type (classsification or regression)
learningRate - learning rate for these experiements
'''
def plotMultiple(x, mapping, title, typee, learningRate):
for fileName, y in mapping.items():
plt.plot(x, y, label=fileName)
plt.xlabel('Steps')
plt.ylabel('Accuracy')
plt.title(title + ' on ' + typee + ' datasets \n' + 'learning rate = ' + str(learningRate))
plt.legend(loc="upper left")
plt.show()
'''
1. A linear network for each of the classification and regression data sets
----------------------------------------------------------------------------
*. Implement logistic regression for the three classification problems
- The classification problems should use cross-entropy loss
*. Implement a simple linear network for the three regression problems
- the regression problems should use mean squared error
Input: None
Output: 1 graph depicting accuracy rates for the 3 graphs
'''
def buildNetworkType1():
yPlotResults = {}
learningRate = 0.005
# iterate through classification sets
for fileName in myToolkit.classificationDatasets:
# 5 vold cross validation
results = 0
print('')
print('building logistic regression model for : ', fileName)
averagedLosses = [0 for i in range(steps) if i % 100 == 0]
averagedLosses = np.array(averagedLosses)
averagedAccuracyPerSteps = np.array(averagedLosses)
folds,validate = myToolkit.cross_validate(fileName,5)
for train,test in folds:
train,test = myToolkit.standardize(train,test)
linearNetwork = LinearNetwork(fileName, train, test)
model, accuracy, loss, accuracyPerSteps = linearNetwork.logisticRegression(steps, learningRate)
averagedLosses = np.add(averagedLosses, np.array(loss))
averagedAccuracyPerSteps = np.add(averagedAccuracyPerSteps, np.array(accuracyPerSteps))
results += accuracy
# plot(fileName, stepsList, averagedAccuracyPerSteps/3, 'logistic regression')
yPlotResults[fileName] = averagedAccuracyPerSteps/5
print('yPlotResults now: ', yPlotResults)
plotMultiple(stepsList, yPlotResults, 'logistic regression', 'classification', learningRate)
# iterate through regression sets
# for fileName in myToolkit.regressionDatasets:
# results = 0
# # 5 vold cross validation
# print('')
# print('building multiple linear regression model for : ', fileName)
# averagedLosses = [0 for i in range(steps) if i % 1000 == 0]
# averagedLosses = np.array(averagedLosses)
# averagedAccuracyPerSteps = np.array([0 for i in range(steps) if i % 1000 == 0])
# folds,validate = myToolkit.cross_validate(fileName,5)
# for train,test in folds:
# train,test = myToolkit.standardize(train,test)
# linearNetwork = LinearNetwork(fileName, train, test)
# model, accuracy, loss, accuracyPerSteps = linearNetwork.linearRegression(steps, learningRate)
# averagedLosses = np.add(averagedLosses, np.array(loss))
# averagedAccuracyPerSteps = np.add(averagedAccuracyPerSteps, np.array(accuracyPerSteps))
# print('model: ', model)
# print('accuracy: ', accuracy)
# print('averagedLosses now: ', averagedLosses)
# print('averagedAccuracyPerSteps now: ', averagedAccuracyPerSteps)
# results += accuracy
# print('averaged results: ', results/5)
# print('plotting for ', fileName)
# # plot(fileName, stepsList, averagedLosses)
# # yPlotResults[fileName] = averagedLosses/5
# yPlotResults[fileName] = averagedAccuracyPerSteps/5
# print('yPlotResults now: ', yPlotResults)
# plotMultiple(stepsList, yPlotResults, 'multiple linear regression', 'regression', learningRate)
# buildNetworkType1()
'''
2. A simple feedforward network with two hidden layers (Input ⇒ Hidden 1 ⇒ Hidden 2 ⇒ Prediction)
for each of the classification and regression data sets
----------------------------------------------------------------------------
- Note that “Prediction” in the above should use a softmax output layer for classification and a linear
output for regression.
Input: None
Output: 1 graph depicting accuracy rates for the 3 graphs
'''
def buildNetworkType2():
steps = 100
stepsList = [step for step in range(steps+1) if step > 0 and step % 10 == 0]
learningRate = 0.005
yPlotResults = {}
# iterate through classification sets
for fileName in myToolkit.classificationDatasets:
# 5 vold cross validation
results = 0
print('')
print('building 2 layer network for : ', fileName)
folds,validate = myToolkit.cross_validate(fileName,5)
averagedLosses = [0 for i in range(steps) if i % 10 == 0]
averagedLosses = np.array(averagedLosses)
averagedAccuracyPerSteps = np.array(averagedLosses)
for train,test in folds:
train,test = myToolkit.standardize(train,test)
nn = FowardFeedNetwork(fileName, train, test)
model, accuracy, loss, accuracyPerSteps = nn.trainNetwork(steps, learningRate)
# averagedLosses = np.add(averagedLosses, np.array(loss))
averagedAccuracyPerSteps = np.add(averagedAccuracyPerSteps, np.array(accuracyPerSteps))
results += accuracy
yPlotResults[fileName] = averagedAccuracyPerSteps/5
print('yPlotResults final: ', yPlotResults)
plotMultiple(stepsList, yPlotResults, 'neural network', 'classification', learningRate)
# iterate through regression sets
# for fileName in myToolkit.regressionDatasets:
# # 5 vold cross validation
# results = 0
# print('')
# print('building 2 layer network for : ', fileName)
# folds,validate = myToolkit.cross_validate(fileName,5)
# averagedLosses = [0 for i in range(steps) if i % 10 == 0]
# averagedLosses = np.array(averagedLosses)
# averagedAccuracyPerSteps = np.array(averagedLosses)
# for train,test in folds:
# train,test = myToolkit.standardize(train,test)
# nn = FowardFeedNetwork(fileName, train, test)
# model, accuracy, loss, accuracyPerSteps = nn.trainNetworkRegression(steps, learningRate)
# # averagedLosses = np.add(averagedLosses, np.array(loss))
# averagedAccuracyPerSteps = np.add(averagedAccuracyPerSteps, np.array(accuracyPerSteps))
# print('model: ', model)
# print('accuracy: ', accuracy)
# print('averagedLosses now: ', averagedLosses)
# print('averagedAccuracyPerSteps: ', averagedAccuracyPerSteps)
# results += accuracy
# print('averaged results: ', results/5)
# print('plotting for ', fileName)
# print('averagedLoses: ', averagedLosses)
# print('averagedLosses/5: ', averagedLosses/5)
# yPlotResults[fileName] = averagedAccuracyPerSteps/5
# print('yPlotResults now: ', yPlotResults)
# plotMultiple(stepsList, yPlotResults, 'neural network', 'regression', learningRate)
# buildNetworkType2()
'''
3. A feedforward network where the first hidden layer is trained from an autoencoder and the second
hidden layer is trained from the prediction part of the network (Input ⇒ Encoding ⇒ Hidden ⇒
Prediction) for each of the classification and regression data sets
----------------------------------------------------------------------------
- Note that “Prediction” in the above should use a softmax output layer for classification and a linear
output for regression.
Input: None
Output: 1 graph depicting accuracy rates for the 3 graphs
'''
def buildNetworkType3():
steps = 300
stepsList = [step for step in range(steps+1) if step > 0 and step % 10 == 0]
learningRate = 0.1
yPlotResults = {}
# iterate through classification sets
for fileName in myToolkit.classificationDatasets:
# 5 vold cross validation
results = 0
print('')
print('building 2 layer auto encoder network for : ', fileName)
folds,validate = myToolkit.cross_validate(fileName,5)
averagedLosses = [0 for i in range(steps) if i % 10 == 0]
averagedLosses = np.array(averagedLosses)
averagedAccuracyPerSteps = np.array(averagedLosses)
for train,test in folds:
train,test = myToolkit.standardize(train,test)
nn = Autoencoder(fileName, train, test)
model, accuracy, loss, accuracyPerSteps = nn.trainNetwork(steps, learningRate)
# averagedLosses = np.add(averagedLosses, np.array(loss))
averagedAccuracyPerSteps = np.add(averagedAccuracyPerSteps, np.array(accuracyPerSteps))
results += accuracy
yPlotResults[fileName] = averagedAccuracyPerSteps/5
print('yPlotResults final: ', yPlotResults)
plotMultiple(stepsList, yPlotResults, 'neural network autoencoder layer', 'classification', learningRate)
buildNetworkType3()