-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
73 lines (50 loc) · 1.93 KB
/
main.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import numpy as np
x_enter = np.array(([3, 1.5], [2, 1], [4, 1.5], [3, 1], [3.5, 0.5], [2, 0.5], [5.5, 1], [1, 1], [4, 1.5]), dtype=float)
y = np.array(([1], [0], [1], [0], [1], [0], [1], [0]), dtype=float) # Output data 1 = red / 0 = blue
x_enter = x_enter / np.amax(x_enter, axis=0)
x = np.split(x_enter, [8])[0]
xPrediction = np.split(x_enter, [8])[1]
def sigmoid(_s):
return 1 / (1 + np.exp(- _s))
class NeuralNetwork(object):
def __init__(self):
self.inputSize = 2
self.outputSize = 1
self.hiddenSize = 3
self.W1 = np.random.randn(self.inputSize, self.hiddenSize) # Matrices 2x3
self.W2 = np.random.randn(self.hiddenSize, self.outputSize) # Matrices 3x1
def sigmoid_prime(self, s):
return s * (1 - s)
def forward(self, _x):
self.z = np.dot(_x, self.W1)
self.z2 = sigmoid(self.z)
self.z3 = np.dot(self.z2, self.W2)
o = sigmoid(self.z3)
return o
def backward(self, X, y, o):
self.o_error = y - o
self.o_delta = self.o_error * self.sigmoid_prime(o)
self.z2_error = self.o_delta.dot(self.W2.T)
self.z2_delta = self.z2_error * self.sigmoid_prime(self.z2)
self.W1 += X.T.dot(self.z2_delta)
self.W2 += self.z2.T.dot(self.o_delta)
def train(self, X, y):
_o = self.forward(X)
self.backward(X, y, _o)
def predict(self):
print('Predicted data after training: ')
print('Input: \n' + str(xPrediction))
print('Output: \n' + str(self.forward(xPrediction)))
if self.forward(xPrediction) < 0.5:
print('Its blue \n')
else:
print('Its red \n')
NN = NeuralNetwork()
for i in range(70000):
print('# ' + str(i), '\n')
print('Input value: \n', str(x))
print('Actual output: \n', str(y))
print('Output predicted: \n', str(np.matrix.round(NN.forward(x), 2)))
print('\n')
NN.train(x, y)
NN.predict()