-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuralnet.py
295 lines (249 loc) · 9.81 KB
/
neuralnet.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
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
#############################################################
# The Basic Node Class #
#############################################################
import math
import random
import pickle
#Constant to determine how much to change the weights
global N
N = 0.05
global c
c = 0
class NeuralNet:
def __init__(self):
#An arbitrary number of layers; in reality we only use four
self.Lists = []#list containing all the layers in the neural net
self.rootNodes = []
def StoreData(self):
"Pickles itself into net_.pkl (stores the net into a file for future use)"
global c
output = open("net" + str(c) + ".pkl","wb")
pickle.dump(self,output)
output.close()
c += 1
def SetInputs(self, featureVector):
"Takes a feature vector and set them as the inputs"
inputLayer = self.Lists[0]
for i in range(len(inputLayer)):
node = inputLayer[i]
feature = featureVector[i]
node.SetOutput(feature)
def ClearNodes(self):
"Sets all node outputs to None"
for layer in self.Lists:
for node in layer:
node.Clear()
def ResetNet(self):
"Resets each node's weights"
for layer in self.Lists:
for node in layer:
node.ResetWeights()
def ComputeOutput(self):
"Computes the output of the neural net"
output = []
for rootNode in self.rootNodes:
output.append(rootNode.GetOutput())
return output
def TrainNet(self,featureVector, targets, constantN=None):
"Trains the net given a feature vector and a target"
global N
if constantN == None:
N = .05
else:
N = constantN
self.ClearNodes()
self.SetInputs(featureVector)
for i in range(len(targets)):
self.rootNodes[i].SetTarget(targets[i])
for layer in self.Lists:
for node in layer:
node.GetWeightChange()
for layer in self.Lists:
for node in layer:
node.ApplyWeightChange()
def ConstructNet(self, dimensions,netSpec):
"""
Constructs a net given the number of dimensions in a feature space
netSpec argument is the number of nodes in each layer
e.g. a netSpec of = [10,20,5] would yield a net with 10 nodes in layer 1
20 nodes in layer 2, and 5 nodes in layer 3
i.e. the number of features we will be evaluating
"""
#First layer is the input nodes; their values will be arbitrarily set
newLayer = []
for i in range(dimensions):
newLayer.append(Node())
self.Lists.append(newLayer)
previousLayer = newLayer
for i in range(len(netSpec)):
newLayer = []
for n in range(netSpec[i]):
newNode = Node(previousLayer)
newLayer.append(newNode)
self.Lists.append(newLayer)
previousLayer = newLayer
self.rootNodes = self.Lists[-1]
class Node:
def __init__(self,inputNodeList = None, defaultWeights = None):
#We generate self.outputnodes automatically when building the neural net
self.outputnodes = []
self.inputs = [] #the input values to the node; we generate these automatically
self.output = None #the output of the node
self.delta = None #the delta variable in backprop
self.target = None #only used for the root node; the target value we are trying to get
self.weightchange = [] #list that stores the changes to the weight we should apply
self.weights = [] #list of the current weights
if not inputNodeList:
#A node on the output layer
self.inputNodes = []
elif not defaultWeights:
#Build a set of default weights randomly for each thingy
self.inputNodes = inputNodeList
k = 1
for i in range(len(inputNodeList)):
# if i%3 == 0:
# randomWeight = (.3*random.random()-.15)
# elif i%3 == 1:
# randomWeight = (.3*random.random()-.75)
# else:
# randomWeight = (.3*random.random()+.45)
randomWeight = (.3*random.random()-.15)
self.weights.append(randomWeight)
else:
#set inputNodes, weights if they are both the same length
if (len(defaultWeights) != len(inputNodeList)):
raise Exception("Weights passed in not the same length as inputNodeList passed in!")
self.weights = defaultWeights
self.inputNodes = inputNodeList
for inputNode in self.inputNodes:
inputNode.outputnodes.append(self)
#############################################################
# TRAINING CODE #
#############################################################
def ResetWeights(self):
"Resets the weights to a random distribution with a gap (Not sure what the best initial weights are!)"
length = len(self.weights)
self.weights = []
k = -1
for i in range(length):
#if i%3 == 0:
# randomWeight = (.3*random.random()-.15)
#elif i%3 == 1:
# randomWeight = (.3*random.random()-.105)
#else:
# randomWeight = (.3*random.random()+.75)
randomWeight = (.3*random.random()-.15)
self.weights.append(randomWeight)
def SetTarget(self,target):
"Sets the target of the node (only used for the root node!)"
self.target = target
def ComputeWeightChange(self):
"Computes the change in weights"
if self.weightchange != []:
return False
for i in range(len(self.inputNodes)):
inputNode = self.inputNodes[i]
a = inputNode.GetOutput()
d = self.GetDelta()
weightChange = N*d*a
self.weightchange.append(weightChange)
def GetWeightChange(self):
"Returns the change in weights, or computes it first and then returns it"
if self.weightchange == []:
self.ComputeWeightChange()
return self.weightchange
def ApplyWeightChange(self):
"Applies the change in weights, or computes it first and then applies it"
if self.weightchange == []:
self.ComputeWeightChange()
for i in range(len(self.weightchange)):
weightChange = self.weightchange[i]
self.weights[i] += weightChange
#WHAT IS GOING ON BELOW?????
g = 0
for k in self.weightchange:
g += k
def ComputeDelta(self):
"Computes the delta value for backprop"
if self.delta != None:
return False
if self.output == None:
self.ComputeOutput()
u = Node.SigmoidDerivative(self.output)
if self.outputnodes == []:
#the output node!
t = self.target
a = self.GetOutput()
self.delta = u*(t-a)
else:
recurse = 0
for i in range(len(self.outputnodes)):
node = self.outputnodes[i]
index = node.inputNodes.index(self)
w = node.weights[index]
delta = node.GetDelta()
recurse += delta*w
self.delta = u*recurse
def GetDelta(self):
"Returns the delta value, or computes the value and then returns it"
if self.delta == None:
self.ComputeDelta()
return self.delta
#############################################################
# CALCULATE OUTPUT CODE #
#############################################################
def ComputeOutput(self):
"Computes the output of the node"
if self.output != None:
return False
if self.inputs == []:
self.GetInput()
output = 0
for i in range(len(self.inputs)):
val = self.inputs[i]
weight = self.weights[i]
output += val*weight
self.output = output
return True
def SetOutput(self,arbitraryOutput):
"Arbitrarily sets the output of the node; used to set the inputs to the neural net"
self.output = arbitraryOutput
def Clear(self):
"Clears the output, input, delta, weightchange, and target attributes"
self.inputs = []
self.output = None
self.delta = None
self.target = None
self.weightchange = []
def GetInput(self):
"Calls GetOutput of all the feeder nodes and builds a vector out of them"
for inputNode in self.inputNodes:
self.inputs.append(inputNode.GetOutput())
pass
def GetOutput(self):
"Returns the output of the current node, or computes it"
if self.output == None:
self.ComputeOutput()
if self.inputNodes == []:
return self.output # if it is an input node then doing do a sigmoid on it!
return self.Sigmoid(self.output)
#############################################################
# USEFUL STATIC FUNCTIONS #
#############################################################
@staticmethod
def Sigmoid(n):
"Returns the sigmoid function"
if n < -200:
return 0
if n > 200:
return 1
return 1/(1+math.e**(-n))
@staticmethod
def SigmoidDerivative(n):
"Returns the derivative of the sigmoid function"
return Node.Sigmoid(n)*(1 - Node.Sigmoid(n))
def LoadNet():
"Loads a net from net.pkl"
inputFile = open("net.pkl","rb")
newNet = pickle.load(inputFile)
return newNet