以八度为单位的梯度下降实现

时间:2012-05-14 21:28:43

标签: octave

我实际上已经在这个问题上奋斗了2个月了。是什么让这些与众不同?

hypotheses= X * theta
temp=(hypotheses-y)'
temp=X(:,1) * temp
temp=temp * (1 / m)
temp=temp * alpha
theta(1)=theta(1)-temp

hypotheses= X * theta
temp=(hypotheses-y)'
temp=temp * (1 / m)
temp=temp * alpha
theta(2)=theta(2)-temp



theta(1) = theta(1) - alpha * (1/m) * ((X * theta) - y)' * X(:, 1);
theta(2) = theta(2) - alpha * (1/m) * ((X * theta) - y)' * X(:, 2);

后者有效。我只是不确定为什么......我很难理解矩阵逆的需要。

7 个答案:

答案 0 :(得分:64)

你在错过了一步的第二个区块的第一个例子中你做了什么不是吗?我假设你用一个矢量连接X.

   temp=X(:,2) * temp

最后一个例子可以使用,但可以更加简化和有效地进行矢量化。

我假设你只有1个功能。它将与多个功能一样工作,因为所有发生的事情是你为每个功能的X矩阵添加一个额外的列。基本上你向x添加一个向量来矢量化截距。

您可以在一行代码中更新thetas的2x1矩阵。使用x连接一个使其成为nx2矩阵的向量,然后您可以通过乘以theta向量(2x1)计算h(x),这是(X * theta)位。

矢量化的第二部分是转置(X * theta) - y),它给出一个1 * n矩阵,当乘以X(一个n * 2矩阵)时,它基本上会聚合两者(h(x) - y)x0和(h(x)-y)x1。根据定义,这两个都是同时完成的。这导致我的新θ的1 * 2矩阵,我只是转换为在矢量周围翻转与θ矢量相同的尺寸。然后,我可以通过alpha和向量减法与theta进行简单的标量乘法。

X = data(:, 1); y = data(:, 2);
m = length(y);
X = [ones(m, 1), data(:,1)]; 
theta = zeros(2, 1);        

iterations = 2000;
alpha = 0.001;

for iter = 1:iterations
     theta = theta -((1/m) * ((X * theta) - y)' * X)' * alpha;
end

答案 1 :(得分:8)

在第一个中,如果X是3x2矩阵且theta是2x1矩阵,那么“假设”将是3x1矩阵。

假设y是一个3x1矩阵,那么你可以执行(假设-y)并获得一个3x1矩阵,然后该3x1的转置就是一个分配给temp的1x3矩阵。

然后将1x3矩阵设置为theta(2),但这不应该是矩阵。

代码的最后两行有效,因为使用上面的mxn示例

(X * theta)

将是一个3x1矩阵。

然后用y(3x1矩阵)减去3x1矩阵,结果为3x1矩阵。

(X * theta) - y

因此,3x1矩阵的转置是1x3矩阵。

((X * theta) - y)'

最后,1x3矩阵乘以3x1矩阵将等于标量或1x1矩阵,这正是您要寻找的。我相信你已经知道,但为了彻底,X(:,2)是3x2矩阵的第二列,使其成为3x1矩阵。

答案 2 :(得分:4)

当您更新时,您需要执行

Start Loop {

temp0 = theta0 - (equation_here);

temp1 = theta1 - (equation_here);


theta0 =  temp0;

theta1 =  temp1;

} End loop

答案 3 :(得分:4)

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
% Performs gradient descent to learn theta. Updates theta by taking num_iters 
% gradient steps with learning rate alpha.

% Number of training examples
m = length(y); 
% Save the cost J in every iteration in order to plot J vs. num_iters and check for convergence 
J_history = zeros(num_iters, 1);

for iter = 1:num_iters
    h = X * theta;
    stderr = h - y;
    theta = theta - (alpha/m) * (stderr' * X)';
    J_history(iter) = computeCost(X, y, theta);
end

end

答案 4 :(得分:2)

可以使用

更简单地对其进行矢量化
h = X * theta   % m-dimensional matrix (prediction our hypothesis gives per training example)
std_err = h - y  % an m-dimensional matrix of errors (one per training example)
theta = theta - (alpha/m) * X' * std_err

记住X是设计矩阵,因此X的每一行代表一个训练示例,X的每一列代表一个给定的组件(例如第零个或第一个组件) ),涵盖所有培训示例。因此,X的每一列正是我们想要与元素std_err相乘的元素,然后求和以获得theta向量的相应分量。

答案 5 :(得分:-1)

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1 : num_iters
    hypothesis = X * theta;
    Error = (hypothesis - y);
    temp = theta - ((alpha / m) * (Error' * X)');
    theta = temp;
    J_history(iter) = computeCost(X, y, theta);
end
end

答案 6 :(得分:-7)

.
.
.
.
.
.
.
.
.
Spoiler alert












m = length(y); % number of training examples
J_history = zeros(num_iters, 1);

for iter = 1:num_iters

% ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
%               theta. 
%
% Hint: While debugging, it can be useful to print out the values
%       of the cost function (computeCost) and gradient here.
% ========================== BEGIN ===========================


t = zeros(2,1);
J = computeCost(X, y, theta);
t = theta - ((alpha*((theta'*X') - y'))*X/m)';
theta = t;
J1 = computeCost(X, y, theta);

if(J1>J),
    break,fprintf('Wrong alpha');
else if(J1==J)
    break;
end;


% ========================== END ==============================

% Save the cost J in every iteration    
J_history(iter) = sum(computeCost(X, y, theta));
end
end
相关问题