我正在从Cousera进行ML,它为线性回归分配产生了一个错误,我不明白这是什么问题。我在Windows 10上使用octave 4.0。代码如下:
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y); % number of training examples
hypo = [m,1];
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.
%
hypo = theta(1) + theta(2) * X;
temp1 = theta(1) - alpha * (1 / m) * sum(hypo);
hypo = theta(1) + theta(2) * X;
temp2 = theta(2) - alpha * (1 / m) * sum((hypo .* X));
theta = [temp1; temp2];
% ===========================================================
% Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta);
end
predict1 = [1, 3.5] * theta;
predict2 = [1, 7.0] * theta;
end
它会在以下位置产生循环错误“A(I)= X:X与I相同的大小”
J_history(iters) = computeCost(X, y, theta);
最佳答案
不鼓励讨论类(class)练习的代码,因此我不会为您解决此问题。我只给你一个指针。computeCost
期望theta
的大小为2,1,而您作为参数发布的theta
的大小为2,2。使用命令keyboard
在特定行上中断/暂停执行,并进入控制台 session 进行调试。检查您发布的变量的维数。您可以使用dbcont
恢复执行。
关于for-loop - Octave 循环误差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39298261/