-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathperceptron.py
More file actions
154 lines (128 loc) · 3.86 KB
/
perceptron.py
File metadata and controls
154 lines (128 loc) · 3.86 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
import sys
import numpy as num
train_data = []
weights = []
def Readfile():
with open("training-data.txt") as file:
cnt,ind = 0,0
train = num.zeros(64, dtype=int)
for line in file:
if cnt == 0:
train[0] = 1
ind+=1
cnt+=1
for element in line:
if element=='#':
train[ind] = 1
ind+=1
elif element=='*':
train[ind] = -1
ind+=1
elif cnt==9:
cnt,ind = 0,0
train_data.append(train)
train = num.zeros(64,dtype=int)
else:
for element in line:
if element=='#':
train[ind] = 1
ind+=1
elif element=='*':
train[ind] = -1
ind+=1
cnt+=1
return train_data
def Actfunc (y_in):
if y_in > 0:
y_out = 1
else:
y_out = -1
return y_out
def Learn(train_data):
weights = []
for j in range(0, 7):
weight = num.random.rand(1, 64)
epochs = 1
while True:
old_weights = weights.copy()
for i in range(0, len(train_data)):
x = train_data[i]
y_in = weight.dot(x)
y = Actfunc(y_in)
if i % 7 == j:
t = 1
else:
t=-1
if y != t:
weight += t * x.T
if num.array_equal(old_weights, weights):
break
epochs += 1
weights.append(weight)
print (weights)
return weights
""" epoch = 0
for j in range(0,7):
weight = num.random.rand( 64,)
backstep = weight.copy()
for i in range (0, len(train_data)):
while True:
s = train_data[i]
y_in = weight.dot(s)
Actfunc(y_in)
if i%7 == j:
target = 1
else :
traget = -1
if Actfunc(y_in) != target :
weight += target * train_data[i]
if num.array_equal(backstep, weight):
weights.append(weight)
break
print(weights)
print (weights)
return weights"""
def test (weights):
character = input("Please enter character: ")
if character == "A":
targ = 0
elif character == "B":
targ = 1
elif character == "C":
targ = 2
elif character == "D":
targ = 3
elif character == "E":
targ = 4
elif character == "J":
targ = 5
elif character == "K":
targ = 6
with open("test-data.txt") as file:
test = num.zeros(64, dtype=int)
ind = 1
test[0]== 1
for line in file:
for element in line:
if element=='#':
test[ind] = 1
ind+=1
elif element=='*':
test[ind] = -1
ind+=1
testweight = num.zeros((64,1), dtype=int)
for j in range (0, 7):
testweight = weights[j]
y_in = testweight.dot(test)
if Actfunc (y_in) == 1 and j == targ:
print("Perceptron recognized character")
elif Actfunc (y_in) == 1 and j != targ:
print (j)
print("Perceptron failed")
elif Actfunc (y_in) == -1 and j == targ:
print("Silly perceptron")
#print (weight)
def main():
Learn(Readfile())
test(Learn(Readfile()))