This repository was archived by the owner on Sep 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
51 lines (41 loc) · 2.66 KB
/
solution.py
File metadata and controls
51 lines (41 loc) · 2.66 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
# Функция активации
def softsign(x):
return x / (abs(x) + 1)
class NeuralNetwork:
def __init__(self, size): # Создааем объект класса NeuralNetwork, передавая ему список размеров каждого слоя
self.neurons = list() # Список слоев нейронов (каждый слой - это список значений нейронов)
self.biases = list() # Список bias'ов нейронов (структура такая же, как и у neurons)
for layer_size in size: # Проходимся по списку размеров слоев
self.neurons.append([0] * layer_size) # Создаем новый слой данного размера
self.biases.append([0] * layer_size) # Создаем список bias'ов для этого слоя
self.weights = list() # Создаем список весов
self.weights.append(list())
for layer in range(1, len(self.neurons)):
self.weights.append(list())
for previous_neuron in range(len(self.neurons[layer - 1])):
self.weights[layer].append(list())
for current_neuron in range(len(self.neurons[layer])):
self.weights[layer][previous_neuron].append(0)
# Устанавливаем веса
def set_weights(self, layer, weights):
counter = 0
# Проходим через все веса предыдущего слоя
for previous_neuron in range(len(self.neurons[layer - 1])):
# Проходим через все веса текущего слоя
for current_neuron in range(len(self.neurons[layer])):
self.weights[layer][previous_neuron][current_neuron] = weights[counter]
# Устанавливаем значение веса на соответствующее по счету
counter += 1
return self.weights[layer]
# Устанавливаем bias'ы
def set_biases(self, layer, biases):
self.biases[layer] = biases
def calculate(self, input):
self.neurons[0] = input
for layer in range(1, len(self.neurons)):
for neuron in range(len(self.neurons[layer])):
self.neurons[layer][neuron] = 0
for previous in range(len(self.neurons[layer - 1])):
self.neurons[layer][neuron] += self.weights[layer][previous][neuron] * self.neurons[layer - 1][previous]
self.neurons[layer][neuron] = softsign(self.neurons[layer][neuron] + self.biases[layer][neuron])
return self.neurons[-1]