Machine learning 多变量梯度下降失败,导致NaN

Machine learning 多变量梯度下降失败,导致NaN,machine-learning,octave,linear-regression,gradient-descent,Machine Learning,Octave,Linear Regression,Gradient Descent,我正在尝试实现梯度下降算法来最小化多重线性算法的代价函数。我使用Andrew Ng在机器学习类中解释的概念。我用的是八度音阶。然而,当我尝试执行代码时,它似乎无法提供解决方案,因为我的θ值计算为“NaN”。我已经附上了成本函数代码和梯度下降代码。有人能帮忙吗 成本函数: function J = computeCostMulti(X, y, theta) m = length(y); % number of training examples J = 0; h=(X*theta); s=

我正在尝试实现梯度下降算法来最小化多重线性算法的代价函数。我使用Andrew Ng在机器学习类中解释的概念。我用的是八度音阶。然而,当我尝试执行代码时,它似乎无法提供解决方案,因为我的θ值计算为“NaN”。我已经附上了成本函数代码和梯度下降代码。有人能帮忙吗

成本函数:

function J = computeCostMulti(X, y, theta)

m = length(y); % number of training examples

J = 0;

h=(X*theta);
s= sum((h-y).^2);
J= s/(2*m);
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)

m = length(y); % number of training examples
J_history = zeros(num_iters, 1);

for iter = 1:num_iters

  a= X*theta -y;
  b = alpha*(X'*a);
  theta = theta - (b/m);

  J_history(iter) = computeCostMulti(X, y, theta);  
end
梯度下降代码:

function J = computeCostMulti(X, y, theta)

m = length(y); % number of training examples

J = 0;

h=(X*theta);
s= sum((h-y).^2);
J= s/(2*m);
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)

m = length(y); % number of training examples
J_history = zeros(num_iters, 1);

for iter = 1:num_iters

  a= X*theta -y;
  b = alpha*(X'*a);
  theta = theta - (b/m);

  J_history(iter) = computeCostMulti(X, y, theta);  
end

我在GNU倍频程中实现了这个算法,并将其分为两个不同的函数,首先需要定义一个梯度函数

function [thetaNew] = compute_gradient (X, y, theta, m)
    thetaNew = (X'*(X*theta'-y))*1/m;
end
然后使用不同的函数来计算梯度下降算法

function [theta] = gd (X, y, alpha, num_iters)
    theta = zeros(1,columns(X));
    for iter = 1:num_iters,
        theta = theta - alpha*compute_gradient(X,y,theta,rows(y))';                
    end
end
编辑1 该算法适用于多重线性回归(多重自变量)和1个自变量的线性回归,我用这个数据集对此进行了测试

age height  weight
41  62  115
21  62  140
31  62  125
21  64  125
31  64  145
41  64  135
41  72  165
31  72  190
21  72  175
31  66  150
31  66  155
21  64  140
对于这个例子,我们想要预测

predicted weight = theta0 + theta1*age + theta2*height
我将这些输入值用于alpha和num_iter

alpha=0.00037
num_iters=3000000
本实验运行梯度下降的输出如下:

theta =
-170.10392    -0.40601     4.99799
所以方程是

predicted weight = -170.10392 - .406*age + 4.997*height
这几乎是梯度的绝对最小值,因为 如果使用PSPP(SPSS的开源替代品),则会出现此问题


希望这有助于确认梯度下降算法对多元线性回归和标准线性回归同样有效

我确实发现了错误,它既不在成本函数的逻辑中,也不在梯度下降函数中。但事实上,在特征规范化逻辑中,我意外地返回了错误的变量,因此它将输出视为“NaN”

这是一个愚蠢的错误:

我以前在做什么

mu= mean(a);
sigma = std(a);
b=(X.-mu);
X= b./sigma;
而不是我应该做的

function [X_norm, mu, sigma] = featureNormalize(X)
%FEATURENORMALIZE Normalizes the features in X 
%   FEATURENORMALIZE(X) returns a normalized version of X where
%   the mean value of each feature is 0 and the standard deviation
%   is 1. This is often a good preprocessing step to do when
%   working with learning algorithms.

% You need to set these values correctly
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));

% ====================== YOUR CODE HERE ======================


mu= mean(X);
sigma = std(X);
a=(X.-mu);
X_norm= a./sigma;

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

end

很明显,我应该使用X_norm来表示X,这就是为什么代码会给出错误的输出

非常感谢您的响应和快速帮助。我在这里没有完全跟着你。我对一个单变量线性回归使用了同样的逻辑,它起作用了。你能详细解释一下吗?谢谢你再次详细解释。然而,我正在做一个作业,我想我们需要在一个函数中添加代码。另外,你添加了θ零向量,我仍然感到困惑,可能是因为我缺乏知识。不知道为什么我不能在一个函数内完成它though@Gaurav:您可以将
gradient
的代码放入另一个函数中,如果你需要把它作为一个单一的函数。克里斯,我仍然很困惑。你能帮我解释一下我的逻辑有什么问题,为什么它需要定义一个额外的函数吗?因为我看到我正在执行与@Jose类似的逻辑。伙计们,非常感谢你们的帮助,我发现了我在功能规范化逻辑方面的错误。我能够成功运行代码并获得所有值。干杯:)所以我以上的逻辑是正确的,我一定会按照你的建议去做