A
A
Andrey Prenrek2022-04-06 15:30:46
Python
Andrey Prenrek, 2022-04-06 15:30:46

How to fix a mistake in training a neural network?

I'm learning to write neural networks, but I'm getting an error in this part of the code when I want to edit the weights to the first scripted layer.

The code
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)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sswwssww, 2022-04-06
@sswwssww

ValueError: operands could not be broadcast together with shapes (1,2) (2,3)

- here it is clearly written what the problem is. You can't dot between such arrays. Reading the doc :
When working with two arrays, NumPy compares their shapes element by element. It starts from the end (that is, the rightmost) dimensions and goes to the left. Two dimensions are compatible if they are equal or if one of them is equal to 1 . If these conditions are not met, a ValueError: operands could not be broadcast together with shapes(ValueError: operands could not be broadcast together, indicating that arrays have incompatible shapes) exception is thrown .

ps By the way, I can only assume because. beginners often have such a problem that you forgot to transpose one of the arrays: for example, transposing 2, 3 into 3, 2 or 1, 2 into 2.1 - you can apply dot to them .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question