在 n 层神经网络中计算梯度的好方法是什么?
权重层:
- 第一层权重:
(n_inputs+1, n_units_layer)-matrix
- 隐藏层权重:
(n_units_layer+1, n_units_layer)-matrix
- 最后一层权重:
(n_units_layer+1, n_outputs)-matrix
笔记:
- 如果只有一个隐藏层,我们将只使用两个(权重)层来表示网络:
inputs --first_layer-> network_unit --second_layer-> output
- 对于具有多个隐藏层的 n 层网络,我们需要实现 (2) 步骤。
有点模糊的伪代码:
weight_layers = [ layer1, layer2 ] # a list of layers as described above
input_values = [ [0,0], [0,0], [1,0], [0,1] ] # our test set (corresponds to XOR)
target_output = [ 0, 0, 1, 1 ] # what we want to train our net to output
output_layers = [] # output for the corresponding layers
for layer in weight_layers:
output <-- calculate the output # calculate the output from the current layer
output_layers <-- output # store the output from each layer
n_samples = input_values.shape[0]
n_outputs = target_output.shape[1]
error = ( output-target_output )/( n_samples*n_outputs )
""" calculate the gradient here """