StasesNeuralNet 0.1.0__tar.gz

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,6 @@
1
+ Metadata-Version: 2.4
2
+ Name: StasesNeuralNet
3
+ Version: 0.1.0
4
+ Summary: My own neural network library
5
+ Requires-Python: >=3.10
6
+ Description-Content-Type: text/markdown
@@ -0,0 +1,37 @@
1
+ This is my first neural network.
2
+
3
+ How to use:
4
+
5
+ 1.Import
6
+
7
+ from StasesNeuralNet import Network
8
+
9
+ 2.Create net
10
+
11
+ Net = Network(
12
+ 2.1.first_input_len
13
+ length of first layer and input vector (int)
14
+
15
+ 2.2.layers_and_their_len
16
+ list of layers and their length (list)
17
+ example: [10, 20, 15, 10, 5, 2, 1]
18
+
19
+ 2.3.you can also set the learnint rate but its already 0.0001
20
+ learning_rate=0.0001
21
+ )
22
+
23
+ 2.Fill the network
24
+
25
+ Net.fill_network()
26
+
27
+ 4.Make your dataset
28
+
29
+ 5.Train net
30
+
31
+ Net.train(input_vector, target_vector)
32
+
33
+ 6.When the network is done, to get answer, use forward()
34
+
35
+ answer = Net.forward(input_vector)
36
+
37
+ This litle project have only RELU
@@ -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
+ Metadata-Version: 2.4
2
+ Name: StasesNeuralNet
3
+ Version: 0.1.0
4
+ Summary: My own neural network library
5
+ Requires-Python: >=3.10
6
+ Description-Content-Type: text/markdown
@@ -0,0 +1,8 @@
1
+ README.txt
2
+ pyproject.toml
3
+ StasesNeuralNet/__init__.py
4
+ StasesNeuralNet/network.py
5
+ StasesNeuralNet.egg-info/PKG-INFO
6
+ StasesNeuralNet.egg-info/SOURCES.txt
7
+ StasesNeuralNet.egg-info/dependency_links.txt
8
+ StasesNeuralNet.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ StasesNeuralNet
@@ -0,0 +1,10 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "StasesNeuralNet"
7
+ version = "0.1.0"
8
+ description = "My own neural network library"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+