StasesNeuralNet 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .network import Network
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import random
|
|
2
|
+
|
|
3
|
+
class Neuron:
|
|
4
|
+
def __init__(self, weights, bias=0):
|
|
5
|
+
self.weights=weights
|
|
6
|
+
self.bias =bias
|
|
7
|
+
self.error =0
|
|
8
|
+
self.weight_delta =[]
|
|
9
|
+
self.bias_delta =0
|
|
10
|
+
|
|
11
|
+
def forward(self, input_vector):
|
|
12
|
+
self.last_input=input_vector
|
|
13
|
+
answer=0
|
|
14
|
+
for i in range(len(input_vector)):
|
|
15
|
+
answer += input_vector[i] *self.weights[i]
|
|
16
|
+
answer+=self.bias
|
|
17
|
+
self.z=answer
|
|
18
|
+
answer=self.activation_func(answer)
|
|
19
|
+
self.output=answer
|
|
20
|
+
return answer
|
|
21
|
+
|
|
22
|
+
def activation_func(self, answer):
|
|
23
|
+
return max(0, answer)
|
|
24
|
+
|
|
25
|
+
def count_delta(self, learning_rate):
|
|
26
|
+
self.weight_delta =[]
|
|
27
|
+
for w in range( len( self.weights)):
|
|
28
|
+
self.weight_delta.append( self.last_input[ w] *self.error *learning_rate)
|
|
29
|
+
|
|
30
|
+
self.bias_delta =self.error *learning_rate
|
|
31
|
+
|
|
32
|
+
def apply_delta(self):
|
|
33
|
+
for w in range( len( self.weights)):
|
|
34
|
+
self.weights[w] +=self.weight_delta[w]
|
|
35
|
+
self.bias +=self.bias_delta
|
|
36
|
+
|
|
37
|
+
self.weight_delta =[]
|
|
38
|
+
self.bias_delta =0
|
|
39
|
+
self.error =0
|
|
40
|
+
|
|
41
|
+
def show_info(self):
|
|
42
|
+
print('weights:',self.weights)
|
|
43
|
+
print('bias:',self.bias)
|
|
44
|
+
print('error:',self.error)
|
|
45
|
+
print('weight delta:',self.weight_delta)
|
|
46
|
+
print('bias delta:',self.bias_delta,'\n')
|
|
47
|
+
|
|
48
|
+
def activation_derivative(self):
|
|
49
|
+
if self.z <=0: return 0
|
|
50
|
+
else: return 1
|
|
51
|
+
|
|
52
|
+
class Network:
|
|
53
|
+
def __init__(self, first_input_len, layers_and_their_len, learning_rate=0.0001):
|
|
54
|
+
self.layers =[]
|
|
55
|
+
self.input_len=first_input_len
|
|
56
|
+
self.LAndTLen=layers_and_their_len
|
|
57
|
+
self.learning_rate=learning_rate
|
|
58
|
+
|
|
59
|
+
def create_neuron(self, weights_count, weights=None):
|
|
60
|
+
if not weights:
|
|
61
|
+
weights=[]
|
|
62
|
+
std = (2 / weights_count) ** 0.5
|
|
63
|
+
|
|
64
|
+
for i in range(weights_count):
|
|
65
|
+
weights.append(random.gauss(0, std))
|
|
66
|
+
return Neuron(weights)
|
|
67
|
+
|
|
68
|
+
def fill_network(self):
|
|
69
|
+
for lyr in range(len(self.LAndTLen)):
|
|
70
|
+
layer=[]
|
|
71
|
+
if lyr ==0:
|
|
72
|
+
weights_count=self.input_len
|
|
73
|
+
else:
|
|
74
|
+
weights_count=self.LAndTLen[lyr-1]
|
|
75
|
+
for n in range(self.LAndTLen[lyr]):
|
|
76
|
+
layer.append( self.create_neuron(weights_count))
|
|
77
|
+
self.layers.append(layer)
|
|
78
|
+
|
|
79
|
+
def show_info(self):
|
|
80
|
+
for l in range(len(self.layers)): # перевірка
|
|
81
|
+
print('\n=#$%#*=~- ' + str(l + 1) + ' layer -~=*#%$#=')
|
|
82
|
+
for n in range(len(self.layers[l])):
|
|
83
|
+
self.layers[l][n].show_info()
|
|
84
|
+
|
|
85
|
+
def layer_forward(self, layer, input_vector):
|
|
86
|
+
new_vector=[]
|
|
87
|
+
for neuron in self.layers[ layer]:
|
|
88
|
+
new_vector.append( neuron.forward( input_vector))
|
|
89
|
+
return new_vector
|
|
90
|
+
|
|
91
|
+
def forward(self, input_vector):
|
|
92
|
+
result=[]
|
|
93
|
+
for lyr in range(len(self.layers)):
|
|
94
|
+
result =self.layer_forward(lyr,input_vector)
|
|
95
|
+
input_vector=result
|
|
96
|
+
self.output=result
|
|
97
|
+
return result
|
|
98
|
+
|
|
99
|
+
def train(self, input_vector, target):
|
|
100
|
+
self.forward(input_vector)
|
|
101
|
+
self.calculate_output_error(target)
|
|
102
|
+
loss =self.calculate_loss(target)
|
|
103
|
+
self.calculate_hidden_errors()
|
|
104
|
+
self.count_deltas()
|
|
105
|
+
self.apply_deltas()
|
|
106
|
+
return loss
|
|
107
|
+
|
|
108
|
+
def calculate_output_error(self, target):
|
|
109
|
+
last_layer = self.layers[-1]
|
|
110
|
+
for i, neuron in enumerate(last_layer):
|
|
111
|
+
error = (target[i] - neuron.output) * neuron.activation_derivative()
|
|
112
|
+
neuron.error = error
|
|
113
|
+
|
|
114
|
+
def calculate_hidden_errors(self):
|
|
115
|
+
for layer_index in range((len(self.layers) -2), -1, -1):
|
|
116
|
+
current_layer =self.layers[layer_index]
|
|
117
|
+
next_layer =self.layers[layer_index +1]
|
|
118
|
+
for i, neuron in enumerate(current_layer):
|
|
119
|
+
sum_error=0
|
|
120
|
+
for next_neuron in next_layer:
|
|
121
|
+
sum_error +=next_neuron.error *next_neuron.weights[i]
|
|
122
|
+
|
|
123
|
+
sum_error *=neuron.activation_derivative()
|
|
124
|
+
neuron.error =sum_error
|
|
125
|
+
|
|
126
|
+
def count_deltas(self):
|
|
127
|
+
for layer in self.layers:
|
|
128
|
+
for neuron in layer:
|
|
129
|
+
neuron.count_delta(self.learning_rate)
|
|
130
|
+
|
|
131
|
+
def apply_deltas(self):
|
|
132
|
+
for layer in self.layers:
|
|
133
|
+
for neuron in layer:
|
|
134
|
+
neuron.apply_delta()
|
|
135
|
+
|
|
136
|
+
def calculate_loss(self,target):
|
|
137
|
+
loss =0
|
|
138
|
+
last_layer = self.layers[-1]
|
|
139
|
+
for i, neuron in enumerate(last_layer):
|
|
140
|
+
loss +=(target[i] -neuron.output) **2
|
|
141
|
+
loss /= len(last_layer)
|
|
142
|
+
return loss
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
StasesNeuralNet/__init__.py,sha256=-dtVyJ78JqVDUv7Vn0f_vk4D3R2BKAMlEsKvD41mYxY,28
|
|
2
|
+
StasesNeuralNet/network.py,sha256=8BUI9FFJiG0mGSNfOwwj9h8IZs4OoIBeG90KSYFanAo,4735
|
|
3
|
+
stasesneuralnet-0.1.0.dist-info/METADATA,sha256=IhtPcvTEM7Mt-gQHLDRTXaE2CXMMkZoe3hRAJWRb1JE,168
|
|
4
|
+
stasesneuralnet-0.1.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
5
|
+
stasesneuralnet-0.1.0.dist-info/top_level.txt,sha256=NmZ6BIVo7myFz1ZS3fMZXk5nkqUnqPRCNYGv6yKiYZA,16
|
|
6
|
+
stasesneuralnet-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
StasesNeuralNet
|