forked from tobegit3hub/ml_implementation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural_network.py
executable file
·47 lines (30 loc) · 998 Bytes
/
neural_network.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
#!/usr/bin/env python
import math
import numpy as np
def sigmoid(x):
return 1.0 / (1 + np.exp(-x))
def sigmoid_derivate(x):
return x * (1 - x)
def main():
np.random.seed(1)
# [4, 3]
features = np.array([[0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]])
# [4, 1]
labels = np.array([[0], [0], [1], [1]])
# weights1 = 2 * np.random.random((3,1)) - 1
weights1 = np.array([[1.0], [1.0], [1.0]])
epoch_number = 100
learning_rate = 0.01
for i in range(epoch_number):
input_layer = features
layer1 = sigmoid(np.dot(input_layer, weights1))
difference1 = labels - layer1
delta1 = -1.0 * difference1 * sigmoid_derivate(layer1)
grad = np.dot(input_layer.T, delta1)
weights1 -= learning_rate * grad
print("Current weights is: {}".format(weights1))
test_dataset = [[0, 0, 1]]
predict_propability = sigmoid(np.dot(test_dataset, weights1))
print("The predict propability is: {}".format(predict_propability))
if __name__ == "__main__":
main()