-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspiking.py
51 lines (37 loc) · 1.36 KB
/
spiking.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
import numpy as np
import numpy.random as rnd
class Net:
groupCount = 3
neuronCount = 10
synapsisCount = 20
def __init__(self):
self.weights = np.zeros((self.neuronCount, self.neuronCount))
for i in range(self.synapsisCount):
self.weights[rnd.randint(self.neuronCount)] \
[rnd.randint(self.neuronCount)] = rnd.rand()
self.tresholds = rnd.random_sample(self.neuronCount)
# self.values = np.zeros(self.neuronCount)
self.values = rnd.random(self.neuronCount)
self.groups = rnd.randint(self.groupCount, size = self.neuronCount)
self.inputs = filter(lambda neuron: self.groups[neuron] == 0,
range(self.neuronCount))
self.outputs = filter(lambda neuron:
self.groups[neuron] == self.groupCount - 1, range(self.neuronCount))
def step(self):
self.randomInputs()
self.valuesNext = np.copy(self.values)
for neuron in range(self.neuronCount):
if self.values[neuron] < self.tresholds[neuron]:
continue
# else fire and reset:
for dest in range(self.neuronCount):
self.valuesNext[dest] += self.weights[neuron][dest] * \
self.values[neuron]
self.valuesNext[neuron] -= self.values[neuron]
self.values = self.valuesNext
def randomInputs(self):
for neuron in self.inputs:
self.values[neuron] = rnd.random()
def printOutputs(self):
print(", ".join(map(lambda neuron: str(self.values[neuron]),
self.outputs)))