From bdecab8cf8155af5927db2df2cb694e0f81d57b9 Mon Sep 17 00:00:00 2001 From: neingeist Date: Sun, 2 Nov 2014 13:27:11 +0100 Subject: [PATCH] Implement back propagation --- ex4/nnCostFunction.m | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/ex4/nnCostFunction.m b/ex4/nnCostFunction.m index fd42d44..6a5f55c 100644 --- a/ex4/nnCostFunction.m +++ b/ex4/nnCostFunction.m @@ -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