-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWeek_4_AND.py
74 lines (46 loc) · 1.33 KB
/
Week_4_AND.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import random as rand
import numpy as np
# importing sys for taking argument
import sys
def signum(x): return 0 if x < 0 else 1
def predict(inputs, target):
result = np.dot(inputs, weights)
result = signum(result)
print("Target = {}, predicted = {}".format(target, result))
def random_weight(size):
return 2 * np.random.random((size,)) - 1
training_data = np.array([
[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 1, 0, 0],
[1, 0, 0, 0],
[1, 1, 0, 0],
[1, 0, 1, 0],
[0, 1, 1, 0],
[1, 1, 1, 1]
])
np.random.seed(1)
weights = random_weight(3)
learning_rate = 0.3
print("W = {}".format(weights))
for j in range(500):
x = rand.choice(training_data)
row = x[:3]
target = x[3:][0]
output = [0, 0, 0]
output = np.dot(row, weights)
output = signum(output)
# d_weights = np.array([0,0,0])
d_weights = [0, 0, 0]
for k in range(len(row)):
d_weights[k] = learning_rate * (target - output) * row[k]
# print("dW = {}".format(d_weights))
weights = weights + d_weights
print("W = {}".format(weights))
if len(sys.argv) > 1:
if sys.argv[1] == 't':
for i in range(len(training_data)):
predict(training_data[i][:3], training_data[i][3:][0])
if sys.argv[1] == 'i':
inputs = [int(i) for i in sys.argv[2:5]]
predict(inputs)