-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
31 lines (29 loc) · 1.06 KB
/
Copy pathmain.py
File metadata and controls
31 lines (29 loc) · 1.06 KB
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
import numpy as np
class Network:
def __init__(self):
#3 random weight for nodes
self.weight = np.random.random((3, 1))
def train(self, given_input, given_output, trainingrange):
for iteration in range(trainingrange):
output = self.think(given_input)
error = given_output - output
adjustment = np.dot(given_input.T, error * self.sigmoid_derivative(output))
self.weight += adjustment
def think(self, given_input):
return self.sigmoid(np.dot(given_input, self.weight))
def sigmoid_derivative(self, x):
#derivative Sigmoid function
return x * (1 - x)
def sigmoid(self, x):
#sigmoid function
return 1 / (1 + np.exp(-x))
training_inputs = np.array([[0,0,1],
[1,1,1],
[1,0,1],
[0,1,1]])
training_output = np.array([[1,0,0.7,0]]).T
nt = Network()
print(nt.weight)
nt.train(training_inputs, training_output, 10)
print(nt.weight)
print(nt.think([0.4,0.2,0]))