| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- %% Machine Learning Online Class
- % Exercise 8 | Anomaly Detection and Collaborative Filtering
- %
- % Instructions
- % ------------
- %
- % This file contains code that helps you get started on the
- % exercise. You will need to complete the following functions:
- %
- % estimateGaussian.m
- % selectThreshold.m
- % cofiCostFunc.m
- %
- % For this exercise, you will not need to change any code in this file,
- % or any other files other than those mentioned above.
- %
- %% =============== Part 1: Loading movie ratings dataset ================
- % You will start by loading the movie ratings dataset to understand the
- % structure of the data.
- %
- fprintf('Loading movie ratings dataset.\n\n');
- % Load data
- load ('ex8_movies.mat');
- % Y is a 1682x943 matrix, containing ratings (1-5) of 1682 movies on
- % 943 users
- %
- % R is a 1682x943 matrix, where R(i,j) = 1 if and only if user j gave a
- % rating to movie i
- % From the matrix, we can compute statistics like average rating.
- fprintf('Average rating for movie 1 (Toy Story): %f / 5\n\n', ...
- mean(Y(1, R(1, :))));
- % We can "visualize" the ratings matrix by plotting it with imagesc
- imagesc(Y);
- ylabel('Movies');
- xlabel('Users');
- fprintf('\nProgram paused. Press enter to continue.\n');
- %% ============ Part 2: Collaborative Filtering Cost Function ===========
- % You will now implement the cost function for collaborative filtering.
- % To help you debug your cost function, we have included set of weights
- % that we trained on that. Specifically, you should complete the code in
- % cofiCostFunc.m to return J.
- % Load pre-trained weights (X, Theta, num_users, num_movies, num_features)
- load ('ex8_movieParams.mat');
- % Reduce the data set size so that this runs faster
- num_users = 4; num_movies = 5; num_features = 3;
- X = X(1:num_movies, 1:num_features);
- Theta = Theta(1:num_users, 1:num_features);
- Y = Y(1:num_movies, 1:num_users);
- R = R(1:num_movies, 1:num_users);
- % Evaluate cost function
- J = cofiCostFunc([X(:) ; Theta(:)], Y, R, num_users, num_movies, ...
- num_features, 0);
-
- fprintf(['Cost at loaded parameters: %f '...
- '\n(this value should be about 22.22)\n'], J);
- fprintf('\nProgram paused. Press enter to continue.\n');
- %% ============== Part 3: Collaborative Filtering Gradient ==============
- % Once your cost function matches up with ours, you should now implement
- % the collaborative filtering gradient function. Specifically, you should
- % complete the code in cofiCostFunc.m to return the grad argument.
- %
- fprintf('\nChecking Gradients (without regularization) ... \n');
- % Check gradients by running checkNNGradients
- checkCostFunction;
- fprintf('\nProgram paused. Press enter to continue.\n');
- %% ========= Part 4: Collaborative Filtering Cost Regularization ========
- % Now, you should implement regularization for the cost function for
- % collaborative filtering. You can implement it by adding the cost of
- % regularization to the original cost computation.
- %
- % Evaluate cost function
- J = cofiCostFunc([X(:) ; Theta(:)], Y, R, num_users, num_movies, ...
- num_features, 1.5);
-
- fprintf(['Cost at loaded parameters (lambda = 1.5): %f '...
- '\n(this value should be about 31.34)\n'], J);
- fprintf('\nProgram paused. Press enter to continue.\n');
- %% ======= Part 5: Collaborative Filtering Gradient Regularization ======
- % Once your cost matches up with ours, you should proceed to implement
- % regularization for the gradient.
- %
- %
- fprintf('\nChecking Gradients (with regularization) ... \n');
- % Check gradients by running checkNNGradients
- checkCostFunction(1.5);
- fprintf('\nProgram paused. Press enter to continue.\n');
- pause;
|