【吴恩达】机器学习第17章推荐系统以及ex8推荐系统编程题

1.基于内容的推荐系统

以电影推荐为例,先介绍以下参数:

r(i,j)表示用户j对于电影i是否进行了评分。1表示已经评分,0表示没有评分。

y^{(i,j)}表示用户j对电影i的评分情况。总共1-5分。

\theta ^{(j)}表示对用户j喜爱电影题材的描述情况.比如(0,5,0)表示每列分别对应玄幻、爱情、动作。这个向量表示用户喜欢爱情,不喜欢玄幻、动作。

x^{(i)}表示电影i的特征描述。与\theta ^{(j)}特征对应,(2,0,0)表示这个电影含玄幻,并没有电影、动作的成分。

对于用户j,它对于一些电影并没有评分,我们需要预测他的评分。那么我们就分析电影的成分x^{(i)}\以及用户的特征\theta ^{(j)},来预测\theta ^{(j)}^T(x^{(i)}),也就是用户J对于电影i的评分。这就是基于内容的推荐系统。对于这种办法来说,我们需要学习\theta ^{(j)},x^{(i)}是我们设定的。

2.协同过滤的推荐系统

对于这种办法来说,\theta ^{(j)}x^{(i)}都需要学习。

求解过程类似于线性回归。通过梯度下降的方法进行最小化。这时候,不再添加\theta _{0}=1;x_{0}=1,x_{0}=1等。

3.低矩阵分解

通过向量化的方法求解协同过滤算法。具体看ex8编程题。

note:如何判断给用户推荐与他观看的电影类似的电影:min \left \| x^{(i)}-x^{(j)} \right \|,如果要推荐5个电影,那么选择距离最小的五个就可以。

note:如果有用户没有给任何电影评分,那么我们可以使用均值归一化的方法,按行计算y的均值,然后给y的所有值都减去均值,再最后预测的时候\theta ^{(j)}^T(x^{(i)})+均值。

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是向量,而不是标量。

猜你喜欢

转载自blog.csdn.net/BRAVE_NO1/article/details/82940342