-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplexus.py
424 lines (371 loc) · 14 KB
/
plexus.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
import random
import math
import threading
import time
usleep = lambda x: time.sleep(x/1000000.0)
class Neuron():
def __init__(self, network):
self.network = network
self.subscriptions = {}
self.publications = {}
self.potential = random.uniform(0.0, 1.0)
self.desired_potential = None
self.loss = None
self.type = 0
self.network.neurons.append(self)
self.fire_counter = 0
self.ban_counter = 0
self.position = (None, None)
self.index = None
def partially_subscribe(self):
if len(self.subscriptions) == 0:
sample_length = int(random.normalvariate(
self.network.connectivity,
self.network.connectivity_sqrt
))
if sample_length > len(self.network.nonmotor_neurons):
sample_length = len(self.network.nonmotor_neurons)
if sample_length <= 0:
sample_length = 0
elected = random.sample(
self.network.nonmotor_neurons,
sample_length
)
for neuron in elected:
if id(neuron) != id(self):
self.subscriptions[neuron] = random.uniform(-1.0, 1.0)
neuron.publications[self] = 0
self.network.initiated_neurons += 1
def calculate_potential(self):
total = 0
for neuron, weight in self.subscriptions.items():
total += neuron.potential * weight
return self.activation_function(total)
def activation_function(self, x):
return 1 / (1 + math.exp(-x))
def derivative(self, x):
return x * (1-x)
def calculate_loss(self):
return self.potential - self.desired_potential
def fire(self):
if self.type != 1:
self.potential = self.calculate_potential()
self.network.fire_counter += 1
self.fire_counter += 1
if self.desired_potential is not None:
self.loss = self.calculate_loss()
if self.loss > 0:
alteration_sign = -1
elif self.loss < 0:
alteration_sign = 1
else:
self.desired_potential = None
return True
alteration_value = (abs(self.loss) ** 2)
alteration_value *= (
self.network.decay_factor
** (self.network.fire_counter/1000)
)
for neuron, weight in self.subscriptions.items():
neuron.desired_potential = neuron.potential
neuron.desired_potential += (
alteration_sign * self.derivative(neuron.potential)
)
self.subscriptions[neuron] = weight
self.subscriptions[neuron] += (
alteration_value * alteration_sign
) * self.derivative(neuron.potential)
class Network():
def __init__(
self,
size,
input_dim=0,
output_dim=0,
connectivity=0.01,
precision=2,
randomly_fire=False,
dynamic_output=False,
visualization=False,
decay_factor=1.0
):
self.precision = precision
print("\nPrecision of the network will be {0}".format(
str(1.0 / (10**precision))
))
self.connectivity = int(size * connectivity)
self.connectivity_sqrt = int(math.sqrt(self.connectivity))
print("Each individual non-sensory neuron will subscribe to {0} \
different neurons".format(
str(int(size * connectivity))
))
self.neurons = []
for i in range(size):
Neuron(self)
print("\n")
print(str(size) + " neurons created")
self.sensory_neurons = []
self.input_dim = input_dim
self.pick_sensory_neurons(self.input_dim)
self.motor_neurons = []
self.output_dim = output_dim
self.pick_motor_neurons(self.output_dim)
self.nonsensory_neurons = [x for x in self.neurons if x.type != 1]
self.nonmotor_neurons = [x for x in self.neurons if x.type != 2]
self.interneurons = [x for x in self.neurons if x.type == 0]
self.randomly_fire = randomly_fire
self.motor_randomly_fire_rate = int(math.sqrt(
len(self.nonsensory_neurons) / len(self.motor_neurons)
))
self.dynamic_output = dynamic_output
self.decay_factor = decay_factor
self.initiated_neurons = 0
self.initiate_subscriptions()
self.fire_counter = 0
self.first_queue = {}
self.next_queue = {}
self.output = []
self.wave_counter = 0
print("\n")
self.freezer = False
self.stop = False
self.thread1 = None
self.thread2 = None
self.thread_kill_signal = False
if visualization:
self.visualize()
self.ignite()
print("")
def initiate_subscriptions(self):
print("")
for neuron in self.neurons:
if neuron.type == 1:
continue
neuron.partially_subscribe()
print("Initiated: {0} neurons\r".format(
str(self.initiated_neurons)
), sep=' ', end='', flush=True)
print("\n")
def add_neurons(self, units):
for i in range(units):
Neuron(self)
print("\n")
print(str(units) + " neurons added")
self.initiate_subscriptions()
def _ignite(self):
motor_fire_counter = 0
ban_list = []
while True:
if self.stop:
break
if self.freezer:
usleep(10)
continue
if self.randomly_fire:
neuron = random.sample(self.nonsensory_neurons, 1)[0]
if neuron.type == 2:
if 1 != random.randint(1, self.motor_randomly_fire_rate):
continue
else:
motor_fire_counter += 1
neuron.fire()
if motor_fire_counter >= len(self.motor_neurons):
if self.dynamic_output:
print("Output: {0}\r".format(
str(self.get_output())
), sep=' ', end='', flush=True)
self.output = self.get_output()
self.wave_counter += 1
motor_fire_counter = 0
else:
if not self.next_queue:
for neuron in self.motor_neurons:
neuron.fire()
for neuron in ban_list:
neuron.ban_counter = 0
ban_list = []
self.output = self.get_output()
self.wave_counter += 1
if self.dynamic_output:
print("Output: {0}\r".format(
str(self.output)
), sep=' ', end='', flush=True)
if not self.first_queue:
for neuron in self.sensory_neurons:
self.first_queue.update(neuron.publications)
self.next_queue = self.first_queue.copy()
current_queue = self.next_queue.copy()
self.next_queue = {}
for neuron in ban_list:
if neuron.ban_counter > self.connectivity_sqrt:
current_queue.pop(neuron, None)
while current_queue:
neuron = random.choice(list(current_queue.keys()))
current_queue.pop(neuron, None)
if neuron.ban_counter <= self.connectivity_sqrt:
if neuron.type == 2:
continue
neuron.fire()
ban_list.append(neuron)
neuron.ban_counter += 1
self.next_queue.update(neuron.publications)
time.sleep(0.001)
def ignite(self):
self.freezer = False
self.stop = False
if not self.thread1:
self.thread1 = threading.Thread(target=self._ignite)
self.thread1.start()
print("Network has been ignited")
def freeze(self):
self.freezer = True
self.stop = True
self.thread1 = None
self.thread2 = None
self.thread_kill_signal = True
print("Network is now frozen")
def breakit(self):
for neuron in self.neurons:
neuron.subscriptions = {}
print("All the subscriptions are now broken")
def pick_sensory_neurons(self, input_dim):
available_neurons = []
for neuron in self.neurons:
if neuron.type == 0:
available_neurons.append(neuron)
for neuron in random.sample(available_neurons, input_dim):
neuron.type = 1
self.sensory_neurons.append(neuron)
print(str(input_dim) + " neuron picked as sensory neuron")
def pick_motor_neurons(self, output_dim):
available_neurons = []
for neuron in self.neurons:
if neuron.type == 0:
available_neurons.append(neuron)
for neuron in random.sample(available_neurons, output_dim):
neuron.type = 2
self.motor_neurons.append(neuron)
print(str(output_dim) + " neuron picked as motor neuron")
def load(self, input_arr, output_arr=None):
if len(self.sensory_neurons) != len(input_arr):
print("Size of the input array: {0}".format(str(len(input_arr))))
print("Number of the sensory neurons: {0}".format(
str(len(self.sensory_neurons))
))
print("Size of the input array and number of the sensory \
neurons are not matching! Please try again")
else:
step = 0
for neuron in self.sensory_neurons:
neuron.potential = input_arr[step]
step += 1
if output_arr is None:
step = 0
self.freezer = True
for neuron in self.nonsensory_neurons:
neuron.desired_potential = None
step += 1
self.freezer = False
else:
if len(self.motor_neurons) != len(output_arr):
print("Size of the output/target array: {0}".format(
str(len(output_arr))
))
print("Number of the motor neurons: {0}".format(
str(len(self.motor_neurons))
))
print("Size of the output/target array and number of the\
motor neurons are not matching! Please try again")
else:
step = 0
for neuron in self.motor_neurons:
neuron.desired_potential = output_arr[step]
step += 1
def get_output(self):
output = []
for neuron in self.motor_neurons:
output.append(round(
neuron.potential,
self.precision
))
return output
def visualize(self):
self.thread2 = threading.Thread(target=self._visualize)
self.thread2.start()
print("Visualization initiated")
def _visualize(self):
import pyqtgraph as pg
import numpy as np
# Enable antialiasing for prettier plots
pg.setConfigOptions(antialias=True)
w = pg.GraphicsWindow()
w.setWindowTitle('Visualization of the Network')
v = w.addViewBox()
v.setAspectLocked()
g = pg.GraphItem()
v.addItem(g)
positions = []
symbols = []
symbol_brushes = []
x = 0
y = 0
x += 1
for neuron in self.sensory_neurons:
y += 1
neuron.position = (x, y)
positions.append(neuron.position)
symbols.append('t')
symbol_brushes.append((250, 194, 5))
neuron.index = len(positions) - 1
x += len(self.sensory_neurons)
y = (len(self.sensory_neurons) - len(self.interneurons)) / 2
for neuron in self.interneurons:
y += 1
neuron.position = (
random.uniform(
x - len(self.sensory_neurons)/1.5,
x + len(self.sensory_neurons)/1.5
),
y
)
positions.append(neuron.position)
symbols.append('h')
symbol_brushes.append((195, 46, 212))
neuron.index = len(positions) - 1
x += len(self.sensory_neurons)
y = (len(self.sensory_neurons) - len(self.motor_neurons)) / 2
for neuron in self.motor_neurons:
y += 1
neuron.position = (x, y)
positions.append(neuron.position)
symbols.append('s')
symbol_brushes.append((19, 234, 201))
neuron.index = len(positions) - 1
while True:
connections = []
lines = []
for neuron2 in self.neurons:
for neuron1, weight in neuron2.subscriptions.items():
connections.append((neuron1.index, neuron2.index))
lines.append((55, 55, 55, ((weight+1)/2)*255, (weight+1)))
positions = np.asarray(positions)
connections = np.asarray(connections)
lines = np.asarray(lines, dtype=[
('red', np.ubyte),
('green', np.ubyte),
('blue', np.ubyte),
('alpha', np.ubyte),
('width', float)
])
g.setData(
pos=positions,
adj=connections,
pen=lines,
size=0.1,
symbolBrush=symbol_brushes,
symbol=symbols,
pxMode=False
) # Update the graph
pg.QtGui.QApplication.processEvents()
if self.thread_kill_signal:
break
time.sleep(0.0333)