Skip to content

About

Exploring grokking and delayed generalization in modular arithmetic.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Grokking modular arithmetic

A small PyTorch project for studying when a network moves from memorizing a subset of modular-addition examples to generalizing across the full operation.

The task is

y = (a + b) mod p

Inputs are concatenated one-hot encodings of a and b. The model is a bias-free, two-layer MLP with a quadratic activation and the mean-field scaling used by Gromov (2023).

Training-fraction study

Training-fraction study

I varied only the proportion of examples used for training while keeping the modulus, model, optimizer, and evaluation protocol fixed. Each condition uses three independent seeds; a seed controls both the dataset split and model initialization. Reported values are the mean and sample standard deviation of the first 99% accuracy crossings.

Training fraction Train crossing Test crossing Delay
0.30 7,277 ± 81 21,327 ± 810 14,050 ± 730
0.40 8,717 ± 25 13,083 ± 101 4,367 ± 91
0.49 9,787 ± 80 12,373 ± 146 2,587 ± 180
0.60 10,727 ± 40 12,293 ± 25 1,567 ± 42
0.70 11,353 ± 76 12,507 ± 119 1,153 ± 61

All 15 runs reached 99% test accuracy. Increasing the training fraction from 0.30 to 0.70 reduced the mean generalization delay by about 92%. Larger training sets took longer to memorize, but test accuracy caught up much sooner after memorization.

The run-level measurements and aggregates are available in results/training_fraction_study/.

Weight-decay study

Weight-decay study

I varied only SGD's coupled weight-decay coefficient at the baseline training fraction. PyTorch SGD applies the update weight ← weight − learning_rate × (gradient + weight_decay × weight), so the large learning rate of 100,000 makes very small coefficients meaningful. Each condition again uses three independent seeds.

Weight decay Effective decay per step Train crossing Test crossing Delay Generalized
0 0 9,787 ± 80 12,373 ± 146 2,587 ± 180 3/3
1e-10 1e-5 10,270 ± 44 12,697 ± 142 2,427 ± 168 3/3
3e-10 3e-5 11,380 ± 50 13,463 ± 146 2,083 ± 181 3/3
1e-9 1e-4 16,117 ± 170 17,513 ± 162 1,397 ± 239 3/3
3e-9 3e-4 — — — 0/3

Through 1e-9, stronger decay narrowed the mean generalization delay by 46% relative to no decay. It did not make training faster overall: both the train and test crossings moved later, with memorization delayed more strongly. At 3e-9, none of the runs memorized or generalized within 40,000 steps; their final accuracies remained near chance. The blank aggregate cells preserve these as censored runs rather than assigning them a misleading delay.

The run-level measurements and aggregates are available in results/weight_decay_study/.

Baseline result

Baseline learning curves

For p = 97, the model reached 99% training accuracy at step 9,780 while test accuracy was still 0.42%. Test accuracy crossed 99% at step 12,540, giving a 2,760-step delay. The 10%-to-90% test transition took 1,350 steps.

Setting Value
Training fraction 0.49
Hidden width 512
Optimizer Full-batch SGD
Learning rate 100,000
Weight decay 0
Updates 40,000
Seed 0

This original single-seed run is retained with its full learning curve and metrics. It is also reused as the 0.49 / seed 0 condition in the study above.

Run it

Requires Python 3.11 or newer and uv.

uv sync
uv run python -m src.train

The command writes a timestamped directory under results/ containing the configuration, metrics, checkpoint, and plot. Command-line overrides are listed by uv run python -m src.train --help.

Run the complete fraction study with:

uv run python -m src.study

The study command reuses compatible completed runs, trains only missing conditions, and refreshes the aggregate tables and plot. Raw training runs are kept locally under results/runs/; the compact study outputs are versioned.

Run the weight-decay study with:

uv run python -m src.weight_decay_study

It follows the same resumable workflow and reuses the three zero-decay runs.

Repository layout

src/data.py       dataset generation and deterministic splitting
src/model.py      quadratic network
src/train.py      experiment configuration, training, and artifact saving
src/study.py      resumable multi-seed sweep and aggregate analysis
src/weight_decay_study.py  controlled weight-decay sweep
results/          curated baseline and study evidence

Reference

Andrey Gromov, Grokking modular arithmetic, 2023.

About

Exploring grokking and delayed generalization in modular arithmetic.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages