-
Notifications
You must be signed in to change notification settings - Fork 0
/
anfis_Mandami.py
469 lines (392 loc) · 16.4 KB
/
anfis_Mandami.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
ANFIS in torch: the ANFIS layers
@author: James Power <james.power@mu.ie> Apr 12 18:13:10 2019
Acknowledgement: twmeggs' implementation of ANFIS in Python was very
useful in understanding how the ANFIS structures could be interpreted:
https://github.com/twmeggs/anfis
'''
import itertools
from collections import OrderedDict
import numpy as np
import math
import torch
import torch.nn.functional as F
from torch.autograd import Variable
from sklearn.preprocessing import MinMaxScaler
dtype = torch.float
class FuzzifyVariable(torch.nn.Module):
'''
Represents a single fuzzy variable, holds a list of its MFs.
Forward pass will then fuzzify the input (value for each MF).
'''
def __init__(self, mfdefs):
super(FuzzifyVariable, self).__init__()
if isinstance(mfdefs, list): # No MF names supplied
mfnames = ['mf{}'.format(i) for i in range(len(mfdefs))]
mfdefs = OrderedDict(zip(mfnames, mfdefs))
self.mfdefs = torch.nn.ModuleDict(mfdefs)
self.padding = 0
self.nterm = (len(mfdefs))
@property
def num_mfs(self):
'''Return the actual number of MFs (ignoring any padding)'''
return len(self.mfdefs)
def members(self):
'''
Return an iterator over this variables's membership functions.
Yields tuples of the form (mf-name, MembFunc-object)
'''
return self.mfdefs.items()
def pad_to(self, new_size):
'''
Will pad result of forward-pass (with zeros) so it has new_size,
i.e. as if it had new_size MFs.
'''
self.padding = new_size - len(self.mfdefs)
def fuzzify(self, x):
'''
Yield a list of (mf-name, fuzzy values) for these input values.
'''
for mfname, mfdef in self.mfdefs.items():
yvals = mfdef(x)
yield(mfname, yvals)
def forward(self, x):
'''
Return a tensor giving the membership value for each MF.
x.shape: n_cases
y.shape: n_cases * n_mfs
'''
y_pred = torch.cat([mf(x) for mf in self.mfdefs.values()], dim=1)
if self.padding > 0:
y_pred = torch.cat([y_pred,
torch.zeros(x.shape[0], self.padding)], dim=1)
return y_pred
class FuzzifyLayer(torch.nn.Module):
'''
A list of fuzzy variables, representing the inputs to the FIS.
Forward pass will fuzzify each variable individually.
We pad the variables so they all seem to have the same number of MFs,
as this allows us to put all results in the same tensor.
'''
def __init__(self, varmfs, varnames=None):
super(FuzzifyLayer, self).__init__()
if not varnames:
self.varnames = ['x{}'.format(i) for i in range(len(varmfs))]
else:
self.varnames = list(varnames)
maxmfs = max([var.num_mfs for var in varmfs])
for var in varmfs:
var.pad_to(maxmfs)
self.varmfs = torch.nn.ModuleDict(zip(self.varnames, varmfs))
@property
def num_in(self):
'''Return the number of input variables'''
return len(self.varmfs)
@property
def max_mfs(self):
''' Return the max number of MFs in any variable'''
return max([var.num_mfs for var in self.varmfs.values()])
def __repr__(self):
'''
Print the variables, MFS and their parameters (for info only)
'''
r = ['Input variables']
for varname, members in self.varmfs.items():
r.append('Variable {}'.format(varname))
for mfname, mfdef in members.mfdefs.items():
r.append('- {}: {}({})'.format(mfname,
mfdef.__class__.__name__,
', '.join(['{}={}'.format(n, p.item())
for n, p in mfdef.named_parameters()])))
##############################################################
for n, p in mfdef.named_parameters():
print('n',n)
print('p',p)
##############################################################
return '\n'.join(r)
def forward(self, x):
''' Fuzzyify each variable's value using each of its corresponding mfs.
x.shape = n_cases * n_in
y.shape = n_cases * n_in * n_mfs
'''
assert x.shape[1] == self.num_in,\
'{} is wrong no. of input values'.format(self.num_in)
y_pred = torch.stack([var(x[:, i:i+1])
for i, var in enumerate(self.varmfs.values())],
dim=1)
return y_pred
class AntecedentLayer(torch.nn.Module):
'''
Form the 'rules' by taking all possible combinations of the MFs
for each variable. Forward pass then calculates the fire-strengths.
'''
def __init__(self, varlist):
super(AntecedentLayer, self).__init__()
# Count the (actual) mfs for each variable:
mf_count = [var.num_mfs for var in varlist]
# Now make the MF indices for each rule:
mf_indices = itertools.product(*[range(n) for n in mf_count])
self.mf_indices = torch.tensor(list(mf_indices))
# mf_indices.shape is n_rules * n_in
def num_rules(self):
return len(self.mf_indices)
def extra_repr(self, varlist=None):
if not varlist:
return None
row_ants = []
mf_count = [len(fv.mfdefs) for fv in varlist.values()]
for rule_idx in itertools.product(*[range(n) for n in mf_count]):
thisrule = []
for (varname, fv), i in zip(varlist.items(), rule_idx):
thisrule.append('{} is {}'
.format(varname, list(fv.mfdefs.keys())[i]))
row_ants.append(' and '.join(thisrule))
return '\n'.join(row_ants)
def forward(self, x):
''' Calculate the fire-strength for (the antecedent of) each rule
x.shape = n_cases * n_in * n_mfs
y.shape = n_cases * n_rules
'''
# Expand (repeat) the rule indices to equal the batch size:
batch_indices = self.mf_indices.expand((x.shape[0], -1, -1))
# Then use these indices to populate the rule-antecedents
ants = torch.gather(x.transpose(1, 2), 1, batch_indices)
# ants.shape is n_cases * n_rules * n_in
# Last, take the AND (= product) for each rule-antecedent
rules = torch.prod(ants, dim=2)
'''
ii = 0
list_fire_rule = []
rules_det = rules.detach().numpy()
while ii<len(rules):
list_fire_rule.append(rules_det[ii])
ii = ii + 1
list_fire_rule = np.array(list_fire_rule)
'''
return rules
class ConsequentLayer(torch.nn.Module):
'''
A simple linear layer to represent the TSK consequents.
Hybrid learning, so use MSE (not BP) to adjust coefficients.
Hence, coeffs are no longer parameters for backprop.
'''
def __init__(self, d_in, d_rule, d_out, n_mfdefs, n_terms):
super(ConsequentLayer, self).__init__()
c_shape = torch.Size([d_rule, d_out, 1])
self._coeff = torch.zeros(c_shape, dtype=dtype, requires_grad=True)
self.d_out = d_out
self.n_mfdefs = n_mfdefs
self.n_terms = n_terms
@property
def coeff(self):
'''
Record the (current) coefficients for all the rules
coeff.shape: n_rules * n_out * (n_in+1)
'''
return self._coeff
@coeff.setter
def coeff(self, new_coeff):
'''
Record new coefficients for all the rules
coeff: for each rule, for each output variable:
a coefficient for each input variable, plus a constant
'''
comb = math.pow(self.n_terms, self.n_mfdefs)
new_coeff = new_coeff.unsqueeze(0).view(int(comb), self.d_out, 1)
assert new_coeff.shape == self.coeff.shape, \
'Coeff shape should be {}, but is actually {}'\
.format(self.coeff.shape, new_coeff.shape)
self._coeff = new_coeff
def fit_coeff(self, x, weights, y_actual):
'''
Use LSE to solve for coeff: y_actual = coeff * (weighted)x
x.shape: n_cases * n_in
weights.shape: n_cases * n_rules
[ coeff.shape: n_rules * n_out * (n_in+1) ]
y.shape: n_cases * n_out
'''
# Append 1 to each list of input vals, for the constant term:
#x_plus = torch.cat([x, torch.ones(x.shape[0], 1)], dim=1)
x_plus = torch.ones(len(x), 1)
# Shape of weighted_x is n_cases * n_rules * (n_in+1)
weighted_x = torch.einsum('bp, bq -> bpq', weights, x_plus)
# Can't have value 0 for weights, or LSE won't work:
weighted_x[weighted_x == 0] = 1e-12
# Squash x and y down to 2D matrices for gels:
weighted_x_2d = weighted_x.view(weighted_x.shape[0], -1)
y_actual_2d = y_actual.view(y_actual.shape[0], -1)
# Use gels to do LSE, then pick out the solution rows:
try:
#coeff_2d, _ = torch.gels(y_actual_2d, weighted_x_2d)
coeff_2d, _ = torch.lstsq(y_actual_2d, weighted_x_2d)
except RuntimeError as e:
print('Internal error in gels', e)
print('Weights are:', weighted_x)
raise e
coeff_2d = coeff_2d[0:weighted_x_2d.shape[1]]
# Reshape to 3D tensor: divide by rules, n_in+1, then swap last 2 dims
#self.coeff = coeff_2d.view(weights.shape[1], x.shape[1]+1, -1).transpose(1, 2)
self.coeff = coeff_2d.view(weights.shape[1], self.d_out, -1).transpose(1, 2) #self.d_out = NUMERO DI TARGET
# coeff dim is thus: n_rules * n_out * (n_in+1)
def forward(self, x):
'''
Calculate: y = coeff * x + const [NB: no weights yet]
x.shape: n_cases * n_in
coeff.shape: n_rules * n_out * (n_in+1)
y.shape: n_cases * n_out * n_rules
'''
# Append 1 to each list of input vals, for the constant term:
#x_plus = torch.cat([x, torch.ones(x.shape[0], 1)], dim=1)
x_plus = torch.ones(len(x), 1)
# Need to switch dimansion for the multipy, then switch back:
y_pred = torch.matmul(self.coeff, x_plus.t())
return y_pred.transpose(0, 2) # swaps cases and rules
class PlainConsequentLayer(ConsequentLayer):
'''
A linear layer to represent the TSK consequents.
Not hybrid learning, so coefficients are backprop-learnable parameters.
'''
def __init__(self, *params):
super(PlainConsequentLayer, self).__init__(*params)
self.register_parameter('coefficients',
torch.nn.Parameter(self._coeff))
@property
def coeff(self):
'''
Record the (current) coefficients for all the rules
coeff.shape: n_rules * n_out * (n_in+1)
'''
return self.coefficients
def fit_coeff(self, x, weights, y_actual):
'''
'''
assert False,\
'Not hybrid learning: I\'m using BP to learn coefficients'
class WeightedSumLayer(torch.nn.Module):
'''
Sum the TSK for each outvar over rules, weighted by fire strengths.
This could/should be layer 5 of the Anfis net.
I don't actually use this class, since it's just one line of code.
'''
def __init__(self):
super(WeightedSumLayer, self).__init__()
def forward(self, weights, tsk):
'''
weights.shape: n_cases * n_rules
tsk.shape: n_cases * n_out * n_rules
y_pred.shape: n_cases * n_out
'''
# Add a dimension to weights to get the bmm to work:
y_pred = torch.bmm(tsk, weights.unsqueeze(2))
return y_pred.squeeze(2)
class AnfisNet(torch.nn.Module):
'''
This is a container for the 5 layers of the ANFIS net.
The forward pass maps inputs to outputs based on current settings,
and then fit_coeff will adjust the TSK coeff using LSE.
'''
def __init__(self, description, invardefs, outvarnames, n_terms, hybrid=True):
super(AnfisNet, self).__init__()
self.description = description
self.outvarnames = outvarnames
self.n_terms = n_terms
self.hybrid = hybrid
varnames = [v for v, _ in invardefs]
mfdefs = [FuzzifyVariable(mfs) for _, mfs in invardefs]
self.n_mfdefs = len(mfdefs)
self.num_in = len(invardefs)
self.num_rules = np.prod([len(mfs) for _, mfs in invardefs])
if self.hybrid:
cl = ConsequentLayer(self.num_in, self.num_rules, self.num_out, self.n_mfdefs, self.n_terms)
else:
cl = PlainConsequentLayer(self.num_in, self.num_rules, self.num_out, self.n_mfdefs, self.n_terms)
self.layer = torch.nn.ModuleDict(OrderedDict([
('fuzzify', FuzzifyLayer(mfdefs, varnames)),
('rules', AntecedentLayer(mfdefs)),
# normalisation layer is just implemented as a function.
('consequent', cl),
# weighted-sum layer is just implemented as a function.
]))
@property
def num_out(self):
return len(self.outvarnames)
@property
def coeff(self):
return self.layer['consequent'].coeff
@coeff.setter
def coeff(self, new_coeff):
self.layer['consequent'].coeff = new_coeff
def fit_coeff(self, x, y_actual):
'''
Do a forward pass (to get weights), then fit to y_actual.
Does nothing for a non-hybrid ANFIS, so we have same interface.
'''
if self.hybrid:
self(x)
self.layer['consequent'].fit_coeff(x, self.weights, y_actual)
def input_variables(self):
'''
Return an iterator over this system's input variables.
Yields tuples of the form (var-name, FuzzifyVariable-object)
'''
return self.layer['fuzzify'].varmfs.items()
def output_variables(self):
'''
Return an list of the names of the system's output variables.
'''
return self.outvarnames
def extra_repr(self):
#print('rere', self.layer['consequent'].coeff[0])
x = self.layer['consequent'].coeff
scaler = MinMaxScaler()
lis = []
i = 0
#####################################################################
y = Variable(x, requires_grad=True)
y = y.detach().numpy()
from scipy.special import softmax
while i < len(y):
scaled = softmax(y[i])
lis.append(scaled)
i = i+1
#####################################################################
rstr = []
vardefs = self.layer['fuzzify'].varmfs
rule_ants = self.layer['rules'].extra_repr(vardefs).split('\n')
for i, crow in enumerate(lis):
rstr.append('Rule {:2d}: IF {}'.format(i, rule_ants[i]))
rstr.append(' ' * 9 + 'THEN {}'.format(crow.tolist()))
return '\n'.join(rstr)
def forward(self, x):
'''
Forward pass: run x thru the five layers and return the y values.
I save the outputs from each layer to an instance variable,
as this might be useful for comprehension/debugging.
ALSO: see that the two 'easy' layers are integrated into the sequence.
'''
self.fuzzified = self.layer['fuzzify'](x)
self.raw_weights = self.layer['rules'](self.fuzzified)
self.weights = F.normalize(self.raw_weights, p=1, dim=1)
self.rule_tsk = self.layer['consequent'](x)
y_pred = torch.bmm(self.rule_tsk, self.weights.unsqueeze(2))
self.y_pred = y_pred.squeeze(2)
return self.y_pred
# These hooks are handy for debugging:
def module_hook(label):
''' Use this module hook like this:
m = AnfisNet()
m.layer.fuzzify.register_backward_hook(module_hook('fuzzify'))
m.layer.consequent.register_backward_hook(modul_hook('consequent'))
'''
return (lambda module, grad_input, grad_output:
print('BP for module', label,
'with out grad:', grad_output,
'and in grad:', grad_input))
def tensor_hook(label):
'''
If you want something more fine-graned, attach this to a tensor.
'''
return (lambda grad:
print('BP for', label, 'with grad:', grad))