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/