-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNN_tutorial2.py
More file actions
35 lines (25 loc) · 1.1 KB
/
NN_tutorial2.py
File metadata and controls
35 lines (25 loc) · 1.1 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
32
33
34
35
import numpy as np
class NeuralNetwork():
def __init__(self):
np.random.seed(1)
self.weight = 2 * np.random.random((3,1)) - 1
self.output = np.empty((4,1))
def forward(self, x):
return 1 / (1+np.exp(-x))
def backward(self, x):
return x * (1-x)
def train(self, training_inputs, training_outputs, training_iterations):
for iteration in range(training_iterations):
input_layer = training_inputs
self.output = self.forward(np.dot(input_layer, self.weight))
error = training_outputs - self.output
adjust = error * self.backward(self.output)
self.weight += np.dot(input_layer.T, adjust)
if __name__ == "__main__":
neural_net = NeuralNetwork()
print("Initial Weights: ", neural_net.weight)
training_input = np.array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
training_output = np.array([[0], [1], [1], [0]])
neural_net.train(training_input, training_output, 50000)
print("outputs after training", neural_net.weight)
print("outputs after training", neural_net.output)