【发布时间】:2018-01-06 15:34:43
【问题描述】:
在 MATLAB/Octave 中,如何在训练示例上不进行任何循环的情况下实现反向传播?
This answer 讲了并行理论,但是如何在实际的 Octave 代码中实现呢?
【问题讨论】:
标签: matlab neural-network octave
在 MATLAB/Octave 中,如何在训练示例上不进行任何循环的情况下实现反向传播?
This answer 讲了并行理论,但是如何在实际的 Octave 代码中实现呢?
【问题讨论】:
标签: matlab neural-network octave
对我来说,最后一块拼图来自computing sum of outer products。
这是我想出的:
% X is a {# of training examples} x {# of features} matrix
% Y is a {# of training examples} x {# of output neurons} matrix
% Theta is a cell matrix containing Theta{1}...Theta{n}
% Number of training examples
m = size(X, 1);
% Get h(X) and z (non-activated output of all neurons in network)
[hX, z, activation] = predict(Theta, X);
% Get error of output layer
layers = 1 + length(Theta);
d{layers} = hX - Y;
% Propagate errors backwards through hidden layers
for layer = layers-1 : -1 : 2
d{layer} = d{layer+1} * Theta{layer};
d{layer} = d{layer}(:, 2:end); % Remove "error" for constant bias term
d{layer} .*= sigmoidGradient(z{layer});
end
% Calculate Theta gradients
for l = 1:layers-1
Theta_grad{l} = zeros(size(Theta{l}));
% Sum of outer products
Theta_grad{l} += d{l+1}' * [ones(m,1) activation{l}];
% Add regularisation term
Theta_grad{l}(:, 2:end) += lambda * Theta{l}(:, 2:end);
Theta_grad{l} /= m;
end
【讨论】: