-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
148 lines (126 loc) · 3.86 KB
/
train.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
import neuralnet
import json
import pickle
#Trains the neural net on the data stored in the testdata.pkl file
#Trains the net to do basic digit recognition
def Indicator(n):
if n > .5: return 1
else : return 0
def testNet():
"Tests the net against inputs ranging from -100,-100 to 100,100"
right = 0
wrong = 0
expectedList = []
actualList = []
i = 0
for dataPoint in testData:
myNet.ClearNodes()
myNet.SetInputs(dataPoint[0])
outputs = map (Indicator,myNet.ComputeOutput())
expected = dataPoint[1]
if expected == outputs:
right += 1
else:
wrong += 1
if i%100 == 0:
print "expected:", expected, "actual:", outputs
i += 1
print "RIGHT:", right
print "WRONG:", wrong
def trainNetDistr(testData,associatedWeights):
"Trains the net giving a particular list of data and associated weights for the test data"
myNet.ResetNet()
length = len(testData)
print max(associatedWeights)
print min(associatedWeights)
for j in range(150): #iterate 120 times over the data set
print "Training iteration", j
if j%10 == 0:
testNet()
if j%50 == 0:
pass
for i in range(length):
data = testData[i]
featureVec = data[0]
target = data[1]
weight = (length/40.0)*associatedWeights[i]
myNet.TrainNet(featureVec,target,weight)
def calcNewWeights(testData,associatedWeights):
"""
Calculates the pseuduo loss and uses it to calculate the new weights;
just a possibly incorrect implementation of the adaboosting algorithm as described at this link:
http://www.mitpressjournals.org/doi/pdf/10.1162/089976600300015178
"""
outputs = []
pseudoError = 0
for i in range(len(testData)):
data = testData[i]
featureVec = data[0]
targets = data[1]
weight = associatedWeights[i]
myNet.ClearNodes()
myNet.SetInputs(featureVec)
outputList = myNet.ComputeOutput()
outputs.append(outputList)
k = 1
for i in range(len(targets)):
target = targets[i]
output = outputList[i]
if target == 1:
k += -output
else:
k += output
pseudoError += weight*k
if pseudoError < 0:
print k
print weight
print pseudoError
alpha = pseudoError/(1-pseudoError)
if alpha < 0:
print alpha
return False
#create newWeights
newWeights = []
for i in range(len(associatedWeights)):
target = testData[i][1]
output = outputs[i]
weight = associatedWeights[i]
k = 0
for i in range(len(targets)):
target = targets[i]
output = outputList[i]
if target == 1:
k += output
else:
k += -output
j = alpha**k
newWeight = weight*j
newWeights.append(newWeight)
#normalize the newWeights
sumOfWeights = sum(newWeights)
if sumOfWeights == 0:
return False #end algorithm here
normalizer = 1/sumOfWeights
for i in range(len(newWeights)):
newWeights[i] = newWeights[i]*normalizer
#print newWeights
length = len(testData)
#print associatedWeights[:20]
#print newWeights[:20]
return newWeights
myNet = neuralnet.NeuralNet()
myNet.ConstructNet(121,[22,50,10])
dataFile = open("testdata.pkl","r")
testData = json.loads(dataFile.read())
print testData[:10]
testWeights = [1.0/len(testData)]*len(testData) #creates a list of 1/n
#adaboosting algorithm
for i in range(5):
myNet.StoreData()
trainNetDistr(testData,testWeights)
testNet()
testWeights = calcNewWeights(testData, testWeights)
if testWeights == False:
break
myNet.StoreData()
testNet()