You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.6 KiB
Matlab
48 lines
1.6 KiB
Matlab
function checkCostFunction(lambda)
|
|
%CHECKCOSTFUNCTION Creates a collaborative filering problem
|
|
%to check your cost function and gradients
|
|
% CHECKCOSTFUNCTION(lambda) Creates a collaborative filering problem
|
|
% to check your cost function and gradients, it will output the
|
|
% analytical gradients produced by your code and the numerical gradients
|
|
% (computed using computeNumericalGradient). These two gradient
|
|
% computations should result in very similar values.
|
|
|
|
% Set lambda
|
|
if ~exist('lambda', 'var') || isempty(lambda)
|
|
lambda = 0;
|
|
end
|
|
|
|
%% Create small problem
|
|
X_t = rand(4, 3);
|
|
Theta_t = rand(5, 3);
|
|
|
|
% Zap out most entries
|
|
Y = X_t * Theta_t';
|
|
Y(rand(size(Y)) > 0.5) = 0;
|
|
R = zeros(size(Y));
|
|
R(Y ~= 0) = 1;
|
|
|
|
%% Run Gradient Checking
|
|
X = randn(size(X_t));
|
|
Theta = randn(size(Theta_t));
|
|
num_users = size(Y, 2);
|
|
num_movies = size(Y, 1);
|
|
num_features = size(Theta_t, 2);
|
|
|
|
numgrad = computeNumericalGradient( ...
|
|
@(t) cofiCostFunc(t, Y, R, num_users, num_movies, ...
|
|
num_features, lambda), [X(:); Theta(:)]);
|
|
|
|
[cost, grad] = cofiCostFunc([X(:); Theta(:)], Y, R, num_users, ...
|
|
num_movies, num_features, lambda);
|
|
|
|
disp([numgrad grad]);
|
|
fprintf(['The above two columns you get should be very similar.\n' ...
|
|
'(Left-Your Numerical Gradient, Right-Analytical Gradient)\n\n']);
|
|
|
|
diff = norm(numgrad-grad)/norm(numgrad+grad);
|
|
fprintf(['If your backpropagation implementation is correct, then \n' ...
|
|
'the relative difference will be small (less than 1e-9). \n' ...
|
|
'\nRelative Difference: %g\n'], diff);
|
|
|
|
end |