Hi all,
I have the following code for one of the assignments on Gradient Descent for Machine Learning, Coursera:
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
data = load('ex1data1.txt'); % read comma separated data
y = data(:, 2);
m = length(y); % number of training examples
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1);
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
delta = zeros(2, 1);
for iter = 1:num_iters
% ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
% theta.
%
% Hint: While debugging, it can be useful to print out the values
% of the cost function (computeCost) and gradient here.
%
for i = 1:m
Xi = X(i,:);
hi = Xi*theta;
delta = delta + (hi-y(i))*(Xi');
end
delta = delta/m;
theta = theta - alpha*delta;
delta = 0;
% ============================================================
% Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta);
end
end
It gives me the following error code:
>> gradientDescent()
Not enough input arguments.
Error in gradientDescent (line 13)
J_history = zeros(num_iters, 1);
I cannot find an answer to this. Any idea how to fix it?

 採用された回答

Matt J
Matt J 2021 年 3 月 29 日

1 投票

Call your function with all 5 input arguments.

6 件のコメント

Nektarios Liaskos
Nektarios Liaskos 2021 年 3 月 29 日
Could you kindly elaborate on this?
I am fairly new to this and struggle to apply what you are recommending.
As far as I can tell num_inter has not been defined (the error lies in the pre-written code that was given with the assignment and is not part of the code I have to write for this task, which is why I am sruggling so much).
Thank you! :)
Matt J
Matt J 2021 年 3 月 29 日
編集済み: Matt J 2021 年 3 月 29 日
num_iters is the fifth input argument to your function.
[theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
It is therefore your job to provide this value as input when you call the function.
Nektarios Liaskos
Nektarios Liaskos 2021 年 3 月 29 日
Oh of course, I sorted it now using the accessory files. Thank you!
Matt J
Matt J 2021 年 3 月 29 日
You're quite welcome, but please Accept-click the answer to indicated that the question has been resolved.
Samuel Valentin Lopez Valenzuela
Samuel Valentin Lopez Valenzuela 2021 年 8 月 6 日
i have the same problem , how did you use accessory files to fix it?
Aniket Anand
Aniket Anand 2021 年 9 月 6 日
Can you please help me out
I don't know how to use accessory files

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDeep Learning Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by