for iter = 1:num_iters
%梯度下降 用户向量
for i = 1:m
%返回有0有1 是逻辑值
ratedIndex1 = R_training(i,:)~=0 ;
%U(i,:) * V‘ 第i个用户分别对每个电影的评分
%sumVec1 第i个用户分别对每个电影的评分 减去真实值
sumVec1 = ratedIndex1 .* (U(i,:) * V‘ - R_training(i,:));
product1 = sumVec1 * V;
derivative1 = product1 + lambda_u * U(i,:);
old_U(i,:) = U(i,:) - theta * derivative1;
end
%梯度下降 电影向量
for j = 1:n
ratedIndex2 = R_training(:,j)~=0;
sumVec2 = ratedIndex2 .* (U * V(j,:)‘ - R_training(:,j));
product2 = sumVec2‘ * U;
derivative2 = product2 + lambda_v * V(j,:);
old_V(j,:) = V(j,:) - theta * derivative2;
end
U = old_U;
V = old_V;
RMSE(i,1) = CompRMSE(train_vec,U,V);
RMSE(i,2) = CompRMSE(probe_vec,U,V);
end
原文:http://www.cnblogs.com/hxsyl/p/4899248.html