-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbgrNN01.py
66 lines (45 loc) · 1.43 KB
/
bgrNN01.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
62
63
64
65
66
#!/usr/bin/python3
#Simple neural network code
# based on https://medium.com/technology-invention-and-more/how-to-build-a-simple-neural-network-in-9-lines-of-python-code-cc8f23647ca1
# Removed matrix codes to make it more readable
# thanks to aib
from math import e
def normalize (x):
return (1 / ( 1 + pow(e, -1*x)))
def denormalize (x):
return (x * (1-x))
def iterateOne(inputs, output, _weights) :
result = 0
for i in range(len(inputs)):
result = result + inputs[i] * _weights[i]
result = normalize(result)
error = result - output
#print ("error :",error)
for i in range(len(inputs)):
_weights[i] = _weights[i] - ( error * inputs[i] * denormalize(result))
#print ("weights :", _weights)
return _weights
def iterateAll(inputList, outputList, _weights):
for i in range(len(inputList)):
iterateOne (inputList[i], outputList[i], _weights)
return _weights
def testNN(inputs, _weights):
result = 0
for i in range(len(inputs)):
result = result + inputs[i] * _weights[i]
result = normalize(result)
print ("result:", result)
def main():
inputList = [[0,0,1],[1,1,1],[1,0,1],[0,1,1]]
outputList = [0,1,1,0]
weights =[1,1,1]
test_inputs = [1,0,0]
iteration = 50
print ("weights :", weights)
for i in range(iteration):
print (i, ".deneme")
weights = iterateAll(inputList, outputList, weights)
testNN(test_inputs, weights)
print ("weights:", weights)
#iterateOne (inputList[2], outputList[2])
main()