forked from serengil/tensorflow-101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
single-layer-perceptron.py
53 lines (36 loc) · 1.27 KB
/
single-layer-perceptron.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
import numpy as np
operator = 'and'
#----------------
atributes = np.array([ [0, 0], [0, 1], [1, 0], [1, 1]])
if operator == 'and':
labels = np.array([0, 0, 0, 1])
elif operator == 'or':
labels = np.array([0, 1, 1, 1])
elif operator == 'xor':
labels = np.array([0, 1, 1, 0])
#----------------
w = [+9, +9] #initial random values for weights
threshold = 5
alpha = 0.5 #learning rate
epoch = 1000 #learning time
#----------------
print("learning rate: ", alpha,", threshold: ", threshold)
for i in range(0, epoch):
print("epoch ", i+1)
global_delta = 0 #this variable is used to terminate the for loop if learning completed in early epoch
for j in range(len(atributes)):
actual = labels[j]
sum = atributes[j][0]*w[0] + atributes[j][1]*w[1]
if sum > threshold: #then fire
predicted = 1
else: #do not fire
predicted = 0
delta = actual - predicted
global_delta = global_delta + abs(delta)
#update weights with respect to the error
for k in range(0, 2):
w[k] = w[k] + delta * alpha
print(atributes[j][0]," ", operator, " ", atributes[j][1], " -> actual: ", actual, ", predicted: ", predicted, " (w: ",w[0],")")
if global_delta == 0:
break
print("------------------------------")