1. 程式人生 > >六、regularized logisitic regssion練習(轉載)

六、regularized logisitic regssion練習(轉載)

method open lose hold on update import 等高線 algo 2個

轉載鏈接:http://www.cnblogs.com/tornadomeet/archive/2013/03/17/2964858.html

在上一講Deep learning:五(regularized線性回歸練習)中已經介紹了regularization項在線性回歸問題中的應用,這節主要是練習regularization項在logistic回歸中的應用,並使用牛頓法來求解模型的參數。參考的網頁資料為:http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=DeepLearning&doc=exercises/ex5/ex5.html。要解決的問題是,給出了具有2個特征的一堆訓練數據集,從該數據的分布可以看出它們並不是非常線性可分的,因此很有必要用更高階的特征來模擬。例如本程序中個就用到了特征值的6次方來求解。

  實驗基礎:

  contour:

  該函數是繪制輪廓線的,比如程序中的contour(u, v, z, [0, 0], ‘LineWidth‘, 2),指的是在二維平面U-V中繪制曲面z的輪廓,z的值為0,輪廓線寬為2。註意此時的z對應的範圍應該與U和V所表達的範圍相同。因為contour函數是用來等高線,而本實驗中只需畫一條等高線,所以第4個參數裏面的值都是一樣的,這裏為[0,0],0指的是函數值z在0和0之間的等高線(很明顯,只能是一條)。

  在logistic回歸中,其表達式為:

  技術分享

  在此問題中,將特征x映射到一個28維的空間中,其x向量映射後為:

  技術分享

  此時加入了規則項後的系統的損失函數為:

  技術分享

  對應的牛頓法參數更新方程為:

  技術分享

  其中:

  技術分享

  公式中的一些宏觀說明(直接截的原網頁):

  技術分享

  實驗結果:

  原訓練數據點的分布情況:

  技術分享

  當lambda=0時所求得的分界曲面:

   技術分享

  當lambda=1時所求得的分界曲面:

  技術分享

  當lambda=10時所求得的分界曲面:

  技術分享

  實驗程序代碼:

%載入數據
clc,clear,close all;
x = load(ex5Logx.dat);
y = load(ex5Logy.dat);

%畫出數據的分布圖
plot(x(find(y),1),x(find(y),2
),o,MarkerFaceColor,b) hold on; plot(x(find(y==0),1),x(find(y==0),2),r+) legend(y=1,y=0) % Add polynomial features to x by % calling the feature mapping function % provided in separate m-file x = map_feature(x(:,1), x(:,2)); %map_feature函數是什麽? [m, n] = size(x); % Initialize fitting parameters theta = zeros(n, 1); % Define the sigmoid function g = inline(1.0 ./ (1.0 + exp(-z))); % setup for Newtons method MAX_ITR = 15; J = zeros(MAX_ITR, 1); % Lambda is the regularization parameter lambda = 1;%lambda=0,1,10,修改這個地方,運行3次可以得到3種結果。 % Newtons Method for i = 1:MAX_ITR % Calculate the hypothesis function z = x * theta; h = g(z); % Calculate J (for testing convergence) J(i) =(1/m)*sum(-y.*log(h) - (1-y).*log(1-h))+ ... (lambda/(2*m))*norm(theta([2:end]))^2; % Calculate gradient and hessian. G = (lambda/m).*theta; G(1) = 0; % extra term for gradient L = (lambda/m).*eye(n); L(1) = 0;% extra term for Hessian grad = ((1/m).*x * (h-y)) + G; H = ((1/m).*x * diag(h) * diag(1-h) * x) + L; % Here is the actual update theta = theta - H\grad; end % Show J to determine if algorithm has converged J % display the norm of our parameters norm_theta = norm(theta) % Plot the results % We will evaluate theta*x over a % grid of features and plot the contour % where theta*x equals zero % Here is the grid range u = linspace(-1, 1.5, 200); v = linspace(-1, 1.5, 200); z = zeros(length(u), length(v)); % Evaluate z = theta*x over the grid for i = 1:length(u) for j = 1:length(v) z(j,i) = map_feature(u(i), v(j))*theta;%這裏繪制的並不是損失函數與叠代次數之間的曲線,而是線性變換後的值 end end z = z; % important to transpose z before calling contour % Plot z = 0 % Notice you need to specify the range [0, 0] contour(u, v, z, [0, 0], LineWidth, 2)%在z上畫出為0值時的界面,因為為0時剛好概率為0.5,符合要求 legend(y = 1, y = 0, Decision boundary) title(sprintf(\\lambda = %g, lambda), FontSize, 14) hold off % Uncomment to plot J % figure % plot(0:MAX_ITR-1, J, o--, MarkerFaceColor, r, MarkerSize, 8) % xlabel(Iteration); ylabel(J)

疑問: 上文中的map_feature是什麽函數?

六、regularized logisitic regssion練習(轉載)