-
Notifications
You must be signed in to change notification settings - Fork 6
/
xor-mlp.py
57 lines (41 loc) · 1.46 KB
/
xor-mlp.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
54
55
56
57
from lib.mlp import NeuralNetwork
import numpy as np
if __name__ == "__main__":
print("MLP Test usin XOR gate")
filename = "XOR.dat"
'''
@dataset: array of arrays
[ [x1, x1, x2, ..., xn, y],
[x1, x1, x2, ..., xn, y],
[x1, x1, x2, ..., xn, y] ]
'''
dataset = np.loadtxt(open(filename, "rb"), delimiter=" ")
input_size = dataset.shape[1] - 1
output_size = 1
nn_size = [input_size, 2, output_size]
print("DataSet: {}".format(dataset))
print("NN SIZE {}".format(nn_size))
mlp = NeuralNetwork(layer_size=nn_size, debug_string=True)
mlp.train(dataset, eta=0.1, threshold=1e-3, max_iterations=100000)
outputs, output = mlp.classify(np.array([0,0]))
print(mlp)
x = np.array([0,0])
outputs, output = mlp.classify(x)
print("==========================")
# print("Z: {}".format(outputs))
print("x: {}, ŷ: {}".format(x, output))
x = np.array([0,1])
outputs, output = mlp.classify(x)
print("==========================")
# print("Z: {}".format(outputs))
print("x: {}, ŷ: {}".format(x, output))
x = np.array([1,0])
outputs, output = mlp.classify(x)
print("==========================")
# print("Z: {}".format(outputs))
print("x: {}, ŷ: {}".format(x, output))
x = np.array([1,1])
outputs, output = mlp.classify(x)
print("==========================")
# print("Z: {}".format(outputs))
print("x: {}, ŷ: {}".format(x, output))