Skip to main content
Open In Colab In this example, we’ll fit a degree-9 polynomial to a small sample of real MISO grid load using stochastic gradient descent (SGD). The loss is the same regularized ridge objective solved in closed form on the linear regression page; the difference is that SGD works toward the solution one update at a time. Instead of guessing the regularization strength λ\lambda, we’ll use an outer Optuna search to find the value λ\lambda^\ast with the lowest held-out error.

The dataset

We’ll treat one day of grid load as a regression problem. The input xx is the normalized time of day, and the target yy is the total load across the MISO system. After fetching the data from the gridstatus.io API, we standardize it to zero mean and unit variance. We then keep just ten readings for training. That tiny sample is intentional: it gives a degree-9 polynomial plenty of room to overfit, just as it did in the closed-form example. The rest of the day’s readings are held out so we can evaluate each value of λ\lambda.
MISO, the Midcontinent Independent System Operator, manages the wholesale electricity market and balances electricity supply and demand across 15 US states and Manitoba. The curve below shows one real day from the gridstatus.io MISO load dataset, fetched through the gridstatus API.MISO system load versus time for 2026-06-30 from gridstatus.io, rising from an overnight low near 84 GW to an afternoon peak near 124 GWReal MISO system load on 2026-06-30, using five-minute readings from the gridstatus.io miso_load dataset.Forecast errors have real consequences here. Grid operators schedule generation ahead of time, but supply and demand must stay balanced from moment to moment. If a forecast is too low, operators may need emergency power purchases and, in severe cases, load shedding. If it is too high, they may commit expensive generators that were not needed. A model that follows noise instead of the underlying demand pattern can therefore make the grid harder and more expensive to operate.
Output from cell 3

Standardized polynomial features

Our model is a degree-9 polynomial, g(x;θ)=yˉ+k=19θkϕk(x).g(x;\boldsymbol{\theta}) = \bar{y} + \sum_{k=1}^{9}\theta_k\,\phi_k(x). The raw terms x,x2,,x9x, x^2, \dots, x^9 have very different scales on [0,1][0,1]. As a result, one gradient step can affect some coefficients far more than others, and the penalty λθ2\lambda\lVert\boldsymbol{\theta}\rVert^2 does not act evenly across them. We avoid that problem by standardizing every feature to zero mean and unit variance. A single learning rate and a single value of λ\lambda then make sense for all nine coefficients. This also matches the feature scaling used on the closed-form page, so the two results can be compared directly. We handle the intercept separately by centering the targets at yˉ\bar{y}.

The regularized objective and the SGD update

SGD minimizes the same ridge objective that the closed-form solution minimizes: the sum of squared residuals plus an 2\ell_2 penalty. J(θ)=i=1n(ϕiθy~i)2+λθ2,y~i=yiyˉ.J(\boldsymbol{\theta}) = \sum_{i=1}^{n}\big(\boldsymbol{\phi}_i^{\top}\boldsymbol{\theta} - \tilde{y}_i\big)^2 + \lambda\lVert\boldsymbol{\theta}\rVert^2, \qquad \tilde{y}_i = y_i - \bar{y}. The minimum satisfies the normal equations (ΦΦ+λI)θ=Φy~(\boldsymbol{\Phi}^{\top}\boldsymbol{\Phi} + \lambda\mathbf{I})\boldsymbol{\theta} = \boldsymbol{\Phi}^{\top}\tilde{\mathbf{y}}. Because both methods use this convention, λ\lambda has the same meaning here as it does in the closed-form solution. At each step, SGD draws a mini-batch B\mathcal{B} containing BB examples. Multiplying the batch sum by n/Bn/B gives us an unbiased estimate of the full gradient: θθη(2nBΦB(ΦBθy~B)+2λθ).\boldsymbol{\theta} \leftarrow \boldsymbol{\theta} - \eta\Big(\tfrac{2n}{B}\,\boldsymbol{\Phi}_{\mathcal{B}}^{\top}(\boldsymbol{\Phi}_{\mathcal{B}}\boldsymbol{\theta} - \tilde{\mathbf{y}}_{\mathcal{B}}) + 2\lambda\boldsymbol{\theta}\Big).

Finding a good value for λ\lambda

The training loss cannot tell us how much regularization to use. It will always favor λ0\lambda \to 0, since weaker shrinkage lets the polynomial fit the ten training points more closely. To choose λ\lambda based on how well the model generalizes, we use two loops:
  • In the inner loop, SGD trains the degree-9 model for one fixed value of λ\lambda and returns its error on the held-out readings.
  • In the outer loop, Optuna proposes values of λ\lambda across a wide logarithmic range. It uses the validation error from each inner run to decide what to try next.
The value with the lowest held-out error becomes λ\lambda^\ast. It is selected from this grid-load dataset rather than copied from the earlier example.
Output from cell 7

Comparing the two solutions

Once the search has found λ\lambda^\ast, we train the degree-9 model one more time and compare its SGD weights with the closed-form ridge solution at the same value of λ\lambda. The two fits should be nearly identical. They minimize the same objective; they simply take different routes to get there.
Output from cell 9

What to take away

  • Choosing λ\lambda is a model-selection problem. Optuna tries candidate values in the outer loop, while SGD trains and evaluates a model in the inner loop. Training loss alone is not enough because it always favors less shrinkage.
  • The regularization term contributes 2λθ2\lambda\boldsymbol{\theta} to the gradient. This shrinks the weights at every update and helps keep the degree-9 polynomial from overfitting.
  • When SGD and the closed-form method use the same standardized features and the same λ\lambda^\ast, they reach the same regularized solution.
  • On this grid-load sample, the search settles near 4×1034\times10^{-3}. That is close in scale to the 3.04×1033.04\times10^{-3} found for the unit-variance sinusoid on the closed-form page, even though the value was tuned independently.
Key references: (Keskar et al., 2016; Bottou et al., 2016; Andrychowicz et al., 2016)

References

  • Andrychowicz, M., Denil, M., Gomez, S., Hoffman, M., Pfau, D., et al. (2016). Learning to learn by gradient descent by gradient descent.
  • Bottou, L., Curtis, F., Nocedal, J. (2016). Optimization Methods for Large-Scale Machine Learning.
  • Keskar, N., Mudigere, D., Nocedal, J., Smelyanskiy, M., Tang, P. (2016). On Large-Batch Training for Deep Learning: Generalization Gap and Sharp Minima.