嗨,我是想實現梯度下降算法的功能:有什麼不對我的梯度下降算法
我對算法的出發點是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
你有輸入數據和結果嗎? – 2014-10-31 12:14:20
@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
哈哈有輸入數據,只要你給我吧!謝謝,我會檢查。 – 2014-10-31 12:20:04