Gradient Descent Calculator with Learning Rate

Gradient Descent Convergence Calculator

The initial position on the horizontal axis.
Controls the size of the steps taken toward the minimum.
How many steps of optimization to perform.
f(x) = x² (Simple Convex Function)
f'(x) = 2x (Gradient for calculation)

Optimization Results

Step Current x Gradient (2x) New x

Understanding Gradient Descent and Learning Rates

Gradient Descent is a first-order iterative optimization algorithm used to find a local minimum of a differentiable function. In the world of Machine Learning and Deep Learning, it is the fundamental engine used to train models by minimizing the error (cost) between predictions and actual data.

The Mathematical Logic

The core logic of Gradient Descent is captured in the update rule:

xnew = xold – α * f'(xold)
  • xold: Your current position (parameter value).
  • α (Learning Rate): A hyperparameter that determines the step size. If α is too high, the algorithm may overshoot the minimum. If α is too low, convergence will be slow.
  • f'(xold): The derivative (gradient) of the function at the current point, which indicates the direction of steepest ascent.

How the Learning Rate Affects Convergence

Choosing the right learning rate is critical for model performance:

  • Small Learning Rate (e.g., 0.001): Ensures reliable convergence but takes many iterations to reach the global minimum.
  • Large Learning Rate (e.g., 0.9): Risks "bouncing" back and forth across the valley or even diverging (getting further away from the minimum).
  • Optimal Learning Rate: Efficiently reaches the minimum without significant oscillation.

Practical Example

Imagine we want to minimize f(x) = x². The minimum is clearly at x = 0. If we start at x = 10 with a learning rate of 0.1:

  1. Step 1: Gradient is 2 * 10 = 20. New x = 10 – (0.1 * 20) = 8.
  2. Step 2: Gradient is 2 * 8 = 16. New x = 8 – (0.1 * 16) = 6.4.
  3. Step 3: Gradient is 2 * 6.4 = 12.8. New x = 6.4 – (0.1 * 12.8) = 5.12.

Notice how each step gets smaller as we approach the minimum where the slope is flatter. This calculator allows you to visualize this process numerically.

function calculateGradientDescent() { var initialX = parseFloat(document.getElementById('initialX').value); var learningRate = parseFloat(document.getElementById('learningRate').value); var iterations = parseInt(document.getElementById('iterations').value); var logBody = document.getElementById('step-log'); var resultContainer = document.getElementById('gd-result-container'); var summary = document.getElementById('final-summary'); // Reset UI logBody.innerHTML = "; resultContainer.style.display = 'block'; if (isNaN(initialX) || isNaN(learningRate) || isNaN(iterations)) { summary.innerHTML = "Please enter valid numeric values."; summary.style.color = "red"; return; } summary.style.color = "#1a73e8″; var currentX = initialX; var rowsHtml = "; for (var i = 1; i <= iterations; i++) { // Function: f(x) = x^2 // Gradient: f'(x) = 2x var gradient = 2 * currentX; var step = learningRate * gradient; var newX = currentX – step; rowsHtml += '' + '' + i + '' + '' + currentX.toFixed(4) + '' + '' + gradient.toFixed(4) + '' + '' + newX.toFixed(4) + '' + ''; currentX = newX; // Safety break for divergence if (Math.abs(currentX) > 1000000) { rowsHtml += 'Calculation stopped: The values are diverging (Learning Rate likely too high).'; break; } } logBody.innerHTML = rowsHtml; summary.innerHTML = "Final Value after " + i + " iterations: x = " + currentX.toFixed(6) + "Cost f(x) = " + (currentX * currentX).toFixed(8); }

Leave a Comment