Untitled


SUBMITTED BY: Guest

DATE: May 19, 2013, 3:44 a.m.

FORMAT: Text only

SIZE: 6.3 kB

HITS: 2317

  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. %
  60. %X = [ones(size(X, 1), 1) X];
  61. p = zeros(size(X, 1), 1);
  62. h1 = sigmoid([ones(m, 1) X] * Theta1');
  63. h2 = sigmoid([ones(m, 1) h1] * Theta2');
  64. tempy = zeros(m, num_labels);
  65. for k = 1:num_labels
  66. tempy(:, k) = (y == k);
  67. endfor
  68. K = num_labels;
  69. for i = 1:m
  70. % iterativ
  71. %for k = 1:K
  72. % temp_y = (y == k);
  73. % J += -temp_y(i) * log(h2(i,k)) - (1 - temp_y(i)) * log(1 - h2(i,k));
  74. %endfor
  75. %vectorizat
  76. J += sum(-tempy(i, :) * log(h2(i, :)') - (1 - tempy(i, :)) * log(1 - h2(i, :)'));
  77. endfor
  78. J = J/m;
  79. %vectorizat
  80. J += lambda/(2*m) * (sum(sum(Theta1(:, 2:end) .* Theta1(:, 2:end)), 2) + sum(sum(Theta2(:, 2:end) .* Theta2(:, 2:end)), 2));
  81. JCost = 0;
  82. %for j = 1:25
  83. % iterativ
  84. %for k = 2:401
  85. %JCost += Theta1(j,k)*Theta1(j,k);
  86. %endfor
  87. %vectorized
  88. % JCost += sum(Theta1(j, 2:end) .* Theta1(j, 2:end));
  89. %endfor
  90. %for j = 1:10
  91. %iterativ
  92. %for k = 2:26
  93. %JCost += Theta2(j,k)*Theta2(j,k);
  94. %endfor
  95. %vectorized
  96. % JCost += sum(Theta2(j, 2:end) .* Theta2(j, 2:end));
  97. %endfor
  98. % -------------------------------------------------------------
  99. Delta_1 = zeros(size(Theta1));
  100. Delta_2 = zeros(size(Theta2));
  101. for t = 1:m
  102. % Pasul 1
  103. a_1 = [1 ; X(t, :)'];
  104. z_2 = Theta1 * a_1;
  105. a_2 = [1 ; sigmoid(z_2)];
  106. z_3 = Theta2 * a_2;
  107. a_3 = sigmoid(z_3);
  108. % Pasul 2
  109. delta_3 = zeros(num_labels, 1);
  110. for k = 1:num_labels
  111. delta_3(k) = a_3(k) - (y == k)(t);
  112. endfor
  113. % Pasul 3
  114. delta_2 = (Theta2)' * delta_3 .* sigmoidGradient([1; z_2]);
  115. delta_2 = delta_2(2:end);
  116. Delta_1 = Delta_1 + delta_2 * (a_1');
  117. Delta_2 = Delta_2 + delta_3 * (a_2');
  118. endfor
  119. % =========================================================================
  120. Theta1_grad(:, 1) = Delta_1(:, 1) ./ m;
  121. Theta2_grad(:, 1) = Delta_2(:, 1) ./ m;
  122. Theta1_grad(:, 2:end) = Delta_1(:, 2:end) ./ m + lambda/m .* Theta1(:, 2:end);
  123. Theta2_grad(:, 2:end) = Delta_2(:, 2:end) ./ m + lambda/m .* Theta2(:, 2:end);
  124. % Unroll gradients
  125. grad = [Theta1_grad(:) ; Theta2_grad(:)];
  126. end

comments powered by Disqus