-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxor.py
executable file
·53 lines (47 loc) · 1.18 KB
/
xor.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
#!/usr/bin/python2
from ann import *
topology = [2,2,1]
delta = []
values = []
weights = []
gradient = []
timesteps = 100000
patterns = 100
offset = 0
eta = 1.0
a = .9
p = 0
numCorrect = 0
allocate_lists(topology,values,weights,delta,gradient)
r.seed(5890)
initialize_weights(weights,topology)
data = gen_xor(patterns)
#data = load_data("training.dat")
#print data
desired = [data[0]]
for i in range(timesteps):
values[0][0] = data[p][0]
values[0][1] = data[p][1]
desired[0] = data[p][2]
#print "timestep: ",i, "pattern: ",p," input: ",values[0][0], " ",values[0][1]
#print "feedforward"
update(weights,values)
#print "MSE"
err = error(values[2],desired)
#print "A: ",values[0][0],"B: ",values[0][1],"Output: ", sigmoid(values[2][0]), "Error: ",err
#print err,desired[0],sigmoid(values[2][0])
#print "backpropagate"
if(err > .1):
backpropagate(values,desired,weights,gradient)
compute_delta(delta,gradient,values,a,topology)
update_weight(delta,weights,eta,topology)
else:
numCorrect += 1
p += 1
if(p >= patterns):
print i, " timesteps"
print "Correctly classified ",numCorrect, " of ",patterns," patterns"
if(numCorrect == patterns):
break
numCorrect = 0
p = offset