error_layer_1 = weights_delta_layer_2 * self.weights_1_2
gradient_layer_1 = result_1 * (1 - result_1)
weights_delta_layer_1 = error_layer_1 * gradient_layer_1
self.weights_0_1 -= np.dot(inputs.reshape(len(inputs), 1), weights_delta_layer_1).T * self.learning_rate
Ошибка:
Traceback (most recent call last):
File "main.py", line 77, in <module>
neiron.train(np.array([1, 0, 1, 0]), 1)
File "main.py", line 60, in train
error_layer_1 = np.dot(weights_delta_layer_2 * self.weights_1_2)
ValueError: operands could not be broadcast together with shapes (1,2) (2,3)
Ввесь код:
import numpy as np
import sys
class ByCar:
def __init__(self, learning_rate=0.05):
self.weights_0_1 = np.random.normal(0.0, 2 ** -0.5, (3, 4))
self.weights_1_2 = np.random.normal(0.0, 2 ** -0.5, (2, 3))
self.weights_2_3 = np.random.normal(0.0, 1, (1, 2))
self.sigmoid_mapper = np.vectorize(self.sigmoid)
self.learning_rate = np.array([learning_rate])
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def print_weights(self):
print(f"Веса первого слоя: \n{self.weights_0_1}")
print(f"Веса второго слоя: \n{self.weights_1_2}")
print(f"Веса третьего слоя: \n{self.weights_2_3}")
def predict(self, inputs):
inputs_1 = np.dot(self.weights_0_1, inputs)
result_1 = self.sigmoid_mapper(inputs_1)
inputs_2 = np.dot(self.weights_1_2, result_1)
result_2 = self.sigmoid_mapper(inputs_2)
inputs_3 = np.dot(self.weights_2_3, result_2)
result_3 = self.sigmoid_mapper(inputs_3)
return result_3
def train(self, inputs, expected_predict):
inputs_1 = np.dot(self.weights_0_1, inputs)
result_1 = self.sigmoid_mapper(inputs_1)
inputs_2 = np.dot(self.weights_1_2, result_1)
result_2 = self.sigmoid_mapper(inputs_2)
inputs_3 = np.dot(self.weights_2_3, result_2)
result_3 = self.sigmoid_mapper(inputs_3)
actual_predict = result_3[0]
error_layer_3 = np.array([actual_predict - expected_predict])
gradient_layer_3 = actual_predict * (1 - actual_predict)
weights_delta_layer_3 = error_layer_3 * gradient_layer_3
self.weights_2_3 -= (np.dot(weights_delta_layer_3, result_2.reshape(1, len(result_2)))) * self.learning_rate
error_layer_2 = weights_delta_layer_3 * self.weights_2_3
gradient_layer_2 = result_2 * (1 - result_2)
weights_delta_layer_2 = error_layer_2 * gradient_layer_2
self.weights_1_2 -= np.dot(result_1.reshape(len(result_1), 1), weights_delta_layer_2).T * self.learning_rate
error_layer_1 = weights_delta_layer_2 * self.weights_1_2
gradient_layer_1 = result_1 * (1 - result_1)
weights_delta_layer_1 = error_layer_1 * gradient_layer_1
self.weights_0_1 -= np.dot(inputs.reshape(len(inputs), 1), weights_delta_layer_1).T * self.learning_rate
print(self.weights_0_1)
neiron = ByCar()
neiron.train(np.array([1, 0, 1, 0]), 1)