ex3_nn.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. %% Machine Learning Online Class - Exercise 3 | Part 2: Neural Networks
  2. % Instructions
  3. % ------------
  4. %
  5. % This file contains code that helps you get started on the
  6. % linear exercise. You will need to complete the following functions
  7. % in this exericse:
  8. %
  9. % lrCostFunction.m (logistic regression cost function)
  10. % oneVsAll.m
  11. % predictOneVsAll.m
  12. % predict.m
  13. %
  14. % For this exercise, you will not need to change any code in this file,
  15. % or any other files other than those mentioned above.
  16. %
  17. %% Initialization
  18. clear ; close all; clc
  19. %% Setup the parameters you will use for this exercise
  20. input_layer_size = 400; % 20x20 Input Images of Digits
  21. hidden_layer_size = 25; % 25 hidden units
  22. num_labels = 10; % 10 labels, from 1 to 10
  23. % (note that we have mapped "0" to label 10)
  24. %% =========== Part 1: Loading and Visualizing Data =============
  25. % We start the exercise by first loading and visualizing the dataset.
  26. % You will be working with a dataset that contains handwritten digits.
  27. %
  28. % Load Training Data
  29. fprintf('Loading and Visualizing Data ...\n')
  30. load('ex3data1.mat');
  31. m = size(X, 1);
  32. % Randomly select 100 data points to display
  33. sel = randperm(size(X, 1));
  34. sel = sel(1:100);
  35. displayData(X(sel, :));
  36. fprintf('Program paused. Press enter to continue.\n');
  37. pause;
  38. %% ================ Part 2: Loading Pameters ================
  39. % In this part of the exercise, we load some pre-initialized
  40. % neural network parameters.
  41. fprintf('\nLoading Saved Neural Network Parameters ...\n')
  42. % Load the weights into variables Theta1 and Theta2
  43. load('ex3weights.mat');
  44. %% ================= Part 3: Implement Predict =================
  45. % After training the neural network, we would like to use it to predict
  46. % the labels. You will now implement the "predict" function to use the
  47. % neural network to predict the labels of the training set. This lets
  48. % you compute the training set accuracy.
  49. pred = predict(Theta1, Theta2, X);
  50. fprintf('\nTraining Set Accuracy: %f\n', mean(double(pred == y)) * 100);
  51. fprintf('Program paused. Press enter to continue.\n');
  52. pause;
  53. % To give you an idea of the network's output, you can also run
  54. % through the examples one at the a time to see what it is predicting.
  55. % Randomly permute examples
  56. rp = randperm(m);
  57. for i = 1:m
  58. % Display
  59. fprintf('\nDisplaying Example Image\n');
  60. displayData(X(rp(i), :));
  61. pred = predict(Theta1, Theta2, X(rp(i),:));
  62. fprintf('\nNeural Network Prediction: %d (digit %d)\n', pred, mod(pred, 10));
  63. % Pause with quit option
  64. s = input('Paused - press enter to continue, q to exit:','s');
  65. if s == 'q'
  66. break
  67. end
  68. end