myex4.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. %% Initialization
  2. clear ; close all; clc
  3. %% Setup the parameters you will use for this exercise
  4. input_layer_size = 400; % 20x20 Input Images of Digits
  5. hidden_layer_size = 25; % 25 hidden units
  6. num_labels = 10; % 10 labels, from 1 to 10
  7. % (note that we have mapped "0" to label 10)
  8. %% =========== Part 1: Loading and Visualizing Data =============
  9. % We start the exercise by first loading and visualizing the dataset.
  10. % You will be working with a dataset that contains handwritten digits.
  11. %
  12. % Load Training Data
  13. fprintf('Loading and Visualizing Data ...\n')
  14. load('ex4data1.mat');
  15. m = size(X, 1);
  16. % Randomly select 100 data points to display
  17. sel = randperm(size(X, 1));
  18. sel = sel(1:100);
  19. % Load the weights into variables Theta1 and Theta2
  20. load('ex4weights.mat');
  21. % Unroll parameters
  22. nn_params = [Theta1(:) ; Theta2(:)];
  23. fprintf('\nFeedforward Using Neural Network ...\n')
  24. % Weight regularization parameter (we set this to 0 here).
  25. lambda = 0;
  26. J = nnCostFunction(nn_params, input_layer_size, hidden_layer_size, ...
  27. num_labels, X, y, lambda);
  28. fprintf(['Cost at parameters (loaded from ex4weights): %f '...
  29. '\n(this value should be about 0.287629)\n'], J);
  30. lambda = 1;
  31. J = nnCostFunction(nn_params, input_layer_size, hidden_layer_size, ...
  32. num_labels, X, y, lambda);
  33. fprintf(['Cost at parameters (loaded from ex4weights)=> lambda = 1: %f '...
  34. '\n(this value should be about 0.383770)\n'], J);
  35. fprintf('\nProgram paused. Press enter to continue.\n');
  36. fprintf('\nEvaluating sigmoid gradient...\n')
  37. g = sigmoidGradient([-1 -0.5 0 0.5 1]);
  38. fprintf('Sigmoid gradient evaluated at [-1 -0.5 0 0.5 1]:\n ');
  39. fprintf('%f ', g);
  40. fprintf('\n\n');
  41. fprintf('Program paused. Press enter to continue.\n');
  42. %% ================ Part 6: Initializing Pameters ================
  43. % In this part of the exercise, you will be starting to implment a two
  44. % layer neural network that classifies digits. You will start by
  45. % implementing a function to initialize the weights of the neural network
  46. % (randInitializeWeights.m)
  47. fprintf('\nInitializing Neural Network Parameters ...\n')
  48. initial_Theta1 = randInitializeWeights(input_layer_size, hidden_layer_size);
  49. initial_Theta2 = randInitializeWeights(hidden_layer_size, num_labels);
  50. % Unroll parameters
  51. initial_nn_params = [initial_Theta1(:) ; initial_Theta2(:)];
  52. %% =============== Part 7: Implement Backpropagation ===============
  53. % Once your cost matches up with ours, you should proceed to implement the
  54. % backpropagation algorithm for the neural network. You should add to the
  55. % code you've written in nnCostFunction.m to return the partial
  56. % derivatives of the parameters.
  57. %
  58. fprintf('\nChecking Backpropagation... \n');
  59. % Check gradients by running checkNNGradients
  60. checkNNGradients;
  61. fprintf('\nProgram paused. Press enter to continue.\n');
  62. pause;
  63. %%%%%%https://github.com/rieder91/MachineLearning/blob/master/Exercise%204/ex4/nnCostFunction.m