-
Notifications
You must be signed in to change notification settings - Fork 1
/
anfis_co.py
290 lines (225 loc) · 11 KB
/
anfis_co.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
import itertools
import numpy as np
from membership import mfDerivs
import copy
class ANFIS_CO:
"""
Class to implement an Adaptive Network-based Fuzzy Inference System with correlated residuals: ANFIS_CO"
Attributes:
X (input)
Y (output)
C (Covariance)
XLen
memClass
memFuncs
memFuncsByVariable
rules
consequents
errors
memFuncsHomo
trainingType
"""
def __init__(self, X, Y, C, memFunction):
self.X = np.array(copy.copy(X))
self.Y = np.array(copy.copy(Y))
self.C = np.array(copy.copy(C)) # the inverse of the covariance matrix over different sites
self.XLen = len(self.X)
self.memClass = copy.deepcopy(memFunction)
self.memFuncs = self.memClass.MFList
self.memFuncsByVariable = [[x for x in range(len(self.memFuncs[z]))] for z in range(len(self.memFuncs))]
self.rules = np.array(list(itertools.product(*self.memFuncsByVariable)))
self.consequents = np.empty(self.Y.ndim * len(self.rules) * (self.X.shape[1] + 1))
self.consequents.fill(0)
self.errors = np.empty(0)
self.memFuncsHomo = all(len(i)==len(self.memFuncsByVariable[0]) for i in self.memFuncsByVariable)
self.trainingType = 'Not trained yet'
def LSE(self, A, B, C, initialGamma = 1000.): # need to consider C as well
covMat = C
cholesky = np.linalg.cholesky(covMat)
cholesky = np.linalg.inv(cholesky)
coeffMat = np.dot(cholesky, A)
rhsMat = np.dot(cholesky, B.reshape(len(B), B.ndim))
S = np.eye(coeffMat.shape[1])*initialGamma
x = np.zeros((coeffMat.shape[1],1)) # need to correct for multi-dim B
for i in range(len(coeffMat[:,0])):
a = coeffMat[i,:]
b = np.sum(rhsMat[i])
b = np.array(b)
# b = np.array(rhsMat[i])
S = S - (np.array(np.dot(np.dot(np.dot(S,np.matrix(a).transpose()),np.matrix(a)),S)))/(1+(np.dot(np.dot(S,a),a)))
x = x + (np.dot(S,np.dot(np.matrix(a).transpose(),(np.matrix(b)-np.dot(np.matrix(a),x)))))
return x
def trainHybridJangOffLine(self, epochs=5, tolerance=1e-5, initialGamma=1000, k=0.01):
self.trainingType = 'trainHybridJangOffLine'
convergence = False
epoch = 1
while (epoch < epochs) and (convergence is not True):
#layer four: forward pass
[layerFour, wSum, w] = forwardHalfPass(self, self.X)
#layer five: least squares estimate
layerFive = np.array(self.LSE(layerFour,self.Y, self.C, initialGamma))
self.consequents = layerFive
layerFive = np.dot(layerFour,layerFive)
# error
# need to be changed
# error = np.sum((self.Y-layerFive.T)**2)
tmpY = self.Y.reshape(len(self.Y), self.Y.ndim)
tmpT = layerFive.T.reshape(len(self.Y), self.Y.ndim)
error = (tmpY-tmpT).transpose().dot(self.C).dot(tmpY-tmpT)
if self.Y.ndim == 1:
error = np.sum(error)
else:
error = error.trace()
print('current error: ', error)
average_error = np.average(np.absolute(self.Y-layerFive.T))
self.errors = np.append(self.errors,error)
if len(self.errors) != 0:
if self.errors[len(self.errors)-1] < tolerance:
convergence = True
# back propagation
if convergence is not True:
cols = range(len(self.X[0,:]))
dE_dAlpha = list(backprop(self, colX, cols, wSum, w, layerFive) for colX in range(self.X.shape[1]))
if len(self.errors) >= 4:
if (self.errors[-4] > self.errors[-3] > self.errors[-2] > self.errors[-1]):
k = k * 1.1
if len(self.errors) >= 5:
if (self.errors[-1] < self.errors[-2]) and (self.errors[-3] < self.errors[-2]) and (self.errors[-3] < self.errors[-4]) and (self.errors[-5] > self.errors[-4]):
k = k * 0.9
## handling of variables with a different number of MFs
t = []
for x in range(len(dE_dAlpha)):
for y in range(len(dE_dAlpha[x])):
for z in range(len(dE_dAlpha[x][y])):
t.append(dE_dAlpha[x][y][z])
eta = k / np.abs(np.sum(t))
if(np.isinf(eta)):
eta = k
## handling of variables with a different number of MFs
dAlpha = copy.deepcopy(dE_dAlpha)
if not(self.memFuncsHomo):
for x in range(len(dE_dAlpha)):
for y in range(len(dE_dAlpha[x])):
for z in range(len(dE_dAlpha[x][y])):
dAlpha[x][y][z] = -eta * dE_dAlpha[x][y][z]
else:
dAlpha = -eta * np.array(dE_dAlpha)
for varsWithMemFuncs in range(len(self.memFuncs)):
for MFs in range(len(self.memFuncsByVariable[varsWithMemFuncs])):
paramList = sorted(self.memFuncs[varsWithMemFuncs][MFs][1])
for param in range(len(paramList)):
self.memFuncs[varsWithMemFuncs][MFs][1][paramList[param]] = self.memFuncs[varsWithMemFuncs][MFs][1][paramList[param]] + dAlpha[varsWithMemFuncs][MFs][param]
epoch = epoch + 1
self.fittedValues = predict(self,self.X)
self.residuals = self.Y - self.fittedValues[:,0]
return self.fittedValues
def plotErrors(self):
if self.trainingType == 'Not trained yet':
print(self.trainingType)
else:
import matplotlib.pyplot as plt
plt.plot(range(len(self.errors)),self.errors,'ro', label='errors')
plt.ylabel('error')
plt.xlabel('epoch')
plt.show()
def plotMF(self, x, inputVar):
import matplotlib.pyplot as plt
from skfuzzy import gaussmf, gbellmf, sigmf
for mf in range(len(self.memFuncs[inputVar])):
if self.memFuncs[inputVar][mf][0] == 'gaussmf':
y = gaussmf(x,**self.memClass.MFList[inputVar][mf][1])
elif self.memFuncs[inputVar][mf][0] == 'gbellmf':
y = gbellmf(x,**self.memClass.MFList[inputVar][mf][1])
elif self.memFuncs[inputVar][mf][0] == 'sigmf':
y = sigmf(x,**self.memClass.MFList[inputVar][mf][1])
plt.plot(x,y,'r')
plt.show()
def plotResults(self):
if self.trainingType == 'Not trained yet':
print(self.trainingType)
else:
import matplotlib.pyplot as plt
plt.plot(range(len(self.fittedValues)),self.fittedValues,'r', label='trained')
plt.plot(range(len(self.Y)),self.Y,'b', label='original')
plt.legend(loc='upper left')
plt.show()
def forwardHalfPass(ANFISObj, Xs):
layerFour = np.empty(0,)
wSum = []
for pattern in range(len(Xs[:,0])):
#layer one
layerOne = ANFISObj.memClass.evaluateMF(Xs[pattern,:])
#layer two
miAlloc = [[layerOne[x][ANFISObj.rules[row][x]] for x in range(len(ANFISObj.rules[0]))] for row in range(len(ANFISObj.rules))]
layerTwo = np.array([np.product(x) for x in miAlloc]).T
if pattern == 0:
w = layerTwo
else:
w = np.vstack((w,layerTwo))
#layer three
wSum.append(np.sum(layerTwo))
if pattern == 0:
wNormalized = layerTwo/wSum[pattern]
else:
wNormalized = np.vstack((wNormalized,layerTwo/wSum[pattern]))
#prep for layer four (bit of a hack)
layerThree = layerTwo/wSum[pattern]
rowHolder = np.concatenate([x*np.append(Xs[pattern,:],1) for x in layerThree])
layerFour = np.append(layerFour,rowHolder)
w = w.T
wNormalized = wNormalized.T
layerFour = np.array(np.array_split(layerFour,pattern + 1))
return layerFour, wSum, w
## calculating the gradient descent, thus need to be changed.
def backprop(ANFISObj, columnX, columns, theWSum, theW, theLayerFive):
paramGrp = [0]* len(ANFISObj.memFuncs[columnX])
for MF in range(len(ANFISObj.memFuncs[columnX])):
parameters = np.empty(len(ANFISObj.memFuncs[columnX][MF][1]))
timesThru = 0
for alpha in sorted(ANFISObj.memFuncs[columnX][MF][1].keys()):
bucket3 = np.empty([len(ANFISObj.X), ANFISObj.Y.ndim])
for rowX in range(len(ANFISObj.X)):
varToTest = ANFISObj.X[rowX,columnX]
tmpRow = np.empty(len(ANFISObj.memFuncs))
tmpRow.fill(varToTest)
bucket2 = np.empty(ANFISObj.Y.ndim)
for colY in range(ANFISObj.Y.ndim):
rulesWithAlpha = np.array(np.where(ANFISObj.rules[:,columnX]==MF))[0]
adjCols = np.delete(columns,columnX)
senSit = mfDerivs.partial_dMF(ANFISObj.X[rowX,columnX],ANFISObj.memFuncs[columnX][MF],alpha)
# produces d_ruleOutput/d_parameterWithinMF
dW_dAplha = senSit * np.array([np.prod([ANFISObj.memClass.evaluateMF(tmpRow)[c][ANFISObj.rules[r][c]] for c in adjCols]) for r in rulesWithAlpha])
bucket1 = np.empty(len(ANFISObj.rules[:,0]))
for consequent in range(len(ANFISObj.rules[:,0])):
fConsequent = np.dot(np.append(ANFISObj.X[rowX,:],1.),ANFISObj.consequents[((ANFISObj.X.shape[1] + 1) * consequent):(((ANFISObj.X.shape[1] + 1) * consequent) + (ANFISObj.X.shape[1] + 1)),colY])
acum = 0
if consequent in rulesWithAlpha:
acum = dW_dAplha[np.where(rulesWithAlpha==consequent)] * theWSum[rowX]
acum = acum - theW[consequent,rowX] * np.sum(dW_dAplha)
acum = acum / theWSum[rowX]**2
bucket1[consequent] = fConsequent * acum
sum1 = np.sum(bucket1)
bucket2[colY] = sum1
bucket3[rowX, :] = bucket2
# sum3 = np.sum(bucket3)
if ANFISObj.Y.ndim == 1:
tmpY = ANFISObj.Y.reshape(len(ANFISObj.Y), 1)
tmpT = theLayerFive.reshape(len(ANFISObj.Y), 1)
sum3 = (-2)*(tmpY - tmpT).transpose().dot(ANFISObj.C).dot(bucket3)
sum3 = np.sum(sum3)
else:
tmpY = ANFISObj.Y.reshape(len(ANFISObj.Y), 1)
tmpT = theLayerFive.reshape(len(ANFISObj.Y), 1)
sum2 = (-2)*(tmpY - tmpT).transpose().dot(ANFISObj.C).dot(bucket3)
sum3 = sum2.trace()
parameters[timesThru] = sum3
timesThru = timesThru + 1
paramGrp[MF] = parameters
return paramGrp
def predict(ANFISObj, varsToTest):
[layerFour, wSum, w] = forwardHalfPass(ANFISObj, varsToTest)
#layer five
layerFive = np.dot(layerFour,ANFISObj.consequents)
return layerFive
if __name__ == "__main__":
print("I am main!")