Advertisement
SergioXp

nnCostFunction

Jan 11th, 2015
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 4.07 KB | None | 0 0
  1. function [J grad] = nnCostFunction(nn_params, ...
  2.                                    input_layer_size, ...
  3.                                    hidden_layer_size, ...
  4.                                    num_labels, ...
  5.                                    X, y, lambda)
  6. %NNCOSTFUNCTION Implements the neural network cost function for a two layer
  7. %neural network which performs classification
  8. %   [J grad] = NNCOSTFUNCTON(nn_params, hidden_layer_size, num_labels, ...
  9. %   X, y, lambda) computes the cost and gradient of the neural network. The
  10. %   parameters for the neural network are "unrolled" into the vector
  11. %   nn_params and need to be converted back into the weight matrices.
  12. %
  13. %   The returned parameter grad should be a "unrolled" vector of the
  14. %   partial derivatives of the neural network.
  15. %
  16.  
  17. % Reshape nn_params back into the parameters Theta1 and Theta2, the weight matrices
  18. % for our 2 layer neural network
  19. Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...
  20.                  hidden_layer_size, (input_layer_size + 1));
  21.  
  22. Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...
  23.                  num_labels, (hidden_layer_size + 1));
  24.  
  25. % Setup some useful variables
  26. m = size(X, 1);
  27. n = size(X, 2);
  28.          
  29. % You need to return the following variables correctly
  30. J = 0;
  31. Theta1_grad = zeros(size(Theta1));
  32. Theta2_grad = zeros(size(Theta2));
  33.  
  34. % ====================== YOUR CODE HERE ======================
  35. % Instructions: You should complete the code by working through the
  36. %               following parts.
  37. %
  38. % Part 1: Feedforward the neural network and return the cost in the
  39. %         variable J. After implementing Part 1, you can verify that your
  40. %         cost function computation is correct by verifying the cost
  41. %         computed in ex4.m
  42. %
  43. % Part 2: Implement the backpropagation algorithm to compute the gradients
  44. %         Theta1_grad and Theta2_grad. You should return the partial derivatives of
  45. %         the cost function with respect to Theta1 and Theta2 in Theta1_grad and
  46. %         Theta2_grad, respectively. After implementing Part 2, you can check
  47. %         that your implementation is correct by running checkNNGradients
  48. %
  49. %         Note: The vector y passed into the function is a vector of labels
  50. %               containing values from 1..K. You need to map this vector into a
  51. %               binary vector of 1's and 0's to be used with the neural network
  52. %               cost function.
  53. %
  54. %         Hint: We recommend implementing backpropagation using a for-loop
  55. %               over the training examples if you are implementing it for the
  56. %               first time.
  57. %
  58. % Part 3: Implement regularization with the cost function and gradients.
  59. %
  60. %         Hint: You can implement this around the code for
  61. %               backpropagation. That is, you can compute the gradients for
  62. %               the regularization separately and then add them to Theta1_grad
  63. %               and Theta2_grad from Part 2.
  64. %
  65. %X=[ones(m, 1) X];
  66.  
  67. Theta1SIN = Theta1(:,2:end);
  68. Theta2SIN = Theta2(:,2:end);
  69.  
  70.  
  71. DELTA1 = zeros(hidden_layer_size,input_layer_size+1);
  72. DELTA2 = zeros(num_labels,hidden_layer_size+1);
  73.  
  74. suma = 0;
  75.  
  76. for i=1:m
  77.     a1 = X(i,:)';
  78.     a1 = [1; a1];
  79.     z2 = Theta1 * a1;
  80.     a2 = sigmoid(z2);
  81.     a2 = [1; a2];
  82.     z3 = Theta2 * a2;
  83.     a3 = sigmoid(z3);
  84.    
  85.     aux = (1:num_labels == y(i));
  86.     h = a3;
  87.     suma += aux*log(h)+(1-aux)*log(1-h);
  88.     delta3 = a3-aux';
  89.     delta2 = (Theta2'*delta3).*((1-a2).*a2);
  90.     delta2 = delta2(2:end);
  91.     DELTA1 = DELTA1 + delta2 * a1';
  92.     DELTA2 = DELTA2 + delta3 * a2';
  93. end
  94. J = (-1/m)*suma;
  95.  
  96. % -------------------------------------------------------------
  97.  
  98. Theta1_grad(:,1) = (1/m) * DELTA1(:,1);
  99. Theta2_grad(:,1) = (1/m) * DELTA2(:,1);
  100. Theta1_grad(:,2:end) = (1/m) * DELTA1(:,2:end) + (lambda/m) * Theta1SIN;
  101. Theta2_grad(:,2:end) = (1/m) * DELTA2(:,2:end) + (lambda/m) * Theta2SIN;
  102.  
  103. % =========================================================================
  104.  
  105. % Unroll gradients
  106. grad = [Theta1_grad(:) ; Theta2_grad(:)];
  107. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement