Course Link: http://ufldl.stanford.edu/tutorial/supervised/LogisticRegression/
This section focuses on the concept of gradients, in the experimental section, comparing the gradient of the previous linear regression with the gradient computed by definition, to count the errors between the two.
Reference: http://blog.csdn.net/lingerlanlan/article/details/38390955
Code I added a bit of comment:
The first code is changed from EX1A_LINREG.M, mainly to get training data and test data, as well as their tags.
%%this Exercise uses a data fromThe UCI repository:% Bache, K. & Lichman, M. (2013). UCI Machine learning Repository% http://archive.ics.uci.edu/ml% Irvine, ca:university of California, School of information andComputer Science.%%Data created by:% Harrison, D. andRubinfeld, D.l.%"'Hedonic Prices andThe demand forClean Air"'% J. Environ. Economics & Management, Vol.5, 81-102, 1978.%Addpath ./Commonaddpath ./common/minfunc_2012/Minfuncaddpath ./common/minfunc_2012/minfunc/compiled% Load Housing Data fromFile.data= Load ('Housing.data');d ATA=data';% put examples in columns%Include a row of 1s as an additional intercept feature.data= [Ones (1,size (data,2)); Data];%Shuffle Examples.data= Data (:, Randperm (Size (data,2)));returns a column of data% Split into train andTest sets get training data and test data, and get the corresponding label% the last row of'Data' isThe median home price.train.x= Data (1:end-1,1:400); TRAIN.Y= Data (end,1:400); test. X= Data (1:end-1,401: End); Test.y= Data (end,401: End); M=size (train. x,2); n=size (train. x,1);%Initialize the coefficient vector theta to random Values.theta= rand (n,1);%generates a number from 0 to 1 for n rows of 1 columns%Run the Minfunc optimizer with LINEAR_REGRESSION.M as the objective.Percent todo:implement the linear regression objective andGradient Computations%inchlinear_regression.m%tic;% options = struct ('Maxiter', 200);% theta =Minfunc (@linear_regression, theta, options, train. X, TRAIN.Y);% fprintf ('optimization took%f seconds.\n', TOC); Grad_check (@linear_regression, Theta,200,train. X,TRAIN.Y)
View Code
the second piece of code is the GRAD_CHECK.M function
function Average_error =Grad_check (fun, theta0, Num_checks, varargin) Delta=1e-3; Sum_error=R; fprintf ('Iter I err'); fprintf ('g_est G f\n') forI=1: Num_checks T=theta0; J= Randsample (Numel (T), 1);% from 1~Numel (T) randomly returns a number T0=t; T0 (j) = T0 (j)-Delta; T1=t; T1 (j) = T1 (j) +Delta; [F,g]= Fun (T, varargin{:});T is the objective function and Varargin is the objective function gradient F0=Fun (T0, varargin{:}); F1=Fun (T1, varargin{:}); G_est= (f1-f0)/(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=sum_error/num_checks;
View Code
UFLDL Tutorial Learning Notes (ii)