NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Fourier Circuits in Neural Networks and Transformers: A Case Study of Modular Arithmetic with Multiple Inputs

Chenyang Li, Yingyu Liang, Zhenmei Shi, Zhao Song, Tianyi Zhou

Abstract

In the evolving landscape of machine learning, a pivotal challenge lies in deciphering the internal representations harnessed by neural networks and Transformers. Building on recent progress toward comprehending how networks execute distinct target functions, our study embarks on an exploration of the underlying reasons behind networks adopting specific computational strategies. We direct our focus to the complex algebraic learning task of modular addition involving $k$ inputs. Our research presents a thorough analytical characterization of the features learned by stylized one-hidden layer neural networks and one-layer Transformers in addressing this task. A cornerstone of our theoretical framework is the elucidation of how the principle of margin maximization shapes the features adopted by one-hidden layer neural networks. Let $p$ denote the modulus, $D_p$ denote the dataset of modular arithmetic with $k$ inputs and $m$ denote the network width. We demonstrate that a neuron count of $ m \geq 2^{2k-2} \cdot (p-1) $, these networks attain a maximum $ L_{2,k+1} $-margin on the dataset $ D_p $. Furthermore, we establish that each hidden-layer neuron aligns with a specific Fourier spectrum, integral to solving modular addition problems. By correlating our findings with the empirical observations of similar studies, we contribute to a deeper comprehension of the intrinsic computational mechanisms of neural networks. Furthermore, we observe similar computational mechanisms in attention matrices of one-layer Transformers. Our work stands as a significant stride in unraveling their operation complexities, particularly in the realm of complex algebraic tasks.

Introduction

Understanding how Large Language Models (LLMs) process logic and mathematics remains a significant challenge in mechanistic interpretability. A notable phenomenon in this domain is "grokking," where a model suddenly achieves perfect generalization long after overfitting the training data. While previous research focused on simple binary addition ($a + b \pmod p$), this study investigates the more generalized task of modular addition with $k$ inputs ($a_1 + a_2 + \dots + a_k \pmod p$). We provide a rigorous theoretical and empirical explanation for the internal representations these networks adopt, demonstrating that they solve modular arithmetic by "discovering" Fourier-based circuits to maximize classification margins.

Methodology

This research utilizes a combination of theoretical proofs and empirical simulations. The theoretical framework is centered on margin maximization, the principle that models prefer solutions placing the widest possible gap between classes. We analyze stylized one-hidden layer Multi-Layer Perceptrons (MLPs) with polynomial activation functions and one-layer Transformers.

Using Discrete Fourier Transforms (DFT) and complex sum-to-product identities, we demonstrate how a network can decompose a modular sum into a series of cosine computations. Models were trained using Stochastic Gradient Descent (SGD) with weight decay on synthetic datasets $D_p$ consisting of all possible combinations of $k$ inputs for prime moduli $p$ (e.g., 5, 11, 31, 47, 97).

Results and Discussion

Neurons as Frequency Specialists

A primary finding of this study is that after training, each hidden neuron effectively becomes a "specialist" for a single Fourier frequency. Visualizations of the weights reveal that they form near-perfect cosine waves, and their power spectra are highly sparse, concentrated on specific frequencies.

Visualization of individual hidden neurons in a trained MLP. The left column shows weight vectors forming distinct cosine waves, while the right column displays their Fourier power spectra, indicating high frequency sparsity.

This "Fourier strategy" is not merely one possible solution; we establish that it is the optimal way to maximize the $L_2$ margin. This explains why SGD naturally gravitates toward these specific circuits. We proved that a network requires a minimum width of $m \ge 2^{2k-2} \cdot (p-1)$ to achieve this maximum margin solution.

Complexity and Grokking Dynamics

As the number of inputs ($k$) increases, the task becomes exponentially more complex. Interestingly, we found that as $k$ grows, the grokking phenomenon becomes less pronounced. The model spends less time in the initial memorization regime and must transition to feature learning more rapidly to fit the increasingly complex data.

Architectural Universality in Transformers

The Fourier-based mechanism is not limited to MLPs. In one-layer Transformers, the attention matrices ($W_{KQ}$) were found to learn 2-dimensional cosine waveforms, mirroring the behavior of MLP hidden layers. This suggests that the adoption of periodic algebraic representations is a fundamental property of neural architectures when solving modular tasks.

Attention matrices in one-layer Transformers learning 2D cosine waveforms, demonstrating the universality of Fourier-based mechanisms across architectures.

Conclusion

This work provides a mathematical foundation for the emergence of Fourier circuits in neural networks. By proving that these circuits are the maximum-margin solutions for modular arithmetic, we offer a clear explanation for why models "grok" these specific representations. While modular arithmetic is a structured "toy" task, these findings represent a significant step toward understanding how more complex models might represent periodic or algebraic structures in natural language data. Future work will explore how these circuits compose across deeper architectures and more diverse datasets.

Summary updated

BibTeX
@inproceedings{li2025fourier,
  author = {Chenyang Li and Yingyu Liang and Zhenmei Shi and Zhao Song and Tianyi Zhou},
  title = {{Fourier Circuits in Neural Networks and Transformers: A Case Study of Modular Arithmetic with Multiple Inputs}},
  booktitle = {International Conference on Artificial Intelligence and Statistics},
  year = {2025},
  url = {https://openreview.net/forum?id=qiij9CAnhs},
}
Figure