【吳恩達】機器學習第17章推薦系統以及ex8推薦系統程式設計題
阿新 • • 發佈:2018-12-13
1.基於內容的推薦系統
以電影推薦為例,先介紹以下引數:
r(i,j)表示使用者j對於電影i是否進行了評分。1表示已經評分,0表示沒有評分。
表示使用者j對電影i的評分情況。總共1-5分。
表示對使用者j喜愛電影題材的描述情況.比如(0,5,0)表示每列分別對應玄幻、愛情、動作。這個向量表示使用者喜歡愛情,不喜歡玄幻、動作。
表示電影i的特徵描述。與特徵對應,(2,0,0)表示這個電影含玄幻,並沒有電影、動作的成分。
對於使用者j,它對於一些電影並沒有評分,我們需要預測他的評分。那麼我們就分析電影的成分\以及使用者的特徵,來預測,也就是使用者J對於電影i的評分。這就是基於內容的推薦系統。對於這種辦法來說,我們需要學習
2.協同過濾的推薦系統
對於這種辦法來說,與都需要學習。
求解過程類似於線性迴歸。通過梯度下降的方法進行最小化。這時候,不再新增,=1等。
3.低矩陣分解
通過向量化的方法求解協同過濾演算法。具體看ex8程式設計題。
note:如何判斷給使用者推薦與他觀看的電影類似的電影:,如果要推薦5個電影,那麼選擇距離最小的五個就可以。
note:如果有使用者沒有給任何電影評分,那麼我們可以使用均值歸一化的方法,按行計算y的均值,然後給y的所有值都減去均值,再最後預測的時候+均值。
note:如果有電影沒有任何評分,我們可以按列進行均值,但是不推薦,因為我們無法保證電影的質量。
4.程式設計作業
function [J, grad] = cofiCostFunc(params, Y, R, num_users, num_movies, ... num_features, lambda) %COFICOSTFUNC Collaborative filtering cost function % [J, grad] = COFICOSTFUNC(params, Y, R, num_users, num_movies, ... % num_features, lambda) returns the cost and gradient for the % collaborative filtering problem. % % Unfold the U and W matrices from params X = reshape(params(1:num_movies*num_features), num_movies, num_features); Theta = reshape(params(num_movies*num_features+1:end), ... num_users, num_features); % You need to return the following values correctly J = 0; X_grad = zeros(size(X)); Theta_grad = zeros(size(Theta)); % ====================== YOUR CODE HERE ====================== % Instructions: Compute the cost function and gradient for collaborative % filtering. Concretely, you should first implement the cost % function (without regularization) and make sure it is % matches our costs. After that, you should implement the % gradient and use the checkCostFunction routine to check % that the gradient is correct. Finally, you should implement % regularization. % % Notes: X - num_movies x num_features matrix of movie features % Theta - num_users x num_features matrix of user features % Y - num_movies x num_users matrix of user ratings of movies % R - num_movies x num_users matrix, where R(i, j) = 1 if the % i-th movie was rated by the j-th user % % You should set the following variables correctly: % % X_grad - num_movies x num_features matrix, containing the % partial derivatives w.r.t. to each element of X % Theta_grad - num_users x num_features matrix, containing the % partial derivatives w.r.t. to each element of Theta % temp=0; for i=1:num_movies, for j=1:num_users, if R(i,j)==1, temp=temp+(Theta(j,:)*X(i,:)'-Y(i,j)).^2; end end J=(1/2)*temp+(lambda/2)*sum(sum(X.^2))+(lambda/2)*sum(sum(Theta.^2)); for i=1:num_movies, idx=find(R(i,:)==1); Theta_temp=Theta(idx,:); Y_temp=Y(i,idx); X_grad(i,:)=(X(i,:)*Theta_temp'-Y_temp)*Theta_temp+lambda*X(i,:); end for i=1:num_users, idx=find(R(:,i)==1); Theta_temp=Theta(i,:); Y_temp=Y(idx,i); Theta_grad(i,:)=(X(idx,:)*Theta_temp'-Y_temp)'*X(idx,:)+lambda*Theta_temp; end % ============================================================= grad = [X_grad(:); Theta_grad(:)]; end
Theta_grad(i,:)=(X(idx,:)*Theta_temp'-Y_temp)'*X(idx,:)+lambda*Theta_temp;標紅的轉置是因為idx是向量,而不是標量。