Implement back propagation
This commit is contained in:
parent
052f0625c3
commit
bdecab8cf8
1 changed files with 42 additions and 0 deletions
|
@ -72,6 +72,48 @@ assert(size(J) == [1 1]);
|
|||
% over the training examples if you are implementing it for the
|
||||
% first time.
|
||||
%
|
||||
|
||||
D_1 = zeros(size(Theta1));
|
||||
D_2 = zeros(size(Theta2));
|
||||
|
||||
for t = 1:m
|
||||
% feed forward this training sample
|
||||
% ---------------------------------------------------------------------------
|
||||
a_1 = X(t,:);
|
||||
% (X already has 1-column)
|
||||
assert(size(a_1) == [1, input_layer_size+1]);
|
||||
|
||||
z_2 = a_1*Theta1';
|
||||
a_2 = sigmoid(z_2);
|
||||
a_2 = [ones(size(a_2, 1)), a_2]; % (bias term)
|
||||
assert(size(a_2) == [1, hidden_layer_size+1]);
|
||||
|
||||
z_3 = a_2*Theta2';
|
||||
a_3 = sigmoid(z_3);
|
||||
h_0 = a_3;
|
||||
assert(size(h_0) == [1, num_labels]);
|
||||
|
||||
% back propagate / error
|
||||
% ---------------------------------------------------------------------------
|
||||
assert(size(y) == [m num_labels]);
|
||||
d_3 = a_3 - y(t,:);
|
||||
assert(size(d_3) == [1, num_labels]);
|
||||
|
||||
d_2 = d_3*Theta2 .* [1, sigmoidGradient(z_2)];
|
||||
d_2 = d_2(2:end);
|
||||
assert(size(d_2) == [1, hidden_layer_size]);
|
||||
|
||||
% accumulate over all m training examples
|
||||
D_2 = D_2 + d_3'*a_2;
|
||||
D_1 = D_1 + d_2'*a_1;
|
||||
end
|
||||
|
||||
% average
|
||||
D_2 /= m;
|
||||
D_1 /= m;
|
||||
Theta2_grad = D_2;
|
||||
Theta1_grad = D_1;
|
||||
|
||||
% Part 3: Implement regularization with the cost function and gradients.
|
||||
%
|
||||
% Hint: You can implement this around the code for
|
||||
|
|
Reference in a new issue