Path: blob/master/Week 9/Programming Assignment - 8/ex8/ex8_cofi.m
616 views
%% Machine Learning Online Class1% Exercise 8 | Anomaly Detection and Collaborative Filtering2%3% Instructions4% ------------5%6% This file contains code that helps you get started on the7% exercise. You will need to complete the following functions:8%9% estimateGaussian.m10% selectThreshold.m11% cofiCostFunc.m12%13% For this exercise, you will not need to change any code in this file,14% or any other files other than those mentioned above.15%1617%% =============== Part 1: Loading movie ratings dataset ================18% You will start by loading the movie ratings dataset to understand the19% structure of the data.20%21fprintf('Loading movie ratings dataset.\n\n');2223% Load data24load ('ex8_movies.mat');2526% Y is a 1682x943 matrix, containing ratings (1-5) of 1682 movies on27% 943 users28%29% R is a 1682x943 matrix, where R(i,j) = 1 if and only if user j gave a30% rating to movie i3132% From the matrix, we can compute statistics like average rating.33fprintf('Average rating for movie 1 (Toy Story): %f / 5\n\n', ...34mean(Y(1, R(1, :))));3536% We can "visualize" the ratings matrix by plotting it with imagesc37imagesc(Y);38ylabel('Movies');39xlabel('Users');4041fprintf('\nProgram paused. Press enter to continue.\n');42pause;4344%% ============ Part 2: Collaborative Filtering Cost Function ===========45% You will now implement the cost function for collaborative filtering.46% To help you debug your cost function, we have included set of weights47% that we trained on that. Specifically, you should complete the code in48% cofiCostFunc.m to return J.4950% Load pre-trained weights (X, Theta, num_users, num_movies, num_features)51load ('ex8_movieParams.mat');5253% Reduce the data set size so that this runs faster54num_users = 4; num_movies = 5; num_features = 3;55X = X(1:num_movies, 1:num_features);56Theta = Theta(1:num_users, 1:num_features);57Y = Y(1:num_movies, 1:num_users);58R = R(1:num_movies, 1:num_users);5960% Evaluate cost function61J = cofiCostFunc([X(:) ; Theta(:)], Y, R, num_users, num_movies, ...62num_features, 0);6364fprintf(['Cost at loaded parameters: %f '...65'\n(this value should be about 22.22)\n'], J);6667fprintf('\nProgram paused. Press enter to continue.\n');68pause;697071%% ============== Part 3: Collaborative Filtering Gradient ==============72% Once your cost function matches up with ours, you should now implement73% the collaborative filtering gradient function. Specifically, you should74% complete the code in cofiCostFunc.m to return the grad argument.75%76fprintf('\nChecking Gradients (without regularization) ... \n');7778% Check gradients by running checkNNGradients79checkCostFunction;8081fprintf('\nProgram paused. Press enter to continue.\n');82pause;838485%% ========= Part 4: Collaborative Filtering Cost Regularization ========86% Now, you should implement regularization for the cost function for87% collaborative filtering. You can implement it by adding the cost of88% regularization to the original cost computation.89%9091% Evaluate cost function92J = cofiCostFunc([X(:) ; Theta(:)], Y, R, num_users, num_movies, ...93num_features, 1.5);9495fprintf(['Cost at loaded parameters (lambda = 1.5): %f '...96'\n(this value should be about 31.34)\n'], J);9798fprintf('\nProgram paused. Press enter to continue.\n');99pause;100101102%% ======= Part 5: Collaborative Filtering Gradient Regularization ======103% Once your cost matches up with ours, you should proceed to implement104% regularization for the gradient.105%106107%108fprintf('\nChecking Gradients (with regularization) ... \n');109110% Check gradients by running checkNNGradients111checkCostFunction(1.5);112113fprintf('\nProgram paused. Press enter to continue.\n');114pause;115116117%% ============== Part 6: Entering ratings for a new user ===============118% Before we will train the collaborative filtering model, we will first119% add ratings that correspond to a new user that we just observed. This120% part of the code will also allow you to put in your own ratings for the121% movies in our dataset!122%123movieList = loadMovieList();124125% Initialize my ratings126my_ratings = zeros(1682, 1);127128% Check the file movie_idx.txt for id of each movie in our dataset129% For example, Toy Story (1995) has ID 1, so to rate it "4", you can set130my_ratings(1) = 4;131132% Or suppose did not enjoy Silence of the Lambs (1991), you can set133my_ratings(98) = 2;134135% We have selected a few movies we liked / did not like and the ratings we136% gave are as follows:137my_ratings(7) = 3;138my_ratings(12)= 5;139my_ratings(54) = 4;140my_ratings(64)= 5;141my_ratings(66)= 3;142my_ratings(69) = 5;143my_ratings(183) = 4;144my_ratings(226) = 5;145my_ratings(355)= 5;146147fprintf('\n\nNew user ratings:\n');148for i = 1:length(my_ratings)149if my_ratings(i) > 0150fprintf('Rated %d for %s\n', my_ratings(i), ...151movieList{i});152end153end154155fprintf('\nProgram paused. Press enter to continue.\n');156pause;157158159%% ================== Part 7: Learning Movie Ratings ====================160% Now, you will train the collaborative filtering model on a movie rating161% dataset of 1682 movies and 943 users162%163164fprintf('\nTraining collaborative filtering...\n');165166% Load data167load('ex8_movies.mat');168169% Y is a 1682x943 matrix, containing ratings (1-5) of 1682 movies by170% 943 users171%172% R is a 1682x943 matrix, where R(i,j) = 1 if and only if user j gave a173% rating to movie i174175% Add our own ratings to the data matrix176Y = [my_ratings Y];177R = [(my_ratings ~= 0) R];178179% Normalize Ratings180[Ynorm, Ymean] = normalizeRatings(Y, R);181182% Useful Values183num_users = size(Y, 2);184num_movies = size(Y, 1);185num_features = 10;186187% Set Initial Parameters (Theta, X)188X = randn(num_movies, num_features);189Theta = randn(num_users, num_features);190191initial_parameters = [X(:); Theta(:)];192193% Set options for fmincg194options = optimset('GradObj', 'on', 'MaxIter', 100);195196% Set Regularization197lambda = 10;198theta = fmincg (@(t)(cofiCostFunc(t, Ynorm, R, num_users, num_movies, ...199num_features, lambda)), ...200initial_parameters, options);201202% Unfold the returned theta back into U and W203X = reshape(theta(1:num_movies*num_features), num_movies, num_features);204Theta = reshape(theta(num_movies*num_features+1:end), ...205num_users, num_features);206207fprintf('Recommender system learning completed.\n');208209fprintf('\nProgram paused. Press enter to continue.\n');210pause;211212%% ================== Part 8: Recommendation for you ====================213% After training the model, you can now make recommendations by computing214% the predictions matrix.215%216217p = X * Theta';218my_predictions = p(:,1) + Ymean;219220movieList = loadMovieList();221222[r, ix] = sort(my_predictions, 'descend');223fprintf('\nTop recommendations for you:\n');224for i=1:10225j = ix(i);226fprintf('Predicting rating %.1f for movie %s\n', my_predictions(j), ...227movieList{j});228end229230fprintf('\n\nOriginal ratings provided:\n');231for i = 1:length(my_ratings)232if my_ratings(i) > 0233fprintf('Rated %d for %s\n', my_ratings(i), ...234movieList{i});235end236end237238239