-
Notifications
You must be signed in to change notification settings - Fork 0
/
ann2.py
46 lines (35 loc) · 1.4 KB
/
ann2.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
from black import out
import numpy as np
class NeuralNetwork():
def __init__(self):
np.random.seed(1)
self.synaptic_weights=2*np.random.random((3,1))-1
def sigmoid(self,x):
return 1/(1+np.exp(-x))
def sigmoid_derivatives(self,x):
return x*(1-x)
def train(self,training_inputs,training_outputs,training_iterations):
for iteration in range(training_iterations):
output=self.think(training_inputs)
error=training_outputs-output
adjustments=np.dot(training_inputs.T,error*self.sigmoid_derivatives(output))
self.synaptic_weights+=adjustments
def think(self,inputs):
inputs=inputs.astype(float)
output=self.sigmoid(np.dot(inputs,self.synaptic_weights))
return output
if __name__== "__main__":
neural_network=NeuralNetwork()
print('Random Synaptic Weights: ')
print(neural_network.synaptic_weights)
training_inputs=np.array([[0,0,1],[1,1,1],[1,0,1],[0,1,1]])
training_outputs=np.array([[0,1,1,0]]).T
neural_network.train(training_inputs,training_outputs,1000)
print('Synaptic weight after training')
print(neural_network.synaptic_weights)
A=str(input("input 1: "))
B=str(input("input 2: "))
C=str(input("input 3: "))
print("New Situation input data= ",A,B,C)
print("Output data: ")
print(neural_network.think(np.array([A,B,C])))