All the grokking experiments in A Fix for Softmax and Cross-Entropy train on modular arithmetic: for the main sweep, from Nanda et al., and and from Doshi et al. for the Slingshot experiments. Each dataset is the full table of input pairs, split once into a training and a test set; the model memorises the training half and the question is whether, and how cleanly, it generalises to the rest. Build the dataset.
modular_dataset(p, task, train_frac, seed)
inputs of shape (N, 2) in row-major order: row is .labels is an integer array of shape (N,) with labels[i] the answer for inputs[i]:
"add" gives , "sq" gives , "cube_add" gives (the cube applies to only). Raise ValueError for any other task string.perm = numpy.random.default_rng(seed).permutation(N) as the first and only random draw, take n_train = floor(train_frac * N) (Python int() of the product), and return train_idx = perm[:n_train], test_idx = perm[n_train:]. With and train_frac = 0.5 that is 480 training pairs and 481 test pairs.(inputs, labels, train_idx, test_idx). The labels must not depend on the seed; only the split does.numpy.meshgrid(..., indexing="ij") or numpy.indices((p, p)) produces the row-major enumeration directly; check that row really is and not .inputs array at once with vectorised integer arithmetic; 112 ** 3 fits comfortably in a 64-bit integer.int(0.5 * 961) is 480.