-
Notifications
You must be signed in to change notification settings - Fork 1
/
NeuralNetwork.py
68 lines (43 loc) · 1.51 KB
/
NeuralNetwork.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
67
68
import numpy as np
np.random.seed(1)
synapticWeights = 2 * np.random.random((3, 1)) - 1
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoidDerivative(x):
return x * (1 - x)
def base(inputs):
inputs = inputs.astype(float)
return sigmoid(np.dot(inputs, synapticWeights))
def train(trainingInputs, trainingOutputs, trainingIterations):
global synapticWeights
for iteration in range(trainingIterations):
output = base(trainingInputs)
error = trainingOutputs - output
adjustments = np.dot(trainingInputs.T, error * sigmoidDerivative(output))
synapticWeights += adjustments
print("Random starting weights: ")
print(synapticWeights)
trainingInputs = np.array([[0,0,1],
[1,1,1],
[1,0,1],
[0,1,1]])
trainingOutputs = np.array([[0,1,1,0]]).T
train(trainingInputs, trainingOutputs, 10000)
print("Weights after training: ")
print(synapticWeights)
print("-------------------")
input1 = str(input("Input 1: "))
input2 = str(input("Input 2: "))
input3 = str(input("Input 3: "))
print("Input data = ", input1, input2, input3)
print("-------------------")
print("Output data: ")
print(base(np.array([input1, input2, input3])))
"""
input 1: 1
input 2: 0
input 3: 0
Output:
somthing close to 1
The neural network learns that if there is a 1 in the first column, that the output should be 1
"""