myex3nn.m 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. %% ================ Part 2: Loading Pameters ================
  38. % In this part of the exercise, we load some pre-initialized
  39. % neural network parameters.
  40. fprintf('\nLoading Saved Neural Network Parameters ...\n')
  41. % Load the weights into variables Theta1 and Theta2
  42. load('ex3weights.mat');
  43. %% ================= Part 3: Implement Predict =================
  44. % After training the neural network, we would like to use it to predict
  45. % the labels. You will now implement the "predict" function to use the
  46. % neural network to predict the labels of the training set. This lets
  47. % you compute the training set accuracy.
  48. pred = predict(Theta1, Theta2, X);
  49. fprintf('\nTraining Set Accuracy: %f\n', mean(double(pred == y)) * 100);
  50. fprintf('Program paused. Press enter to continue.\n');
  51. % To give you an idea of the network's output, you can also run
  52. % through the examples one at the a time to see what it is predicting.
  53. % Randomly permute examples