myex8.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. %% Machine Learning Online Class
  2. % Exercise 8 | Anomaly Detection and Collaborative Filtering
  3. %
  4. % Instructions
  5. % ------------
  6. %
  7. % This file contains code that helps you get started on the
  8. % exercise. You will need to complete the following functions:
  9. %
  10. % estimateGaussian.m
  11. % selectThreshold.m
  12. % cofiCostFunc.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. %% =============== Part 1: Loading movie ratings dataset ================
  18. % You will start by loading the movie ratings dataset to understand the
  19. % structure of the data.
  20. %
  21. fprintf('Loading movie ratings dataset.\n\n');
  22. % Load data
  23. load ('ex8_movies.mat');
  24. % Y is a 1682x943 matrix, containing ratings (1-5) of 1682 movies on
  25. % 943 users
  26. %
  27. % R is a 1682x943 matrix, where R(i,j) = 1 if and only if user j gave a
  28. % rating to movie i
  29. % From the matrix, we can compute statistics like average rating.
  30. fprintf('Average rating for movie 1 (Toy Story): %f / 5\n\n', ...
  31. mean(Y(1, R(1, :))));
  32. % We can "visualize" the ratings matrix by plotting it with imagesc
  33. imagesc(Y);
  34. ylabel('Movies');
  35. xlabel('Users');
  36. fprintf('\nProgram paused. Press enter to continue.\n');
  37. %% ============ Part 2: Collaborative Filtering Cost Function ===========
  38. % You will now implement the cost function for collaborative filtering.
  39. % To help you debug your cost function, we have included set of weights
  40. % that we trained on that. Specifically, you should complete the code in
  41. % cofiCostFunc.m to return J.
  42. % Load pre-trained weights (X, Theta, num_users, num_movies, num_features)
  43. load ('ex8_movieParams.mat');
  44. % Reduce the data set size so that this runs faster
  45. num_users = 4; num_movies = 5; num_features = 3;
  46. X = X(1:num_movies, 1:num_features);
  47. Theta = Theta(1:num_users, 1:num_features);
  48. Y = Y(1:num_movies, 1:num_users);
  49. R = R(1:num_movies, 1:num_users);
  50. % Evaluate cost function
  51. J = cofiCostFunc([X(:) ; Theta(:)], Y, R, num_users, num_movies, ...
  52. num_features, 0);
  53. fprintf(['Cost at loaded parameters: %f '...
  54. '\n(this value should be about 22.22)\n'], J);
  55. fprintf('\nProgram paused. Press enter to continue.\n');
  56. %% ============== Part 3: Collaborative Filtering Gradient ==============
  57. % Once your cost function matches up with ours, you should now implement
  58. % the collaborative filtering gradient function. Specifically, you should
  59. % complete the code in cofiCostFunc.m to return the grad argument.
  60. %
  61. fprintf('\nChecking Gradients (without regularization) ... \n');
  62. % Check gradients by running checkNNGradients
  63. checkCostFunction;
  64. fprintf('\nProgram paused. Press enter to continue.\n');
  65. %% ========= Part 4: Collaborative Filtering Cost Regularization ========
  66. % Now, you should implement regularization for the cost function for
  67. % collaborative filtering. You can implement it by adding the cost of
  68. % regularization to the original cost computation.
  69. %
  70. % Evaluate cost function
  71. J = cofiCostFunc([X(:) ; Theta(:)], Y, R, num_users, num_movies, ...
  72. num_features, 1.5);
  73. fprintf(['Cost at loaded parameters (lambda = 1.5): %f '...
  74. '\n(this value should be about 31.34)\n'], J);
  75. fprintf('\nProgram paused. Press enter to continue.\n');
  76. %% ======= Part 5: Collaborative Filtering Gradient Regularization ======
  77. % Once your cost matches up with ours, you should proceed to implement
  78. % regularization for the gradient.
  79. %
  80. %
  81. fprintf('\nChecking Gradients (with regularization) ... \n');
  82. % Check gradients by running checkNNGradients
  83. checkCostFunction(1.5);
  84. fprintf('\nProgram paused. Press enter to continue.\n');
  85. pause;