/
main.py
165 lines (123 loc) · 4.03 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
from train_validate import TrainerValidator
from data import Data
from model_evaluation import ModelEvaluation
from test import Test
from SquaredErrorLinearClassifier import SquaredErrorLinearClassifier
from LogisticLinearClassifier import LogisticLinearClassifier
import matplotlib.pyplot as plt
import scipy as sp
from error import Error
def main():
#Binary MLP
testBinary()
#Multi-Way MLP
testMultiWay()
#Squared error linear classifier
testSquaredError()
#Logistic error linear classifier
testLogisticError()
plt.show()
def testBinary() :
k=2
data = Data(k, 0, 0)
data.importDataFromMat()
data.normalize()
train = TrainerValidator(k, 70, 100, 10, 0.1, 0.2, 1, data)
train.trainAndClassify()
train.plotResults()
test = Test(train.getMLP(), data, k)
test.classify()
test.examples()
test.plot_confusion_matrix()
def testMultiWay() :
k=5
data = Data(k, 0, 0)
data.importDataFromMat()
data.normalize()
train = TrainerValidator(k, 70, 80, 60, 0.004, 0.1, 1, data)
train.trainAndClassify()
train.plotResults()
test = Test(train.getMLP(), data, k)
test.classify()
test.examples()
test.plot_confusion_matrix()
def testSquaredError() :
k=5
data = Data(k, 0, 0)
data.importDataFromMat()
data.normalize()
sq = SquaredErrorLinearClassifier(2**10, k)
sq.train(data.train_left, data.train_right, data.train_cat)
results, cat = sq.classify(data.test_left, data.test_right)
sq.confusion_matrix(cat, data.test_cat.argmax(axis=0))
err = Error()
err, misclass = err.norm_total_error(results.T, data.test_cat, k)
print "Error on the test set "+str(err)
print "Misclassification ratio on the test set "+str(misclass)
def testLogisticError() :
k=5
data = Data(k, 0, 0)
data.importDataFromMat()
data.normalize()
lg = LogisticLinearClassifier(0.03, 0.03, 576, k, data)
err_train, miss_train, err_val, miss_val = lg.train(30)
mis_fig = plt.figure()
ax2 = mis_fig.add_subplot(111)
ax2.plot(err_val, label='error (validation)')
ax2.plot(err_train, label='error (training)')
title = "std(val)=%f std(err)=%f" % (sp.std(err_val), sp.std(err_train) )
mis_fig.suptitle(title)
ax2.set_ylabel('error')
ax2.set_xlabel('epoch')
plt.legend()
mis_fig = plt.figure()
ax2 = mis_fig.add_subplot(111)
ax2.plot(miss_val, label='misclassification ratio (validation)')
ax2.plot(miss_train, label='misclassification ratio (training)')
mis_fig.suptitle(title)
ax2.set_ylabel('misclassification ratio')
ax2.set_xlabel('epoch')
plt.legend()
results, cat = lg.classify(data.test_left, data.test_right)
lg.confusion_matrix(cat, data.test_cat.argmax(axis=0))
err = Error()
err, misclass = err.norm_total_error(results.T, data.test_cat, k)
print "Error on the test set "+str(err)
print "Misclassification ratio on the test set "+str(misclass)
def findMuNu() :
k=5
evalModel = ModelEvaluation()
evalModel.findNuMu(80, 60, 1, k)
def findMuNuLinearClassifier() :
k=5
evalModel = ModelEvaluation()
evalModel.findNuMuLinearClass(1, k)
def findH1H2() :
k=2
evalModel = ModelEvaluation()
evalModel.findNuMu(0.001, 0.1, 1, k)
def compareParameters() :
k=5
data = Data(k, 0, 0)
data.importDataFromMat()
data.normalize()
train = TrainerValidator(k, 40, 80, 60, 0.001, 0.1, 1, data)
train.trainAndClassify()
train2 = TrainerValidator(k, 40, 80, 60, 0.04, 0.1, 1, data)
train2.trainAndClassify()
train3 = TrainerValidator(k, 40, 80, 60, 0.1, 0.1, 1, data)
train3.trainAndClassify()
error_fig = plt.figure()
ax1 = error_fig.add_subplot(111)
ax1.plot(train.validation_error, label='validation error mu=0.1 nu=0.001')
ax1.plot(train.training_error, label='training error mu=0.1 nu=0.001')
ax1.plot(train2.validation_error, label='validation error mu=0.1 nu=0.04')
ax1.plot(train2.training_error, label='training error mu=0.1 nu=0.04')
ax1.plot(train3.validation_error, label='validation error mu=0.1 nu=0.1')
ax1.plot(train3.training_error, label='training error mu=0.1 nu=0.1')
ax1.set_ylabel('error')
ax1.set_xlabel('epoch')
title = "Validation and training errors k=5 H1=80 H2=60 batchsize=1"
error_fig.suptitle(title)
plt.legend()
main()