forked from PINTO0309/DeepLearningMugenKnock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perceptron_not.py
45 lines (32 loc) · 822 Bytes
/
perceptron_not.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
import numpy as np
np.random.seed(0)
xs = np.array([[0], [1]], dtype=np.float32)
ts = np.array([1, 0], dtype=np.float32)
lr = 0.1
# perceptron
w = np.random.normal(0., 1, [1])
b = np.random.normal(0., 1, [1])
print("weight >>", w)
print("bias >>", b)
# add bias
z1 = xs
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# train
for ite in range(5000):
ite += 1
# feed forward
ys = sigmoid(np.dot(z1, w) + b)
#print("iteration:", ite, "y >>", ys)
En = -(ts - ys) * ys * (1 - ys)
grad_w = np.dot(z1.T, En)
grad_b = np.dot(np.ones([En.shape[0]]), En)
w -= lr * grad_w
b -= lr * grad_b
print("training finished!")
print("weight >>", w)
print("bias >>", b)
# test
for i in range(2):
ys = sigmoid(np.dot(z1[i], w) + b)[0]
print("in >>", xs[i], ", out >>", ys)