How to fix gradient descent code?

22 Ansichten (letzte 30 Tage)
Jackwhale
Jackwhale am 30 Mär. 2016
Kommentiert: sivarao K am 10 Nov. 2021
I am a novice trying to do a gradient descent with one variable, but cannot figure out how to fix my code (below). Not sure if my for-part is correct. This is the error message: "In an assignment A(:) = B, the number of elements in A and B must be the same." Please help?
data = load('data.txt' );
X = data(:, 1); y = data(:, 2);
m = length(y);
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1); % initialize fitting parameters
num_iters = 1500;
alpha = 0.01;
J = computeCost(X, y, theta)
m = length(y);
J = sum(( X * theta - y ) .^2 )/( 2 * m );
[theta J_history] = gradientDescent(X, y, theta, alpha, num_iters)
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
h=(theta(1)+ theta(2)*X)';
theta(1) = theta(1) - alpha * (1/m) * h * X(:, 1);
theta(2) = theta(2) - alpha * (1/m) * h * X(:, 2);
% Save the cost J in every iteration
J_history(num_iters) = computeCost(X, y, theta);
end
  2 Kommentare
Walter Roberson
Walter Roberson am 30 Mär. 2016
Please show the complete error message, everything in red.
Jackwhale
Jackwhale am 30 Mär. 2016
This is the complete error message: "In an assignment A(:) = B, the number of elements in A and B must be the same."

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Torsten
Torsten am 30 Mär. 2016
theta(1) - alpha * (1/m) * h * X(:, 1)
and
theta(2) - alpha * (1/m) * h * X(:, 2)
are 2x1 vectors which are assigned to scalars in the lines
theta(1) = theta(1) - alpha * (1/m) * h * X(:, 1);
theta(2) = theta(2) - alpha * (1/m) * h * X(:, 2);
This is not possible.
Best wishes
Torsten.
  3 Kommentare
Torsten
Torsten am 30 Mär. 2016
I must admit that I don't understand what your code does.
To answer your question, you had to include comments and explain in more detail the underlying problem and the algorithm to solve it.
Best wishes
Torsten.
Jackwhale
Jackwhale am 30 Mär. 2016
To clarify the goal, the objective is to predict the profitability of a food delivery truck when expanding to a new city, based on the city population size. The fi rst data column is the population, the second column is the profi t of a food truck in that city. The chosen approach is the batch gradient descent algorithm, changing the parameters to come closer to the optimal values that will minimise the cost function J(). The idea however is to monitor J(), so as to check the convergence of the gradient descent implementation. The loop structure has been provided, I only need to supply the updates to theta within each iteration - to minimise J(). I have implemented computeCost correctly, but am struggling with implementing the gradient descent correctly.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Torsten
Torsten am 30 Mär. 2016
Bearbeitet: Torsten am 30 Mär. 2016
I don't know why you use such a complicated approach.
Just execute
data = load('data.txt' );
A = [ones(length(data(:,1)),1), data(:,1)];
b = data(:,2);
theta = A \ b
to get your optimum theta values.
Best wishes
Torsten.
  14 Kommentare
Torsten
Torsten am 31 Mär. 2016
You seem to have a strange MATLAB version.
If I set
num_iters=1001,
I get
theta =
5.2147549
- 0.5733459
J_history(1001)
ans =
0.8554026
thus the results expected.
Best wishes
Torsten.
Torsten
Torsten am 31 Mär. 2016
I only need to supply the updates to theta within each iteration.
If you can't read from the code I supplied how theta is updated every iteration, then you should really start with MATLAB principles.

Melden Sie sich an, um zu kommentieren.


Agbakoba Chukwunoso
Agbakoba Chukwunoso am 6 Dez. 2020
Pls help me out.. I'm trying to find gradientdescent with this code but when I run it, it returns gradientdescents to me not the value . data = load('ex1data1.txt'); % text file conatins 2 values in each row separated by commas X = [ones(m, 1), data(:,1)]; theta = zeros(2, 1); iterations = 1500; alpha = 0.01; function [theta, J_history] = gradientdescent(X, y, theta, alpha, num_iters) m = length(y); % number of training examples J_history = zeros(num_iters, 1); for iter = 1:num_iters k=1:m; j1=(1/m)*sum((theta(1)+theta(2).*X(k,2))-y(k)) j2=(1/m)*sum(((theta(1)+theta(2).*X(k,2))-y(k)).*X(k,2)) theta(1)=theta(1)-alpha*(j1); theta(2)=theta(2)-alpha*(j2); end end
  2 Kommentare
Agbakoba Chukwunoso
Agbakoba Chukwunoso am 6 Dez. 2020
data = load('ex1data1.txt');
% text file conatins 2 values in each row separated by commas
X = [ones(m, 1), data(:,1)];
theta = zeros(2, 1);
iterations = 1500;
alpha = 0.01;
function [theta, J_history] = gradientdescent(X, y, theta, alpha, num_iters)
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
k=1:m;
j1=(1/m)*sum((theta(1)+theta(2).*X(k,2))-y(k))
j2=(1/m)*sum(((theta(1)+theta(2).*X(k,2))-y(k)).*X(k,2))
theta(1)=theta(1)-alpha*(j1);
theta(2)=theta(2)-alpha*(j2);
end
end
sivarao K
sivarao K am 10 Nov. 2021
here 'y' not defined but it excuting how?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Mathematics 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