octave - 下标索引必须是实数正整数或逻辑数

标签 octave gradient-descent

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

% Initialize some useful values  
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.
%

    hypothesis = x*theta;
    theta_0 = theta(1) - alpha(1/m)*sum((hypothesis-y)*x);
    theta_1 = theta(2) - alpha(1/m)*sum((hypothesis-y)*x);
    theta(1) = theta_0;
    theta(2) = theta_1;








% ============================================================

% Save the cost J in every iteration    
    J_history(iter) = computeCost(X, y, theta);

end

end

我不断收到此错误
error: gradientDescent: subscript indices must be either positive integers less than 2^31 or logicals

在这条线上,就在第一个 theta 和 = 之间
theta_0 = theta(1) - alpha(1/m)*sum((hypothesis-y)*x);

我对 Octave 非常陌生,所以请放轻松,并且
先感谢您。
这是来自第 2 周机器学习类(class)的类(class)

最佳答案

99% 确定你的错误在 topsig 指出的那一行,你有 alpha(1/m)
如果您为您的函数提供输入值示例以及您希望作为输出看到的内容,这将有所帮助,但我从您的评论中假设
% taking num_iters gradient steps with learning rate alpha
那个alpha是常数,不是函数。因此,您的线路是 alpha(1/m)中间没有任何运算符。当您编制索引时, Octave 会看到这一点 alpha值为 1/m .

即,如果你有一个数组

x = [3 4 5]
x*(2) = [6 8 10]  %% two times each element in the array
x(2) = [4]  %% second element in the array

你所做的似乎没有意义,因为 'm = length(y)' 将输出一个标量,所以
x = [3 4 5]; m = 3;
x*(1/m) = x*(1/3) = [1 1.3333 1.6666]  %% element / 3
x(1/m) = ___error___  %% the 1/3 element in the array makes no sense

请注意,对于某些错误,它始终表示错误的位置在赋值运算符处(行首的等号)。如果它指向那里,您通常必须在行中的其他地方查找实际错误。在这里,它因为您试图应用非整数下标而对您大喊大叫 (1/m)

关于octave - 下标索引必须是实数正整数或逻辑数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31711770/

相关文章:

octave - 如何将两行或两列相乘?

octave - Octave代码中 "Sections"的用途是什么?

matlab - 为什么 && 运算符在 Matlab/Octave 中似乎不起作用?

python - 梯度下降多元线性回归

machine-learning - 训练过程中出现Nans的常见原因

octave - 在具有透明背景的 Octave 中保存绘图

matlab - Octave /Matlab错误

python - Python 对数下降曲线上的梯度下降

python - 具有两个特征的梯度下降计算特征空间

machine-learning - 梯度下降似乎失败了