Octave 下标索引必须是实正整数或逻辑数
我一直在犯这个错误Octave 下标索引必须是实正整数或逻辑数,octave,gradient-descent,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
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
我对八度音阶很陌生,所以请对我放松点,还有
先谢谢你。
这是第2周机器学习课程的内容,99%确定您的错误在topsig指出的线上,您有
alpha(1/m)
如果你给函数一个输入值的例子,以及你希望看到的输出,这会有所帮助,但我是从你的评论中假设的
%以学习率alpha采取数量梯度步骤
α是一个常数,而不是一个函数。因此,您的行alpha(1/m)
之间没有任何运算符。octave在索引值为1/m
的alpha
时看到了这一点
i、 例如,如果你有一个数组
theta_0 = theta(1) - alpha(1/m)*sum((hypothesis-y)*x);
您所做的似乎没有意义,因为'm=length(y)'将输出标量,所以
x = [3 4 5]
x*(2) = [6 8 10] %% two times each element in the array
x(2) = [4] %% second element in the array
请注意,对于某些错误,它始终指示错误的位置在赋值运算符处(行首的等号)。如果它指向那里,您通常必须在该行的其他位置查找实际错误。这里,它对你大喊大叫,因为你试图应用一个非整数下标
(1/m)
Iter-1从1-1=0开始,但倍频程是基于1的(数组从1开始),即使我这样做了,它仍然会给出一个错误。我不认为你需要在theta(Iter-1)
和X(Iter,2)
?上一次迭代已经存储在theta
中,您在每次迭代中都要更改theta
?我更改了代码,因为老实说,我不知道上次在做什么。这一次,我仍然在同一个地方得到相同的错误alpha(1/m)
或者可能alpha*(1/m)
?是的,我知道发生了什么,我希望它只使用括号而不是乘法符号进行乘法
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