algorithm - 我的梯度下降算法有什么问题

标签 algorithm matlab optimization machine-learning gradient

您好,我正在尝试为一个函数实现梯度下降算法:

enter image description here

我的算法起点是 w = (u,v) = (2,2)。学习率为 eta = 0.01 和 bound = 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

这是我的机器学习类(class)中的一个练习,但出于某种原因,我的结果全都错了。代码中一定有问题。我已经尝试调试和调试它,但没有发现任何错误......有人能确定我的问题是什么吗?......换句话说,你能检查代码是否是给定函数的有效梯度下降算法?

如果我的问题不太清楚或者您需要更多信息,请告诉我 :)

感谢您的努力和帮助! =)

这是我五次迭代的结果以及其他人得到的结果:

PARAMETERS: w = [2,2], eta = 0.01, bound = 10^-14, iters = 5

enter image description here

最佳答案

正如下面讨论的问题:我会说其他人是错误的......你的最小化导致 E(u,v) 的值更小,检查:

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

关于algorithm - 我的梯度下降算法有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26674311/

相关文章:

python - 使用 Numpy 和 Cython 加速距离矩阵计算

c++ - 如何使插入排序更快?

c# - 建议一种针对大型已知集进行颜色模式匹配的算法

linux - Matlab linux GUI 故障

javascript - 从 CDN,head 或 body 哪里加载脚本或 css?

python-3.x - 为什么我在 Tensorflow 中构建自定义优化器时得到 "NotImplementedError()"

sql - 如何为子集构建基于集合的查询?

c# - 求某公历年农历新年公历的算法

matlab - 如何从函数句柄中获取函数子集,函数句柄是 Matlab 中的函数向量

c++ - 如何在 C++ 中创建这个 yml 文件(使用 opencv)