nnCostFunction.bak 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. % Reshape nn_params back into the parameters Theta1 and Theta2, the weight matrices
  17. % for our 2 layer neural network
  18. Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...
  19. hidden_layer_size, (input_layer_size + 1));
  20. Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...
  21. num_labels, (hidden_layer_size + 1));
  22. % Setup some useful variables
  23. m = size(X, 1);
  24. % You need to return the following variables correctly
  25. J = 0;
  26. Theta1_grad = zeros(size(Theta1));
  27. Theta2_grad = zeros(size(Theta2));
  28. % ====================== YOUR CODE HERE ======================
  29. % Instructions: You should complete the code by working through the
  30. % following parts.
  31. %
  32. % Part 1: Feedforward the neural network and return the cost in the
  33. % variable J. After implementing Part 1, you can verify that your
  34. % cost function computation is correct by verifying the cost
  35. % computed in ex4.m
  36. %
  37. % Part 2: Implement the backpropagation algorithm to compute the gradients
  38. % Theta1_grad and Theta2_grad. You should return the partial derivatives of
  39. % the cost function with respect to Theta1 and Theta2 in Theta1_grad and
  40. % Theta2_grad, respectively. After implementing Part 2, you can check
  41. % that your implementation is correct by running checkNNGradients
  42. %
  43. % Note: The vector y passed into the function is a vector of labels
  44. % containing values from 1..K. You need to map this vector into a
  45. % binary vector of 1's and 0's to be used with the neural network
  46. % cost function.
  47. %
  48. % Hint: We recommend implementing backpropagation using a for-loop
  49. % over the training examples if you are implementing it for the
  50. % first time.
  51. %
  52. % Part 3: Implement regularization with the cost function and gradients.
  53. %
  54. % Hint: You can implement this around the code for
  55. % backpropagation. That is, you can compute the gradients for
  56. % the regularization separately and then add them to Theta1_grad
  57. % and Theta2_grad from Part 2.
  58. %
  59. % part 1
  60. X1 = [ones(m, 1) X];
  61. z2 = X1 * Theta1';
  62. a2 = sigmoid(z2);
  63. a2_1 = [ones(m, 1) a2];
  64. z3 = a2_1 * Theta2';
  65. a3 = sigmoid(z3);
  66. %sel = randperm(size(a3, 1));
  67. %sel = sel(1:20);
  68. %out = a3(sel,:)
  69. % This method uses an indexing trick to vectorize the creation of 'y_matrix',
  70. % where each element of 'y' is mapped to a single-value row vector copied from an eye matrix.
  71. % check the notes in machine learning / resources /programming exercise 4
  72. Theta1_no_bias = Theta1(:, 2:end);
  73. Theta2_no_bias = Theta2(:, 2:end);
  74. %sum(sum(Theta1_no_bias .^ 2))
  75. %sum(sum(Theta2_no_bias .^ 2))
  76. J_reg = lambda / (2 * m) * ...
  77. (sum(sum(Theta1_no_bias .^ 2)) + sum(sum(Theta2_no_bias .^ 2)));
  78. y_matrix = eye(num_labels)(y,:);
  79. J = 1/m * sum(sum(-y_matrix .* log(a3) .- (1 .- y_matrix) .* log(1 - a3))) ...
  80. + J_reg;
  81. % part 2
  82. %fprintf ('-> size a3=%f y=%f mask=%f \n', size(a3), size(y), size(mask));
  83. for t = 1: m
  84. endfor
  85. % -------------------------------------------------------------
  86. % =========================================================================
  87. % Unroll gradients
  88. grad = [Theta1_grad(:) ; Theta2_grad(:)];
  89. end