cofiCostFunc_bak.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. function [J, grad] = cofiCostFunc(params, Y, R, num_users, num_movies, ...
  2. num_features, lambda)
  3. %COFICOSTFUNC Collaborative filtering cost function
  4. % [J, grad] = COFICOSTFUNC(params, Y, R, num_users, num_movies, ...
  5. % num_features, lambda) returns the cost and gradient for the
  6. % collaborative filtering problem.
  7. %
  8. % Unfold the U and W matrices from params
  9. X = reshape(params(1:num_movies*num_features), num_movies, num_features);
  10. Theta = reshape(params(num_movies*num_features+1:end), ...
  11. num_users, num_features);
  12. % You need to return the following values correctly
  13. J = 0;
  14. X_grad = zeros(size(X));
  15. Theta_grad = zeros(size(Theta));
  16. %fprintf('-----------size num movies %d\n', num_movies);
  17. % ====================== YOUR CODE HERE ======================
  18. % Instructions: Compute the cost function and gradient for collaborative
  19. % filtering. Concretely, you should first implement the cost
  20. % function (without regularization) and make sure it is
  21. % matches our costs. After that, you should implement the
  22. % gradient and use the checkCostFunction routine to check
  23. % that the gradient is correct. Finally, you should implement
  24. % regularization.
  25. %
  26. % Notes: X - num_movies x num_features matrix of movie features
  27. % Theta - num_users x num_features matrix of user features
  28. % Y - num_movies x num_users matrix of user ratings of movies
  29. % R - num_movies x num_users matrix, where R(i, j) = 1 if the
  30. % i-th movie was rated by the j-th user
  31. %
  32. % You should set the following variables correctly:
  33. %
  34. % X_grad - num_movies x num_features matrix, containing the
  35. % partial derivatives w.r.t. to each element of X
  36. % Theta_grad - num_users x num_features matrix, containing the
  37. % partial derivatives w.r.t. to each element of Theta
  38. %
  39. % Add intercept term to X
  40. X = [ones(num_movies, 1) X];
  41. X_grad = zeros(size(X));
  42. % Initialize fitting parameters
  43. Theta = [zeros(num_users, 1) Theta];
  44. Theta_grad = zeros(size(X));
  45. regT = lambda / 2 * sum(sum(Theta(:,2:end).^2));
  46. regX = lambda / 2 * sum(sum(X(:,2:end).^2));
  47. % step 1 -> select only Rij == 1 -> X (5x 3+1) * Theta'(3+1 x4) -> Y (5x4)
  48. % step 2 sum i of Y -> sum j of Y -> Y -> (1x1)
  49. % refer to lecture notes pg 18
  50. J = 1 / 2 * sum(sum( R .* (X * Theta' - Y).^2)) + regT + regX;
  51. for i = 1:num_movies
  52. idx = find(R(i,:) == 1);
  53. %num_users_rated = size(idx);
  54. %dumpsize('idx', idx);
  55. %fprintf('there are %d users that rated the movie at %dth row (tot movies %d)\n', columns(num_users_rated), i, num_movies);
  56. Theta_temp = Theta(idx,:);
  57. %dumpsize('Theta_temp', Theta_temp);
  58. Y_temp = Y(i,idx);
  59. %dumpsize('Y_temp', Y_temp);
  60. X_grad(i,:) = (X(i,:) * Theta_temp' - Y_temp) * Theta_temp;
  61. endfor
  62. % for each user j,
  63. % -- determine the theta_gradient of user j
  64. for j = 1:num_users
  65. % find all movies rated by user j
  66. % that means, select column j of R, go down through each row from 1 to num_movies, record the
  67. % array index where the cell(i,j) == 1
  68. idx = find(R(:,j) == 1);
  69. %fprintf('there are %d movies that were rated by user %d (tot users %d)\n', columns(idx), i, num_users);
  70. % example:
  71. % R = [0;1;0;1;0]
  72. % then idx = [2, 4, 5]
  73. % and Y at the jth column = [0;4;0;3;5]
  74. % thus Y_temp = [4;3;5]
  75. % X_temp row 1 = contents of row 2 and all columns from X
  76. % X_temp row 2 = contents of row 4 and all columns from X
  77. % X_temp row 3 = contents of row 5 and all columns from X
  78. Y_temp = Y(idx,j);
  79. X_temp = X(idx,:);
  80. Theta_grad(j,:) = (X_temp * Theta(j,:)' - Y_temp)' * X_temp;
  81. endfor
  82. % get rid of the intercept term X0 and theta0
  83. X_grad = X_grad(:,2:end);
  84. Theta_grad = Theta_grad(:,2:end);
  85. % =============================================================
  86. grad = [X_grad(:); Theta_grad(:)];
  87. end