使用矢量化的梯度下降的八度代码无法正确更新成本函数 [英] Octave code for gradient descent using vectorization not updating cost function correctly

查看:91
本文介绍了使用矢量化的梯度下降的八度代码无法正确更新成本函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用矢量化实现了以下用于梯度下降的代码,但是代价函数似乎并没有正确地递减.相反,代价函数会随着每次迭代的增加而增加.

I have implemented following code for gradient descent using vectorization but it seems the cost function is not decrementing correctly.Instead the cost function is increasing with each iteration.

假设theta为n + 1个向量,y为m个向量,X为设计矩阵m *(n + 1)

Assuming theta to be an n+1 vector, y to be a m vector and X to be design matrix m*(n+1)

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)

m = length(y); % number of training examples
n = length(theta); % number of features
J_history = zeros(num_iters, 1);
error = ((theta' * X')' - y)*(alpha/m);
descent = zeros(size(theta),1);

for iter = 1:num_iters
for i = 1:n
   descent(i) = descent(i) + sum(error.* X(:,i));
   i = i + 1;
end 

theta = theta - descent;
J_history(iter) = computeCost(X, y, theta);
disp("the value of cost function is : "), disp(J_history(iter));
iter = iter + 1;
end

计算成本函数为:

function J = computeCost(X, y, theta)
m = length(y);
J = 0;
for i = 1:m,
   H = theta' * X(i,:)';
   E = H - y(i);
   SQE = E^2;
   J = (J + SQE);
   i = i+1;
end;
J = J / (2*m);

推荐答案

您可以进一步矢量化它:

You can vectorise it even further:

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
    m = length(y); 
    J_history = zeros(num_iters, 1);

    for iter = 1:num_iters

       delta = (theta' * X'-y')*X;
       theta = theta - alpha/m*delta';
       J_history(iter) = computeCost(X, y, theta);

    end

end

这篇关于使用矢量化的梯度下降的八度代码无法正确更新成本函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆