-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest.py
320 lines (270 loc) · 10.5 KB
/
test.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
from numpy import random
import numpy as np
from timer import call_repeatedly
#Sigmoid Function
def sigmoid(x):
return 1 / (1 + np.exp(-x))
#a relu activation function
def relu(self, x):
return np.maximum(0,x)
#
neurons_size = 4
neurons_ids = np.arange(0, neurons_size)
neurons_excites = random.randint(0,size=neurons_size)
print("Neurons Size", neurons_size)
print("Neurons Id", neurons_ids)
print("Neurons Excites", neurons_excites)
print()
"""Neurons stacks"""
neurons_stacks_size = neurons_size
neurons_stacks = {}
def update_neurons_stacks(value):
for key in neurons_stacks:
neurons_stacks[key] = 0
neurons_stacks[value] = 1
sorted_a = []
for key in neurons_stacks:
sorted_a.append(key)
# sorted_a.sort()
# print("Sorted_a", sorted_a)
print("Stacks size", neurons_stacks_size)
"""Neurons Stacks"""
synapse_size = 4
synapse_hosts = []
synapse_target = []
for i in range(0, neurons_size):
for x in range(0, neurons_size):
if i != x:
synapse_hosts.append(i)
synapse_target.append(x)
synapse_size = len(synapse_hosts)
synapse_weights = np.random.random(size=synapse_size)
synapse_hebians = np.zeros(synapse_size)
"""Hebbians stacks"""
hebbians_stacks_size = synapse_hebians.size
hebbians_stacks = {}
def update_hebbians_stacks(value):
for key in hebbians_stacks:
hebbians_stacks[key] = 0
hebbians_stacks[value] = 1
sorted_a = []
for key in hebbians_stacks:
sorted_a.append(key)
# sorted_a.sort()
# print("Sorted_a", sorted_a)
print("Stacks size", hebbians_stacks_size)
"""Hebbians Stacks"""
# def find_hebbian():
# for indd in range(0, synapse_size):
# host = synapse_hosts[indd]
# target = synapse_target[indd]
# last_average = synapse_hebians[indd]
# new_average = (host + target) / 2
# np.append(synapse_hebians, (last_average + new_average) / 2)
# update_stacks(indd,new_average)
def find_hebbian():
neurons_connected_with_values = {}
for indd in range(0, synapse_size):
host = neurons_excites[synapse_hosts[indd]]
target = neurons_excites[synapse_target[indd]]
last_average = synapse_hebians[indd]
new_average = (host + target) / 2
synapse_hebians[indd] = (last_average + new_average) / 2
for indd in range(0, len(synapse_hosts)):
neuron_index = synapse_hosts[indd]
synapse_value = synapse_hebians[indd]
if neuron_index in neurons_connected_with_values:
neurons_connected_with_values[neuron_index].append(synapse_value)
else:
neurons_connected_with_values[neuron_index] = [synapse_value]
for avvg in neurons_connected_with_values:
neurons_excites[avvg] = np.average(neurons_connected_with_values[avvg])
for values in neurons_excites:
update_neurons_stacks(values)
for values in synapse_hebians:
update_hebbians_stacks(values)
find_hebbian()
call_repeatedly(20, find_hebbian)
print("Synapse size", 4)
print("Synapse Host", synapse_hosts)
print("Synapse Target", synapse_target)
print("Synapse Hebians", synapse_hebians)
print("Synapse Weights", synapse_weights)
print()
perceptron_size = synapse_size
perceptron_outputs = []
perceptron_weights = synapse_weights
perceptron_losses = []
for i in range(0, perceptron_size):
synapse_weights = np.random.random(size=synapse_size)
perceptron_weights = synapse_weights
synapse_host_excite = neurons_excites[synapse_hosts[i] - 1]
synapse_target_excite = neurons_excites[synapse_target[i] - 1]
output = (synapse_host_excite * synapse_hebians[i]) + perceptron_weights[i]
loss = output - synapse_target_excite
perceptron_outputs.append(output)
perceptron_losses.append(loss)
neurons_excites[synapse_target[i] - 1] = output
print("Perceptron Size", perceptron_size)
print("Perceptron Outputs", perceptron_outputs)
print("Perceptron Weights", np.array(perceptron_weights))
print("Perceptron Losses", np.array(perceptron_losses))
class VisionNeuron():
def __init__(self, inputs, input_size, bias=None):
self.input_size = input_size
self.inputs = inputs
if bias:
self.bias = bias
else:
self.bias = random.rand()
self.weights = random.random(size=input_size)
self.calculateOutput(inputs=self.inputs)
# print("Bias ", self.bias)
# print("Weights ", self.weights)
# print("Output ", self.output)
def checkActivation(self, neuron):
for i in range(0, self.input_size):
for j in range(0, neuron.input_size):
if self.inputs[i] == neuron.inputs[j]:
self.excite = 1
neuron.excite = 1
return
self.excite = 0
neuron.excite = 0
return
def recalculateWeights(self):
self.weights = random.random(size=self.input_size)
def recalcuateBias(self):
self.bias = random.rand()
def calculateOutput(self, inputs):
self.inputs = inputs
output = 0
for j in range(0, self.input_size):
output += self.inputs[j] * self.weights[j]
output += self.bias
self.output = sigmoid(output)
#implement a Backprop and Optimizer function for the perceptrons
def backprop(self, output, target, learning_rate):
#calculate the loss
loss = output - target
#calculate the gradient of the loss
gradient = relu_derivative(output)
gradient *= loss
#update the weights
for i in range(0, self.input_size):
self.weights[i] -= learning_rate * gradient * self.inputs[i]
#update the bias
self.bias -= learning_rate * gradient
return loss
"""
def update(self, inputs, learning_rate):
#calculate the output
output = 0
for j in range(0, self.input_size):
output += self.inputs[j] * self.weights[j]
output += self.bias
self.output = sigmoid(output)
#calculate the loss
loss = output - target
#calculate the gradient of the loss
gradient = sigmoid_derivative(output)
gradient *= loss
#update the weights
for i in range(0, self.input_size):
self.weights[i] -= learning_rate * gradient * self.inputs[i]
#update the bias
self.bias -= learning_rate * gradient
return loss
"""
def optimizer(self, inputs, target, learning_rate, epochs):
for i in range(0, epochs):
loss = self.backprop(inputs, target, learning_rate)
# print("Loss", loss)
return loss
def sigmoid_derivative(self, output):
return output * (1 - output)
#a relu activation function
def relu(self, x):
return np.maximum(0,x)
#derivative of a relu activation function
def relu_derivative(self, x):
return np.where(x <= 0, 0, 1)
#a function that checks every value passed to it by the vision function and if new form a new neuron and load it to that neuron
def vision_function(inputs, input_size):
neurons = []
for i in range(0, input_size):
if inputs[i] not in neurons:
neurons.append(inputs[i])
neurons_size = len(neurons)
neurons_ids = np.arange(0, neurons_size)
neurons_excites = random.randint(0,size=neurons_size)
for i in range(0, neurons_size):
neurons[i] = VisionNeuron(inputs=inputs, input_size=input_size)
# print("Neuron", i, "Excite", neurons[i].excite)
# print("Neuron", i, "Output", neurons[i].output)
return neurons
def Feeder(inputs, input_size):
neurons = vision_function(inputs, input_size)
for i in range(0, input_size):
for j in range(0, input_size):
neurons[i].checkActivation(neurons[j])
return neurons
def Feeder_with_weights(inputs, input_size):
neurons = Feeder(inputs, input_size)
for i in range(0, input_size):
for j in range(0, input_size):
if neurons[i].excite == 1 and neurons[j].excite == 1:
neurons[i].recalculateWeights()
neurons[j].recalculateWeights()
neurons[i].recalcuateBias()
neurons[j].recalcuateBias()
neurons[i].optimizer(inputs, inputs[j], 0.1, 100)
neurons[j].optimizer(inputs, inputs[i], 0.1, 100)
# print("Neuron", i, "Weights", neurons[i].weights)
# print("Neuron", j, "Weights", neurons[j].weights)
return neurons
def NeuralNet(inputs, input_size):
neurons = Feeder_with_weights(inputs, input_size)
for i in range(0, input_size):
neurons[i].excite = 0
neurons[i].calculateOutput(inputs=inputs)
# print("Neuron", i, "Excite", neurons[i].excite)
# print("Neuron", i, "Output", neurons[i].output)
return neurons
def FeedForward(x, input_size):
return NeuralNet(x, input_size)
def BackPropagation(x, input_size):
neurons = FeedForward(x, input_size)
for i in range(0, input_size):
neurons[i].calculateOutput(inputs=x)
for j in range(0, input_size):
neurons[i].checkActivation(neurons[j])
neurons[i].recalculateWeights()
neurons[i].recalcuateBias()
neurons[i].optimizer(x, x[j], 0.1, 100)
# print("Neuron", i, "Weights", neurons[i].weights)
return neurons
def Train(x, input_size, epochs):
for i in range(0, epochs):
neurons = BackPropagation(x, input_size)
return neurons
def Predict(x, input_size):
neurons = BackPropagation(x, input_size)
output = neurons[0].output
for i in range(1, input_size):
output += neurons[i].output
output /= input_size
return output
def main():
inputs = [0.1, 0.2, 0.6, 0.2]
input_size = len(inputs)
epochs = 4
#for i in range(0, input_size):
# print("Neuron", i, "Excite", neurons[i].excite)
# print("Neuron", i, "Output", neurons[i].output)
# print(FeedForward(inputs, input_size))
# print(BackPropagation(inputs, input_size))
# print(Train(inputs, input_size, epochs))
# print(Predict(inputs, input_size))
if __name__ == '__main__':
main()