Theta computed from Gradient Descent in Linear Regression with one model produces NaN

8 次查看(过去 30 天)
I am running a simple linear regression model with one variable, trying to compute the unit cost of a unit based on the sizes available.The theta value produced from gradient descent function is NaN so I cannot plot the linear regression line. Could someone please help me fix this problem?
data = load('data.txt');
X = data(:,1);
y = data (:, 2);
plotData(X,y);
m = length(X);
X = [ones(m,1), data(:,1)];
theta = zeros(2,1);
J = computeCost(X,y,theta);
fprintf('The cost function J = \n%f \n', J); %need revision
iterations = 1500;
alpha = 0.1;
theta = gradientDescent(X, y, theta, alpha, iterations);
fprintf('Theta computed from gradient descent:\n%f, \n%f', theta(1), theta(2));
  2 个评论
Saurav Shrestha
Saurav Shrestha 2020-8-6
Here is my gradientDescent function
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y);
J_history = zeros(num_iters, 1);
for iter = 1 : num_iters
prediction = X * theta;
error = (prediction - y);
theta = theta - ((alpha/m) * ((error)'*X)');
J_history (iter) = computeCost(X, y, theta);
end
end
Safoura Tanbakouei
Safoura Tanbakouei 2022-1-12
编辑:Safoura Tanbakouei 2022-1-12
@Saurav Shrestha I am doing the same exercise. I do not receive an error but the theta computed from the Gradient Descent is 0 and the plot I get is this figure attached.
This is my code:
temp0 = theta(1,1) - (alpha/m)*sum((X*theta-y));
temp1 = theta(2,1) - (alpha/m)*sum((X*theta-y).*X(:,2));
theta(1,1) = temp0;
theta(2,1) = temp1;

请先登录,再进行评论。

回答(1 个)

Raunak Gupta
Raunak Gupta 2020-8-14
Hi,
From the gradientDescent function mentioned in the comments I can see that theta is starting from zero column vector. So, for the very first iteration the prediction vector will also be zero. This makes a big change to the theta value in next iteration. Also, I don’t think the update equation of theta is written such that it will converge. So, I would suggest changing the starting values of theta vector and revisiting the updating equation of theta in gradient descent. I don’t think that computeCost is affecting the theta value.

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by