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

8 Ansichten (letzte 30 Tage)
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 Kommentare
Saurav Shrestha
Saurav Shrestha am 6 Aug. 2020
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 am 12 Jan. 2022
Bearbeitet: Safoura Tanbakouei am 12 Jan. 2022
@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;

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Raunak Gupta
Raunak Gupta am 14 Aug. 2020
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.

Kategorien

Mehr zu Linear and Nonlinear Regression finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by