-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathperceptron_momentum.py
executable file
·56 lines (34 loc) · 1.19 KB
/
perceptron_momentum.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
#!/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 = 1000
learning_rate = 0.01
momentum_gama = 0.9
velocity = np.array([[0.0], [0.0], [0.0]])
for i in range(epoch_number):
predict = sigmoid(np.dot(features, weights1))
delta1 = (predict - labels) * predict * (1 - predict)
grad = np.dot(features.T, delta1)
velocity = momentum_gama * velocity + learning_rate * grad
weights1 -= velocity
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))
test_dataset = [[1, 0, 1]]
predict_propability = sigmoid(np.dot(test_dataset, weights1))
print("The predict propability is: {}".format(predict_propability))
if __name__ == "__main__":
main()