You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

nnCostFunction.m 4.1 kB

8 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. temp_y = zeros(m,num_labels);
  60. for i = 1 : m
  61. temp_y(i,y(i)) = 1;
  62. end
  63. p = zeros(size(X, 1), 1);
  64. X = [ones(m, 1), X];
  65. a2 = sigmoid(X * Theta1');
  66. a2 = [ones(m, 1), a2];
  67. hx = sigmoid(a2 * Theta2');
  68. %无需对hx的结果进行统一化(提取max),因为hx对每一个值的预估都是有用的数据,可以用来计算J
  69. J = 1 / m * sum(sum(-temp_y .* log(hx) - (1 - temp_y) .* log(1 - hx))) + lambda / (2 * m) * (sum((Theta1(:, 2:end) .^ 2)(:)) + (sum((Theta2(:, 2:end) .^ 2)(:))));
  70. delta_3 = hx - temp_y;
  71. delta_2 = delta_3 * Theta2.* a2 .* (1 - a2);
  72. delta_2 = delta_2(:, 2:end);
  73. Theta2_grad = 1 / m * (Theta2_grad + delta_3' * a2);
  74. Theta1_grad = 1 / m * (Theta1_grad + delta_2' * X);
  75. %Regularized Neural Networks
  76. Theta2_grad(:, 2:end) = Theta2_grad(:, 2:end) + lambda / m * (Theta2(:, 2:end));
  77. Theta1_grad(:, 2:end) = Theta1_grad(:, 2:end) + lambda / m * (Theta1(:, 2:end));
  78. % -------------------------------------------------------------
  79. % =========================================================================
  80. % Unroll gradients
  81. grad = [Theta1_grad(:) ; Theta2_grad(:)];
  82. end

机器学习

Contributors (1)