梯度下降倍频程代码

时间:2018-07-12 14:08:15

标签: machine-learning octave gradient-descent

需要帮助来完成此功能。尝试找出derJ时出错:

error: X(0,_): subscripts must be either integers 1 to (2^63)-1 or logicals

我的代码:

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
        predictions = X * theta;   % hypothesis

        % derivative term for cost function
        derJ = (1 / m) * sum ( (predictions - y) * X(iter-1, 2) );

        % updating theta values
        theta           = theta - (alpha * derJ);
        J_history(iter) = computeCost (X, y, theta);
    end
end

1 个答案:

答案 0 :(得分:0)

您的代码状态为X(iter - 1, 2),但是在您的for循环中,iter1开始。

因此,在第一次迭代中,X(iter - 1, 2)的计算结果为X(0,2),而0在matlab中不是有效的索引。