for-loop - Octave 循环误差

标签 for-loop syntax-error octave

我正在从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/

相关文章:

.net - 这个 CIL 代码是做什么的?为什么需要第3步?

Python 无法更新 JSON 字符串的坐标

matlab - Octave 绘图点作为动画

syntax-error - 用5.5编写的PHP函数在升级到7.0时抛出错误

matlab - 如何在 MATLAB 中针对一列对二维数组进行排序?

matlab - 为什么/什么时候我应该更喜欢 MATLAB 而不是 Octave?

c++ - 将显示数组的 5 个 for 循环压缩为 1?

r - 使用一次分割回归而不是循环从数百个回归中提取系数?

java - 如何在文件阅读器中使用 `for`?

vba - 在 VBA 中调用子程序