简单神经网络实现 03

实现简单反向传播。
简单神经网络实现 03_第1张图片

import numpy as np


def sigmoid(x):
    """
    Calculate sigmoid
    """
    return 1 / (1 + np.exp(-x))


x = np.array([0.5, 0.1, -0.2])
target = 0.6
learnrate = 0.5

weights_input_hidden = np.array([[0.5, -0.6],
                                 [0.1, -0.2],
                                 [0.1, 0.7]])

weights_hidden_output = np.array([0.1, -0.3])

## Forward pass
hidden_layer_input = np.dot(x, weights_input_hidden)
hidden_layer_output = sigmoid(hidden_layer_input)

output_layer_in = np.dot(hidden_layer_output, weights_hidden_output)
output = sigmoid(output_layer_in)


## Backwards pass
## TODO: Calculate error
error = target - output
print('error',error)
# TODO: Calculate error gradient for output layer
del_err_output = error * output * (1 - output)
print('del_err_output',del_err_output)
# TODO: Calculate error gradient for hidden layer

del_err_hidden = np.dot(del_err_output, weights_hidden_output) * hidden_layer_output * (1 - hidden_layer_output)
print('del_err_hidden',del_err_hidden)
# TODO: Calculate change in weights for hidden layer to output layer
delta_w_h_o = learnrate * del_err_output * hidden_layer_output
print('delta_w_h_o',delta_w_h_o)
# TODO: Calculate change in weights for input layer to hidden layer
delta_w_i_o = learnrate * del_err_hidden * x[:, None]
print('delta_w_i_o',delta_w_i_o)
print('Change in weights for hidden layer to output layer:')
print(delta_w_h_o)
print('Change in weights for input layer to hidden layer:')
print(delta_w_i_o)

你可能感兴趣的:(深度学习)