# Gradient surfing: the hidden role of regularization

> *Produced under the mentorship of Evan Hubinger as part of the* [*SERI ML Alignment Theory Scholars Program*](https://serimats.org/) *\- Winter 2022 Cohort*

  
[In a previous post, I demonstrated that Brownian motion near singularities defies our expectations from "regular" physics.](https://www.lesswrong.com/posts/2N7eEKDuL5sHQou3N/spooky-action-at-a-distance-in-the-loss-landscape) Singularities trap random motion and take up more of the equilibrium distribution than you'd expect from the Gibbs measure.

In the computational probability community, this is a [well-known *pathology*](https://arxiv.org/pdf/1701.02434.pdf). Sampling techniques like Hamiltonian Monte Carlo get stuck in corners, and this is something to avoid. You typically don't want biased estimates of the distribution you're trying to sample.

In deep learning, I argued, this behavior might be less a bug than a feature.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/a8de52b28cf40acebefa1ade9d1592effdbf4e429b4c6291.png)

Regularization may have a hidden function to make the set of minimum-loss points more navigable. Simply drifting around this set of points privileges simple solutions, even in the absence of explicit complexity penalties. Or not. The evidence isn't conclusive.

The [claim of singular learning theory](https://www.lesswrong.com/posts/fovfuFdpuEwQzJu2w/neural-networks-generalize-because-of-this-one-weird-trick) is that models near singularities have lower effective dimensionality. From Occam's razor, we know that simpler models generalize better, so if the dynamics of SGD get stuck at singularities, it would suggest an explanation (at least in part) for why SGD *works*: the geometry of the loss landscape biases your optimizer towards good solutions.

This is not a particularly novel claim. Similar versions of the claim been made before by [Mingard et al.](https://towardsdatascience.com/neural-networks-are-fundamentally-bayesian-bee9a172fad8) and [Valle Pérez et al.](https://www.semanticscholar.org/paper/Deep-learning-generalizes-because-the-map-is-biased-P%C3%A9rez-Camargo/7a327286d3549be1607c1ce153b041b08be151f8). But from what I can tell, the proposed mechanism, of singularity "stickiness", is quite different.

Moreover, it offers a new possible explanation for the role of regularization. If exploring the set of points with minimum training loss is enough to get to generalization, then perhaps the role of regularizer is not just to privilege "simpler" functions but also to make exploration *possible*.

In the absence of regularization, SGD can't easily move between points of equal loss. When it reaches the bottom of a valley, it's pretty much stuck. Adding a term like weight decay breaks this invariance. It frees the neural network to surf the loss basin, so it can *accidentally* stumble across better generalizing solutions.

So could we improve generalization by exploring the bottom of the loss basin in other ways — without regularization or even without SGD? Could we, for example, get a model to grok through random drift?

…No. We can't. 

That is to say I haven't succeeded yet. Still, in the spirit of "null results are results", let me share the toy model that motivated this hypothesis and the experiments that have (as of yet) failed to confirm it.

The inspiration: a toy model
============================

First, let's take a look at the model that inspired the hypothesis.

Let's begin by modifying [the example of the previous post](https://www.lesswrong.com/posts/2N7eEKDuL5sHQou3N/spooky-action-at-a-distance-in-the-loss-landscape) to include an optional regularization term controlled by $\lambda$:

$$
U(\mathbf x) = a\left(\min(x_02, x_12)\right) + \frac{\lambda}{2} (\mathbf x - \mathbf c) ^2.
$$

We deliberately center the regularization away from the origin at $\mathbf{c} = (-1,\, -1)$ so it doesn't already privilege the singularity at the origin.

Now, instead of viewing $U(\mathbf x)$ as a potential and exploring it with Brownian motion, we'll treat it as a loss function and use stochastic gradient descent to optimize for $\mathbf x$.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/5f35f63b16a399230d5f384a383b43c71fc1571b86d301c1.png)

Including regularization breaks the symmetry of the minimum loss set. Unlike before, we'll remove the toroidal boundary conditions (to avoid nasty discontinuities at the boundaries).

We'll start our optimizer at a uniformly sampled random point in this region and take $T=100$ steps down the gradient (with optional momentum controlled by $\beta$). After each gradient step, we'll inject a bit of Gaussian noise to simulate the "stochasticity." Altogether, the update rule for $\mathbf x$ is as follows:

$$
\mathbf x^{(t+1)} \leftarrow \mathbf x^{(t)} + \eta  \mathbf v^{(t+1)}+ \mathbf \delta^{(t)},
$$

with momentum updated according to:

$$
\mathbf v^{(t+1)} \leftarrow \beta \cdot\mathbf v^{(t)} + (1-\beta)\cdot (-\nabla_{\mathbf x} U),
$$

and noise given by,

$$
\delta_i^{(t)} \sim \mathcal N(0, \epsilon^2).
$$

If we sample the final obtained position, $\mathbf x^{(T)}$ over independent initializations, then, in the absence of regularization and in the presence of a small noise term, we'll get a distribution that looks like the figure on the left.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/8670b4df134828e8945efdfe885144df8233f3543f1e7ca3.png)

Under vanilla SGD, the singularity becomes *repulsive* (at least for this toy model). We can recover the stickiness of the singularity by increasing the amount of noise or adding a (relatively) small regularization term.

Unlike the case of random motion, the singularity at the origin is now *repulsive*. Good luck finding those simple solutions now.

However, as soon as we turn on the regularization (middle figure) or increase the noise term (figure on the right), the singularity once again becomes favored. This is true *even though the origin no longer minimizes the overall loss*.

So noise and regularization[^is776f207ge] bias SGD towards singularities. If you buy that singularities correspond to simpler solutions, this might mean a novel, unexplored[^xidw4ij8h9p] inductive bias towards generalization.

Grokking through random motion
==============================

Let's test this hypothesis in a more realistic setting.

Here's one application: can we use random drift at the bottom of the loss basin to induce [grokking](https://arxiv.org/abs/2201.02177)?[^\[3\]^](#fn-JCAvp3oaiJadGjvaq-3) In other words, given a model with low training error but high test error, can we improve test performance just by increasing the amount of "lateral" motion (that preserves rather than lowers loss).

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/fe8ee5059fe1fb3b175a0ef37f466787c978c2f71dc462fc.png)

Could we grok simply by exploring the set of minimum loss points and getting "stuck" at good solutions?

In particular, can we use noise to make a model grok even in the absence of regularization (which is currently a requirement to make models grok with SGD)?

If this were true, it wouldn't give us enough evidence to distinguish the contribution of singularities from other factors like [simple functions taking up more weight-space volume](https://towardsdatascience.com/neural-networks-are-fundamentally-bayesian-bee9a172fad8), but it'd be a start.

Unfortunately, it seems pretty difficult to grok without regularization.[^ip9epc57lh]

Some of the techniques I've used to explore the loss basin have had minor success (up to a $\sim 5\%$ sustained bump in test accuracy over Adam without weight decay). But nothing approaches the sustained grokking of Adam with weight decay.

Weird regularizations and additional noise
------------------------------------------

One way to encourage lateral motion is to abuse the regularization term. I tried both a repulsive variant (where I reward the model for displacement from its SGD-primed starting point) and a variant that selected for large weight norms (by punishing distance from a larger weight norm). I also tried a variant that simply adds extra noise after each gradient step.

All of these improve test performance somewhat. What seems to happen is that these variants dislodge the model from a very fragile initial equilibrium to find a new slightly better and more stable solution. It outperforms Adam without weight decay but quickly plateaus (and, in the case of "antiregularization", slowly declines after reaching a peak).

Of the three, antiregularization is able to reach the highest improvements in accuracy (up to 40%), but the improvements are unstable, and final performance is worse than it started out. Still, the fact that improving generalization is at all possible while increasing weight norm is intriguing. And the fact that the variance in performance is so high could be evidence that favors an explanation in terms of singularities over flat basins, but high-dimensional spaces are weird, and the results are anything but conclusive.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/debc57d00601bcd1126a07f7a6a9731402076a32968f7c4b.png)

It's hard to beat Adam + WD.

Note: these variants only work for small-to-zero momentum, and they work best near the inflection point of $50\%$ test accuracy.

Hamiltonian Monte Carlo
-----------------------

Another idea to explore the set of minimum loss points is to steal from physics: the trick behind [Hamiltonian Monte Carlo](https://en.wikipedia.org/wiki/Hamiltonian_Monte_Carlo) is to view the loss as a potential energy and then to simulate a physical particle moving through that potential energy landscape.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/f82382354fb37c115ce64690d00e041cc18c473d4e24d6d9.png)

Hamiltonian Monte Carlo turns your sampling problem into a physics simulation \[[source](https://elevanth.org/blog/2017/11/28/build-a-better-markov-chain/)\].

From the equipartition theorem, we can associate the starting loss to an inverse temperature, which describes a physical system with that loss as its average.

The system will still accept changes that decrease the loss, but it will also occasionally accept changes that increase the loss. Over the long run, these two forces will balance out, so the expected loss remains constant.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/1ac22061593a7a72208b02d253ebfa592de0c017e7c72f35.png)

Though this would be the "cleanest" form of exploration (it is precisely the Brownian motion discussed [previously](https://www.lesswrong.com/posts/2N7eEKDuL5sHQou3N/spooky-action-at-a-distance-in-the-loss-landscape)), it doesn't seem to work (regardless of the choice of hyperparameters and of where I start HMC along the grokking curve).

Then again, I also can't get this to work even in the presence of regularization. In this case, by lowering the temperature, HMC should [limit to a slow version of gradient descent](https://en.wikipedia.org/wiki/Simulated_annealing). [The code seems right](https://drive.google.com/file/d/17HXuDM4qhBzktVwuaC-O59MOCJbbwR-r/view?usp=sharing), so I'm inclined to write it off to high-dimensional spaces being hard and my compute budget being limited.

What does this mean for singularities?
--------------------------------------

The negative result tells us that the strong form of the claim "regularization = navigability" is probably wrong. Having a smaller weight norm actually is good for generalization (just as the [learning theorists would have you believe](https://arxiv.org/abs/1808.01174)). You'll have better luck moving along the set of minimum loss weights in the way that minimizes the norm than in any other way.

But the observation that you can, for a time, increase generalization performance by selecting for much larger norms suggests we can't outright reject the weaker version of the claim. Simply exploring the minimum loss set may — *still* — privilege simpler solutions.

[^is776f207ge]: Momentum doesn't have much of an effect on this example. 

[^xidw4ij8h9p]: From what I've seen so far. 

[^ip9epc57lh]: On the standard modular addition task. 

[^xl4jcynukyn]: Or maybe fortunately. Otherwise, this would seem a whole lot more capabilities-risky.