Skip to content
E
Educatum

Learning Rate in Machine Learning

Curated by Educatum AI teamLast updated:
Neural Networks and Deep LearningNN
Direct Answer

The learning rate is a hyperparameter that scales the magnitude of parameter updates during gradient-based optimization. Too small a learning rate causes slow convergence and risks stalling, whereas too large a learning rate causes overshooting, oscillation, or divergence, making careful tuning and learning rate schedules essential.

Study Card: Learning Rate in Machine Learning

Direct Answer

The learning rate η\eta is a hyperparameter that controls how much model parameters θ\theta are updated against the loss gradient in each optimization step:

θt+1=θt−η∇θL(θt)\theta_{t+1} = \theta_t - \eta \nabla_{\theta} \mathcal{L}(\theta_t)

A learning rate that is too small produces tiny steps and slow convergence; a learning rate that is too large causes updates to overshoot the minimum, oscillate, or diverge outright. Choosing an effective learning rate is critical for training and is typically handled via validation tuning (grid search, random search, or Bayesian optimization), learning rate schedules, and adaptive optimizers.

Key Terms

  • Hyperparameter: A configuration value set prior to training rather than learned directly from data.
  • Gradient Descent: An iterative optimization algorithm that updates parameters in the direction of steepest descent (−∇θL-\nabla_\theta \mathcal{L}).
  • Loss Function: An objective measuring the discrepancy between model predictions and target values, such as MSE L(θ)=1m∥Xθ−y∥22\mathcal{L}(\theta) = \frac{1}{m}\|X\theta - y\|_2^2 with gradient ∇θL(θ)=2mXT(Xθ−y)\nabla_\theta \mathcal{L}(\theta) = \frac{2}{m}X^T(X\theta - y).
  • Learning Rate Schedule / Decay: A strategy that reduces (or warms up and decays) η\eta over training so large initial steps transition into fine-grained convergence near a minimum.
  • Stability Threshold: For convex quadratic objectives with Hessian eigenvalues bounded by L=λmax⁡(H)L = \lambda_{\max}(H), gradient descent requires η<2/L\eta < 2 / L to avoid divergence.

Example

Consider training a linear regression model on house prices. Suppose a simplified model predicts via a single baseline bias parameter θ0\theta_0 (initially USD 200,000) for a house whose true price is USD 250,000, minimizing half-squared error L(θ0)=12(θ0−y)2\mathcal{L}(\theta_0) = \frac{1}{2}(\theta_0 - y)^2. The gradient is ∂L∂θ0=θ0−y=−50,000\frac{\partial \mathcal{L}}{\partial \theta_0} = \theta_0 - y = -50{,}000, so gradient descent updates θ0←θ0−η(−50,000)=θ0+η⋅50,000\theta_0 \leftarrow \theta_0 - \eta(-50{,}000) = \theta_0 + \eta \cdot 50{,}000.

With η=0.1\eta = 0.1, the parameter updates by +5,000 to USD 205,000 in one step; with η=0.001\eta = 0.001, it inches up by only +50 to USD 200,050. Conversely, if η\eta were greater than 2.0 in this 1D quadratic example, each step would overshoot USD 250,000 by a larger margin and diverge.

Code Implementation

import numpy as np
import matplotlib.pyplot as plt

# Sample data (linear regression: y = 4 + 3x + noise)
np.random.seed(42)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)

X_b = np.c_[np.ones((100, 1)), X]  # Add x0 = 1 bias column

# Gradient Descent implementation minimizing MSE = (1/m) * ||X*theta - y||^2
def gradient_descent(X, y, learning_rate=0.1, n_iterations=1000):
    """Batch Gradient Descent."""
    m, n = X.shape
    theta = np.random.randn(n, 1)  # Random initialization

    for _ in range(n_iterations):
        gradients = (2 / m) * X.T.dot(X.dot(theta) - y)
        theta = theta - learning_rate * gradients

    return theta

# Train with different learning rates
theta_small_lr = gradient_descent(X_b, y, learning_rate=0.001, n_iterations=100)
# learning_rate=1.0 exceeds the stability limit 2 / lambda_max for this design matrix and diverges
theta_large_lr = gradient_descent(X_b, y, learning_rate=1.0, n_iterations=100)
theta_optimal_lr = gradient_descent(X_b, y, learning_rate=0.1, n_iterations=1000)

# Print learned parameters
print(f"Theta with small lr:\n{theta_small_lr}\n")
print(f"Theta with large lr (diverged):\n{theta_large_lr}\n")
print(f"Theta with optimal lr:\n{theta_optimal_lr}")

# Visualize predictions from each run
X_new = np.array([[0], [2]])
X_new_b = np.c_[np.ones((2, 1)), X_new]

y_predict_small = X_new_b.dot(theta_small_lr)
y_predict_large = X_new_b.dot(theta_large_lr)
y_predict_optimal = X_new_b.dot(theta_optimal_lr)

plt.plot(X, y, "b.")
plt.plot(X_new, y_predict_small, "r-", label="Small LR (0.001)")
plt.plot(X_new, y_predict_large, "g-", label="Large LR (1.0, diverged)")
plt.plot(X_new, y_predict_optimal, "y-", label="Good LR (0.1)")

plt.xlabel("$x_1$")
plt.ylabel("y", rotation=0)
plt.axis([0, 2, 0, 15])
plt.legend(loc="upper left")
plt.grid()
plt.show()
  • Adaptive Optimization Algorithms (Adam, RMSprop, AdaGrad): Maintain per-parameter running gradient statistics to scale step sizes dynamically across coordinates.
  • Momentum: Accumulates an exponentially decaying velocity vector of past gradients to accelerate movement along consistent directions and dampen oscillations in high-curvature ravines.
  • Learning Rate Schedules: Strategies such as linear warmup followed by cosine decay, step decay, or cyclical learning rates improve stability early in training and precision late in training.
  • Stochastic Gradient Descent (SGD): Approximates the full gradient from single points or mini-batches. Because mini-batch gradient noise introduces variance around minima, SGD is actually more sensitive to fixed learning rates and typically requires learning rate decay to converge smoothly.
  • Batch Size: Larger batch sizes average over more samples, lowering gradient estimate variance (scaling inversely with batch size) and performing fewer updates per epoch; consequently, larger batch sizes generally support larger learning rates (e.g., linear or square-root scaling up to a critical batch size), whereas smaller, noisier batches require smaller learning rates.
View all →

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.