Path: blob/master/Week 5/Programming Assignment - 4/machine-learning-ex4/ex4/nnCostFunction.m
864 views
function [J grad] = nnCostFunction(nn_params, ...1input_layer_size, ...2hidden_layer_size, ...3num_labels, ...4X, y, lambda)5%NNCOSTFUNCTION Implements the neural network cost function for a two layer6%neural network which performs classification7% [J grad] = NNCOSTFUNCTON(nn_params, hidden_layer_size, num_labels, ...8% X, y, lambda) computes the cost and gradient of the neural network. The9% parameters for the neural network are "unrolled" into the vector10% nn_params and need to be converted back into the weight matrices.11%12% The returned parameter grad should be a "unrolled" vector of the13% partial derivatives of the neural network.14%1516% Reshape nn_params back into the parameters Theta1 and Theta2, the weight matrices17% for our 2 layer neural network18Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...19hidden_layer_size, (input_layer_size + 1));2021Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...22num_labels, (hidden_layer_size + 1));2324% Setup some useful variables25m = size(X, 1);2627% You need to return the following variables correctly28J = 0;29Theta1_grad = zeros(size(Theta1));30Theta2_grad = zeros(size(Theta2));3132% ====================== YOUR CODE HERE ======================33% Instructions: You should complete the code by working through the34% following parts.35%36% Part 1: Feedforward the neural network and return the cost in the37% variable J. After implementing Part 1, you can verify that your38% cost function computation is correct by verifying the cost39% computed in ex4.m40%41% Part 2: Implement the backpropagation algorithm to compute the gradients42% Theta1_grad and Theta2_grad. You should return the partial derivatives of43% the cost function with respect to Theta1 and Theta2 in Theta1_grad and44% Theta2_grad, respectively. After implementing Part 2, you can check45% that your implementation is correct by running checkNNGradients46%47% Note: The vector y passed into the function is a vector of labels48% containing values from 1..K. You need to map this vector into a49% binary vector of 1's and 0's to be used with the neural network50% cost function.51%52% Hint: We recommend implementing backpropagation using a for-loop53% over the training examples if you are implementing it for the54% first time.55%56% Part 3: Implement regularization with the cost function and gradients.57%58% Hint: You can implement this around the code for59% backpropagation. That is, you can compute the gradients for60% the regularization separately and then add them to Theta1_grad61% and Theta2_grad from Part 2.62%63% Step 1: Feedforward & Cost Function64a1 = [ones(m,1) X];65z2 = a1 * Theta1';66a2 = sigmoid(z2);67a2 = [ones(m,1) a2];68z3 = a2 * Theta2';69a3 = sigmoid(z3);70% Cost function71for k=1:num_labels, yCls = y==k;72J = J - (1/m)*sum(((yCls.*log(a3(:,k))) + (((1-yCls).* log(1-a3(:,k))))));73end;74% Regularizing cost function75regTheta1 = Theta1;76regTheta2 = Theta2;77regTheta1(:,1) = 0;78regTheta2(:,1) = 0;7980regSum = sum(sum(regTheta1.^2)) + sum(sum(regTheta2.^2));81J = J + (lambda/(2*m))*regSum;8283% Computing gradient84for t = 1:m,85a1 = [1; X(t,:)'];86z2 = Theta1*a1;87a2 = [1;sigmoid(z2)];88z3 = Theta2*a2;89a3 = sigmoid(z3);9091yi = ([1:num_labels]==y(t))';92% Child Delta93delta3 = a3 - yi;94delta2 = (Theta2(:,2:end)'*delta3) .* sigmoidGradient(z2);95% Father Delta96Theta1_grad = Theta1_grad + delta2 * a1';97Theta2_grad = Theta2_grad + delta3 * a2';98end;99% -------------------------------------------------------------100Theta1_grad = (1/m) * Theta1_grad + (lambda/m) * [zeros(hidden_layer_size,1) Theta1(:,2:end)];101Theta2_grad = (1/m) * Theta2_grad + (lambda/m) * [zeros(num_labels,1) Theta2(:,2:end)];102% =========================================================================103104% Unroll gradients105grad = [Theta1_grad(:) ; Theta2_grad(:)];106107108end109110111