多个变量的梯度下降失败,导致 NaN
Gradient Descent failing for multiple variables, results in NaN
我正在尝试实施梯度下降算法来最小化多重线性算法的成本函数。我正在使用 Andrew Ng 在机器学习 class 中解释的概念。我正在使用八度。但是,当我尝试执行代码时,似乎无法提供解决方案,因为我的 theta 值计算为 "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
我在 GNU Octave 中实现了这个算法,我把它分成了 2 个不同的函数,首先你需要定义一个梯度函数
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_iters
alpha=0.00037
num_iters=3000000
本次实验运行梯度下降的输出结果如下:
theta =
-170.10392 -0.40601 4.99799
所以等式是
predicted weight = -170.10392 - .406*age + 4.997*height
这几乎是梯度的绝对最小值,因为真实结果为
这个问题如果使用 PSPP(SPSS 的开源替代品)是
predicted weight = -175.17 - .40*age + 5.07*height
希望这有助于确认梯度下降算法对多元线性回归和标准线性回归同样有效
我确实发现了这个错误,它既不在成本函数的逻辑中,也不在梯度下降函数的逻辑中。但确实在特征规范化逻辑中,我不小心返回了错误的变量,因此导致输出为 "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 insated of X,这就是导致代码给出错误输出的原因
我正在尝试实施梯度下降算法来最小化多重线性算法的成本函数。我正在使用 Andrew Ng 在机器学习 class 中解释的概念。我正在使用八度。但是,当我尝试执行代码时,似乎无法提供解决方案,因为我的 theta 值计算为 "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
我在 GNU Octave 中实现了这个算法,我把它分成了 2 个不同的函数,首先你需要定义一个梯度函数
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_iters
alpha=0.00037
num_iters=3000000
本次实验运行梯度下降的输出结果如下:
theta =
-170.10392 -0.40601 4.99799
所以等式是
predicted weight = -170.10392 - .406*age + 4.997*height
这几乎是梯度的绝对最小值,因为真实结果为 这个问题如果使用 PSPP(SPSS 的开源替代品)是
predicted weight = -175.17 - .40*age + 5.07*height
希望这有助于确认梯度下降算法对多元线性回归和标准线性回归同样有效
我确实发现了这个错误,它既不在成本函数的逻辑中,也不在梯度下降函数的逻辑中。但确实在特征规范化逻辑中,我不小心返回了错误的变量,因此导致输出为 "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 insated of X,这就是导致代码给出错误输出的原因