-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerceptron 3 Camadas - Time Delay.py
159 lines (100 loc) · 5.22 KB
/
Perceptron 3 Camadas - Time Delay.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
import pandas as pd
import numpy as np
class pmc_3_layers:
def __init__(self, n1, n2, n3):
# Número de elementos em cada camada
self.n1 = n1
self.n2 = n2
self.n3 = n3
# Inicializando pesos com valores aleatórios (LeCun)
self.w = []
self.w.append(np.random.default_rng().uniform(-(2.4/n1), (2.4/n1),(n2, n1+1)))
self.w.append(np.random.default_rng().uniform(-(2.4/n1), (2.4/n1),(n3, n2+1)))
# Criando a função que realiza o "passo forward"
def forward(self, variables_updated):
# Função sigmoide
gfunc = np.vectorize(lambda a : 1/(1+np.exp(-a)))
# Primeira camada
i1 = np.atleast_2d(self.w[0]@variables_updated)
y1 = np.atleast_2d(gfunc(i1))
y1 = np.insert(y1, 0, -1, axis = 0)
# Segunda camada
i2 = np.atleast_2d(self.w[1]@y1)
y2 = np.atleast_2d(gfunc(i2))
return i1, y1, i2, y2
# Criando a função que realiza o "passo backward"
def backward(self, variable, classe, i1, y1, i2, y2):
variable = np.atleast_2d(variable)
classe = np.atleast_2d(classe)
# Derivada da função sigmoide
glinhafunc = np.vectorize(lambda a : np.exp(-a)/((1+np.exp(-a))**2))
# Segunda camada
glinha2 = np.atleast_2d(glinhafunc(i2))
grad2 = np.atleast_2d((classe.T - y2)*glinha2)
previous_w1 = self.w[1]
self.w[1] = np.atleast_2d(self.w[1] + self.taxa_aprendizado*[email protected] + self.momentum*(self.w[1] - self.previous_w[1]))
self.previous_w[1] = previous_w1
# Primeira camada
glinha1 = np.atleast_2d(glinhafunc(i1))
grad1 = np.atleast_2d(([email protected][1][:, 1:])*glinha1.T)
previous_w0 = self.w[0]
self.w[0] = np.atleast_2d(self.w[0] + self.taxa_aprendizado*[email protected] + self.momentum*(self.w[0] - self.previous_w[0]))
self.previous_w[0] = previous_w0
# Criando a função que calcula o erro quadrático médio
def eqm(self):
eqm = 0
for i in range(len(self.variables) - self.n1):
self.variables_updated = np.insert(self.variables[i : (i+self.n1)], 0, -1, axis = 0)
i1, y1, i2, y2 = self.forward(self.variables_updated)
eqm = eqm - (((self.output[i] - y2)**2)/2).sum()
eqm = eqm/len(self.variables_updated)
return eqm
# Criando a função que realiza o treinamento do modelo
def train(self, variables, taxa_aprendizado, precision, momentum):
self.variables = variables
self.output = self.variables[self.n1:]
self.taxa_aprendizado = taxa_aprendizado
self.precision = precision
self.momentum = momentum
self.epoch = 1
print('The inicial weight matrices are:')
print(self.w)
print('\n')
self.previous_w = []
self.previous_w.append(np.zeros(self.w[0].shape))
self.previous_w.append(np.zeros(self.w[1].shape))
while True:
if (self.epoch==1)|((self.epoch%100)==0):
print('\n')
print('######################################')
print('Starting a new epoch number %i' %(self.epoch))
print('\n')
previous_eqm = self.eqm()
for i in range(len(self.variables) - self.n1):
self.variables_updated = np.insert(self.variables[i : (i+self.n1)], 0, -1, axis = 0)
i1, y1, i2, y2 = self.forward(self.variables_updated)
self.backward(self.variables_updated, self.output[i], i1, y1, i2, y2)
current_eqm = self.eqm()
self.current_eqm = current_eqm
if (self.epoch==1)|((self.epoch%100)==0):
print('EQM difference:')
print(current_eqm - previous_eqm)
if (abs(current_eqm - previous_eqm) <= precision):
print("Training finished in %s epochs" % (self.epoch))
print('\n')
print("The final eqm was %f" % (self.current_eqm))
print('\n')
print('The final weight matrices are:')
print(self.w)
break
self.epoch = self.epoch + 1
# Criando a função que realiza a previsão à partir do modelo já treinado
def predict(self, variables):
resultados = np.atleast_2d(np.zeros((len(variables), self.n3)))
variables = np.insert(variables, 0, self.variables[-self.n1 :], axis = 0)
for i in range(len(resultados)):
variables_updated = np.insert(variables[i : (i+self.n1)], 0, -1, axis = 0)
i1, y1, i2, y2 = self.forward(variables_updated)
for j in range(len(y2)):
resultados[i][j] = y2[j]
return resultados