-
Notifications
You must be signed in to change notification settings - Fork 1
/
NeuralF.py
255 lines (220 loc) · 8.7 KB
/
NeuralF.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
import numpy as np
import math
import copy
import matplotlib.pyplot as plt
def identite(x):
return x
def sigmoid(x):
try:
ans = 1 / (1 + np.exp(-x))
except OverflowError:
ans = 0.5
print('Overflow !!!!!!!')
return ans
def sigmoidder(x):
return sigmoid(x) * (1 - sigmoid(x))
def logit(x): #Réciproque de la sigmoïde
return np.ln(x / (1 - x))
class Function:
def __init__(self, fonc, der): #On définit une fonction à partir de son expression et sa dérivée
self.fonc = fonc
self.der = der
class Layer:
def __init__(self, name, tin, tout, type = 'hidden'): #tin et tout sont des tailles, type peut être input, output ou hidden
self.name = name
self.type = type
self.sizein = tin
self.sizeout = tout
#if type != 'output':
self.weights = np.random.rand(self.sizein, self.sizeout) * 2 - 1
#self.weights = np.full((self.sizein, self.sizeout), 0.5)
self.biases = np.random.rand(self.sizein) #* 2 - 1
#self.biases = np.zeros(self.sizein)
self.data = np.zeros(self.sizein)
def propagate(self, target):
"""Fait la propagation de self à target."""
if self.sizeout != target.sizein:
print('Erreur de dimension ' + self.name)
return 0
res = identite(np.dot(self.data, self.weights) + target.biases) #ICI
target.data = res
def putdata(self, data):
"""Initialise la data de self à data."""
if len(data) != self.sizein:
print('Erreur de dimension ' + self.name)
return 0
for i in range(len(data)):
self.data[i] = data[i]
def printdata(self):
print(self.name + ' data')
for x in self.data:
print(x)
def printweights(self):
print(self.name + ' weights')
for x in self.weights:
print(x)
def printbiases(self):
print(self.name + ' biases')
for x in self.biases:
print(x)
def returndata(self):
if self.type != 'output':
print("Attention, ce n'est pas une couche output _returndata_ " + self.name)
return self.data
class Network:
def __init__(self, name, Layers, number):
self.layers = Layers
self.name = name
self.layersnumber = number
self.epochs = 0 #Le nombre de cycles de retropropagation deja faits
def save(self, name):
"""Sauvegarde le réseau sous forme de texte."""
Texte = name + '!'
for i in range(self.layersnumber):
couche = self.layers[i]
Texte += couche.name + ';' + str(couche.sizein) + ';' + str(couche.sizeout) + ';' + str(list(couche.weights)) + ';' + str(list(couche.biases)) + ';' + couche.type
Texte += '!'
with open(name + '.txt', 'w') as fichier:
fichier.write(Texte)
def load(self, name):
LAYERS = []
self.layers = []
self.layersnumber = 0
if not ('.txt' in name):
name += '.txt'
with open(name, 'r') as fichier:
data = fichier.readlines()[0].strip()
tout = data.split('!')
self.name = tout[0]
couches = tout[1:]
for i in range(len(couches)):
elements = couches[i].split(';')
print(elements)
LAYERS.append(Layer(elements[0], int(elements[1]), int(elements[2]), np.array(elements[3]), np.array(elements[4]), type = elements[5]))
self.layers = LAYERS
self.layersnumber = len(couches)
def inputdata(self, data):
"""Rentre les données dans la couche input du réseau."""
self.layers[0].putdata(data)
def outputdata(self):
"""Sort les données de la couche output du réseau."""
return (self.layers[self.layersnumber - 1]).returndata()
def propagatelayers(self):
"""Fait la propagation de tout le réseau."""
for i in range(self.layersnumber - 1):
self.layers[i].propagate(self.layers[i + 1])
def test(self, data):
self.inputdata(data)
self.propagatelayers()
return self.outputdata()
def lossOne(self, datain, expect):
"""Propage le réseau avec datain en entrée et renvoie [(expect - real) ** 2]."""
self.inputdata(datain)
self.propagatelayers()
resultat = self.outputdata()
lenExpect = len(expect)
ret = np.zeros(lenExpect)
if lenExpect != len(resultat):
print('Erreur de dimension _test_')
return 0
res = 0
for i in range(lenExpect):
res += (expect[i] - resultat[i]) ** 2
return res
def lossAll(self, DATAIN, EXPECT):
"""Fait la fonction loss sur un paquet de données."""
loss = 0
for i in range(len(DATAIN)):
datain = DATAIN[i]
expect = EXPECT[i]
loss += self.lossOne(datain, expect)
return loss
def train(self, DATAIN, EXPECT, pers, number):
global plot
n = len(DATAIN)
for i in range(number):
print(i)
perte = self.lossAll(DATAIN, EXPECT)
if plot:
LOSS.append(perte)
print(perte)
for j in range(n):
#persistance = pers * np.exp(- i)
self.backward(DATAIN[j], EXPECT[j], pers)
self.epochs += number
def backward(self, datain, expect, pers):
self.inputdata(datain)
self.propagatelayers()
result = self.outputdata()
DELTA = []
for i in reversed(range(0, self.layersnumber)):
if i == self.layersnumber - 1:
data = self.layers[i].data
"""delta = (data - expect) * data * (1 - data)"""
delta = (np.expand_dims(data, 0) - np.expand_dims(expect, 0))
deltai = copy.deepcopy(delta[0,:])
DELTA.append(deltai)
else:
res = np.zeros(self.layers[i].sizein)
for a in range(self.layers[i].sizein):
for j in range(self.layers[i].sizeout):
#print(deltai[j] * self.layers[i].weights[a, j])
res[a] += deltai[j] * self.layers[i].weights[a, j] * pers
DELTA.append(res)
deltai = copy.deepcopy(res)
#print(DELTA)
for i in range(len(DELTA) - 1):
data = np.expand_dims(copy.deepcopy(self.layers[self.layersnumber - i - 2].data), axis=0)
delta = np.expand_dims(copy.deepcopy(DELTA[i]), axis = 0)
res = pers * data.T.dot(delta)
self.layers[self.layersnumber - i - 2].weights -= res
#DATAIN = np.array([np.array([1., 0.]), np.array([0., 1.])])
#EXPECT = np.array([np.array([1., 0.]), np.array([0., 1.])])
#DATAIN = np.array([np.array([np.random.rand(), np.random.rand()]) for _ in range(100)])
DATAIN = np.array([[i] for i in np.arange(-2, 2.5, 0.5)])
EXPECT = DATAIN ** 2
"""for i in range(100):
DATAIN[0, i], DATAIN[1, i] = np.random.rand(), np.random.rand()"""
Layer1 = Layer('1', 1, 16, type='input')
Layer2 = Layer('4', 4, 4, type='hidden')
Layer3 = Layer('3', 4, 4, type='hidden')
Layer4 = Layer('4', 16, 1, type='hidden')
Layer5 = Layer('5', 1, 1, type='output')
#Reseau = Network('Test', [Layer1, Layer2, Layer3, Layer4, Layer5], 5)
plot = True
if plot:
LOSS = []
def plotloss():
time = np.arange(0, len(LOSS))
plt.plot(time, LOSS, label='Coût selon le nombre de générations')
plt.yscale('log')
plt.legend(loc='best')
plt.xlabel('Générations')
plt.ylabel('Coût')
plt.savefig('Cost')
plt.show()
TEST = np.arange(-10, 10, 0.1)
LAYERS = [Layer1] + [Layer(str(i + 1), 16, 16, type = 'hidden') for i in range(4)] + [Layer4] + [Layer5]
Reseau = Network('test', LAYERS, len(LAYERS))
def plotPosNeg(name, title):
POSITIF = []
NEGATIF = []
for i in range(len(TEST)):
pos, neg = Reseau.test([TEST[i]])
POSITIF.append(pos)
NEGATIF.append(neg)
plt.plot(TEST, POSITIF, label = 'Sortie -')
plt.plot(TEST, NEGATIF, label = 'Sortie +')
plt.xlabel('Nombre en entrée')
plt.ylabel('Résultats')
plt.title(title)
plt.legend(loc='best')
plt.grid()
plt.show()
plt.savefig(name + '.png')
#Reseau.train([DATAIN[0]], [DATAIN[0]], 0.1, 100)
#Reseau.train([[1, 0]], [[1, 0]], 0.1, 10)
"""Reseau.train(DATAIN[0], EXPECT[0], 0.01, 50)
Reseau.inputdata(DATAIN[0])
Reseau.propagatelayers()
print(Reseau.outputdata())"""