-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
88 lines (66 loc) · 1.61 KB
/
main.py
File metadata and controls
88 lines (66 loc) · 1.61 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright Austin Ward 2018
import numpy as np
'''
Creating neural network with the following shape
O---->O---->O --> 0
\ ^ \ ^
\ / \ /
X X
/ \ / \
/ v / v
O---->O---->O --> 1
'''
def sigmoid(z, derive=False):
if derive:
return z * (1 - z)
return 1/(1 + np.exp(-z))
features = np.array([
[0, 0],
[0, 1],
[1, 0],
[1, 1]
])
labels = np.array([
[1, 0],
[0, 1],
[0, 1],
[1, 0]
])
hidden_units = 2
output_units = 2
w01 = np.random.random((len(features[0]), hidden_units))
w12 = np.random.random((hidden_units, output_units))
# learning rate
eta = 0.1
# change to 20000 for training
epochs = range(20000)
for _ in epochs:
# Feed forward
z_h = np.dot(features, w01)
a_h = sigmoid(z_h)
z_o = np.dot(a_h, w12)
a_o = sigmoid(z_o)
# Squared error function
a_o_err = (1/2) * np.power(a_o - labels, 2)
# Backprop
# Output layer
delta_a_o_err = a_o - labels
delta_z_o = sigmoid(a_o, derive=True)
delta_w12 = a_h
delta_output = np.dot(delta_w12.T, (delta_a_o_err * delta_z_o))
# Hidden layer
delta_a_h = np.dot(delta_a_o_err * delta_z_o, w12.T)
delta_z_h = sigmoid(a_h, derive=True)
delta_w01 = features
delta_hidden = np.dot(delta_w01.T, delta_a_h*delta_z_h)
# Update weights
w01 -= (eta * delta_hidden)
w12 -= (eta * delta_output)
z_h = np.dot(features, w01)
a_h = sigmoid(z_h)
z_o = np.dot(a_h, w12)
a_o = sigmoid(z_o)
print(a_o)
for o, l in zip(a_o, labels):
print("Label: " + str(l))
print("Prediction: " + str(o))