Skip to content

Commit c479782

Browse files
committed
Run and Training functions
Outlined run and training functions
1 parent db4f611 commit c479782

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

and_net.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,23 @@ def binary_to_spikes(self, val, nsteps=1000):
7171
return a_spikes
7272

7373
# Runs the neural net
74+
# Maybe include option to run without training neuron
7475
def run_net(self, input):
75-
output = []
76-
return output
76+
# Initializes a current for the three input neurons
77+
xIn = self.spikes_to_current(self.binary_to_spikes(input[0]), self.connections[0].weight)
78+
yIn = self.spikes_to_current(self.binary_to_spikes(input[1]), self.connections[1].weight)
79+
teachIn = self.spikes_to_current(self.binary_to_spikes(input[1] and input[0]), self.connections[2].weight)
80+
self.neurons[0].sim(xIn, 1000)
81+
self.neurons[1].sim(yIn, 1000)
82+
self.neurons[2].sim(teachIn, 1000)
83+
# Calculates the input current for the output by using an or function on all 3 spike outputs
84+
outIn = []
85+
for i in range(1000):
86+
outIn[i] = self.neurons[0].output[i] or self.neurons[1].output[i] or self.neurons[2].output[i]
87+
self.neurons[3].sim(self.spikes_to_current(outIn, 1), 1000)
88+
for i in range(1000):
89+
output = output + self.neurons[3].output[i]
90+
if output > 5:
91+
return 1
92+
else:
93+
return 0

training.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
net = and_net([], [])
33
net.create_simple_net()
44
# X at [0], Y at [1], Teacher at [2]
5-
net.connections[0].weight = .7
6-
net.connections[1].weight = .3
7-
net.connections[2].weight = .5
5+
net.connections[0].weight = 1
6+
net.connections[1].weight = 1
7+
net.connections[2].weight = 1
88
# data[0] holds X, data[1] holds Y, output calculated by data[0][i] && data[1][i]
99
data = [[]]
1010
lines = open(input("Enter Filepath: "), "r").readlines()
@@ -15,6 +15,17 @@
1515
data[j][i] = 0
1616
else:
1717
data[j][i] = 1
18-
#TODO does training and results and_net with proper weights
18+
# Trains the network
1919
for i in range(runs):
20-
pass
20+
#out = net.run_net([data[0][i], data[1][i], data[0][i] and data[1][i]])
21+
#TODO Find better formula
22+
for i in range(1000):
23+
for j in range(2):
24+
if net.neurons[j].output[i] == net.neurons[3].output[i] == 1:
25+
#increase
26+
net.connections[j].weight += .1
27+
elif net.neurons[j].output[i] == 1 and net.neurons[3].output[i] == 0:
28+
#decrease
29+
net.connections[j].weight -= .1
30+
for i in range(3):
31+
net.neurons[i].clear

0 commit comments

Comments
 (0)