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 8618103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:01:32+00:00 2026-06-12T06:01:32+00:00

I am trying to run gradient descent and cannot get the same result as

  • 0

I am trying to run gradient descent and cannot get the same result as octaves built-in fminunc, when using exactly the same data

My Code is

%for 5000 iterations
for iter = 1:5000

%%Calculate the cost and the new gradient
[cost, grad] = costFunction(initial_theta, X, y);


%%Gradient = Old Gradient - (Learning Rate * New Gradient)
initial_theta = initial_theta - (alpha * grad);

end 

Where costFunction calucates the cost and gradient, when given an example (X,y) and parameters(theta).

a built-in octave function fminunc also calling costFunction and with the same data finds a much much better answer in far fewer iterations.

Given that octave uses the same cost function i assume the costFunction is correct.

I have tried decreasing the learning rate in case i am hitting a local minima and increasing the number of iterations, the cost stops decreasing so i think it seems that it has found the minimum, but the final theta still has a much larger cost and is no where near as accurate

even if fminunc is using a better alogoritm hould gradient descent eventually find the same answer with enough iterations and a smaller learning rate?

or can anyone see if i am doing anything wrong?

Thank you for any and all help.

  • 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-12T06:01:33+00:00Added an answer on June 12, 2026 at 6:01 am

    Your comments are wrong, but the algorithm is good.

    In gradient descent it’s easy to fall into numerical problems, then I suggest to perform feature normalization.

    Also, if you’re unsure about your learning rate, try to adjust it dynamically. Something like:

    best_cost = Inf;
    best_theta = initial_theta;
    alpha = 1;
    
    for iter = 1:500
      [cost, grad] = costFunction(best_theta, X_reg, y);
    
      if (cost < best_cost)
        best_theta = best_theta - alpha * grad;
        best_cost = cost;
      else
        alpha = alpha * 0.99
      end
    end
    

    Moreover remember that different answers can give the same decision boundaries. For example for hypothesis h(x) = x(0) + theta(1) * x(1) + theta(2) * x(2) these answers give the same boundary:

    theta = [5, 10, 10];
    theta = [10, 20, 20];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When trying to run a junit test I get the following error - java.lang.ClassCastException:
When trying to run an Eclipse Dynamic Web Project under a Tomcat setup using
Trying to run a C++ program , I get segmentation faults which appear to
Im trying to run this code . Basically I want the https://admin:qwerty123@ '$dmp':7777/set_param?'$params command
When trying to run my project I get this error and I don´t know
I'm learning about Qt Model/View with Ruby and I'm trying run the following code
Trying to run a very simple flot.js example, I have the following code to
When trying run this code: At first it prints Process some_id BEFORE enter for
Trying to run a java test using bluecove, I got: Native Library bluecove_x64 not
Trying to run scrapy crawl command using crontab under virtual environment, and getting below

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.