-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxor_problem.py
More file actions
51 lines (40 loc) · 1.2 KB
/
xor_problem.py
File metadata and controls
51 lines (40 loc) · 1.2 KB
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
#!/usr/bin/env python3
import neural_network as nn
import random
#XOR Data table
inputs = [[0, 0],
[0, 1],
[1, 0],
[1, 1]]
outputs = [[0],
[1],
[1],
[0]]
#Creating a Neural Network with a 2 input, 4 hidden layer, and 1 output architecture
#Learning rate of .75
brain = nn.NeuralNetwork(2, 4, 1, 0.25)
#Creating Training data
training_data = []
for i in range(4):
training_data.append([inputs[i], outputs[i]])
#5000 Epochs of training
for n in range(5000):
random.shuffle(training_data)
for i in training_data:
brain.train(i[0], i[1])
#Calculate the Number of correct answers
correct = 0
incorrect = 0
for i in range(1000):
pos = random.randint(0, len(training_data)-1)
prediction = brain.predict(training_data[pos][0])
target = training_data[pos][1][0]
rounded = 1 if prediction > 0.7 else 0
correct = correct + 1 if rounded == target else correct + 0
incorrect = incorrect + 1 if rounded != target else incorrect + 0
print(str((correct/1000) * 100) + " % Correct" )
#Predictions on the various options
print(brain.predict([1,0]))
print(brain.predict([0,1]))
print(brain.predict([0,0]))
print(brain.predict([1,1]))