-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tarefa7.1.py
390 lines (299 loc) · 9.63 KB
/
Tarefa7.1.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
import math
import os
import threading
import time
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.animation as anim
import matplotlib.pyplot as plot
import numpy as np
from numpy.linalg import linalg
MAX_ITER = 10000
# Minimum of 3 elements, first is inputs, second is RBF layer
# and last is outputs
NEURONS_BY_LAYER = [2, 4, 2, 1]
ETA = .3
ERROR_THRESHOLD = 0.1
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
PATH = os.path.join(THIS_FOLDER, 'data/xor.txt')
GAMA = .9
MOMENTUM = False
# style.use('fivethirtyeight')
np.random.seed(int(time.time()))
fig = plot.figure()
ax = fig.add_subplot(1, 1, 1)
xs = []
ys = []
lock = threading.Lock()
stop_anim = False
def animate(i):
lock.acquire()
if len(xs) > 0 and len(ys) > 0:
ax.plot(xs, ys, 'b-')
last_x = xs[len(xs) - 1]
last_y = ys[len(ys) - 1]
xs.clear()
ys.clear()
xs.append(last_x)
ys.append(last_y)
lock.release()
an = anim.FuncAnimation(fig, animate, interval=50, blit=False)
plot.interactive(False)
plot.show(block=False)
def mag(x):
if x.shape[0] == 1:
return np.asscalar(np.sqrt(np.dot(x, x.T)))
elif x.shape[1] == 1:
return np.asscalar(np.sqrt(np.dot(x.T, x)))
else:
return linalg.norm(x)
def norm(x):
return x / mag(x)
def transfer_f(x):
return sigmoid(x)
def derivative_tf(x):
return sigmoid_derivative(x)
def step_tf(x):
return (x > 0) * 1
def step_derivative(x):
return (np.abs(x) < 0.001) * .5
def linear_tf(x):
return x
def linear_derivative(x):
return np.ones(x.shape)
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
s = sigmoid(x)
return s * (1 - s)
def rbf(x, c, s):
# Equivalente à -(x - c)²/sigma, mas aplicado como uma operação entre matrizes
return np.exp(np.sum(-((x[..., np.newaxis] - c[np.newaxis, ...]) ** 2) / s[np.newaxis, ...], axis=1))
def coalesce_error(x):
# e = amax(abs(x))
e = np.average(abs(x))
# e = mag(x)
return e
np.random.seed(int(time.time()))
# Carrega arquivo de treinamento passado
def load_dataset(path):
data = np.loadtxt(path,
comments=';;', delimiter=',')
data = np.array(data)
_i = NEURONS_BY_LAYER[0]
_o = NEURONS_BY_LAYER[-1]
x = data[:, :_i]
y = data[:, -_o:]
s = data.shape[0]
return x, y, s
def distance_graph(x, c):
return np.sqrt(np.sum((x[..., np.newaxis] - c[np.newaxis, ...]) ** 2, axis=1))
def pertains_graph(x, c):
# Grafo de distancia (pontos x -versus- medias m)
d = distance_graph(x, c.T)
m = np.reshape(np.amin(d, axis=1), (-1, 1))
return (d == m) * 1, d
def farthest_graph(x, c):
# Grafo de grupos
u, d = pertains_graph(x, c.T)
# Marca zeros com -1 para evitar confusão na seleção de pontos com distância 0
u = np.where(u == 0, -1, u)
# Números positivos indicam distância do ponto até o centro do grupo
g = u * d
# Indice de linha do maior valor
# (é por causa desta linha que marcamos com números
# negativos distância de grupos não pertencentes, pois
# grupos de um único elemento tem distância zero, o que
# faria pontos de outros grupo serem selecionados caso
# tambem fossem marcados com 0)
m = np.argmax(g, axis=0)
# Indices de coluna dos grupos
n = np.arange(u.shape[1])
# Marca com 1 os pontos mais distante de cada grupo
f = np.zeros(np.shape(u))
f[m, n] = 1
# Matrix de pertinencia original
u = (u > 0) * 1
# Um grupo sem elementos não tera nenhum ponto selecionado por causa de f * u
return f * u, d, u
def k_means_clustering(x, k):
dim = x.shape[1]
# Maximo de cada eixo (coluna)
axis_max = np.amax(x, axis=0)
# Minimo de cada eixo (coluna)
axis_min = np.amin(x, axis=0)
# Medias inicializadas aleatoriamente (dentro do maximo e minimo em cada eixo)
m = np.random.random((k, dim)) * (axis_max - axis_min) + axis_min
lm = None
# Continua execucao ate nao haver mais alteração nas medias
while not np.array_equal(m, lm):
lm = m
# Matriz de pertinencia (pontos x -versus- medias m)
u, d = pertains_graph(x, m)
# Quantidade de pontos em cada grupo
n = np.sum(u, axis=0)
# Somatório das coordenadas dos pontos
s = np.dot(x.T, u)
# Calculo das centroides
c = (s / n).T
# Atualiza novas médias com centróides dos grupos encontrados
m = np.where(np.isnan(c), m, c)
return m.T
def dispersion(x, c):
# existem duas formas de fazer o calculo:
# Primeira: usando a média dos N vetores mais proximos =. sigma = 1/N somatorio ||centro mais proximo - vetores mais proximos||, N = vetores mais proximos
# Segunda: pelo ponto mais distantes pertencentes ao cluster do centro Cj
# A segunda e mais facil a implementacao
#
# 1 - Recebe sigma e os centros
# 2 - dentro de um loop, calcula qual é o maior em ||centro - entradai || e atribui o maior para sigma
# repare que quem se altera é somente a entrada
# 3 - retorna sigma
# Matriz indica qual ponto x é o mais distante de cada grupo
f, _, _ = farthest_graph(x, c)
# Marcando com nan grupos vazios
n = np.sum(f, axis=0)
f = f / n
# Seleção do vetor mais distante
v = np.dot(x.T, f)
# Distância entre vetor e centro do grupo
d = np.abs(c - v)
# Disperação padrão para distância zero ou grupo vazio
s = np.where(np.logical_and(d != 0, np.logical_not(np.isnan(d))), d, 0.1)
# Aumenta em 20% area de dispersão para melhorar englobamento
return s * 1.2
def training():
time.sleep(1)
x, y, n_patterns = load_dataset(PATH)
# Parametro de centro para RBF
c = k_means_clustering(x, NEURONS_BY_LAYER[1])
# c = np.array([[0, 0, 1, 1],
# [0, 1, 0, 1]])
# Vetor de dispersão
sigma = dispersion(x, c)
# sigma = np.array([[.1, .1, .1, .1],
# [.1, .1, .1, .1]])
# Aplicando a RBF
rbf_input = rbf(x, c, sigma)
x_p = np.hstack((np.ones((n_patterns, 1)), rbf_input))
y_p = y
NEURONS_BY_LAYER.pop(0)
w = []
# Vetor de pesos aleatorizado para cada camada
n_layers = len(NEURONS_BY_LAYER) - 1
for i in range(n_layers):
row = NEURONS_BY_LAYER[i] + 1
col = NEURONS_BY_LAYER[i + 1] + (1 if i < n_layers - 1 else 0)
rn = np.random.random((row, col))
wl = 2 * rn - 1
w.append(wl)
stopped_by_error_threshold = True
gi = 0
gama = GAMA
d2_w = []
for w_it in w:
d2_w.append(w_it * 0)
eta = ETA
for epoch in range(MAX_ITER):
for i in range(len(w) - 1):
w[i][:, 0] = 0
w[i][0][0] = 1
g = []
u = []
y_n = x_p
for i, w_it in enumerate(w):
u_n = np.dot(y_n, w_it)
g_n = transfer_f(u_n)
if i < len(w) - 1:
g_n[:, 0] = 1
u.append(u_n)
g.append(g_n)
y_n = g_n
dg = list(map(lambda u_it: derivative_tf(u_it), u))
e_n = y_p - g[-1]
e = [e_n]
for dg_it, w_it in zip(reversed(dg[1:]), reversed(w[1:])):
delta_n = e_n * dg_it
e_n = np.dot(delta_n, w_it.T)
e.insert(0, e_n)
if np.average(coalesce_error(e[-1])) < ERROR_THRESHOLD:
print("Final Epoch :")
print(epoch)
stopped_by_error_threshold = True
break
if epoch % 500 == 0:
print("Current Epoch:")
print(epoch)
print("Errors: ")
for i, e_it in enumerate(e):
print(" Layer: %d" % i)
print(" Avg: %.6f" % np.average(abs(e_it)))
print(" Mag: %.6f\n" % mag(e_it))
print("\nEtha")
print(eta)
print("\nWeights")
print(w)
print("\nError")
print(e)
lock.acquire()
xs.append(gi)
ys.append(coalesce_error(e[-1]))
lock.release()
gi += 1
e = list(map(lambda e_it, dg_it: e_it * dg_it, e, dg))
y_h = g[:-1]
y_h.insert(0, x_p)
d_w = list(map(lambda y_it, e_it: np.dot(y_it.T, e_it) / n_patterns, y_h, e))
for i in range(len(w)):
w[i] += eta * d_w[i] + (gama * d2_w[i] if MOMENTUM else 0)
d2_w = list(map(lambda d_w_it: eta * d_w_it, d_w))
if not stopped_by_error_threshold:
print("Iteration :")
print(MAX_ITER)
print("\nInput ")
print(x)
print("\nRBF Layer")
print(x_p)
print("\nCenter")
print(c)
print("\nDispersion")
print(sigma)
# testing output
g = []
y_n = x_p
for i, w_it in enumerate(w):
u_n = np.dot(y_n, w_it)
g_n = transfer_f(u_n)
if i < len(w) - 1:
g_n[:, 0] = 1
g.append(g_n)
y_n = g_n
print("\nOutput")
print(y_n)
print("\nTraining Data")
print(y_p)
print("\nError")
dg = list(map(lambda g_it: derivative_tf(g_it), g))
e_n = y_p - g[-1]
e = [e_n]
for dg_it, w_it in zip(reversed(dg[1:]), reversed(w[1:])):
delta_n = e_n * dg_it
e_n = np.dot(delta_n, w_it.T)
e.insert(0, e_n)
for i, e_it in enumerate(e):
print(" Layer: %d" % i)
print(" Avg: %.6f" % np.average(abs(e_it)))
print(" Mag: %.6f\n" % mag(e_it))
print("\nEtha")
print(eta)
print("\nSynaptic Weights")
for i in range(len(w) - 1):
w[i][:, 0] = 0
w[i][0][0] = 1
for i, w_it in enumerate(w):
print("Layer: %d" % i)
print(w_it)
while True:
time.sleep(0)
t = threading.Thread(target=training)
t.start()