-
Notifications
You must be signed in to change notification settings - Fork 0
/
4-6-9 core_nn SOL_again.py
44 lines (37 loc) · 1.43 KB
/
4-6-9 core_nn SOL_again.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
#again... writing single neuron network from the head....
import numpy as nu
# input
input_data = nu.array([[1, 1, 0], [0, 1, 0], [0, 0, 1], [1, 1, 1]])
output_data = nu.array([[1, 0, 0, 1]]).T
test = nu.array([1, 0, 0])
class NeuralNetwork():
# weights
def __init__(self):
nu.random.seed(1)
self.weights = 2 * nu.random.random((3, 1)) - 1
def sigmoid(self, x):
# multiplyi in one neuron plus normalize in sygmoid
return 1 / (1 + nu.exp(-x))
def derivative(self, x):
# count derivative to make backpropagation
return x * (1 - x)
def train(self, input_data, output_data, nr_iterations):
for i in iter(range(nr_iterations)):
output = self.think(input_data)
error = output_data - output
adjustment = nu.dot(input_data.T, error * self.derivative(output))
self.weights += adjustment
if (i % 1000 == 0):
print ("error after %s iteration: %s" % (i, str(nu.mean(nu.abs(error)))))
def think(self, input_data):
return self.sigmoid(nu.dot(input_data, self.weights))
nn = NeuralNetwork()
# print (nn.weights)
print ("NN's result without training:", nn.think(test))
print ("Initial weights: ")
print (nn.weights)
nn.train(input_data, output_data, 10000)
print ("Trained weights: ", nn.weights)
test = [1, 0, 0]
print ("Now we'll predict the output value for new data: ", test)
print (nn.think(nu.array(test)))