%% Model Building with Gradient Descent close all clear clc % Data represents housing prices. The first column % is square footage, second column is price (fictional). % We want to find a function that predicts price based % on the square footage. X=[1100 199000 1400 245000 1425 319000 1550 240000 1600 312000 1700 279000 1700 310000 1875 308000 2350 405000 2450 324000]; %% Step 0: Preprocess the data plot(X(:,1),X(:,2),'k^-') % Rescale the data mx=mean(X,1); sx=std(X,1); % Keep these around in case you need to rescale Xs=(X-mx)./sx; % new data. figure(1) plot(Xs(:,1),Xs(:,2),'k^-') % We'll break up the data for Step 2: x=Xs(:,1); t=Xs(:,2); %Targets %% Step 1: Randomly initialize the training parameters and other constants m=randn; b=randn; MaxIters=20; alpha = 0.1; tol = 1e-8; %% Main Loop: Steps 2-4 for i=1:MaxIters % Step 2: Calculate the error at each point y=m*x+b; ErrVec=t-y; Error(i)=sum(ErrVec.*ErrVec); % Step 3: Calculuate the gradient: temp1=ErrVec.*(-x); temp2=ErrVec.*(-1); %These are vectors Em=2*sum(temp1); Eb=2*sum(temp2); %These scalars make up gradient % Step 3A: Adjust the step size using backtracking line search flag=true; while flag temp=[m;b]-alpha*[Em;Eb]; LHS=sum(temp.*temp); RHS=Error(i)-(alpha/2)*(Em^2+Eb^2); if LHS<=RHS flag=false; break else alpha=alpha/2; end end %End of while loop for step size % Uncomment if you want to see the step sizes: %fprintf('Step size is %f\n',alpha); % Step 4: Adjust the parameters using gradient descent. m=m-alpha*Em; b=b-alpha*Eb; % Stop if the gradient is very close to zero fprintf('Gradient is %f\n',Em^2+Eb^2); if (Em^2+Eb^2)<=tol fprintf('Solution found in %d steps\n',i) break end end %% Closing: We'll visualize our results: figure(2) plot(Error) figure(1) hold on t=linspace(min(x),max(x)); z=m*t+b; plot(t,z,'r-'); hold off