-
-
Save AdroitAnandAI/c69f995b78c0d174bb66cc56eeb5535a to your computer and use it in GitHub Desktop.
collaborative filtering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [J, grad] = cofiCostFunc(params, Y, R, num_users, num_movies, ... | |
num_features, lambda) | |
% 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)); | |
% Compute the cost function and gradient for collaborative filtering. | |
% Implemented the cost function and gradient | |
J = sum(sum(R.*((X*Theta' - Y).^2)))/2 + ... | |
(sum(sum(Theta.^2)) + sum(sum(X.^2)))*lambda/2; | |
X_grad = (R.*(X*Theta' - Y))*Theta + lambda.*X; | |
Theta_grad = (R.*(X*Theta' - Y))'*X + lambda.*Theta; | |
grad = [X_grad(:); Theta_grad(:)]; | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment