-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn_network.py
More file actions
132 lines (80 loc) · 3.2 KB
/
n_network.py
File metadata and controls
132 lines (80 loc) · 3.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# coding: utf-8
# In[60]:
import numpy as np
import sys
# In[61]:
class PartyNN(object):
def __init__(self, learning_rate=0.1):
self.weights_0_1 = np.random.normal(0.0, 2 ** -0.5, (2, 3))
self.weights_1_2 = np.random.normal(0.0, 1, (1, 2))
self.sigmoid_mapper = np.vectorize(self.sigmoid)
self.learning_rate = np.array([learning_rate])
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def predict(self, inputs):
inputs_1 = np.dot(self.weights_0_1, inputs)
outputs_1 = self.sigmoid_mapper(inputs_1)
inputs_2 = np.dot(self.weights_1_2, outputs_1)
outputs_2 = self.sigmoid_mapper(inputs_2)
return outputs_2
def train(self, inputs, expected_predict):
inputs_1 = np.dot(self.weights_0_1, inputs)
outputs_1 = self.sigmoid_mapper(inputs_1)
inputs_2 = np.dot(self.weights_1_2, outputs_1)
outputs_2 = self.sigmoid_mapper(inputs_2)
actual_predict = outputs_2[0]
error_layer_2 = np.array([actual_predict - expected_predict])
gradient_layer_2 = actual_predict * (1 - actual_predict)
weights_delta_layer_2 = error_layer_2 * gradient_layer_2
self.weights_1_2 -= (np.dot(weights_delta_layer_2, outputs_1.reshape(1, len(outputs_1)))) * self.learning_rate
error_layer_1 = weights_delta_layer_2 * self.weights_1_2
gradient_layer_1 = outputs_1 * (1 - outputs_1)
weights_delta_layer_1 = error_layer_1 * gradient_layer_1
self.weights_0_1 -= np.dot(inputs.reshape(len(inputs), 1), weights_delta_layer_1).T * self.learning_rate
# In[62]:
def MSE(y, Y):
return np.mean((y-Y)**2)
# In[66]:
train = [
([0, 0, 0], 0),
([0, 0, 1], 1),
([0, 1, 0], 0),
([0, 1, 1], 0),
([1, 0, 0], 1),
([1, 0, 1], 1),
([1, 1, 0], 0),
([1, 1, 1], 0),
]
# In[78]:
#epochs = 5000
#learning_rate = 0.05
epochs = 6000
learning_rate = 0.08
network = PartyNN(learning_rate=learning_rate)
for e in range(epochs):
inputs_ = []
correct_predictions = []
for input_stat, correct_predict in train:
network.train(np.array(input_stat), correct_predict)
inputs_.append(np.array(input_stat))
correct_predictions.append(np.array(correct_predict))
train_loss = MSE(network.predict(np.array(inputs_).T), np.array(correct_predictions))
sys.stdout.write("\rProgress: {}, Training loss: {}".format(str(100 * e/float(epochs))[:4], str(train_loss)[:5]))
# In[79]:
for input_stat, correct_predict in train:
print("For input: {} the prediction is: {}, expected: {}".format(
str(input_stat),
str(network.predict(np.array(input_stat)) > .5),
str(correct_predict == 1)))
# In[80]:
for input_stat, correct_predict in train:
print("For input: {} the prediction is: {}, expected: {}".format(
str(input_stat),
str(network.predict(np.array(input_stat))),
str(correct_predict == 1)))
# In[81]:
network.weights_0_1
# In[82]:
network.weights_1_2
# In[ ]: