-
Notifications
You must be signed in to change notification settings - Fork 0
/
Perceptron.py
103 lines (80 loc) · 3.02 KB
/
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
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
import random
class Perceptron:
def __init__(self):
self.eta = 0.01
self.maxError = 0
self.sumError = 1
self.max_epoch = 5000
self.targetList = []
self.weights = []
self.errorList = []
self.allWeights = []
self.currentList= []
def generateWeights(self):
self.weights.clear()
self.errorList.clear()
for i in range(36):
weight = random.random() + 1
self.weights.append(weight)
for i in range(300):
self.errorList.append(100)
def getOutput(self, inputArray, weightList):
sum = 0
sum += -1 * weightList[0]
for i in range(35):
sum += (inputArray[i] * weightList[i+1])
if(sum > 0):
return 1
return 0
def getTarget(self, label, perceptronValue):
if(label == perceptronValue):
return 1
return 0
def activating(self, inputList):
# loops through each perceptron
for i in range(10):
# generate weights
self.iterations = 0
self.sumError = 1
self.generateWeights()
self.targetList.clear()
for j in range(300):
self.targetList.append(self.getTarget(inputList[j][-1], i))
while(self.sumError != 0 and self.iterations != self.max_epoch):
self.iterations += 1
#print("iteration:", self.iterations)
# loops through all tests
for h in range(300):
output = self.getOutput(inputList[h], self.weights)
target = self.targetList[h]
# finds the error
error = target - output
self.errorList[h] = abs(error)
self.weights[0] += self.eta * error * -1
for k in range(35):
self.weights[k+1] += self.eta * error * inputList[h][k]
self.sumError = 0
for errors in self.errorList:
self.sumError += errors
for j in range(36):
self.allWeights.append(self.weights[j])
def classification(self, array1D):
value = []
sum = 0
storedValue = -1
for i in range(10): #loops through all 10 perceptrons
tempArr = []
for j in range(36):
index = i*36 + j
tempArr.append(self.allWeights[index])
value.append(self.getOutput(array1D, tempArr))
# tests for exclusivity
for i in range(10):
if(value[i] == 1):
storedValue = i
sum += value[i]
print(value)
if(sum > 1 or sum == 0):
return "error"
else:
return storedValue