标签:ufldl 深度学习 deep learning gradient
% 说明:grad_check 参数 % fun为函数 % num_checks 检查次数 % varagin为参数列 var1,var2,var3...这个varagin必须放在function最后一个项 function average_error = grad_check(fun, theta0, num_checks, varargin) delta=1e-3; sum_error=0; fprintf(‘ Iter i err‘); fprintf(‘ g_est g f\n‘) for i=1:num_checks T = theta0; j = randsample(numel(T),1); T0=T; T0(j) = T0(j)-delta; T1=T; T1(j) = T1(j)+delta; [f,g] = fun(T, varargin{:}); %由于fun是linear_regression或logistic_regression f0 = fun(T0, varargin{:}); %所以这里的varagin{:}参数为train.X,train.y f1 = fun(T1, varargin{:}); g_est = (f1-f0) / (2*delta); error = abs(g(j) - g_est); fprintf(‘% 5d % 6d % 15g % 15f % 15f % 15f\n‘, ... i,j,error,g(j),g_est,f); sum_error = sum_error + error; end average_error =sum_error/num_checks;
% Gradient Check average_error = grad_check(@linear_regression_vec,theta,50,train.X,train.y); fprintf(‘Average error :%f\n‘,average_error);
深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 4:Debugging: Gradient Checking
标签:ufldl 深度学习 deep learning gradient
原文地址:http://blog.csdn.net/songrotek/article/details/41309615