-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
executable file
·149 lines (117 loc) · 4.1 KB
/
code.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
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
import numpy as np
def read_file():
with open("IrisData.txt", "r") as flowerFile:
flowerData = np.empty([50, 5, 3])
noSample = type = 0
next(flowerFile)
for line in flowerFile:
line = line.split(",")
line[-1] = type
flowerData[noSample % 50, :, type] = np.array(line)
noSample += 1
if noSample % 50 == 0:
type += 1
np.random.shuffle(flowerData)
return flowerData
############################################################################
class BackPropagation:
def __init__(self, eta, epochs, bias, neurons, fn, data):
self.noSampleType = len(data[:, 0, 0])
self.noFeature = len(data[0, :-1, 0])
self.noOut = len(data[0, 0, :])
self.toTrain = 30 # < 50 noSampleType
self.toTest = self.noSampleType - self.toTrain
self.noData = self.noSampleType * self.noOut
self.noTrain = self.toTrain * self.noOut
self.noTest = self.toTest * self.noOut
self.eta = eta
self.epochs = epochs
self.bias = bias
self.neurons = [self.noFeature] + neurons + [self.noOut]
self.layers = len(self.neurons)
obj = Activation()
self.activation = (
[obj.sigmoid, obj.sigmoid_]
if fn == "Sigmoid"
else [obj.hyperbolicTangent, obj.hyperbolicTangent_]
if fn == "Hyperbolic Tangent"
else None
)
self.trainSample, self.testSample = self.divide(data)
self.W, self.net, self.error = self.network()
self.indexer = 0
def train(self):
algorithm = [self.forward, self.backward, self.update]
[
[[_() for _ in algorithm] for _ in range(self.noTrain)]
for _ in range(self.epochs)
]
def forward(self):
self.net[0][1:] = self.trainSample[self.indexer % self.noTrain, :-1, np.newaxis]
for i in range(self.layers - 1):
self.net[i + 1][1:] = self.activation[0](np.dot(self.W[i], self.net[i]))
def backward(self):
actual = np.zeros(self.net[-1][1:].shape)
actual[int(self.trainSample[self.indexer % self.noTrain, -1])] = 1
self.indexer += 1
self.error[-1] = self.activation[1](self.net[-1][1:]) * (
actual - self.net[-1][1:]
)
for i in reversed(range(self.layers - 2)):
self.error[i] = self.activation[1](self.net[i + 1][1:]) * np.dot(
self.W[i + 1][:, 1:].transpose(), self.error[i + 1]
)
def update(self):
for i in range(self.layers - 1):
self.W[i] += self.eta * np.dot(self.error[i], self.net[i].transpose())
def test(self):
success = 0
for i in range(self.noTest):
self.net[0][1:] = self.testSample[i, :-1, np.newaxis]
for j in range(self.layers - 1):
self.net[j + 1][1:] = self.activation[0](np.dot(self.W[j], self.net[j]))
if self.testSample[i, -1] == np.argmax(self.net[-1][1:]):
success += 1
return round((success / self.noTest) * 100, 1)
def divide(self, data):
train, test = np.empty([self.noTrain, self.noFeature + 1]), np.empty(
[self.noTest, self.noFeature + 1]
)
for i in range(self.toTrain):
for j in range(self.noOut):
train[i * self.noOut + j] = data[i, :, j]
for i in range(self.toTest):
for j in range(self.noOut):
test[i * self.noOut + j] = data[i + self.toTrain, :, j]
np.random.shuffle(train)
np.random.shuffle(test)
return train, test
def network(self):
W, net, error = ([] for _ in range(3))
for i in range(self.layers - 1):
W.append(
np.insert(
np.random.rand(self.neurons[i + 1], self.neurons[i])
/ np.sqrt(self.neurons[i] + 1),
0,
self.bias,
axis=1,
)
)
error.append(np.empty([self.neurons[i + 1], 1]))
for i in range(self.layers):
net.append(np.insert(np.empty([self.neurons[i], 1]), 0, self.bias, axis=0))
return W, net, error
############################################################################
class Activation:
def __init__(self):
self.a = 1 # > 0 slope of sigmoid
self.b = 1 # > 0 case tanh
def sigmoid(self, x):
return 1 / (1 + np.exp(-self.a * x))
def sigmoid_(self, x):
return self.a * x * (1 - x)
def hyperbolicTangent(self, x):
return self.a * np.tanh(x * self.b)
def hyperbolicTangent_(self, x):
return (self.b / self.a) * (self.a - x) * (self.a + x)