Guest User

Untitled

a guest
Jan 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 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.  
  28. % You need to return the following variables correctly
  29. J = 0;
  30. Theta1_grad = zeros(size(Theta1));
  31. Theta2_grad = zeros(size(Theta2));
  32.  
  33. % ====================== YOUR CODE HERE ======================
  34. % Instructions: You should complete the code by working through the
  35. % following parts.
  36. %
  37. % Part 1: Feedforward the neural network and return the cost in the
  38. % variable J. After implementing Part 1, you can verify that your
  39. % cost function computation is correct by verifying the cost
  40. % computed in ex4.m
  41. %
  42. % Part 2: Implement the backpropagation algorithm to compute the gradients
  43. % Theta1_grad and Theta2_grad. You should return the partial derivatives of
  44. % the cost function with respect to Theta1 and Theta2 in Theta1_grad and
  45. % Theta2_grad, respectively. After implementing Part 2, you can check
  46. % that your implementation is correct by running checkNNGradients
  47. %
  48. % Note: The vector y passed into the function is a vector of labels
  49. % containing values from 1..K. You need to map this vector into a
  50. % binary vector of 1's and 0's to be used with the neural network
  51. % cost function.
  52. %
  53. % Hint: We recommend implementing backpropagation using a for-loop
  54. % over the training examples if you are implementing it for the
  55. % first time.
  56. %
  57. % Part 3: Implement regularization with the cost function and gradients.
  58. %
  59. % Hint: You can implement this around the code for
  60. % backpropagation. That is, you can compute the gradients for
  61. % the regularization separately and then add them to Theta1_grad
  62. % and Theta2_grad from Part 2.
  63. %
  64.  
  65. a1 = [ones(m, 1) X];
  66.  
  67. z2 = a1 * Theta1';
  68. a2 = sigmoid(z2);
  69. a2 = [ones(size(a2,1), 1) a2];
  70.  
  71. z3 = a2 * Theta2';
  72. a3 = sigmoid(z3);
  73. h = a3;
  74.  
  75. y_Vec = zeros(m,num_labels);
  76.  
  77. for i = 1:m
  78. y_Vec(i,y(i)) = 1;
  79. end
  80.  
  81.  
  82. J = 1/m * sum(sum(-1 * y_Vec .* log(h)-(1-y_Vec) .* log(1-h)));
  83.  
  84. reg = (sum(sum(Theta1(:,2:end).^2)) + sum(sum(Theta2(:,2:end).^2))) * (lambda/(2*m));
  85.  
  86. J = J + reg;
  87.  
  88. % Part 2 implementation
  89.  
  90.  
  91.  
  92. for t = 1:m
  93.  
  94. % For the input layer-where l=1:
  95. a1 = [1; X(t,:)'];
  96.  
  97. % For the hidden layers-where l=2:
  98. z2 = Theta1 * a1;
  99. a2 = [1; sigmoid(z2)];
  100.  
  101. z3 = Theta2 * a2;
  102. a3 = sigmoid(z3);
  103.  
  104. yy = ([1:num_labels]==y(t))';
  105. % For the delta_values:
  106. delta_3 = a3 - yy;
  107.  
  108. delta_2 = (Theta2' * delta_3) .* [1; sigmoidGradient(z2)];
  109. delta_2 = delta_2(2:end); % Taking of the bias row
  110.  
  111.  
  112. % Big delta update
  113. Theta1_grad = Theta1_grad + delta_2 * a1';
  114. Theta2_grad = Theta2_grad + delta_3 * a2';
  115. end
  116.  
  117. Theta1_grad = (1/m) * Theta1_grad + (lambda/m) * [zeros(size(Theta1, 1), 1) Theta1(:,2:end)];
  118. Theta2_grad = (1/m) * Theta2_grad + (lambda/m) * [zeros(size(Theta2, 1), 1) Theta2(:,2:end)];
  119.  
  120. % -------------------------------------------------------------
  121.  
  122. % =========================================================================
  123.  
  124. % Unroll gradients
  125. grad = [Theta1_grad(:) ; Theta2_grad(:)];
  126.  
  127.  
  128. end
Add Comment
Please, Sign In to add comment