function check_grad(f, x0, varargin) % a simple function that checks the correctness of gradient. % INPUT % f - a function handle of f(x) that returns function values and gradients given parameter x % x0 - the location near which the gradient will be evaluted. % For a correct gradiet, the displayed ratio should be near 1.0 % % to check why the code works there is a useful link: % http://ufldl.stanford.edu/tutorial/supervised/DebuggingGradientChecking/ % % Jiayu, Dec 2, 2015 delta = rand(size(x0)); delta = delta ./ norm(delta); epsilon = 10.^[-7:-1]; [f0, df0] = feval(f, x0, varargin{:}); for i = 1:length(epsilon) [f_left] = feval(f, x0-epsilon(i)*delta, varargin{:}); [f_right] = feval(f, x0+epsilon(i)*delta, varargin{:}); ys(i) = (f_right - f_left) / 2; ys_hat(i) = df0' * epsilon(i)*delta; fprintf('epsilon: %d , gradient: %d \n', epsilon(i), ys(i) / ys_hat(i)); end