필터 지우기
필터 지우기

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

조회 수: 16 (최근 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