Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7927541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:21:24+00:00 2026-06-03T19:21:24+00:00

I implemented a gradient descent algorithm to minimize a cost function in order to

  • 0

I implemented a gradient descent algorithm to minimize a cost function in order to gain a hypothesis for determining whether an image has a good quality. I did that in Octave. The idea is somehow based on the algorithm from the machine learning class by Andrew Ng

Therefore I have 880 values “y” that contains values from 0.5 to ~12. And I have 880 values from 50 to 300 in “X” that should predict the image’s quality.

Sadly the algorithm seems to fail, after some iterations the value for theta is so small, that theta0 and theta1 become “NaN”. And my linear regression curve has strange values…

here is the code for the gradient descent algorithm:
(theta = zeros(2, 1);, alpha= 0.01, iterations=1500)

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


    tmp_j1=0;
for i=1:m, 
    tmp_j1 = tmp_j1+ ((theta (1,1) + theta (2,1)*X(i,2)) - y(i));
end

    tmp_j2=0;
for i=1:m, 
    tmp_j2 = tmp_j2+ (((theta (1,1) + theta (2,1)*X(i,2)) - y(i)) *X(i,2)); 
end

    tmp1= theta(1,1) - (alpha *  ((1/m) * tmp_j1))  
    tmp2= theta(2,1) - (alpha *  ((1/m) * tmp_j2))  

    theta(1,1)=tmp1
    theta(2,1)=tmp2

    % ============================================================

    % Save the cost J in every iteration    
    J_history(iter) = computeCost(X, y, theta);
end
end

And here is the computation for the costfunction:

function J = computeCost(X, y, theta)   %

m = length(y); % number of training examples
J = 0;
tmp=0;
for i=1:m, 
    tmp = tmp+ (theta (1,1) + theta (2,1)*X(i,2) - y(i))^2; %differenzberechnung
end
J= (1/(2*m)) * tmp
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:21:26+00:00Added an answer on June 3, 2026 at 7:21 pm

    I think that your computeCost function is wrong.
    I attended NG’s class last year and I have the following implementation (vectorized):

    m = length(y);
    J = 0;
    predictions = X * theta;
    sqrErrors = (predictions-y).^2;
    
    J = 1/(2*m) * sum(sqrErrors);
    

    The rest of the implementation seems fine to me, although you could also vectorize them.

    theta_1 = theta(1) - alpha * (1/m) * sum((X*theta-y).*X(:,1));
    theta_2 = theta(2) - alpha * (1/m) * sum((X*theta-y).*X(:,2));
    

    Afterwards you are setting the temporary thetas (here called theta_1 and theta_2) correctly back to the “real” theta.

    Generally it is more useful to vectorize instead of loops, it is less annoying to read and to debug.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement Stochastic gradient descent in MATLAB. I followed the algorithm exactly
Already implemented performance boosters : - Get compatible image of GraphicsConfiguration to draw on
I'm studying simple machine learning algorithms, beginning with a simple gradient descent, but I've
I have just implemented these ToggleButtons using ControlTemplates that set the Image content according
I need to put an alpha blended gradient border around an image. My problem
I'm looking for some advice on how to go about implementing Gradient (steepest) Descent
How can I implement cross browser opacity gradient (not color gradient)? See following code:
Implemented a generic repository with several Methods. One of those is this: public IEnumerable<T>
I implemented a select from-to pair of lists on a page and added add/remove
I implemented a drop down navigation menus in my blog, but it's not working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.