-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNetwork.py
More file actions
208 lines (152 loc) · 5.68 KB
/
Copy pathNeuralNetwork.py
File metadata and controls
208 lines (152 loc) · 5.68 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import random
import numpy as np
class Relu:
def __init__(self):
self.output = None
self.div = None
self.input = None
def forward(self, layer_output):
self.input = layer_output
for i in range(len(layer_output)):
layer_output[i] = max(0, layer_output[i])
self.output = layer_output
def backward(self, layer_output):
relu_output = layer_output.copy()
for i in range(len(layer_output)):
if layer_output[i] <= 0:
relu_output[i] = 0
else:
relu_output[i] = 1
self.div = relu_output
class Softmax:
def __init__(self):
self.output = None
self.div = None
self.input = None
def forward(self, layer_output):
self.input = layer_output
exp_values = np.exp(layer_output - np.max(layer_output))
probabilities = exp_values / np.sum(exp_values)
self.output = probabilities
def backward(self, _):
self.div = 1
class Sigmoid:
def __init__(self):
self.output = None
self.div = None
self.input = None
def forward(self, layer_output):
self.input = layer_output
def backward(self, layer_output):
pass
class Tanh:
def __init__(self):
self.output = None
self.div = None
self.input = None
def forward(self, layer_output):
self.input = layer_output
def backward(self, layer_output):
pass
class Layer:
def __init__(self, weights, activation_function=None):
self.weights = weights
self.dropout_table = np.ones(self.weights.shape[1])
self.dropout_multiplayer = 1
self.dropout = False
self.activation_function = activation_function
self.input = None
self.output = None
def forward(self, input: list):
self.input = input
if self.dropout:
self.output = np.dot(self.weights*self.dropout_table, input) * self.dropout_multiplayer
else:
self.output = np.dot(self.weights, input)
if self.activation_function is None:
return
self.activation_function.forward(self.output)
self.activation_function.backward(self.output)
self.output = self.activation_function.output
def backward(self, delta, alpha):
weight_delta = np.outer(delta, self.input)
if self.dropout:
weight_delta = weight_delta*self.dropout_table
self.adjust_weights(weight_delta, alpha)
def dropout_on(self):
self.dropout = True
n = self.weights.shape[1]
print(n)
for _ in range(n//2):
self.dropout_table[random.randint(0, n-1)] = 0
self.dropout_multiplayer = n/sum(self.dropout_table)
def dropout_off(self):
self.dropout = False
self.dropout_table = np.ones(self.weights.shape[1])
def adjust_weights(self, weight_delta, alpha):
self.weights = self.weights - (alpha * weight_delta)
def get_input_size(self):
return len(self.weights)
class NeuralNetwork:
def __init__(self, alpha, input_size, dropout_probability=0.3):
self.alpha = alpha
self.layers = []
self.series = []
self.goals = []
self.input_size = input_size
self.dropout_probability = dropout_probability
def add_layer(self, n, weight_range=(-0.1, 0.1), activation_function=Relu()):
if not len(self.layers) == 0:
last_input_size = self.layers[len(self.layers)-1].get_input_size()
else:
last_input_size = self.input_size
new_weights = np.random.uniform(weight_range[0], weight_range[1], size=(n, last_input_size))
new_layer = Layer(new_weights, activation_function)
self.layers.append(new_layer)
def random_dropout_on(self):
for i in range(len(self.layers)):
layer = self.layers[i]
if i == len(self.layers) - 1:
return
if random.random() < self.dropout_probability:
layer.dropout_on()
def random_dropout_off(self):
for i in range(len(self.layers)):
layer = self.layers[i]
if i == len(self.layers) - 1:
return
layer.dropout_off()
def fit(self, _input: list, expected_output: list, random_dropout=False):
if random_dropout:
self.random_dropout_on()
layer_input = _input
for layer in self.layers:
layer.forward(layer_input)
layer_input = layer.output
output_delta = (2 / len(layer_input)) * (np.subtract(layer_input, expected_output))
delta = output_delta
for layer in reversed(self.layers):
delta = delta * layer.activation_function.div
layer.backward(delta, self.alpha)
delta = np.dot(np.transpose(layer.weights), delta)
if random_dropout:
self.random_dropout_off()
def predict(self, input: list):
layer_input = input
for layer in self.layers:
layer.forward(layer_input)
layer_input = layer.output
return layer_input
def save_weights(self, file_name):
pass
def load_weights(self, file_name):
pass
def do_epochs(self, epochs):
for epoch_num in range(0, epochs):
correct = 0
for series_num in range(0, len(self.series)):
output_ = self.fit(self.series[series_num], self.goals[series_num])
if np.argmax(output_) == np.argmax(self.goals[series_num]):
correct += 1
print("Epoch:", epoch_num + 1, "Accuracy:", correct / len(self.series) * 100)
return self.weights_list