@@ -10,17 +10,12 @@ def __init__(self, neurons, connections):
10
10
self .neurons = [] # A list of lif_neuron objects
11
11
self .connections = [] # A list of lif_connection objects
12
12
13
+ run_time = 1000
14
+
13
15
# Function calculates current from an array of spikes from a neuron's output activity
14
16
def spikes_to_current (self , a_spikes , conversion_factor ):
15
17
16
- spike_count = 0
17
- for i in range (len (a_spikes )):
18
- if a_spikes [i ] == 1 :
19
- spike_count += 1
20
-
21
- spikes_per_second = spike_count / len (a_spikes )
22
-
23
- return spikes_per_second * conversion_factor * 100000
18
+ return sum (a_spikes ) / len (a_spikes ) * conversion_factor * 50000
24
19
25
20
# Configure the network to solve problem 3
26
21
def create_simple_net (self ):
@@ -47,17 +42,17 @@ def spikes_to_binary(self, output_neuron, threshold=4):
47
42
if output_neuron .output [i ] == 1 :
48
43
spike_count += 1
49
44
50
- spike_frequency = spike_count / len ( output_neuron . output )
45
+ spike_frequency = spike_count
51
46
52
47
if (spike_frequency > threshold ):
53
48
return 1
54
49
55
50
return 0
56
51
57
- # Converts a single bit into 1000 ms array of spikes
52
+ # Converts a single bit into an array of spikes
58
53
# A value of 0 makes 5Hz spikes
59
54
# A value of 1 makes 15Hz spikes
60
- def binary_to_spikes (self , val , nsteps = 1000 ):
55
+ def binary_to_spikes (self , val , nsteps = run_time ):
61
56
62
57
a_spikes = [0 ] * nsteps
63
58
nspikes = 5
@@ -66,33 +61,36 @@ def binary_to_spikes(self, val, nsteps=1000):
66
61
nspikes = 15
67
62
68
63
interval = int (nsteps / nspikes )
69
- for i in range (1000 ):
64
+ for i in range (nsteps ):
70
65
if (i % interval == 0 ):
71
66
a_spikes [i ] = 1
72
67
73
68
return a_spikes
74
69
75
70
# Runs the neural net
76
71
# Maybe include option to run without training neuron
77
- def run_net (self , input ):
72
+ def run_net (self , input , time = run_time ):
78
73
# Initializes a current for the three input neurons
79
74
xIn = self .spikes_to_current (self .binary_to_spikes (input [0 ]), self .connections [0 ].weight )
80
75
yIn = self .spikes_to_current (self .binary_to_spikes (input [1 ]), self .connections [1 ].weight )
81
76
# Change and to xor/or
82
- teachIn = self .spikes_to_current (self .binary_to_spikes (input [1 ] and input [0 ]), self .connections [2 ].weight )
83
- self .neurons [0 ].sim (xIn , 1000 )
84
- self .neurons [1 ].sim (yIn , 1000 )
85
- self .neurons [2 ].sim (teachIn , 1000 )
77
+ # teachIn = self.spikes_to_current(self.binary_to_spikes(input[1] and input[0]), self.connections[2].weight)
78
+ self .neurons [0 ].sim (xIn , time )
79
+ self .neurons [1 ].sim (yIn , time )
80
+ # self.neurons[2].sim(teachIn, time )
86
81
# Calculates the input current for the output by using an or function on all 3 spike outputs
87
82
outIn = []
88
- for i in range (1000 ):
89
- outIn .append (self .neurons [0 ].output [i ] or self .neurons [1 ].output [i ] or self .neurons [2 ].output [i ])
90
- self .neurons [3 ].sim (self .spikes_to_current (outIn , 1 ), 1000 )
83
+ for i in range (time ):
84
+ if (self .neurons [0 ].output [i ] == 1 or self .neurons [1 ].output [i ] == 1 ):
85
+ outIn .append (1 )
86
+ else :
87
+ outIn .append (0 )
88
+ #Conversion factor scales # of spikes from output
89
+ self .neurons [3 ].sim (self .spikes_to_current (outIn , .5 ), time )
91
90
output = 0
92
- for i in range (1000 ):
91
+ for i in range (time ):
93
92
output = output + self .neurons [3 ].output [i ]
94
93
if output > 5 :
95
94
return 1
96
95
else :
97
- return 0
98
-
96
+ return 0
0 commit comments