2014-10-31 32 views
5

嗨,我是想實現梯度下降算法的功能:有什麼不對我的梯度下降算法

enter image description here

我對算法的出發點是W =(U,V)=( 2,2)。學習率是eta = 0.01和界限= 10^-14。這裏是我的MATLAB代碼:

function [resultTable, boundIter] = gradientDescent(w, iters, bound, eta) 
% FUNCTION [resultTable, boundIter] = gradientDescent(w, its, bound, eta) 
% 
% DESCRIPTION: 
% - This function will do gradient descent error minimization for the 
% function E(u,v) = (u*exp(v) - 2*v*exp(-u))^2. 
% 
% INPUTS: 
% 'w' a 1-by-2 vector indicating initial weights w = [u,v] 
% 'its' a positive integer indicating the number of gradient descent 
% iterations 
% 'bound' a real number indicating an error lower bound 
% 'eta' a positive real number indicating the learning rate of GD algorithm 
% 
% OUTPUTS: 
% 'resultTable' a iters+1-by-6 table indicating the error, partial 
% derivatives and weights for each GD iteration 
% 'boundIter' a positive integer specifying the GD iteration when the error 
% function got below the given error bound 'bound' 
% 


% The error function 
E = @(u,v) (u*exp(v) - 2*v*exp(-u))^2; 

% Partial derivative of E with respect to u 
pEpu = @(u,v) 2*(u*exp(v) - 2*v*exp(-u))*(exp(v) + 2*v*exp(-u)); 
% Partial derivative of E with respect to v 
pEpv = @(u,v) 2*(u*exp(v) - 2*v*exp(-u))*(u*exp(v) - 2*exp(-u)); 

% Initialize boundIter 
boundIter = 0; 
% Create a table for holding the results 
resultTable = zeros(iters+1, 6); 
% Iteration number 
resultTable(1, 1) = 0; 
% Error at iteration i 
resultTable(1, 2) = E(w(1), w(2)); 
% The value of pEpu at initial w = (u,v) 
resultTable(1, 3) = pEpu(w(1), w(2)); 
% The value of pEpv at initial w = (u,v) 
resultTable(1, 4) = pEpv(w(1), w(2)); 
% Initial u 
resultTable(1, 5) = w(1); 
% Initial v 
resultTable(1, 6) = w(2); 

% Loop all the iterations 
for i = 2:iters+1 

    % Save the iteration number 
    resultTable(i, 1) = i-1; 
    % Update the weights 
    temp1 = w(1) - eta*(pEpu(w(1), w(2))); 
    temp2 = w(2) - eta*(pEpv(w(1), w(2))); 
    w(1) = temp1; 
    w(2) = temp2; 
    % Evaluate the error function at new weights 
    resultTable(i, 2) = E(w(1), w(2)); 
    % Evaluate pEpu at the new point 
    resultTable(i, 3) = pEpu(w(1), w(2)); 
    % Evaluate pEpv at the new point 
    resultTable(i, 4) = pEpv(w(1), w(2)); 
    % Save the new weights 
    resultTable(i, 5) = w(1); 
    resultTable(i, 6) = w(2); 
    % If the error function is below a specified bound save this iteration 
    % index 
    if E(w(1), w(2)) < bound 
     boundIter = i-1; 
    end 

end 

這是在我的機器學習課程的練習,但由於某些原因,我的成績都是錯誤的。代碼中必須有錯誤。我試過調試和調試它,並沒有發現任何錯誤...有人可以確定我的問題在這裏?...換句話說,你可以檢查代碼是否爲給定函數的有效梯度下降算法?

請讓我知道,如果我的問題是太不清楚,或者如果你需要更多的信息:)

謝謝你的努力和幫助! =)

這裏是我的結果五個迭代和別人有:

參數:W = [2,2],埃塔= 0.01,勢必= 10^-14,iters = 5

enter image description here

+0

你有輸入數據和結果嗎? – 2014-10-31 12:14:20

+0

@AnderBiguri嗨,這個問題沒有輸入數據。重點在於最小化具有梯度下降的給定函數E(u,v)。起點是w =(u,v)=(2,2),eta = 0.01,bound = 10^-14。 'iters'參數可以自由選擇,例如iters = 50.我將用五次迭代發佈我的結果,然後從我的課程討論論壇獲得其他人的相應結果。 – jjepsuomi 2014-10-31 12:18:18

+1

哈哈有輸入數據,只要你給我吧!謝謝,我會檢查。 – 2014-10-31 12:20:04

回答

4

隨着問題討論如下:我會說,別人都錯了......你的最小化導致的E(u,v)較小的值,請檢查:

E(1.4,1.6) = 37.8 >> 3.6 = E(0.63, -1.67) 
+0

+1謝謝:) – jjepsuomi 2014-10-31 13:19:39

+1

歡迎你接受你的建議的答案;) – matheburg 2014-10-31 13:19:52

2

(我不只是評論道歉,但我是新來的SO,不能發表評論。)

看來,你的算法是做正確的事。你想要確定的是,在每一步能量都在減少(它是這樣)。有幾個原因可能會導致你的數據點可能與班級中的其他人不一致:他們可能是錯的(你或班上其他人),他們可能是從不同的角度開始的,他們可能使用了不同的步長(你是什麼我打電話給eta)。

理想情況下,您不想硬編碼迭代次數。你想繼續下去,直到你達到當地的最低值(希望這是全球最低)。爲了檢查這一點,你希望兩個偏導數都是零(或非常接近)。此外,爲了確保你處於本地分鐘(不是本地最大或鞍點),你應該檢查E_uu * E_vv - E_uv^2的符號和E_uu的符號:http://en.wikipedia.org/wiki/Second_partial_derivative_test瞭解詳情(第二個衍生測試,在頂部)。如果你發現自己處於局部最大或鞍點,你的梯度會告訴你不要移動(因爲偏導數是0)。既然你知道這不是最優的,你必須干擾你的解決方案(有時稱爲模擬退火)。

希望這會有所幫助。

+0

+1謝謝@TravisJ出色答卷: )你也是對的。我得到了正確的答案。看起來其他人確實是錯的:) – jjepsuomi 2014-10-31 13:21:14

4

不是一個完整的答案,但讓我們去爲它:

我在你的代碼添加了一個陰謀的一部分,所以你可以看到怎麼回事。

u1=resultTable(:,5); 
v1=resultTable(:,6); 
E1=E(u1,v1); 
E1(E1<bound)=NaN; 
[x,y]=meshgrid(-1:0.1:5,-5:0.1:2);Z=E(x,y); 
surf(x,y,Z) 

hold on 
plot3(u1,v1,E1,'r') 
plot3(u1,v1,E1,'r*') 

enter image description here 結果表明,你的算法是做該功能的正確的事情。所以,正如其他人所說,或所有其他人都是錯的,或者你沒有使用正在開始的正確方程式。

+0

+1非常好,謝謝@AnderBiguri非常感謝你的努力! :) – jjepsuomi 2014-10-31 13:24:51