/
neuralnetworks.py
422 lines (356 loc) · 15.6 KB
/
neuralnetworks.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
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
import numpy as np
import scaledconjugategradient as scg
import mlutils as ml # for draw()
from copy import copy
import sys # for sys.float_info.epsilon
import pdb
######################################################################
### class NeuralNetwork
######################################################################
class NeuralNetwork:
def __init__(self, ni,nhs,no):
if nhs == 0 or nhs == [0] or nhs is None or nhs == [None]:
nhs = None
else:
try:
nihs = [ni] + list(nhs)
except:
nihs = [ni] + [nhs]
nhs = [nhs]
if nhs is not None:
self.Vs = [(np.random.uniform(-1,1,size=(1+nihs[i],nihs[i+1])) / np.sqrt(nihs[i])) for i in range(len(nihs)-1)]
self.W = np.zeros((1+nhs[-1],no))
# self.W = (np.random.uniform(-1,1,size=(1+nhs[-1],no)) / np.sqrt(nhs[-1]))
else:
self.Vs = None
self.W = np.zeros((1+ni,no))
# self.W = 0*np.random.uniform(-1,1,size=(1+ni,no)) / np.sqrt(ni)
self.ni,self.nhs,self.no = ni,nhs,no
self.Xmeans = None
self.Xstds = None
self.Tmeans = None
self.Tstds = None
self.trained = False
self.reason = None
self.errorTrace = None
self.numberOfIterations = None
def train(self,X,T,nIterations=100,verbose=False,
weightPrecision=0,errorPrecision=0):
if self.Xmeans is None:
self.Xmeans = X.mean(axis=0)
self.Xstds = X.std(axis=0)
self.Xconstant = self.Xstds == 0
self.XstdsFixed = copy(self.Xstds)
self.XstdsFixed[self.Xconstant] = 1
X = self._standardizeX(X)
if T.ndim == 1:
T = T.reshape((-1,1))
if self.Tmeans is None:
self.Tmeans = T.mean(axis=0)
self.Tstds = T.std(axis=0)
self.Tconstant = self.Tstds == 0
self.TstdsFixed = copy(self.Tstds)
self.TstdsFixed[self.Tconstant] = 1
T = self._standardizeT(T)
# Local functions used by scg()
def objectiveF(w):
self._unpack(w)
Y,_ = self._forward_pass(X)
return 0.5 * np.mean((Y - T)**2)
def gradF(w):
self._unpack(w)
Y,Z = self._forward_pass(X)
delta = (Y - T) / (X.shape[0] * T.shape[1])
dVs,dW = self._backward_pass(delta,Z)
return self._pack(dVs,dW)
scgresult = scg.scg(self._pack(self.Vs,self.W), objectiveF, gradF,
xPrecision = weightPrecision,
fPrecision = errorPrecision,
nIterations = nIterations,
verbose=verbose,
ftracep=True)
self._unpack(scgresult['x'])
self.reason = scgresult['reason']
self.errorTrace = np.sqrt(scgresult['ftrace']) # * self.Tstds # to unstandardize the MSEs
self.numberOfIterations = len(self.errorTrace)
self.trained = True
return self
def use(self,X,allOutputs=False):
Xst = self._standardizeX(X)
Y,Z = self._forward_pass(Xst)
Y = self._unstandardizeT(Y)
if Z is None:
return (Y,None) if allOutputs else Y
else:
return (Y,Z[1:]) if allOutputs else Y
def getNumberOfIterations(self):
return self.numberOfIterations
def getErrorTrace(self):
return self.errorTrace
def draw(self,inputNames = None, outputNames = None):
ml.draw(self.Vs+[self.W], inputNames, outputNames)
def _forward_pass(self,X):
if self.nhs is None:
# no hidden units, just linear output layer
Y = np.dot(X, self.W[1:,:]) + self.W[0:1,:]
Zs = [X]
else:
Zprev = X
Zs = [Zprev]
for i in range(len(self.nhs)):
V = self.Vs[i]
Zprev = np.tanh(np.dot(Zprev,V[1:,:]) + V[0:1,:])
Zs.append(Zprev)
Y = np.dot(Zprev, self.W[1:,:]) + self.W[0:1,:]
return Y, Zs
def _backward_pass(self,delta,Z):
if self.nhs is None:
# no hidden units, just linear output layer
dW = np.vstack((np.dot(np.ones((1,delta.shape[0])),delta), np.dot( Z[0].T, delta)))
dVs = None
else:
dW = np.vstack((np.dot(np.ones((1,delta.shape[0])),delta), np.dot( Z[-1].T, delta)))
dVs = []
delta = (1-Z[-1]**2) * np.dot( delta, self.W[1:,:].T)
for Zi in range(len(self.nhs),0,-1):
Vi = Zi - 1 # because X is first element of Z
dV = np.vstack(( np.dot(np.ones((1,delta.shape[0])), delta),
np.dot( Z[Zi-1].T, delta)))
dVs.insert(0,dV)
delta = np.dot( delta, self.Vs[Vi][1:,:].T) * (1-Z[Zi-1]**2)
return dVs,dW
def _standardizeX(self,X):
result = (X - self.Xmeans) / self.XstdsFixed
result[:,self.Xconstant] = 0.0
return result
def _unstandardizeX(self,Xs):
return self.Xstds * Xs + self.Xmeans
def _standardizeT(self,T):
result = (T - self.Tmeans) / self.TstdsFixed
result[:,self.Tconstant] = 0.0
return result
def _unstandardizeT(self,Ts):
return self.Tstds * Ts + self.Tmeans
def _pack(self,Vs,W):
if Vs is None:
return np.array(W.flat)
else:
return np.hstack([V.flat for V in Vs] + [W.flat])
def _unpack(self,w):
if self.nhs is None:
self.W[:] = w.reshape((self.ni+1, self.no))
else:
first = 0
numInThisLayer = self.ni
for i in range(len(self.Vs)):
self.Vs[i][:] = w[first:first+(numInThisLayer+1)*self.nhs[i]].reshape((numInThisLayer+1,self.nhs[i]))
first += (numInThisLayer+1) * self.nhs[i]
numInThisLayer = self.nhs[i]
self.W[:] = w[first:].reshape((numInThisLayer+1,self.no))
def __repr__(self):
str = 'NeuralNetwork({}, {}, {})'.format(self.ni,self.nhs,self.no)
# str += ' Standardization parameters' + (' not' if self.Xmeans == None else '') + ' calculated.'
if self.trained:
str += '\n Network was trained for {} iterations. Final error is {}.'.format(self.numberOfIterations,
self.errorTrace[-1])
else:
str += ' Network is not trained.'
return str
######################################################################
### class NeuralNetworkClassifier
######################################################################
def makeIndicatorVars(T):
""" Assumes argument is N x 1, N samples each being integer class label """
return (T == np.unique(T)).astype(int)
class NeuralNetworkClassifier(NeuralNetwork):
def __init__(self,ni,nhs,no):
#super(NeuralNetworkClassifier,self).__init__(ni,nh,no)
NeuralNetwork.__init__(self,ni,nhs,no-1)
def _multinomialize(self,Y):
# fix to avoid overflow
mx = max(0,np.max(Y))
expY = np.exp(Y-mx)
# print('mx',mx)
denom = np.exp(-mx) + np.sum(expY,axis=1).reshape((-1,1)) + sys.float_info.epsilon
Y = np.hstack((expY / denom, np.exp(-mx)/denom))
return Y
def train(self,X,T,
nIterations=100,weightPrecision=0,errorPrecision=0,verbose=False):
if self.Xmeans is None:
self.Xmeans = X.mean(axis=0)
self.Xstds = X.std(axis=0)
self.Xconstant = self.Xstds == 0
self.XstdsFixed = copy(self.Xstds)
self.XstdsFixed[self.Xconstant] = 1
X = self._standardizeX(X)
self.classes, counts = np.unique(T,return_counts=True)
self.mostCommonClass = self.classes[np.argmax(counts)] # to break ties
if self.no != len(self.classes)-1:
raise ValueError(" In NeuralNetworkClassifier, the number of outputs must be one less than\n the number of classes in the training data. The given number of outputs\n is %d and number of classes is %d. Try changing the number of outputs in the\n call to NeuralNetworkClassifier()." % (self.no, len(self.classes)))
T = makeIndicatorVars(T)
# Local functions used by gradientDescent.scg()
def objectiveF(w):
self._unpack(w)
Y,_ = self._forward_pass(X)
Y = self._multinomialize(Y)
Y[Y==0] = sys.float_info.epsilon
return -np.mean(T * np.log(Y))
def gradF(w):
self._unpack(w)
Y,Z = self._forward_pass(X)
Y = self._multinomialize(Y)
delta = (Y[:,:-1] - T[:,:-1]) / (X.shape[0] * (T.shape[1]-1))
dVs,dW = self._backward_pass(delta,Z)
return self._pack(dVs,dW)
scgresult = scg.scg(self._pack(self.Vs,self.W), objectiveF, gradF,
xPrecision = weightPrecision,
fPrecision = errorPrecision,
nIterations = nIterations,
ftracep=True,
verbose=verbose)
self._unpack(scgresult['x'])
self.reason = scgresult['reason']
self.errorTrace = scgresult['ftrace']
self.numberOfIterations = len(self.errorTrace) - 1
self.trained = True
return self
def use(self,X,allOutputs=False):
Xst = self._standardizeX(X)
Y,Z = self._forward_pass(Xst)
Y = self._multinomialize(Y)
classes = self.classes[np.argmax(Y,axis=1)].reshape((-1,1))
# If any row has all equal values, then all classes have same probability.
# Let's return the most common class in these cases
classProbsEqual = (Y == Y[:,0:1]).all(axis=1)
if sum(classProbsEqual) > 0:
classes[classProbsEqual] = self.mostCommonClass
if Z is None:
return (classes,Y,None) if allOutputs else classes
else:
return (classes,Y,Z[1:]) if allOutputs else classes
######################################################################
### Test code, not run when this file is imported
######################################################################
if __name__== "__main__":
import matplotlib.pyplot as plt
plt.ion() # for use in ipython
print( '\n------------------------------------------------------------')
print( "Regression Example: Approximate f(x) = 1.5 + 0.6 x + 0.4 sin(x)")
# print( ' Neural net with 1 input, 5 hidden units, 1 output')
nSamples = 10
X = np.linspace(0,10,nSamples).reshape((-1,1))
T = 1.5 + 0.6 * X + 0.8 * np.sin(1.5*X)
T[np.logical_and(X > 2, X < 3)] *= 3
T[np.logical_and(X > 5, X < 7)] *= 3
nSamples = 100
Xtest = np.linspace(0,10,nSamples).reshape((-1,1)) + 10.0/nSamples/2
Ttest = 1.5 + 0.6 * Xtest + 0.8 * np.sin(1.5*Xtest) + np.random.uniform(-2,2,size=(nSamples,1))
Ttest[np.logical_and(Xtest > 2, Xtest < 3)] *= 3
Ttest[np.logical_and(Xtest > 5,Xtest < 7)] *= 3
# # nnet = NeuralNetwork(1,(5,4,3,2),1)
# # nnet = NeuralNetwork(1,(10,2,10),1)
# # nnet = NeuralNetwork(1,(5,5),1)
nnet = NeuralNetwork(1,(3,3,3,3),1)
nnet.train(X,T,errorPrecision=1.e-10,weightPrecision=1.e-10,nIterations=1000)
print( "scg stopped after",nnet.getNumberOfIterations(),"iterations:",nnet.reason)
Y = nnet.use(X)
Ytest,Ztest = nnet.use(Xtest, allOutputs=True)
print( "Final RMSE: train", np.sqrt(np.mean((Y-T)**2)),"test",np.sqrt(np.mean((Ytest-Ttest)**2)))
# import time
# t0 = time.time()
# for i in range(100000):
# Ytest,Ztest = nnet.use(Xtest, allOutputs=True)
# print( 'total time to make 100000 predictions:',time.time() - t0)
# print( 'Inputs, Targets, Estimated Targets')
# print( np.hstack((X,T,Y)))
plt.figure(1)
plt.clf()
nHLayers = len(nnet.nhs)
nPlotRows = 3 + nHLayers
plt.subplot(nPlotRows,2,1)
plt.plot(nnet.getErrorTrace())
plt.xlabel('Iterations');
plt.ylabel('RMSE')
plt.title('Regression Example')
plt.subplot(nPlotRows,2,3)
plt.plot(X,T,'o-')
plt.plot(X,Y,'o-')
plt.text(8,12, 'Layer {}'.format(nHLayers+1))
plt.legend(('Train Target','Train NN Output'),loc='lower right',
prop={'size':9})
plt.subplot(nPlotRows,2,5)
plt.plot(Xtest,Ttest,'o-')
plt.plot(Xtest,Ytest,'o-')
plt.xlim(0,10)
plt.text(8,12, 'Layer {}'.format(nHLayers+1))
plt.legend(('Test Target','Test NN Output'),loc='lower right',
prop={'size':9})
colors = ('blue','green','red','black','cyan','orange')
for i in range(nHLayers):
layer = nHLayers-i-1
plt.subplot(nPlotRows,2,i*2+7)
plt.plot(Xtest,Ztest[layer]) #,color=colors[i])
plt.xlim(0,10)
plt.ylim(-1.1,1.1)
plt.ylabel('Hidden Units')
plt.text(8,0, 'Layer {}'.format(layer+1))
plt.subplot(2,2,2)
nnet.draw(['x'],['sine'])
plt.draw()
# Now train multiple nets to compare error for different numbers of hidden layers
if False: # make True to run multiple network experiment
def experiment(hs,nReps,nIter,X,T,Xtest,Ytest):
results = []
for i in range(nReps):
nnet = NeuralNetwork(1,hs,1)
nnet.train(X,T,weightPrecision=0,errorPrecision=0,nIterations=nIter)
# print( "scg stopped after",nnet.getNumberOfIterations(),"iterations:",nnet.reason)
(Y,Z) = nnet.use(X, allOutputs=True)
Ytest = nnet.use(Xtest)
rmsetrain = np.sqrt(np.mean((Y-T)**2))
rmsetest = np.sqrt(np.mean((Ytest-Ttest)**2))
results.append([rmsetrain,rmsetest])
return results
plt.figure(2)
plt.clf()
results = []
# hiddens [ [5]*i for i in range(1,6) ]
hiddens = [[12], [6,6], [4,4,4], [3,3,3,3], [2,2,2,2,2,2],
[24], [12]*2, [8]*3, [6]*4, [4]*6, [3]*8, [2]*12]
for hs in hiddens:
r = experiment(hs,30,100,X,T,Xtest,Ttest)
r = np.array(r)
means = np.mean(r,axis=0)
stds = np.std(r,axis=0)
results.append([hs,means,stds])
print( hs, means,stds)
rmseMeans = np.array([x[1].tolist() for x in results])
plt.clf()
plt.plot(rmseMeans,'o-')
ax = plt.gca()
plt.xticks(range(len(hiddens)),[str(h) for h in hiddens])
plt.setp(plt.xticks()[1], rotation=30)
plt.ylabel('Mean RMSE')
plt.xlabel('Network Architecture')
print( '\n------------------------------------------------------------')
print( "Classification Example: XOR, approximate f(x1,x2) = x1 xor x2")
print( ' Using neural net with 2 inputs, 3 hidden units, 2 outputs')
X = np.array([[0,0],[1,0],[0,1],[1,1]])
T = np.array([[1],[2],[2],[1]])
nnet = NeuralNetworkClassifier(2,(4,),2)
nnet.train(X,T,weightPrecision=1.e-10,errorPrecision=1.e-10,nIterations=100)
print( "scg stopped after",nnet.getNumberOfIterations(),"iterations:",nnet.reason)
(classes,y,Z) = nnet.use(X, allOutputs=True)
print( 'X(x1,x2), Target Classses, Predicted Classes')
print( np.hstack((X,T,classes)))
print( "Hidden Outputs")
print( Z)
plt.figure(3)
plt.clf()
plt.subplot(2,1,1)
plt.plot(np.exp(-nnet.getErrorTrace()))
plt.xlabel('Iterations');
plt.ylabel('Likelihood')
plt.title('Classification Example')
plt.subplot(2,1,2)
nnet.draw(['x1','x2'],['xor'])