Exercise: Linear Regression
題目地址: http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=DeepLearning&doc=exercises/ex2/ex2.html
題目給出的數據是一組2-8歲男童的身高。x是年齡,y是身高。樣本數m=50.
使用gradient descent來做線性回歸。
step1:數據準備。
加載數據:
>> x=load('ex2x.dat');
>> y=load('ex2y.dat');
可視化數據:
figure % open a new figure window plot(x, y, 'o'); ylabel('Height in meters') xlabel('Age in years')效果如下:
我們將模型設定為:
Hθ(x) = θ0 + θ1x
因此,還需要在數據集x的坐標統一加上1,相當于x0=1
m = length(y); % store the number of training examples x = [ones(m, 1), x]; % Add a column of ones to x增加了這一列以后,要特別注意,現在年齡這一變量已經移動到了第二列上。
step2:線性回歸
先來回憶一下我們的計算模型:
批量梯度下降(注意不是online梯度下降)的規則是:
題目要求:
1、使用步長為0.07的學習率來進行梯度下降。權重向量θ={θ0,θ1}初始化為0。計算一次迭代后的權重向量θ的值。
首先初始化權重向量theta
>> theta=zeros(1,2)
theta =???? 0???? 0
計算迭代一次時的權重向量:
delta=(x*theta')-y;
sum=delta'*x;
delta_theta=sum*0.07/m;
theta1=theta - delta_theta;
求得的theta1的結果為:
theta1 =??? 0.0745??? 0.3800
?
2、迭代進行gradient descent,直到收斂到一個點上。
測試代碼(每100步輸出一條theta直線,直到迭代結束):
function [ result ] = gradientDescent( x,y,alpha,accuracy ) %GRADIENTDESCENT Summary of this function goes here % Detailed explanation goes here orgX=x; plot(orgX, y, 'o'); ylabel('Height in meters') xlabel('Age in years') m=length(y); x=[ones(m,1),x]; theta = zeros(1,2); hold on; times = 0; while(1)times = times + 1;delta=(x*theta')-y;sum=delta'*x;delta_theta=sum*alpha/m;theta1=theta - delta_theta;if(all(abs(theta1(:) - theta(:)) < accuracy))result = theta;break;endtheta = theta1;if(mod(times,100) == 0)plot(x(:,2), x*theta', '-','color',[(mod(times/100,256))/256 128/256 2/256]); % remember that x is now a matrix with 2 columns% and the second column contains the time infolegend('Training data', 'Linear regression');end endend效果如下:
可以看到,theta所確定的直線慢慢逼近數據集。
潔凈版的代碼(只輸出最終的theta結果,不輸出中間步驟):
function [ result ] = gradientDescent( x,y,alpha,accuracy ) %GRADIENTDESCENT Summary of this function goes here % Detailed explanation goes here orgX=x; plot(orgX, y, 'o'); ylabel('Height in meters') xlabel('Age in years') m=length(y); x=[ones(m,1),x]; theta = zeros(1,2); hold on; while(1)delta=(x*theta')-y;sum=delta'*x;delta_theta=sum*alpha/m;theta1=theta - delta_theta;if(all(abs(theta1(:) - theta(:)) < accuracy))result = theta;plot(x(:,2), x*theta', '-','color',[200/256 128/256 2/256]); % remember that x is now a matrix with 2 columns% and the second column contains the time infolegend('Training data', 'Linear regression');break;endtheta = theta1; endend結果:
執行代碼
result=gradientDescent(x,y,0.07,0.00000001)
其中0.07為步長(即學習率),0.00000001為兩個浮點矩陣的相近程度(即在這個程度內,視為這兩個浮點矩陣相等)
收斂時的theta值為:
theta =??? 0.7502 ?? 0.0639
?
3、現在,我們已經訓練出了權重向量theta的值,我們可以用它來預測一下別的數據。
predict the height for a two boys of age 3.5 and age 7
代碼如下:
>> boys=[3.5;7];
>> boys=[ones(2,1),boys];
>> heights=boys*theta';
執行結果:
heights =
??? 0.9737
??? 1.1973
?
4、理解J(θ)
?
J(θ)是與權重向量相關的cost function。權重向量的選取,會影響cost的大小。我們希望cost可以盡量小,這樣相當于尋找cost function的最小值。查找一個函數的最小值有一個簡單的方法,就是對該函數求導(假設該函數可導),然后取導數為0時的點,該點即為函數的最小值(也可能是極小值)。梯度下降就是對J(θ)求偏導后,一步一步逼近導數為0的點的方法。
因為我們在此練習中使用的θ向量只有兩個維度,因此可以使用matlab的plot函數進行可視化。在一般的應用中,θ向量的維度很高,會形成超平面,因此無法簡單的用plot的方法進行可視化。
代碼:
function [] = showJTheta( x,y ) %SHOW Summary of this function goes here % Detailed explanation goes here J_vals = zeros(100, 100); % initialize Jvals to 100x100 matrix of 0's theta0_vals = linspace(-3, 3, 100); theta1_vals = linspace(-1, 1, 100); for i = 1:length(theta0_vals)for j = 1:length(theta1_vals)t = [theta0_vals(i); theta1_vals(j)];J_vals(i,j) = sum(sum((x*t - y).^2,2),1)/(2*length(y));end end% Plot the surface plot % Because of the way meshgrids work in the surf command, we need to % transpose J_vals before calling surf, or else the axes will be flipped J_vals = J_vals' figure; surf(theta0_vals, theta1_vals, J_vals); axis([-3, 3, -1, 1,0,40]); xlabel('\theta_0'); ylabel('\theta_1')end執行:
x=load('ex2x.dat');
y=load('ex2y.dat');
x=[ones(length(y),1),x];
showJTheta(x,y);
結果
加上等高線的繪制語句
figure;
% Plot the cost function with 15 contours spaced logarithmically
% between 0.01 and 100
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 2, 15));
xlabel('\theta_0'); ylabel('\theta_1');
效果
總結
以上是生活随笔為你收集整理的Exercise: Linear Regression的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Lucene的评分(score)机制的简
- 下一篇: Exercise: Logistic R