рассчитать смещение скрытого слоя - PullRequest
0 голосов
/ 25 августа 2018

Я разработал в python небольшой код, который использует 4 нейрона (2 входа, 3 нейрона в скрытом слое и 1 выходной нейрон), код действительно специфический, потому что я хотел тщательно понять каждую операцию. Это работает, но у меня все еще есть одна проблема с уклоном!

for epoch in range(epochs):
    layer1, predictions = predict_output_neural(features, weights_11, weights_12, weights_13, weight_ouput, bias_11, bias_12, bias_13, bias_output)
    if epoch % 10 == 0:
        layer1, predictions = predict_output_neural(features, weights_11, weights_12, weights_13, weight_ouput, bias_11, bias_12, bias_13, bias_output)
        print (cost(predictions, targets))
    """
        There are a lot of things to do here !
        to do the back propagation, we will first train the ouput neural
    """
    #Init gradient
    weights_gradient_output = np.zeros(weight_ouput.shape)
    bias_gradient_output = 0

    weights_gradient_11 = np.zeros(weights_11.shape)
    bias_gradient_11 = 0

    weights_gradient_12 = np.zeros(weights_12.shape)
    bias_gradient_12 = 0

    weights_gradient_13 = np.zeros(weights_12.shape)
    bias_gradient_13 = 0
    #Go throught each row
    for neural_input, feature, target, prediction in zip(layer1, features, targets, predictions):

        output_error = prediction - target
        output_delta = output_error * derivative_activation_y(prediction)

        error_neural_hidden_11 = output_delta * weight_ouput[0]
        error_neural_hidden_12 = output_delta * weight_ouput[1]
        error_neural_hidden_13 = output_delta * weight_ouput[2]


        error_neural_11 = error_neural_hidden_11 * derivative_activation_y(neural_input[0])
        error_neural_12 = error_neural_hidden_12 * derivative_activation_y(neural_input[1])
        error_neural_13 = error_neural_hidden_13 * derivative_activation_y(neural_input[2])

        weights_gradient_output += neural_input * output_delta
        #bias_output += output_delta

        weights_gradient_11 += feature * error_neural_11
        #bias_11 += error_neural_11

        weights_gradient_12 += feature * error_neural_12
        #bias_12 += error_neural_12

        weights_gradient_13 += feature * error_neural_13
        #bias_13 += error_neural_13


    #Update the weights and bias
    weight_ouput = weight_ouput - (learning_rate * weights_gradient_output)
    bias_output = bias_output - (learning_rate * bias_gradient_output)
    weights_11 =  weights_11 - (learning_rate * weights_gradient_11)
    bias_11 =  bias_11 - (learning_rate * bias_gradient_11)
    weights_12 =  weights_12 - (learning_rate * weights_gradient_12)
    bias_12 =  bias_12 - (learning_rate * bias_gradient_12)
    weights_13 =  weights_13 - (learning_rate * weights_gradient_13)
    bias_13 =  bias_13 - (learning_rate * bias_gradient_13)

Это дает мне хорошие результаты, но как только я раскомментирую строки, где я изменяю смещение каждого нейрона, это становится супер неправильно! Он сходится к 0,5 (например, 0,4999999)

Ты знаешь почему? похоже, обновление градиента смещения хорошее, не так ли?

1 Ответ

0 голосов
/ 25 августа 2018

Если вы посмотрите здесь свой код накопления градиента,

    weights_gradient_output += neural_input * output_delta
    #bias_output += output_delta

Вы добавляете свои градиенты непосредственно к смещению вместо bias_gradient_output. Следовательно, ваши обновления смещения используют скорость обучения 1, которая, вероятно, выше, чем вы предполагали. (Аналогичная проблема с bias_11 и т. Д.).

...