One of the parameters is converging faster than the other with Gradient Descent

My introduction to univariate linear regression using gradient descent was a hands-on experience in JavaScript.

const LEARNING_RATE = 0.000001;

let m = 0;
let b = 0;

const hypothesis = x => m * x + b;

const learn = (alpha) => {
  if (x.length <= 0) return;

  let sum1 = 0;
  let sum2 = 0;

  for (var i = 0; i < x.length; i++) {
    sum1 += hypothesis(x[i]) - y[i];
    sum2 += (hypothesis(x[i]) - y[i]) * x[i];
  }

  b = b - alpha * sum1 / (x.length);
  m = m - alpha * sum2 / (x.length);
}

// continuing the learning process until convergence is achieved with learn(LEARNING_RATE);

The adjustment of the slope for m in the hypothesis function was rapid, however, the intersection at the y-axis seemed to be resistant to change. I resorted to utilizing a distinct learning rate for b to address this issue.

const learn = (alpha) => {
  if (x.length <= 0) return;

  let sum1 = 0;
  let sum2 = 0;

  for (var i = 0; i < x.length; i++) {
    sum1 += hypothesis(x[i]) - y[i];
    sum2 += (hypothesis(x[i]) - y[i]) * x[i];
  }

  b = b - 100000 * alpha * sum1 / (x.length);
  m = m - alpha * sum2 / (x.length);
}

I am looking for guidance on what might be going wrong with the algorithm. The code can be accessed via a GitHub repository and further information can be found in this article.

Answer №1

It is important to focus on the speed of convergence to the global minimum rather than the speed of changes in bias when analyzing a model. While there may be no errors in the model itself (unless the 2/N coefficient is forgotten, but this only affects m and b coefficients), it is crucial to consider how updates are made based on prediction errors in gradient descent.

When the bias has a small error, the updates will reflect small changes, which is expected behavior for the model. For more information and examples, check out this resource.

However, making custom changes to the learning rate can result in abnormal behavior and issues with reaching the global minimum. It is advisable to take this course for a better understanding of gradient descent optimization.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Generating personalized MongoDB collections for individual users - A step-by-step guide

My query is more about the procedure rather than a specific coding issue. I am working on a node application and using DHTMLX calendar. What I aim for is to have each user with their own set of events on their individual calendar. Currently, the implement ...

Retrieve data using the designated key and convert it into JSON format

If I have the following JSON array: [ {"data": [ {"W":1,"A1":"123"}, {"W":1,"A1":"456"}, {"W":2,"A1":"4578"}, {"W":2,"A1":"2423"}, {"W":2,"A1":"2432"}, {"W":2,"A1":"24324" ...

What is the solution for resolving the problem of the cursor jumping to the end when converting numbers in JavaScript?

After exploring the inquiries regarding converting digits in JavaScript, such as What's the solution and the right way to convert digits in JavaScript? and How to convert numbers in JavaScript, and problems with commands to remove non-numeric characte ...

Why do I keep receiving the error message "Cannot set property of undefined"?

In the midst of a small project that involves utilizing nuxt, js, and axios, I encountered an issue when attempting to assign the response data to my formFields object. Despite having declared formFields within the data section, I kept receiving an error m ...

How can we effectively implement conditional rendering when dealing with components that are nearly identical?

Depending on whether the user is a professor, student, or not logged in, I render different landing pages. The landing pages are quite similar, with the only distinction being the buttons displayed. While I could easily achieve this using inline conditions ...

The window.addEventListener function is failing to work properly on mobile devices

Hey there! I am facing an issue in my JS code. I wrote this code because I want the menu to close when a visitor clicks on another div (not the menu) if it is open. The code seems to be working fine in developer tools on Chrome or Firefox, but it's no ...

The onclick functionality is not functioning properly within email communications

My JavaScript code contains an AJAX call within Datatables, and this snippet of code is causing an issue: { "data": null, "width": "10%", "render": function(data){ icon2 = '<center><button type="button" class="btn btn-info ...

Utilizing ng-repeat $index for locating an element within an array

Within my $scope, there is a property called $scope.cars, which is an array of cars. Users have the ability to delete a car from this array. When calling the delete function deleteThis, I pass the $index parameter created by ng-repeat. However, in the Ja ...

What could be causing my state not to change in Nextjs even though I followed the quick start guide for Easy Peasy?

I recently encountered an issue while trying to implement easy peasy for global state management in my nextjs app. The problem I faced was that the state would only update when I changed pages, which seemed odd. To better understand what was going on, I de ...

The JSX snippet accurately displays the expected value on some pages, but displays an incorrect value on other pages

{_id === friendId || <IconButton onClick={() => patchFriend() } sx={{ backgroundColor: primaryLight, p: "0.6rem" }} > {isFriend ? ( <PersonRemoveOutlined sx={{ color: primaryDark }} /> ...

Exploring a Discord.js collection: tips for accessing and manipulating objects within an array in the collection

I have a discord.js Collection that contains information about dispatcher and queue objects. Here is the structure: Collection(1) [Map] { '403547647215927306' => { dispatcher: StreamDispatcher { _writableState: [WritableState], ...

Who needs a proper naming convention when things are working just fine? What's the point of conventions if they don't improve functionality?

I am a newcomer to the world of JavaScript programming and stumbled upon this example while practicing. <html> <head> <script type="text/javascript"> function changeTabIndex() { document.getElementById('1').tabIndex="3" d ...

What is the best way to add or delete data when specific radio buttons are chosen?

Hey there, I'm facing an issue where the data is being appended regardless of which radio button is selected. Can someone help me with a solution on how to properly add and remove data based on the selected radio button? $( document ).ready(functio ...

While validating in my Angular application, I encountered an error stating that no index signature with a parameter of type 'string' was found on type 'AbstractControl[]'

While trying to validate my Angular application, I encountered the following error: src/app/register/register.component.ts:45:39 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used ...

Fetch a single random profile image from a Facebook user's list of friends using Javascript

I'm currently facing an issue with displaying a random profile picture of the user's friends on Facebook. I attempted to troubleshoot it myself but ended up crashing the login button and now it's not functioning properly. Can someone please ...

An unexpected error occurred: [$injector:modulerr] (unidentified)

My website is running smoothly with AngularJS on one page, but I'm encountering an error in the console on other pages that do not use AngularJS. The error message reads: Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.13/$in ...

Are there any other options besides using the React Material-UI makeStyles() function for styling class Components?

While experimenting with the makeStyles() function in Material-UI's React library, I encountered a specific error message: The use of hooks is limited to the body of a function component. Below is a snippet of the code that triggered this error: ...

implementing one active line item at a time in Vue

Within my Vue template, I have a small unordered list: <ul style="border-bottom:none !important; text-decoration:none"> <li class="commentToggle" v-bind:class="{active:commentActive}" v-on:click="setInputName('new')">New Comment ...

Is it possible to share a MySQL connection for cross-module usage in Node/Express by utilizing promise-mysql?

Currently, I am trying to import and utilize a database module within one of my controllers. Despite successfully establishing the initial connection, I am encountering an error when accessing any of my routes through the browser: "Cannot read property ...

What's Next? Redirecting Pages in Node.js Express after Handling POST Requests

Is it possible to redirect to a different page from a post request? module.exports = function(app) { app.post('/createStation', function(request, response){ response.redirect('/'); //I'm having trouble getting ...