forked from maxlorenz/Simple_NN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneural_network.py
61 lines (46 loc) · 1.73 KB
/
neural_network.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
from random import random, choice
class Neuron(object):
def __init__(self, num_inputs):
self.inputs = []
self.learning_rate = 0.01
self.weights = [random() for _ in range(num_inputs)]
self.bias = random()
def activation(self, x):
"ReLU function"
return (x > 0) * x
def sensitivity(self, x):
"Derivative of ReLU"
return (x > 0) * 1
def output(self):
c = sum([w * i for w, i in zip(self.weights, self.inputs)])
return self.activation(self.bias + c)
def adjust(self, error):
correction = self.sensitivity(self.output())
correction *= self.learning_rate * error
self.weights = [w + correction * i
for w, i in zip(self.weights, self.inputs)]
self.bias += correction
class NeuralNetwork(object):
def __init__(self, inputs=2, hidden_neurons=2):
self.hidden = [Neuron(inputs)
for _ in range(hidden_neurons)]
self.y = Neuron(hidden_neurons)
def predict(self, input):
for h in self.hidden:
h.inputs = input
self.y.inputs = [h.output() for h in self.hidden]
return self.y.output()
def learn(self, input, target):
error = target - self.predict(input)
self.y.adjust(error)
for h, w in zip(self.hidden, self.y.weights):
h.adjust(error * w)
if __name__ == "__main__":
nn = NeuralNetwork(inputs=2, hidden_neurons=4)
training = [([0, 0], 0), ([0, 1], 1), ([1, 0], 1), ([1, 1], 0)]
for epoc in range(100000):
input, target = choice(training)
nn.learn(input, target)
for input, target in training:
print('IN: {}, EXPECTED: {}, RESULT: {:.2f}'
.format(input, target, nn.predict(input)))