-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathneural_network_from_scratch.py
57 lines (43 loc) · 1.34 KB
/
neural_network_from_scratch.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
# Neural Network from scratch
import matplotlib.pyplot as plt
import numpy as np
def sigmoid(x):
return 1.0 / (1 + np.exp(-x))
def sigmoid_derivative(d):
return d * (1 - d)
class NeuralNetwork:
def __init__(self, x, y):
self.input = x
self.w1 = np.random.rand(self.input.shape[1], 4)
self.w2 = np.random.rand(4, 1)
self.y = y
self.op = np.zeros(y.shape)
self.loss = []
def feedforward(self):
self.layer1 = sigmoid(np.dot(self.input, self.w1))
self.op = sigmoid(np.dot(self.layer1, self.w2))
self.loss.append(np.sum((self.y - self.op) ** 2))
def backprop(self):
d_w2 = np.dot(
self.layer1.T, (2 * (self.y - self.op) * sigmoid_derivative(self.op))
)
d_w1 = np.dot(
self.input.T,
(
np.dot(2 * (self.y - self.op) * sigmoid_derivative(self.op), self.w2.T)
* sigmoid_derivative(self.layer1)
),
)
self.w1 += d_w1
self.w2 += d_w2
if __name__ == "__main__":
X = np.array([[0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]])
y = np.array([[0], [1], [1], [0]])
nn = NeuralNetwork(X, y)
for i in range(1500):
nn.feedforward()
nn.backprop()
print(nn.op)
plt.plot(range(0, 1500), nn.loss)
plt.grid(True)
plt.show()